permissions_functions.inc.php 21 KB

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