roles.php 8.1 KB

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