tripal_analysis_privacy.inc 3.6 KB

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