roles.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @package chamilo.permissions
  4. */
  5. require '../inc/global.inc.php';
  6. require_once 'permissions_functions.inc.php';
  7. require_once 'all_permissions.inc.php';
  8. $tool_name = get_lang('Roles'); // title of the page (should come from the language file)
  9. Display::display_header($tool_name);
  10. // ACTIONS
  11. // storing all the permission for a given role when the checkbox approach is used
  12. if ($_POST['StoreRolePermissions'])
  13. {
  14. if (!empty($_POST['role_name']))
  15. {
  16. $table_role = Database::get_course_table(TABLE_ROLE);
  17. $sql = "INSERT INTO $table_role (role_name, role_comment, default_role)
  18. VALUES ('".Database::escape_string($_POST['role_name'])."','".Database::escape_string($_POST['role_comment'])."','".Database::escape_string($_POST['default_role'])."')";
  19. $result = Database::query($sql);
  20. $role_id = Database::insert_id();
  21. $result_message = store_permissions('role', $role_id);
  22. }
  23. else
  24. {
  25. $result_message = get_lang('ErrorPleaseGiveRoleName');
  26. }
  27. }
  28. // storing a permission for a given role when the image approach is used
  29. if (isset($_GET['action']) AND isset($_GET['permission']) AND isset($_GET['tool']))
  30. {
  31. if ($_GET['action'] == 'grant' OR $_GET['action'] == 'revoke')
  32. {
  33. $result_message = store_one_permission('role', $_GET['action'], $role_id, $_GET['tool'], $_GET['permission']);
  34. }
  35. }
  36. // deleting a role
  37. if (isset($_GET['action']) AND isset($_GET['role_id']) AND $_GET['action'] == 'delete') {
  38. //deleting the assignments fo this role: users
  39. $table = Database::get_course_table(TABLE_ROLE_USER);
  40. $sql = "DELETE FROM $table WHERE role_id='".intval($_GET['role_id'])."'";
  41. $result = Database::query($sql);
  42. // deleting the assignments of this role: groups
  43. $table = Database::get_course_table(TABLE_ROLE_GROUP);
  44. $sql = "DELETE FROM $table WHERE role_id='".intval($_GET['role_id'])."'";
  45. $result = Database::query($sql);
  46. // deleting the permissions of this role
  47. $table = Database::get_course_table(TABLE_ROLE_PERMISSION);
  48. $sql = "DELETE FROM $table WHERE role_id='".intval($_GET['role_id'])."'";
  49. $result = Database::query($sql);
  50. // deleting the role
  51. $table_role = Database::get_course_table(TABLE_ROLE);
  52. $sql = "DELETE FROM $table_role WHERE role_id='".intval($_GET['role_id'])."'";
  53. $result = Database::query($sql);
  54. $result_message = get_lang('RoleDeleted');
  55. }
  56. // displaying the return message of the actions
  57. if (isset($result_message)) {
  58. echo Display::return_message($result_message);
  59. }
  60. // ADDING A NEW ROLE (FORM AND LINK)
  61. echo '<img src="../img/add.png" /> <a href="roles.php?action=add">'.get_lang('AddRole').'</a>';
  62. if ($_GET['action'] == 'add')
  63. {
  64. echo "<form method=\"post\" action=\"".api_get_self()."\">";
  65. echo "\n<table>";
  66. echo "\n\t<tr>";
  67. echo "\n\t\t<td>";
  68. echo get_lang('RoleName');
  69. echo "\n\t\t</td>";
  70. echo "\n\t\t<td>";
  71. echo "\n\t\t\t<input type='text' name='role_name'>";
  72. echo "\n\t\t</td>";
  73. echo "\n\t</tr>";
  74. echo "\n\t<tr>";
  75. echo "\n\t\t<td>";
  76. echo get_lang('RoleComment');
  77. echo "\n\t\t</td>";
  78. echo "\n\t\t<td>";
  79. echo "\n\t\t\t<textarea name='role_comment'></textarea>";
  80. echo "\n\t\t</td>";
  81. echo "\n\t</tr>";
  82. echo "\n\t<tr>";
  83. echo "\n\t\t<td>";
  84. echo get_lang('DefaultRole');
  85. echo "\n\t\t</td>";
  86. echo "\n\t\t<td>";
  87. echo "\n\t\t\t<input type=\"checkbox\" name=\"default_role\" value=\"1\">";
  88. echo "\n\t\t</td>";
  89. echo "\n\t</tr>";
  90. echo "\n</table>";
  91. echo "<table class=\"data_table\">\n";
  92. // the header
  93. if (api_get_setting('permissions') == 'limited') {
  94. $header_array = $rights_limited;
  95. }
  96. if (api_get_setting('permissions') == 'full') {
  97. $header_array = $rights_full;
  98. }
  99. echo "\t<tr>\n";
  100. echo "\t\t<th>".get_lang('Module')."</th>\n";
  101. foreach ($header_array as $header_key => $header_value) {
  102. echo "\t\t<th>".get_lang($header_value)."</th>\n";
  103. }
  104. echo "\t</tr>\n";
  105. // the main area with the checkboxes or images
  106. foreach ($tool_rights as $tool=>$rights) // $tool_rights contains all the possible tools and their rights
  107. {
  108. echo "\t<tr>\n";
  109. echo "\t\t<td>\n";
  110. echo get_lang($tool);
  111. echo "\t\t</td>\n";
  112. foreach ($header_array as $key => $value) {
  113. echo "\t\t<td align='center'>\n";
  114. display_checkbox_matrix(array(), $tool, $value);
  115. echo "\t\t</td>\n";
  116. }
  117. echo "\t</tr>\n";
  118. }
  119. echo "</table>\n";
  120. echo "<input type=\"Submit\" name=\"StoreRolePermissions\" value=\"".get_lang('StorePermissions')."\">";
  121. echo "</form>";
  122. }
  123. // DISPLAYING THE EXISTING ROLES
  124. // platform roles
  125. $all_roles = get_all_roles('platform');
  126. foreach ($all_roles as $role)
  127. {
  128. echo '<div><a href="roles.php?action=view&amp;role_id='.$role['role_id'].'&amp;scope=platform">'.$role['role_name'].'</a></div>';
  129. echo '<div>'.$role['role_comment'].'</div><br />';
  130. if ($role['role_id'] == $_GET['role_id'])
  131. {
  132. $current_role_info = $role;
  133. }
  134. }
  135. // course roles
  136. $all_roles = get_all_roles();
  137. foreach ($all_roles as $role) {
  138. echo '<div><a href="roles.php?action=view&amp;role_id='.$role['role_id'].'">'.$role['role_name'].'</a><a href="roles.php?action=delete&amp;role_id='.$role['role_id'].'"><img src="../img/delete.gif" /></a></div>';
  139. echo '<div>'.$role['role_comment'].'</div><br />';
  140. if ($role['role_id'] == $_GET['role_id'])
  141. {
  142. $current_role_info = $role;
  143. }
  144. }
  145. // DISPLAYING THE PERMISSIONS OF A GIVEN ROLE
  146. if ($_GET['role_id']) {
  147. $current_role_permissions = get_permissions('role', $_GET['role_id']);
  148. // LIMITED OR FULL
  149. $current_role_permissions = limited_or_full($current_role_permissions);
  150. if (api_get_setting('permissions') == 'limited')
  151. {
  152. $header_array = $rights_limited;
  153. }
  154. if (api_get_setting('permissions') == 'full')
  155. {
  156. $header_array = $rights_full;
  157. }
  158. // ---------------------------------------------------
  159. // DISPLAYING THE MATRIX
  160. // ---------------------------------------------------
  161. echo "<form method=\"post\" action=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."\">";
  162. // the list of the roles for the user
  163. echo get_lang('PermissionsOfRole').':'.$current_role_info['role_name'].'<br />';
  164. if ($_GET['scope'] == 'platform')
  165. {
  166. echo get_lang('IsPlatformRoleNotEditable').'<br />';
  167. }
  168. echo "<table class=\"data_table\">\n";
  169. // the header
  170. echo "\t<tr>\n";
  171. echo "\t\t<th>".get_lang('Module')."</th>\n";
  172. foreach ($header_array as $header_key => $header_value) {
  173. echo "\t\t<th>".get_lang($header_value)."</th>\n";
  174. }
  175. echo "\t</tr>\n";
  176. // the main area with the checkboxes or images
  177. foreach ($tool_rights as $tool=>$rights) // $tool_rights contains all the possible tools and their rights
  178. {
  179. echo "\t<tr>\n";
  180. echo "\t\t<td>\n";
  181. echo get_lang($tool);
  182. echo "\t\t</td>\n";
  183. foreach ($header_array as $key => $value) {
  184. echo "\t\t<td align='center'>\n";
  185. if (in_array($value, $rights)) {
  186. if ($setting_visualisation == 'checkbox') {
  187. display_checkbox_matrix(
  188. $current_role_permissions,
  189. $tool,
  190. $value
  191. );
  192. }
  193. if ($setting_visualisation == 'image') {
  194. if ($_GET['scope'] == 'platform') {
  195. $roles_editable = false;
  196. } else {
  197. $roles_editable = true;
  198. }
  199. display_image_matrix(
  200. $current_role_permissions,
  201. $tool,
  202. $value,
  203. '',
  204. '',
  205. $roles_editable
  206. );
  207. }
  208. }
  209. echo "\t\t</td>\n";
  210. }
  211. echo "\t</tr>\n";
  212. }
  213. echo "</table>\n";
  214. if ($setting_visualisation == 'checkbox') {
  215. echo "<input type=\"Submit\" name=\"StoreRolePermissions\" value=\"".get_lang('StorePermissions')."\">";
  216. }
  217. echo "</form>";
  218. }
  219. Display::display_footer();