group_permissions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //includes
  22. require_once "root.php";
  23. require_once "resources/require.php";
  24. require_once "resources/check_auth.php";
  25. require_once "resources/paging.php";
  26. //check permisions
  27. if (permission_exists('group_permissions') || if_group("superadmin")) {
  28. //access granted
  29. }
  30. else {
  31. echo "access denied";
  32. exit;
  33. }
  34. //add multi-lingual support
  35. $language = new text;
  36. $text = $language->get();
  37. //get the list of installed apps from the core and mod directories
  38. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  39. $x = 0;
  40. foreach ($config_list as &$config_path) {
  41. include($config_path);
  42. $x++;
  43. }
  44. //get the group uuid passed
  45. $group_uuid = $_REQUEST['group_uuid'];
  46. //if there are no permissions listed in v_group_permissions then set the default permissions
  47. $sql = "select count(*) from v_group_permissions ";
  48. $database = new database;
  49. $group_permission_count = $database->select($sql, null, 'column');
  50. if ($group_permission_count == 0) {
  51. //no permissions found add the defaults
  52. foreach ($apps as $app) {
  53. foreach ($app['permissions'] as $row) {
  54. foreach ($row['groups'] as $index => $group_name) {
  55. //add the record
  56. $array['group_permissions'][$index]['group_permission_uuid'] = uuid();
  57. $array['group_permissions'][$index]['permission_name'] = $row['name'];
  58. $array['group_permissions'][$index]['group_name'] = $group_name;
  59. $array['group_permissions'][$index]['group_uuid'] = $group_uuid;
  60. }
  61. if (is_array($array) && sizeof($array) != 0) {
  62. $database = new database;
  63. $database->app_name = 'groups';
  64. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  65. $database->save($array);
  66. unset($array);
  67. }
  68. }
  69. }
  70. }
  71. unset($sql, $group_name);
  72. //lookup domain uuid (if any) and name
  73. $sql = "select domain_uuid, group_name from v_groups ";
  74. $sql .= "where group_uuid = :group_uuid ";
  75. $parameters['group_uuid'] = $group_uuid;
  76. $database = new database;
  77. $row = $database->select($sql, $parameters, 'row');
  78. if (is_array($row) && sizeof($row) != 0) {
  79. $domain_uuid = $row["domain_uuid"];
  80. $group_name = $row["group_name"];
  81. }
  82. unset($sql, $parameters, $row);
  83. //add the search string
  84. $search = strtolower($_GET["search"]);
  85. if (strlen($search) > 0) {
  86. $sql_search .= " and lower(permission_name) like :search ";
  87. $parameters['search'] = '%'.$search.'%';
  88. }
  89. //get the permissions assigned to this group
  90. $sql = "select * from v_group_permissions ";
  91. $sql .= "where group_name = :group_name ";
  92. if (is_uuid($domain_uuid)) {
  93. $sql .= "and domain_uuid = :domain_uuid ";
  94. $parameters['domain_uuid'] = $domain_uuid;
  95. }
  96. else {
  97. $sql .= "and domain_uuid is null ";
  98. }
  99. $sql .= $sql_search;
  100. $parameters['group_name'] = $group_name;
  101. $database = new database;
  102. $result = $database->select($sql, $parameters, 'all');
  103. if (is_array($result) && sizeof($result) != 0) {
  104. foreach ($result as &$row) {
  105. $permissions_db[$row["permission_name"]] = "true";
  106. }
  107. }
  108. unset($sql, $parameters, $result, $row);
  109. //list all the permissions in the database
  110. foreach ($apps as $app) {
  111. if (isset($app['permissions'])) foreach ($app['permissions'] as $row) {
  112. if ($permissions_db[$row['name']] == "true") {
  113. $permissions_db_checklist[$row['name']] = "true";
  114. }
  115. else {
  116. $permissions_db_checklist[$row['name']] = "false";
  117. }
  118. }
  119. }
  120. //process the http post
  121. if (count($_POST)>0) {
  122. if (is_array($_POST['permissions_form']) && @sizeof($_POST['permissions_form'])) {
  123. foreach ($_POST['permissions_form'] as $permission) {
  124. $permissions_form[$permission] = "true";
  125. }
  126. }
  127. //list all the permissions
  128. foreach ($apps as $app) {
  129. if (is_array($app['permissions']) && @sizeof($app['permissions']) != 0) {
  130. foreach ($app['permissions'] as $row) {
  131. if ($permissions_form[$row['name']] == "true") {
  132. $permissions_form_checklist[$row['name']] = "true";
  133. }
  134. else {
  135. $permissions_form_checklist[$row['name']] = "false";
  136. }
  137. }
  138. }
  139. }
  140. //list all the permissions
  141. foreach ($apps as $app) {
  142. if (is_array($app['permissions']) && @sizeof($app['permissions']) != 0) {
  143. foreach ($app['permissions'] as $row) {
  144. $permission = $row['name'];
  145. if ($permissions_db_checklist[$permission] == "true" && $permissions_form_checklist[$permission] == "true") {
  146. //matched do nothing
  147. }
  148. if ($permissions_db_checklist[$permission] == "false" && $permissions_form_checklist[$permission] == "false") {
  149. //matched do nothing
  150. }
  151. if ($permissions_db_checklist[$permission] == "true" && $permissions_form_checklist[$permission] == "false") {
  152. //delete the record
  153. $array['group_permissions'][0]['group_name'] = $group_name;
  154. $array['group_permissions'][0]['permission_name'] = $permission;
  155. $array['group_permissions'][0]['group_uuid'] = $group_uuid;
  156. $database = new database;
  157. $database->app_name = 'groups';
  158. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  159. $database->delete($array);
  160. unset($array);
  161. foreach ($apps as $app) {
  162. if (is_array($app['permissions']) && @sizeof($app['permissions']) != 0) {
  163. foreach ($app['permissions'] as $row) {
  164. if ($row['name'] == $permission) {
  165. $array['menu_item_groups'][0]['menu_item_uuid'] = $row['menu']['uuid'];
  166. $array['menu_item_groups'][0]['group_name'] = $group_name;
  167. $array['menu_item_groups'][0]['menu_uuid'] = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  168. $p = new permissions;
  169. $p->add('menu_item_group_delete', 'temp');
  170. $database = new database;
  171. $database->app_name = 'groups';
  172. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  173. $database->delete($array);
  174. unset($array);
  175. $p->delete('menu_item_group_delete', 'temp');
  176. $sql = "select menu_item_parent_uuid from v_menu_items ";
  177. $sql .= "where menu_item_uuid = :menu_item_uuid ";
  178. $sql .= "and menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286' ";
  179. $parameters['menu_item_uuid'] = $row['menu']['uuid'];
  180. $database = new database;
  181. $menu_item_parent_uuid = $database->select($sql, $parameters, 'column');
  182. unset($sql, $parameters);
  183. $sql = "select count(*) from v_menu_items as i, v_menu_item_groups as g ";
  184. $sql .= "where i.menu_item_uuid = g.menu_item_uuid ";
  185. $sql .= "and i.menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286' ";
  186. $sql .= "and i.menu_item_parent_uuid = :menu_item_parent_uuid ";
  187. $sql .= "and g.group_name = :group_name ";
  188. $parameters['menu_item_parent_uuid'] = $menu_item_parent_uuid;
  189. $parameters['group_name'] = $group_name;
  190. $database = new database;
  191. $result_count = $database->select($sql, $parameters, 'column');
  192. if ($result_count == 0) {
  193. $array['menu_item_groups'][0]['menu_item_uuid'] = $menu_item_parent_uuid;
  194. $array['menu_item_groups'][0]['group_name'] = $group_name;
  195. $array['menu_item_groups'][0]['menu_uuid'] = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  196. $p = new permissions;
  197. $p->add('menu_item_group_delete', 'temp');
  198. $database = new database;
  199. $database->app_name = 'groups';
  200. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  201. $database->delete($array);
  202. unset($array);
  203. $p->delete('menu_item_group_delete', 'temp');
  204. }
  205. unset($sql, $parameters, $result_count);
  206. }
  207. }
  208. }
  209. }
  210. //set the permission to false in the permissions_db_checklist
  211. $permissions_db_checklist[$permission] = "false";
  212. }
  213. if ($permissions_db_checklist[$permission] == "false" && $permissions_form_checklist[$permission] == "true") {
  214. //add the record
  215. $array['group_permissions'][0]['group_permission_uuid'] = uuid();
  216. if (is_uuid($domain_uuid)) {
  217. $array['group_permissions'][0]['domain_uuid'] = $domain_uuid;
  218. }
  219. $array['group_permissions'][0]['permission_name'] = $permission;
  220. $array['group_permissions'][0]['group_name'] = $group_name;
  221. $array['group_permissions'][0]['group_uuid'] = $group_uuid;
  222. $database = new database;
  223. $database->app_name = 'groups';
  224. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  225. $database->save($array);
  226. unset($array);
  227. foreach ($apps as $app) {
  228. if (is_array($app['permissions']) && @sizeof($app['permissions']) != 0) {
  229. foreach ($app['permissions'] as $row) {
  230. if ($row['name'] == $permission) {
  231. $array['menu_item_groups'][0]['menu_uuid'] = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  232. $array['menu_item_groups'][0]['menu_item_uuid'] = $row['menu']['uuid'];
  233. $array['menu_item_groups'][0]['group_name'] = $group_name;
  234. $p = new permissions;
  235. $p->add('menu_item_group_add', 'temp');
  236. $database = new database;
  237. $database->app_name = 'groups';
  238. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  239. $database->save($array);
  240. unset($array);
  241. $p->delete('menu_item_group_add', 'temp');
  242. $sql = "select menu_item_parent_uuid from v_menu_items ";
  243. $sql .= "where menu_item_uuid = :menu_item_uuid ";
  244. $sql .= "and menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286' ";
  245. $parameters['menu_item_uuid'] = $row['menu']['uuid'];
  246. $database = new database;
  247. $menu_item_parent_uuid = $database->select($sql, $parameters, 'column');
  248. unset($sql, $parameters);
  249. $sql = "select count(*) from v_menu_item_groups ";
  250. $sql .= "where menu_item_uuid = :menu_item_uuid ";
  251. $sql .= "and group_name = :group_name ";
  252. $sql .= "and menu_uuid = 'b4750c3f-2a86-b00d-b7d0-345c14eca286' ";
  253. $parameters['menu_item_uuid'] = $menu_item_parent_uuid;
  254. $parameters['group_name'] = $group_name;
  255. $database = new database;
  256. $result_count = $database->select($sql, $parameters, 'column');
  257. if ($result_count == 0) {
  258. $array['menu_item_groups'][0]['menu_uuid'] = 'b4750c3f-2a86-b00d-b7d0-345c14eca286';
  259. $array['menu_item_groups'][0]['menu_item_uuid'] = $menu_item_parent_uuid;
  260. $array['menu_item_groups'][0]['group_name'] = $group_name;
  261. $p = new permissions;
  262. $p->add('menu_item_group_add', 'temp');
  263. $database = new database;
  264. $database->app_name = 'groups';
  265. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  266. $database->save($array);
  267. unset($array);
  268. $p->delete('menu_item_group_add', 'temp');
  269. }
  270. unset($sql, $parameters, $result_count);
  271. }
  272. }
  273. }
  274. }
  275. //set the permission to true in the permissions_db_checklist
  276. $permissions_db_checklist[$permission] = "true";
  277. }
  278. }
  279. }
  280. }
  281. message::add($text['message-update']);
  282. header("Location: groups.php");
  283. return;
  284. }
  285. //include the header
  286. $document['title'] = $text['title-group_permissions'];
  287. require_once "resources/header.php";
  288. //show the content
  289. echo "<div class='action_bar' id='action_bar'>\n";
  290. echo " <div class='heading'><b>".$text['header-group_permissions'].'<i>'.escape($group_name)."</i></b></div>\n";
  291. echo " <div class='actions'>\n";
  292. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'collapse'=>'hide-sm-dn','link'=>'groups.php']);
  293. echo "<form id='form_search' class='inline' method='get'>\n";
  294. echo "<input type='hidden' name='group_uuid' value='".escape($group_uuid)."'>\n";
  295. echo "<input type='text' class='txt list-search' name='search' id='search' value=\"".escape($search)."\" placeholder=\"".$text['label-search']."\" onkeydown='list_search_reset();'>";
  296. echo button::create(['label'=>$text['button-search'],'icon'=>$_SESSION['theme']['button_icon_search'],'type'=>'submit','id'=>'btn_search','collapse'=>'hide-sm-dn','style'=>($search != '' ? 'display: none;' : null)]);
  297. echo button::create(['label'=>$text['button-reset'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'btn_reset','collapse'=>'hide-sm-dn','link'=>'group_permissions.php?group_uuid='.urlencode($group_uuid),'style'=>($search == '' ? 'display: none;' : null)]);
  298. echo button::create(['type'=>'button','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'collapse'=>'hide-sm-dn','style'=>'margin-left: 15px;','onclick'=>"document.getElementById('frm').submit();"]);
  299. echo " </form>\n";
  300. echo " </div>\n";
  301. echo " <div style='clear: both;'></div>\n";
  302. echo "</div>\n";
  303. echo $text['description-group_permissions']."\n";
  304. echo "<br /><br />\n";
  305. echo "<form method='post' name='frm' id='frm'>\n";
  306. foreach ($apps as $app_index => $app) {
  307. //skip apps for which there are no permissions
  308. if (!is_array($app['permissions']) || sizeof($app['permissions']) == 0) { continue; }
  309. //skip apps for which search doesn't match at least one permission
  310. if ($search) {
  311. $permission_matched = false;
  312. foreach ($app['permissions'] as $row) {
  313. if (substr_count(strtolower($row['name']), strtolower($search)) > 0) {
  314. $permission_matched = true;
  315. break;
  316. }
  317. }
  318. if (!$permission_matched) { continue; }
  319. }
  320. $app_name = $app['name'];
  321. $description = $app['description']['en-us'];
  322. //used to hide apps, even if permissions don't exist
  323. $array_apps_unique[] = str_replace(' ','_',strtolower($app['name']));
  324. echo "<b>".$app_name."</b><br />\n";
  325. if ($description != '') { echo $description."<br />\n"; }
  326. echo "<br>";
  327. echo "<table class='list'>\n";
  328. echo " <tr class='list-header'>\n";
  329. echo " <th class='checkbox'>\n";
  330. echo " <input type='checkbox' id='checkbox_all_".$app_index."' name='checkbox_all' onclick=\"list_all_toggle('".$app_index."');\">\n";
  331. echo " </th>\n";
  332. echo " <th class='pct-60'>".$text['label-permission_permissions']."</th>\n";
  333. echo " <th class='pct-40 hide-xs'>".$text['label-permission_description']."&nbsp;</th>\n";
  334. echo " <tr>\n";
  335. foreach ($app['permissions'] as $permission_index => $row) {
  336. //skip permission if doesn't match search
  337. if ($search && substr_count(strtolower($row['name']), strtolower($search)) == 0) { continue; }
  338. $checked = ($permissions_db_checklist[$row['name']] == "true") ? "checked='checked'" : null;
  339. echo "<tr class='list-row'>\n";
  340. echo " <td class='checkbox'>\n";
  341. echo " <input type='checkbox' name='permissions_form[]' id='perm_".$app_index."_".$permission_index."' class='checkbox_".$app_index."' ".$checked." value='".escape($row['name'])."' onclick=\"if (!this.checked) { document.getElementById('checkbox_all_".$app_index."').checked = false; }\">\n";
  342. echo " </td>\n";
  343. echo " <td class='no-wrap' onclick=\"if (document.getElementById('perm_".$app_index."_".$permission_index."').checked) { document.getElementById('perm_".$app_index."_".$permission_index."').checked = false; document.getElementById('checkbox_all_".$app_index."').checked = false; } else { document.getElementById('perm_".$app_index."_".$permission_index."').checked = true; }\">".escape($row['name'])."</td>\n";
  344. echo " <td class='description overflow hide-xs' onclick=\"if (document.getElementById('perm_".$app_index."_".$permission_index."').checked) { document.getElementById('perm_".$app_index."_".$permission_index."').checked = false; document.getElementById('checkbox_all_".$app_index."').checked = false; } else { document.getElementById('perm_".$app_index."_".$permission_index."').checked = true; }\">".escape($row['description'])."&nbsp;</td>\n";
  345. echo "</tr>\n";
  346. //populate search/filter arrays
  347. $array_apps[] = str_replace(' ','_',strtolower($app['name']));
  348. $array_apps_original[] = $app['name'];
  349. $array_permissions[] = $row['name'];
  350. $array_descriptions[] = str_replace('"','\"',$row['description']);
  351. $app_permissions[$app_index][] = "perm_".$app_index."_".$permission_index;
  352. }
  353. echo "</table>\n";
  354. echo "<br /><br />\n";
  355. }
  356. echo "<input type='hidden' name='group_uuid' value='".escape($group_uuid)."'>\n";
  357. echo "<input type='hidden' name='domain_uuid' value='".escape($domain_uuid)."'>\n";
  358. echo "</form>\n";
  359. //show the footer
  360. require_once "resources/footer.php";
  361. ?>