users.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * users class
  23. *
  24. * @method null delete
  25. * @method null toggle
  26. * @method null copy
  27. */
  28. if (!class_exists('users')) {
  29. class users {
  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. /**
  41. * called when the object is created
  42. */
  43. public function __construct() {
  44. //assign the variables
  45. $this->app_name = 'users';
  46. $this->app_uuid = '112124b3-95c2-5352-7e9d-d14c0b88f207';
  47. $this->name = 'user';
  48. $this->table = 'users';
  49. $this->toggle_field = 'user_enabled';
  50. $this->toggle_values = ['true','false'];
  51. $this->location = 'users.php';
  52. }
  53. /**
  54. * called when there are no references to a particular object
  55. * unset the variables used in the class
  56. */
  57. public function __destruct() {
  58. foreach ($this as $key => $value) {
  59. unset($this->$key);
  60. }
  61. }
  62. /**
  63. * delete rows from the database
  64. */
  65. public function delete($records) {
  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 the delete array
  80. $x = 0;
  81. foreach ($records as $record) {
  82. //add to the array
  83. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  84. //get the user_uuid
  85. $user_uuid = $record['uuid'];
  86. //get the user's domain from v_users
  87. if (permission_exists('user_domain')) {
  88. $sql = "select domain_uuid from v_users ";
  89. $sql .= "where user_uuid = :user_uuid ";
  90. $parameters['user_uuid'] = $user_uuid;
  91. $database = new database;
  92. $domain_uuid = $database->select($sql, $parameters, 'column');
  93. unset($sql, $parameters);
  94. }
  95. else {
  96. $domain_uuid = $_SESSION['domain_uuid'];
  97. }
  98. //required to be a superadmin to delete a member of the superadmin group
  99. $superadmin_list = superadmin_list();
  100. if (if_superadmin($superadmin_list, $user_uuid)) {
  101. if (!if_group("superadmin")) {
  102. //access denied - do not delete the user
  103. header("Location: index.php");
  104. return;
  105. }
  106. }
  107. //delete the user settings
  108. $array['user_settings'][$x]['user_uuid'] = $user_uuid;
  109. $array['user_settings'][$x]['domain_uuid'] = $domain_uuid;
  110. //delete the groups the user is assigned to
  111. $array['user_groups'][$x]['user_uuid'] = $user_uuid;
  112. $array['user_groups'][$x]['domain_uuid'] = $domain_uuid;
  113. //delete the user
  114. $array['users'][$x]['user_uuid'] = $user_uuid;
  115. $array['users'][$x]['domain_uuid'] = $domain_uuid;
  116. //increment the id
  117. $x++;
  118. }
  119. }
  120. //delete the checked rows
  121. if (is_array($array) && @sizeof($array) != 0) {
  122. //execute
  123. $p = new permissions;
  124. $p->add('user_setting_delete', 'temp');
  125. $p->add('user_group_delete', 'temp');
  126. //execute delete
  127. $database = new database;
  128. $database->app_name = $this->app_name;
  129. $database->app_uuid = $this->app_uuid;
  130. $database->delete($array);
  131. unset($array);
  132. $p->delete('user_setting_delete', 'temp');
  133. $p->delete('user_group_delete', 'temp');
  134. //set message
  135. message::add($text['message-delete']);
  136. }
  137. unset($records);
  138. }
  139. }
  140. }
  141. /**
  142. * toggle a field between two values
  143. */
  144. public function toggle($records) {
  145. if (permission_exists($this->name.'_edit')) {
  146. //add multi-lingual support
  147. $language = new text;
  148. $text = $language->get();
  149. //validate the token
  150. $token = new token;
  151. if (!$token->validate($_SERVER['PHP_SELF'])) {
  152. message::add($text['message-invalid_token'],'negative');
  153. header('Location: '.$this->location);
  154. exit;
  155. }
  156. //toggle the checked records
  157. if (is_array($records) && @sizeof($records) != 0) {
  158. //get current toggle state
  159. foreach($records as $record) {
  160. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  161. $uuids[] = "'".$record['uuid']."'";
  162. }
  163. }
  164. if (is_array($uuids) && @sizeof($uuids) != 0) {
  165. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  166. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  167. $database = new database;
  168. $rows = $database->select($sql, $parameters, 'all');
  169. if (is_array($rows) && @sizeof($rows) != 0) {
  170. foreach ($rows as $row) {
  171. $states[$row['uuid']] = $row['toggle'];
  172. }
  173. }
  174. unset($sql, $parameters, $rows, $row);
  175. }
  176. //build update array
  177. $x = 0;
  178. foreach($states as $uuid => $state) {
  179. //create the array
  180. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  181. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  182. //increment the id
  183. $x++;
  184. }
  185. //save the changes
  186. if (is_array($array) && @sizeof($array) != 0) {
  187. //save the array
  188. $database = new database;
  189. $database->app_name = $this->app_name;
  190. $database->app_uuid = $this->app_uuid;
  191. $database->save($array);
  192. unset($array);
  193. //set message
  194. message::add($text['message-toggle']);
  195. }
  196. unset($records, $states);
  197. }
  198. }
  199. }
  200. /**
  201. * copy rows from the database
  202. */
  203. public function copy($records) {
  204. if (permission_exists($this->name.'_add')) {
  205. //add multi-lingual support
  206. $language = new text;
  207. $text = $language->get();
  208. //validate the token
  209. $token = new token;
  210. if (!$token->validate($_SERVER['PHP_SELF'])) {
  211. message::add($text['message-invalid_token'],'negative');
  212. header('Location: '.$this->location);
  213. exit;
  214. }
  215. //copy the checked records
  216. if (is_array($records) && @sizeof($records) != 0) {
  217. //get checked records
  218. foreach($records as $record) {
  219. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  220. $uuids[] = "'".$record['uuid']."'";
  221. }
  222. }
  223. //create the array from existing data
  224. if (is_array($uuids) && @sizeof($uuids) != 0) {
  225. $sql = "select * from v_".$this->table." ";
  226. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  227. $database = new database;
  228. $rows = $database->select($sql, $parameters, 'all');
  229. if (is_array($rows) && @sizeof($rows) != 0) {
  230. $x = 0;
  231. foreach ($rows as $row) {
  232. //copy data
  233. $array[$this->table][$x] = $row;
  234. //add copy to the description
  235. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  236. $array[$this->table][$x]['username'] = $row['username'].'-'.$text['label-copy'];
  237. //increment the id
  238. $x++;
  239. }
  240. }
  241. unset($sql, $parameters, $rows, $row);
  242. }
  243. //save the changes and set the message
  244. if (is_array($array) && @sizeof($array) != 0) {
  245. //save the array
  246. $database = new database;
  247. $database->app_name = $this->app_name;
  248. $database->app_uuid = $this->app_uuid;
  249. $database->save($array);
  250. unset($array);
  251. //set message
  252. message::add($text['message-copy']);
  253. }
  254. unset($records);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. ?>