permissions_functions.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. * This files contains the common functions for the permissions
  4. *
  5. * A list of all the functions (in no particular order)
  6. * ----------------------------------------------------
  7. * store_permissions($content,$id)
  8. * get_permissions($content,$id)
  9. * limited_or_full($current_permissions)
  10. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  11. * @package chamilo.permissions
  12. */
  13. /**
  14. * This function stores the permissions in the correct table.
  15. * Since Checkboxes are used we do not know which ones are unchecked.
  16. * That's why we first delete them all (for the given user/group/role
  17. * and afterwards we store the checked ones only.
  18. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  19. * @param $id the id of the user, group or role
  20. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  21. * @version 1.0
  22. */
  23. function store_permissions($content, $id)
  24. {
  25. // Which database are we using (depending on the $content parameter)
  26. if($content=='user')
  27. {
  28. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  29. $id_field = user_id;
  30. }
  31. if($content=='group')
  32. {
  33. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  34. $id_field = group_id;
  35. }
  36. if($content=='role')
  37. {
  38. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  39. $id_field = role_id;
  40. }
  41. // We first delete all the existing permissions for that user/group/role
  42. $sql="DELETE FROM $table WHERE $id_field = '".Database::escape_string($id)."'";
  43. $result=Database::query($sql);
  44. // looping through the post values to find the permission (containing the string permission* )
  45. foreach ($_POST as $key => $value)
  46. {
  47. if(strstr($key,"permission*"))
  48. {
  49. list($brol,$tool,$action)=explode("*",$key);
  50. $sql="INSERT INTO $table ($id_field,tool,action) VALUES ('".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($action)."')";
  51. $result=Database::query($sql);
  52. }
  53. }
  54. return get_lang('PermissionsStored');
  55. }
  56. /**
  57. * This function stores one permission in the correct table.
  58. * @param $content are we storing rights for a user, a group or a role (the database depends on it)
  59. * @param $action are we granting or revoking a permission?
  60. * @param $id the id of the user, group or role
  61. * @param $tool the tool
  62. * @param $permission the permission the user, group or role has been granted or revoked
  63. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  64. * @version 1.0
  65. */
  66. function store_one_permission($content, $action, $id, $tool,$permission)
  67. {
  68. global $rights_full;
  69. // for some reason I don't know, he can't get to the $rights_full array, so commented the following lines out.
  70. // check
  71. //if(!in_array($permission, $rights_full))
  72. //{
  73. // return get_lang('Error');
  74. //}
  75. // Which database are we using (depending on the $content parameter)
  76. if($content=='user')
  77. {
  78. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  79. $id_field = user_id;
  80. }
  81. if($content=='group')
  82. {
  83. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  84. $id_field = group_id;
  85. }
  86. if($content=='role')
  87. {
  88. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  89. $id_field = role_id;
  90. }
  91. // grating a right
  92. if($action=='grant')
  93. {
  94. $sql="INSERT INTO $table ($id_field,tool,action) VALUES ('".Database::escape_string($id)."','".Database::escape_string($tool)."','".Database::escape_string($permission)."')";
  95. $result=Database::query($sql);
  96. if($result)
  97. {
  98. $result_message=get_lang('PermissionGranted');
  99. }
  100. }
  101. if($action=='revoke')
  102. {
  103. $sql="DELETE FROM $table WHERE $id_field = '".Database::escape_string($id)."' AND tool='".Database::escape_string($tool)."' AND action='".Database::escape_string($permission)."'";
  104. $result=Database::query($sql);
  105. if($result)
  106. {
  107. $result_message=get_lang('PermissionRevoked');
  108. }
  109. }
  110. return $result_message;
  111. }
  112. /**
  113. * This function retrieves the existing permissions of a user, group or role.
  114. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  115. * @param $id the id of the user, group or role
  116. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  117. * @version 1.0
  118. */
  119. function get_permissions($content, $id)
  120. {
  121. $currentpermissions=array();
  122. // Which database are we using (depending on the $content parameter)
  123. if($content == 'user')
  124. {
  125. $table=Database::get_course_table(TABLE_PERMISSION_USER);
  126. $id_field = user_id;
  127. }
  128. elseif($content == 'group')
  129. {
  130. $table=Database::get_course_table(TABLE_PERMISSION_GROUP);
  131. $id_field = group_id;
  132. }
  133. elseif($content == 'role')
  134. {
  135. $table=Database::get_course_table(TABLE_ROLE_PERMISSION);
  136. $id_field = role_id;
  137. }
  138. elseif($content == 'platform_role')
  139. {
  140. $table=Database::get_main_table(TABLE_ROLE_PERMISSION);
  141. $id_field = role_id;
  142. }
  143. elseif($content == 'task')
  144. {
  145. $table=Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  146. $id_field = task_id;
  147. }
  148. // finding all the permissions. We store this in a multidimensional array
  149. // where the first dimension is the tool.
  150. $sql="
  151. SELECT * FROM " . $table . "
  152. WHERE " . $id_field . "='" . Database::escape_string($id) . "'";
  153. $result = Database::query($sql);
  154. while($row = Database::fetch_array($result))
  155. $currentpermissions[$row['tool']][] = $row['action'];
  156. return $currentpermissions;
  157. }
  158. /**
  159. * the array that contains the current permission a user, group or role has will now be changed depending on
  160. * the Dokeos Config Setting for the permissions (limited [add, edit, delete] or full [view, add, edit, delete, move, visibility]
  161. * @param $content are we retrieving the rights of a user, a group or a role (the database depends on it)
  162. * @param $id the id of the user, group or role
  163. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  164. * @version 1.0
  165. * @todo currently there is a setting user_permissions and group_permissions. We should merge this in one config setting.
  166. */
  167. function limited_or_full($current_permissions)
  168. {
  169. if(api_get_setting('permissions')=='limited')
  170. {
  171. foreach ($current_permissions as $tool=>$tool_rights)
  172. {
  173. // we loop through the possible permissions of a tool and unset the entry if it is view
  174. // if it is visibility or move we have to grant the edit right
  175. foreach ($tool_rights as $key=>$value)
  176. {
  177. if($value=='View')
  178. {
  179. unset($current_permissions[$tool][$key]);
  180. }
  181. if($value=='Visibility' OR $value=='Move')
  182. {
  183. if(!in_array('Edit',$current_permissions[$tool]))
  184. {
  185. $current_permissions[$tool][]='Edit';
  186. }
  187. unset($current_permissions[$tool][$key]);
  188. }
  189. //else
  190. //{
  191. // $current_permissions[$tool][]=$value;
  192. //}
  193. }
  194. }
  195. return $current_permissions;
  196. }
  197. if(api_get_setting('permissions')=='full')
  198. {
  199. return $current_permissions;
  200. }
  201. }
  202. /**
  203. * This function displays a checked or unchecked checkbox. The checkbox will be checked if the
  204. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  205. * does not have the right
  206. * @param $permission_array the array that contains all the permissions of the user, group, role
  207. * @param $tool the tool we want to check a permission for
  208. * @param $permission the permission we want to check for
  209. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  210. * @version 1.0
  211. */
  212. function display_checkbox_matrix($permission_array, $tool, $permission, $inherited_permissions=array())
  213. {
  214. $checked="";
  215. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  216. {
  217. $checked="checked";
  218. }
  219. echo "\t\t\t<input type=\"checkbox\" name=\"permission*$tool*$permission\" $checked>\n";
  220. }
  221. /**
  222. * This function displays a checked or unchecked image. The image will be checked if the
  223. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  224. * does not have the right
  225. * @param $permission_array the array that contains all the permissions of the user, group, role
  226. * @param $tool the tool we want to check a permission for
  227. * @param $permission the permission we want to check for
  228. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  229. * @version 1.0
  230. */
  231. function display_image_matrix($permission_array, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  232. {
  233. if ($course_admin)
  234. {
  235. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  236. }
  237. else
  238. {
  239. if(in_array($permission,$inherited_permissions[$tool]))
  240. {
  241. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  242. }
  243. else
  244. {
  245. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  246. {
  247. if($editable)
  248. {
  249. $url=api_get_self();
  250. foreach($_GET as $key=>$value)
  251. {
  252. $parameter[$key]=$value;
  253. }
  254. $parameter['action']='revoke';
  255. $parameter['permission']=$permission;
  256. $parameter['tool']=$tool;
  257. foreach ($parameter as $key=>$value)
  258. {
  259. $urlparameters.=$key.'='.$value.'&amp;';
  260. }
  261. $url=$url.'?'.$urlparameters;
  262. echo "\t\t\t <a href=\"".$url."\">";
  263. }
  264. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/>";
  265. if($editable)
  266. {
  267. echo "</a>";
  268. }
  269. }
  270. else
  271. {
  272. if($editable)
  273. {
  274. $url=api_get_self();
  275. foreach($_GET as $key=>$value)
  276. {
  277. $parameter[$key]=$value;
  278. }
  279. $parameter['action']='grant';
  280. $parameter['permission']=$permission;
  281. $parameter['tool']=$tool;
  282. foreach ($parameter as $key=>$value)
  283. {
  284. $urlparameters.=$key.'='.$value.'&amp;';
  285. }
  286. $url=$url.'?'.$urlparameters;
  287. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  288. echo "\t\t\t <a href=\"".$url."\">";
  289. }
  290. echo "<img src=\"../img/wrong.gif\" border=\"0\"/>";
  291. if($editable)
  292. {
  293. echo "</a>";
  294. }
  295. }
  296. }
  297. }
  298. }
  299. /**
  300. * Slightly modified: Toon Keppens
  301. * This function displays a checked or unchecked image. The image will be checked if the
  302. * user, group or role has the permission for the given tool, unchecked if the user, group or role
  303. * does not have the right
  304. * @param $permission_array the array that contains all the permissions of the user, group, role
  305. * @param $tool the tool we want to check a permission for
  306. * @param $permission the permission we want to check for
  307. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  308. * @version 1.0
  309. */
  310. function display_image_matrix_for_blogs($permission_array, $user_id, $tool, $permission,$inherited_permissions=array(), $course_admin=false, $editable=true)
  311. {
  312. if ($course_admin)
  313. {
  314. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  315. }
  316. else
  317. {
  318. if(!empty($inherited_permissions) and in_array($permission,$inherited_permissions[$tool]))
  319. {
  320. echo "\t\t\t<img src=\"../img/checkbox_on3.gif\" border=\"0\"/ title=\"".get_lang('PermissionGrantedByGroupOrRole')."\">";
  321. }
  322. else
  323. {
  324. if(is_array($permission_array[$tool]) AND in_array($permission,$permission_array[$tool]))
  325. {
  326. if($editable)
  327. {
  328. $url=api_get_self();
  329. foreach($_GET as $key=>$value)
  330. {
  331. $parameter[$key]=$value;
  332. }
  333. $parameter['action']='manage_rights';
  334. $parameter['do']='revoke';
  335. $parameter['permission']=$permission;
  336. $parameter['tool']=$tool;
  337. $parameter['user_id']=$user_id;
  338. foreach ($parameter as $key=>$value)
  339. {
  340. $urlparameters.=$key.'='.$value.'&amp;';
  341. }
  342. $url=$url.'?'.$urlparameters;
  343. echo "\t\t\t <a href=\"".$url."\">";
  344. }
  345. echo "<img src=\"../img/checkbox_on2.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermission')."\">";
  346. if($editable)
  347. {
  348. echo "</a>";
  349. }
  350. }
  351. else
  352. {
  353. if($editable)
  354. {
  355. $url=api_get_self();
  356. foreach($_GET as $key=>$value)
  357. {
  358. $parameter[$key]=$value;
  359. }
  360. $parameter['action']='manage_rights';
  361. $parameter['do']='grant';
  362. $parameter['permission']=$permission;
  363. $parameter['tool']=$tool;
  364. $parameter['user_id']=$user_id;
  365. foreach ($parameter as $key=>$value)
  366. {
  367. $urlparameters.=$key.'='.$value.'&amp;';
  368. }
  369. $url=$url.'?'.$urlparameters;
  370. //echo "\t\t\t <a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=grant&amp;permission=$permission&amp;tool=$tool\">";
  371. echo "\t\t\t <a href=\"".$url."\">";
  372. }
  373. echo "<img src=\"../img/wrong.gif\" border=\"0\"/ title=\"".get_lang('UserHasPermissionNot')."\">";
  374. if($editable)
  375. {
  376. echo "</a>";
  377. }
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. * This function displays a list off all the roles of the course (and those defined by the platform admin)
  384. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  385. * @version 1.0
  386. */
  387. function display_role_list($current_course_roles, $current_platform_roles)
  388. {
  389. global $setting_visualisation;
  390. $coures_roles_table=Database::get_course_table(TABLE_ROLE);
  391. $platform_roles_table=Database::get_main_table(TABLE_ROLE);
  392. /*
  393. // platform roles
  394. $sql="SELECT * FROM $platform_roles_table";
  395. $result=Database::query($sql);
  396. while ($row=Database::fetch_array($result))
  397. {
  398. if(in_array($row['role_id'], $current_platform_roles))
  399. {
  400. $checked='checked';
  401. $image='checkbox_on2.gif';
  402. $action='revoke';
  403. }
  404. else
  405. {
  406. $checked='';
  407. $image='wrong.gif';
  408. $action='grant';
  409. }
  410. if($setting_visualisation=='checkbox')
  411. {
  412. echo "<input type=\"checkbox\" name=\"role*platform*".$row['role_id']."\" $checked>";
  413. }
  414. if($setting_visualisation=='image')
  415. {
  416. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=platform\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  417. }
  418. echo $row['role_name']."<br />\n";
  419. echo $row['role_comment']."<br />\n";
  420. }
  421. */
  422. // course roles
  423. $sql="SELECT * FROM $coures_roles_table";
  424. $result=Database::query($sql);
  425. while ($row=Database::fetch_array($result))
  426. {
  427. if(in_array($row['role_id'], $current_course_roles))
  428. {
  429. $checked='checked';
  430. $image='checkbox_on2.gif';
  431. $action='revoke';
  432. }
  433. else
  434. {
  435. $checked='';
  436. $image='wrong.gif';
  437. $action='grant';
  438. }
  439. if($setting_visualisation=='checkbox')
  440. {
  441. echo "<input type=\"checkbox\" name=\"role*course*".$row['role_id']."\" $checked>";
  442. }
  443. if($setting_visualisation=='image')
  444. {
  445. echo "<a href=\"".str_replace('&', '&amp;', $_SERVER['REQUEST_URI'])."&amp;action=$action&amp;role=".$row['role_id']."&amp;scope=course\"><img src=\"../img/".$image."\" border=\"0\"/></a>";
  446. }
  447. echo $row['role_name']." <a href=\"../permissions/roles.php?role_id=".$row['role_id']."&amp;scope=course\"><img src=\"../img/edit.gif\" /></a><br />\n";
  448. echo $row['role_comment']."<br />\n";
  449. }
  450. }
  451. /**
  452. * This function gets all the current roles of the user or group
  453. * @param $content are we finding the roles for a user or a group (the database depends on it)
  454. * @param $id the id of the user or group
  455. * @return array that contains the name of the roles the user has
  456. * @todo consider having a separate table that contains only an id and a name of the role.
  457. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  458. * @version 1.0
  459. */
  460. function get_roles($content,$id, $scope='course')
  461. {
  462. if($content=='user')
  463. {
  464. $table=Database::get_course_table(TABLE_ROLE_USER);
  465. $id_field = user_id;
  466. }
  467. if($content=='group')
  468. {
  469. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  470. $id_field = group_id;
  471. }
  472. $table_role=Database::get_course_table(TABLE_ROLE);
  473. $current_roles=array();
  474. //$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";$sql="SELECT role.role_id FROM $table role_group_user, $table_role role WHERE role_group_user.$id_field = '$id' AND role_group_user.role_id=role.role_id AND role_group_user.scope='".$scope."'";
  475. $sql="SELECT role_id FROM $table WHERE $id_field = '$id' AND scope='".$scope."'";
  476. $result=Database::query($sql);
  477. while ($row=Database::fetch_array($result))
  478. {
  479. $current_roles[]=$row['role_id'];
  480. }
  481. return $current_roles;
  482. }
  483. /**
  484. * This function gets all the current roles of the user or group
  485. * @return array that contains the name of the roles the user has
  486. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  487. * @version 1.0
  488. */
  489. function get_all_roles($content='course')
  490. {
  491. if($content=='course')
  492. {
  493. $table_role=Database::get_course_table(TABLE_ROLE);
  494. }
  495. if($content=='platform')
  496. {
  497. $table_role=Database::get_main_table(TABLE_ROLE);
  498. }
  499. $current_roles=array();
  500. $sql="SELECT * FROM $table_role";
  501. $result=Database::query($sql);
  502. while ($row=Database::fetch_array($result))
  503. {
  504. $roles[]=$row;
  505. }
  506. return $roles;
  507. }
  508. /**
  509. * This function gets all the roles that are defined
  510. * @param $content are we finding the roles for a user or a group (the database depends on it)
  511. * @param $id the id of the user or group
  512. * @param string Deprecated parameter allowing use of 'platform' scope - the corresponding tables don't exist anymore so the scope is always set to 'course'
  513. * @return array that contains the name of the roles the user has
  514. * @todo consider having a separate table that contains only an id and a name of the role.
  515. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  516. * @version 1.0
  517. */
  518. function get_roles_permissions($content,$id, $scope='course')
  519. {
  520. if($content == 'user')
  521. {
  522. $table=Database::get_course_table(TABLE_ROLE_USER);
  523. $id_field = user_id;
  524. }
  525. if($content == 'group')
  526. {
  527. $table = Database::get_course_table(TABLE_ROLE_GROUP);
  528. $id_field = group_id;
  529. }
  530. // course roles or platform roles
  531. $scope = 'course';
  532. if($scope == 'course')
  533. {
  534. $table_role = Database::get_course_table(TABLE_ROLE);
  535. $table_role_permissions = Database::get_course_table(TABLE_ROLE_PERMISSION);
  536. }
  537. if($scope == 'platform')
  538. {
  539. $table_role = Database::get_main_table(TABLE_ROLE);
  540. $table_role_permissions = Database::get_main_table(TABLE_ROLE_PERMISSION);
  541. }
  542. $current_roles = array();
  543. $sql = "
  544. SELECT *
  545. FROM
  546. " . $table . " role_group_user,
  547. " . $table_role . " role,
  548. " . $table_role_permissions . " role_permissions
  549. WHERE
  550. role_group_user.scope = '" . $scope . "' AND
  551. role_group_user." . $id_field . " = '" . $id . "' AND
  552. role_group_user.role_id = role.role_id AND
  553. role.role_id = role_permissions.role_id";
  554. $result = Database::query($sql);
  555. while($row=Database::fetch_array($result))
  556. $current_role_permissions[$row['tool']][]=$row['action'];
  557. return $current_role_permissions;
  558. }
  559. /**
  560. * This function is called when we assign a role to a user or a group
  561. * @param $content are we assigning a role to a group or a user
  562. * @param $action we can grant a role to a group or user or revoke it
  563. * @param $id the user_id of the user or the group_id of the group
  564. * @param $role_id the id of the role we are giving to a user or a group.
  565. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  566. */
  567. function assign_role($content, $action, $id, $role_id, $scope='course')
  568. {
  569. // Which database are we using (depending on the $content parameter)
  570. if($content=='user')
  571. {
  572. $table=Database::get_course_table(TABLE_ROLE_USER);
  573. $id_field = user_id;
  574. }
  575. elseif($content=='group')
  576. {
  577. $table=Database::get_course_table(TABLE_ROLE_GROUP);
  578. $id_field = group_id;
  579. }
  580. else
  581. {
  582. return get_lang('Error');
  583. }
  584. // grating a right
  585. if($action=='grant')
  586. {
  587. $sql="INSERT INTO $table (role_id, scope, $id_field) VALUES ('".Database::escape_string($role_id)."','".Database::escape_string($scope)."','".Database::escape_string($id)."')";
  588. $result=Database::query($sql);
  589. if($result)
  590. {
  591. $result_message=get_lang('RoleGranted');
  592. }
  593. }
  594. if($action=='revoke')
  595. {
  596. $sql="DELETE FROM $table WHERE $id_field = '".Database::escape_string($id)."' AND role_id='".Database::escape_string($role_id)."'";
  597. $result=Database::query($sql);
  598. if($result)
  599. {
  600. $result_message=get_lang('RoleRevoked');
  601. }
  602. }
  603. return $result_message;
  604. }
  605. /**
  606. * This function merges permission arrays. Each permission array has the following structure
  607. * a permission array has a tool contanst as a key and an array as a value. This value array consists of all the permissions that are granted in that tool.
  608. */
  609. function permission_array_merge($array1, $array2)
  610. {
  611. foreach ($array2 as $tool=>$permissions)
  612. {
  613. foreach ($permissions as $permissionkey=>$permissionvalue)
  614. {
  615. $array1[$tool][]=$permissionvalue;
  616. }
  617. }
  618. return $array1;
  619. }
  620. function my_print_r($array)
  621. {
  622. echo '<pre>';
  623. print_r($array);
  624. echo '</pre>';
  625. }
  626. ?>