roles.php 7.9 KB

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