groups.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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) 2016-2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * groups class provides methods for add, delete groups, and add default groups
  23. *
  24. * @method null delete
  25. * @method null toggle
  26. * @method null copy
  27. */
  28. if (!class_exists('groups')) {
  29. class groups {
  30. /**
  31. * declare the variables
  32. */
  33. private $database;
  34. private $app_name;
  35. private $app_uuid;
  36. public $group_uuid;
  37. private $groups;
  38. public $group_level;
  39. private $name;
  40. private $table;
  41. private $toggle_field;
  42. private $toggle_values;
  43. private $location;
  44. private $user_uuid;
  45. private $domain_uuid;
  46. /**
  47. * called when the object is created
  48. */
  49. public function __construct(database $database = null, $domain_uuid = null, $user_uuid = null) {
  50. //assign the variables
  51. $this->app_name = 'groups';
  52. $this->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  53. //handle the database object
  54. if (isset($database)) {
  55. $this->database = $database;
  56. }
  57. else {
  58. $this->database = new database;
  59. }
  60. //set the application name and uuid
  61. $this->database->app_name = $this->app_name;
  62. $this->database->app_uuid = $this->app_uuid;
  63. //set the domain_uuid
  64. if (is_uuid($domain_uuid)) {
  65. $this->domain_uuid = $domain_uuid;
  66. }
  67. //set the user_uuid
  68. if (is_uuid($user_uuid)) {
  69. $this->user_uuid = $user_uuid;
  70. }
  71. //get the list of groups the user is a member of
  72. if (!empty($domain_uuid) && !empty($user_uuid)) {
  73. //get the groups and save them to the groups variable
  74. $this->groups = $this->assigned();
  75. //get the users group level
  76. $group_level = 0;
  77. foreach ($this->groups as $row) {
  78. if ($this->group_level < $row['group_level']) {
  79. $this->group_level = $row['group_level'];
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * get the list of groups the user is assigned to
  86. */
  87. public function get_groups() {
  88. //return the groups
  89. return $this->groups;
  90. }
  91. /**
  92. * delete rows from the database
  93. */
  94. public function delete($records) {
  95. //assign the variables
  96. $this->name = 'group';
  97. $this->table = 'groups';
  98. $this->location = 'groups.php';
  99. if (permission_exists($this->name.'_delete')) {
  100. //add multi-lingual support
  101. $language = new text;
  102. $text = $language->get();
  103. //validate the token
  104. $token = new token;
  105. if (!$token->validate($_SERVER['PHP_SELF'])) {
  106. message::add($text['message-invalid_token'],'negative');
  107. header('Location: '.$this->location);
  108. exit;
  109. }
  110. //delete multiple records
  111. if (is_array($records) && @sizeof($records) != 0) {
  112. //build array of checked records
  113. foreach ($records as $x => $record) {
  114. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  115. $array[$this->table][$x][$this->name.'_uuid'] = $record['uuid'];
  116. $array['group_permissions'][$x][$this->name.'_uuid'] = $record['uuid'];
  117. }
  118. }
  119. //delete the checked rows
  120. if (is_array($array) && @sizeof($array) != 0) {
  121. //grant temporary permissions
  122. $p = permissions::new();
  123. $p->add('group_permission_delete', 'temp');
  124. //execute delete
  125. $this->database->delete($array);
  126. unset($array);
  127. //revoke temporary permissions
  128. $p->delete('group_permission_delete', 'temp');
  129. //set message
  130. message::add($text['message-delete']);
  131. }
  132. unset($records);
  133. }
  134. }
  135. }
  136. public function delete_members($records) {
  137. //assign the variables
  138. $this->name = 'group_member';
  139. $this->table = 'user_groups';
  140. $this->location = 'group_members.php?group_uuid='.$this->group_uuid;
  141. if (permission_exists($this->name.'_delete')) {
  142. //add multi-lingual support
  143. $language = new text;
  144. $text = $language->get();
  145. //validate the token
  146. $token = new token;
  147. if (!$token->validate($_SERVER['PHP_SELF'])) {
  148. message::add($text['message-invalid_token'],'negative');
  149. header('Location: '.$this->location);
  150. exit;
  151. }
  152. //delete multiple records
  153. if (is_array($records) && @sizeof($records) != 0) {
  154. //build array of checked records
  155. foreach ($records as $x => $record) {
  156. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  157. $array[$this->table][$x]['user_uuid'] = $record['uuid'];
  158. $array[$this->table][$x]['group_uuid'] = $this->group_uuid;
  159. }
  160. }
  161. //delete the checked rows
  162. if (is_array($array) && @sizeof($array) != 0) {
  163. //grant temporary permissions
  164. $p = permissions::new();
  165. $p->add('user_group_delete', 'temp');
  166. //execute delete
  167. $this->database->delete($array);
  168. unset($array);
  169. //revoke temporary permissions
  170. $p->delete('user_group_delete', 'temp');
  171. //set message
  172. message::add($text['message-delete']);
  173. }
  174. unset($records);
  175. }
  176. }
  177. }
  178. /**
  179. * toggle a field between two values
  180. */
  181. public function toggle($records) {
  182. //assign the variables
  183. $this->name = 'group';
  184. $this->table = 'groups';
  185. $this->toggle_field = 'group_protected';
  186. $this->toggle_values = ['true','false'];
  187. $this->location = 'groups.php';
  188. if (permission_exists($this->name.'_edit')) {
  189. //add multi-lingual support
  190. $language = new text;
  191. $text = $language->get();
  192. //validate the token
  193. $token = new token;
  194. if (!$token->validate($_SERVER['PHP_SELF'])) {
  195. message::add($text['message-invalid_token'],'negative');
  196. header('Location: '.$this->location);
  197. exit;
  198. }
  199. //toggle the checked records
  200. if (is_array($records) && @sizeof($records) != 0) {
  201. //get current toggle state
  202. foreach($records as $record) {
  203. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  204. $uuids[] = "'".$record['uuid']."'";
  205. }
  206. }
  207. if (is_array($uuids) && @sizeof($uuids) != 0) {
  208. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  209. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  210. $sql .= "and ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  211. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  212. $rows = $this->database->select($sql, $parameters, 'all');
  213. if (is_array($rows) && @sizeof($rows) != 0) {
  214. foreach ($rows as $row) {
  215. $states[$row['uuid']] = $row['toggle'];
  216. }
  217. }
  218. unset($sql, $parameters, $rows, $row);
  219. }
  220. //build update array
  221. $x = 0;
  222. foreach($states as $uuid => $state) {
  223. //create the array
  224. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  225. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  226. //increment the id
  227. $x++;
  228. }
  229. //save the changes
  230. if (is_array($array) && @sizeof($array) != 0) {
  231. //save the array
  232. $this->database->save($array);
  233. unset($array);
  234. //set message
  235. message::add($text['message-toggle']);
  236. }
  237. unset($records, $states);
  238. }
  239. }
  240. }
  241. /**
  242. * copy rows from the database
  243. */
  244. public function copy($records) {
  245. //assign the variables
  246. $this->name = 'group';
  247. $this->table = 'groups';
  248. $this->location = 'groups.php';
  249. if (permission_exists($this->name.'_add')) {
  250. //add multi-lingual support
  251. $language = new text;
  252. $text = $language->get();
  253. //validate the token
  254. $token = new token;
  255. if (!$token->validate($_SERVER['PHP_SELF'])) {
  256. message::add($text['message-invalid_token'],'negative');
  257. header('Location: '.$this->location);
  258. exit;
  259. }
  260. //copy the checked records
  261. if (is_array($records) && @sizeof($records) != 0) {
  262. //get checked records
  263. foreach($records as $record) {
  264. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  265. $uuids[] = "'".$record['uuid']."'";
  266. }
  267. }
  268. //create the array from existing data
  269. if (is_array($uuids) && @sizeof($uuids) != 0) {
  270. //primary table
  271. $sql = "select * from v_".$this->table." ";
  272. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  273. $sql .= "and ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  274. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  275. $rows = $this->database->select($sql, $parameters, 'all');
  276. if (is_array($rows) && @sizeof($rows) != 0) {
  277. $y = 0;
  278. foreach ($rows as $x => $row) {
  279. $primary_uuid = uuid();
  280. //copy data
  281. $array[$this->table][$x] = $row;
  282. //overwrite
  283. $array[$this->table][$x][$this->name.'_uuid'] = $primary_uuid;
  284. $array[$this->table][$x][$this->name.'_description'] = trim($row[$this->name.'_description']).' ('.$text['label-copy'].')';
  285. //permissions sub table
  286. $sql_2 = "select * from v_group_permissions where group_uuid = :group_uuid";
  287. $parameters_2['group_uuid'] = $row['group_uuid'];
  288. $rows_2 = $this->database->select($sql_2, $parameters_2, 'all');
  289. if (is_array($rows_2) && @sizeof($rows_2) != 0) {
  290. foreach ($rows_2 as $row_2) {
  291. //copy data
  292. $array['group_permissions'][$y] = $row_2;
  293. //overwrite
  294. $array['group_permissions'][$y]['group_permission_uuid'] = uuid();
  295. $array['group_permissions'][$y]['group_uuid'] = $primary_uuid;
  296. //increment
  297. $y++;
  298. }
  299. }
  300. unset($sql_2, $parameters_2, $rows_2, $row_2);
  301. }
  302. }
  303. unset($sql, $parameters, $rows, $row);
  304. }
  305. //save the changes and set the message
  306. if (is_array($array) && @sizeof($array) != 0) {
  307. //save the array
  308. $this->database->save($array);
  309. unset($array);
  310. //set message
  311. message::add($text['message-copy']);
  312. }
  313. unset($records);
  314. }
  315. }
  316. }
  317. /**
  318. * add defaults groups
  319. */
  320. public function defaults() {
  321. //if the are no groups add the default groups
  322. $sql = "select * from v_groups ";
  323. $sql .= "where domain_uuid is null ";
  324. $result = $this->database->select($sql, null, 'all');
  325. if (count($result) == 0) {
  326. $x = 0;
  327. $array['groups'][$x]['group_uuid'] = uuid();
  328. $array['groups'][$x]['domain_uuid'] = null;
  329. $array['groups'][$x]['group_name'] = 'superadmin';
  330. $array['groups'][$x]['group_level'] = '80';
  331. $array['groups'][$x]['group_description'] = 'Super Administrator Group';
  332. $array['groups'][$x]['group_protected'] = 'false';
  333. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  334. $x++;
  335. $array['groups'][$x]['group_uuid'] = uuid();
  336. $array['groups'][$x]['domain_uuid'] = null;
  337. $array['groups'][$x]['group_name'] = 'admin';
  338. $array['groups'][$x]['group_level'] = '50';
  339. $array['groups'][$x]['group_description'] = 'Administrator Group';
  340. $array['groups'][$x]['group_protected'] = 'false';
  341. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  342. $x++;
  343. $array['groups'][$x]['group_uuid'] = uuid();
  344. $array['groups'][$x]['domain_uuid'] = null;
  345. $array['groups'][$x]['group_name'] = 'user';
  346. $array['groups'][$x]['group_level'] = '30';
  347. $array['groups'][$x]['group_description'] = 'User Group';
  348. $array['groups'][$x]['group_protected'] = 'false';
  349. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  350. $x++;
  351. $array['groups'][$x]['group_uuid'] = uuid();
  352. $array['groups'][$x]['domain_uuid'] = null;
  353. $array['groups'][$x]['group_name'] = 'agent';
  354. $array['groups'][$x]['group_level'] = '20';
  355. $array['groups'][$x]['group_description'] = 'Call Center Agent Group';
  356. $array['groups'][$x]['group_protected'] = 'false';
  357. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  358. $x++;
  359. $array['groups'][$x]['group_uuid'] = uuid();
  360. $array['groups'][$x]['domain_uuid'] = null;
  361. $array['groups'][$x]['group_name'] = 'fax';
  362. $array['groups'][$x]['group_level'] = '20';
  363. $array['groups'][$x]['group_description'] = 'Fax User Group';
  364. $array['groups'][$x]['group_protected'] = 'false';
  365. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  366. $x++;
  367. $array['groups'][$x]['group_uuid'] = uuid();
  368. $array['groups'][$x]['domain_uuid'] = null;
  369. $array['groups'][$x]['group_name'] = 'public';
  370. $array['groups'][$x]['group_level'] = '10';
  371. $array['groups'][$x]['group_description'] = 'Public Group';
  372. $array['groups'][$x]['group_protected'] = 'false';
  373. $group_uuids[$array['groups'][$x]['group_name']] = $array['groups'][$x]['group_uuid'];
  374. //add the temporary permissions
  375. $p = permissions::new();
  376. $p->add("group_add", "temp");
  377. $p->add("group_edit", "temp");
  378. //save the data to the database
  379. $this->database->save($array);
  380. unset($array);
  381. //remove the temporary permission
  382. $p->delete("group_add", "temp");
  383. $p->delete("group_edit", "temp");
  384. }
  385. unset($result);
  386. //if there are no permissions listed in v_group_permissions then set the default permissions
  387. $sql = "select count(*) from v_group_permissions ";
  388. $sql .= "where domain_uuid is null ";
  389. $num_rows = $this->database->select($sql, null, 'column');
  390. if ($num_rows == 0) {
  391. //build the apps array
  392. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  393. $x = 0;
  394. foreach ($config_list as $config_path) {
  395. include($config_path);
  396. $x++;
  397. }
  398. //no permissions found add the defaults
  399. foreach($apps as $app) {
  400. if (is_array($app['permissions'])) foreach ($app['permissions'] as $row) {
  401. if (is_array($row['groups'])) foreach ($row['groups'] as $group) {
  402. $x++;
  403. $array['group_permissions'][$x]['group_permission_uuid'] = uuid();
  404. $array['group_permissions'][$x]['domain_uuid'] = null;
  405. $array['group_permissions'][$x]['permission_name'] = $row['name'];
  406. $array['group_permissions'][$x]['permission_protected'] = 'false';
  407. $array['group_permissions'][$x]['permission_assigned'] = 'true';
  408. $array['group_permissions'][$x]['group_name'] = $group;
  409. $array['group_permissions'][$x]['group_uuid'] = $group_uuids[$group];
  410. }
  411. }
  412. }
  413. unset($group_uuids);
  414. //add the temporary permissions
  415. $p = permissions::new();
  416. $p->add("group_permission_add", "temp");
  417. $p->add("group_permission_edit", "temp");
  418. //save the data to the database
  419. $this->database->save($array);
  420. unset($array);
  421. //remove the temporary permission
  422. $p->delete("group_permission_add", "temp");
  423. $p->delete("group_permission_edit", "temp");
  424. }
  425. }
  426. /**
  427. * get the groups assigned to the user
  428. */
  429. public function assigned() {
  430. $sql = "select ";
  431. $sql .= "u.user_group_uuid, ";
  432. $sql .= "u.domain_uuid, ";
  433. $sql .= "u.user_uuid, ";
  434. $sql .= "u.group_uuid, ";
  435. $sql .= "g.group_name, ";
  436. $sql .= "g.group_level ";
  437. $sql .= "from ";
  438. $sql .= "v_user_groups as u, ";
  439. $sql .= "v_groups as g ";
  440. $sql .= "where u.domain_uuid = :domain_uuid ";
  441. $sql .= "and u.user_uuid = :user_uuid ";
  442. $sql .= "and u.group_uuid = g.group_uuid ";
  443. $parameters['domain_uuid'] = $this->domain_uuid;
  444. $parameters['user_uuid'] = $this->user_uuid;
  445. $groups = $this->database->select($sql, $parameters, 'all');
  446. unset($sql, $parameters);
  447. if (!empty($groups)) {
  448. return $groups;
  449. }
  450. else {
  451. return [];
  452. }
  453. }
  454. /**
  455. * add the assigned groups to the session array
  456. */
  457. public function session() {
  458. $_SESSION["groups"] = $this->groups;
  459. $_SESSION["user"]["groups"] = $this->groups;
  460. $_SESSION["user"]["group_level"] = $this->group_level;
  461. }
  462. }
  463. }
  464. ?>