roles.php 7.4 KB

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