tripal_analysis_privacy.inc 3.1 KB

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