tripal_analysis_privacy.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * Perform permission check (required node_privacy_byrole module).
  5. *
  6. * @ingroup tripal_legacy_analysis
  7. */
  8. /**
  9. * Perform permission check by analysis_id only if 'node_privacy_byrole' module
  10. * is enabled
  11. *
  12. * @param $analysis_id
  13. * Which analysis to check if the user has permission to view
  14. *
  15. * @ingroup tripal_legacy_analysis
  16. */
  17. function tripal_analysis_check_permission($analysis_id) {
  18. if (module_exists('node_privacy_byrole')) {
  19. global $user;
  20. $roles = $user->roles;
  21. $node_access = 0;
  22. foreach ($roles AS $rid => $role) {
  23. $p_sql = "
  24. SELECT grant_view
  25. FROM {node_access} NA
  26. INNER JOIN {chado_analysis} CA ON NA.nid = CA.nid
  27. WHERE analysis_id = :analysis_id AND gid = :gid";
  28. $access = db_query($p_sql, [
  29. ':analysis_id' => $analysis_id,
  30. ':gid' => $rid,
  31. ])->fetchField();
  32. if ($access == 1) {
  33. $node_access = 1;
  34. break;
  35. }
  36. }
  37. if ($node_access == 1 || $user->uid == 1) {
  38. return TRUE;
  39. }
  40. else {
  41. return FALSE;
  42. }
  43. // If 'node_privacy_byrole' module is not enabled, return TRUE;
  44. }
  45. else {
  46. return TRUE;
  47. }
  48. }
  49. /**
  50. * Set permissions for features associated with an analysis
  51. *
  52. * @todo Stephen: ensure the $nid param is defined correctly
  53. *
  54. * @param $analysis_id
  55. * Features associated with this analysis will have their permissions set
  56. * @param $nid
  57. * The node ID of the analysis
  58. *
  59. * @ingroup tripal_legacy_analysis
  60. */
  61. function tripal_analysis_set_feature_permission($analysis_id, $nid) {
  62. print "Updating feature permissions:\n";
  63. // Get features associated with the analysis
  64. $sql = "SELECT feature_id FROM {analysisfeature} WHERE analysis_id = :analysis_id";
  65. $features = chado_query($sql, [':analysis_id' => $analysis_id]);
  66. // Convert feature_id into node_id
  67. $feature_nids = [];
  68. $counter = 0;
  69. $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  70. while ($feature = $features->fetchObject()) {
  71. $feature_nids[$counter] = db_query($sql, [':feature_id' => $feature->feature_id])->fetchField();
  72. $counter++;
  73. }
  74. //Convert analysis_id into node_id
  75. $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = :analysis_id", [':analysis_id' => $analysis_id]));
  76. // Get available roles
  77. $roles = array_keys(user_roles());
  78. $interval = intval(($counter + 1) * 0.01);
  79. $idx_iterations = 1;
  80. // Update permission node by node
  81. foreach ($feature_nids AS $fnid) {
  82. if ($idx_iterations % $interval == 0) {
  83. $percentage = (int) ($idx_iterations / ($counter + 1) * 100);
  84. tripal_set_job_progress($job_id, $percentage);
  85. print $percentage . "% ";
  86. }
  87. db_query("DELETE FROM {node_privacy_byrole} WHERE nid = :nid AND realm = 'node_privacy_byrole_role'", [':nid' => $fnid]);
  88. foreach ($roles AS $rid) {
  89. // Get permissions of this analysis for this role
  90. $rsql = "SELECT * FROM {node_privacy_byrole} WHERE gid = :gid AND nid = :nid AND realm = 'node_privacy_byrole_role'";
  91. $ana_perm = db_query($rsql, [
  92. ':gid' => $rid,
  93. ':nid' => $ana_nid,
  94. ])->fetchObject();
  95. db_query("INSERT INTO {node_privacy_byrole} (nid, gid, realm, grant_view, grant_update, grant_delete)
  96. VALUES (:nid, :gid, :realm, :grant_view, :grant_update, :grant_delete)",
  97. [
  98. ':nid' => $fnid,
  99. ':gid' => $rid,
  100. ':realm' => 'node_privacy_byrole_role',
  101. ':grant_view' => $ana_perm->grant_view,
  102. ':grant_update' => $ana_perm->grant_update,
  103. ':grant_delete' => $ana_perm->grant_delete,
  104. ]);
  105. }
  106. $node = node_load($fnid);
  107. node_save($node);
  108. $idx_iterations++;
  109. }
  110. }