groups.php 15 KB

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