permissions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. Copyright (C) 2016 - 2024 All Rights Reserved.
  17. */
  18. /**
  19. * permission class
  20. *
  21. * @method string add
  22. * @method string delete
  23. * @method string exists
  24. */
  25. if (!class_exists('permissions')) {
  26. class permissions {
  27. private $database;
  28. private $domain_uuid;
  29. private $user_uuid;
  30. private $groups;
  31. private $permissions;
  32. private static $permission;
  33. /**
  34. * called when the object is created
  35. */
  36. public function __construct($database = null, $domain_uuid = null, $user_uuid = null) {
  37. //intitialize as empty arrays
  38. $this->groups = [];
  39. $this->permissions = [];
  40. //handle the database object
  41. if (isset($database)) {
  42. $this->database = $database;
  43. }
  44. else {
  45. $this->database = database::new();
  46. }
  47. //set the domain_uuid
  48. if (!empty($domain_uuid) && is_uuid($domain_uuid)) {
  49. $this->domain_uuid = $domain_uuid;
  50. }
  51. elseif (isset($_SESSION['domain_uuid']) && is_uuid($_SESSION['domain_uuid'])) {
  52. $this->domain_uuid = $_SESSION['domain_uuid'];
  53. }
  54. //set the user_uuid
  55. if (!empty($user_uuid) && is_uuid($user_uuid)) {
  56. $this->user_uuid = $user_uuid;
  57. }
  58. elseif (isset($_SESSION['user_uuid']) && is_uuid($_SESSION['user_uuid'])) {
  59. $this->user_uuid = $_SESSION['user_uuid'];
  60. }
  61. //get the permissions
  62. if (isset($_SESSION['permissions'])) {
  63. $this->permissions = $_SESSION['permissions'];
  64. }
  65. else {
  66. //create the groups object
  67. $groups = new groups($this->database, $this->domain_uuid, $this->user_uuid);
  68. $this->groups = $groups->assigned();
  69. //get the list of groups assigned to the user
  70. if (!empty($this->groups)) {
  71. $this->assigned();
  72. }
  73. }
  74. }
  75. /**
  76. * get the array of permissions
  77. */
  78. public function get_permissions() {
  79. return $this->permissions;
  80. }
  81. /**
  82. * Add the permission
  83. * @var string $permission
  84. */
  85. public function add($permission, $type) {
  86. //add the permission if it is not in array
  87. if (!$this->exists($permission)) {
  88. $this->permissions[$permission] = $type;
  89. }
  90. }
  91. /**
  92. * Remove the permission
  93. * @var string $permission
  94. */
  95. public function delete($permission, $type) {
  96. if ($this->exists($permission) && !empty($this->permissions[$permission])) {
  97. if ($type === "temp") {
  98. if ($this->permissions[$permission] === "temp") {
  99. unset($this->permissions[$permission]);
  100. }
  101. }
  102. else {
  103. if ($this->permissions[$permission] !== "temp") {
  104. unset($this->permissions[$permission]);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Check to see if the permission exists
  111. * @var string $permission
  112. */
  113. public function exists($permission_name) {
  114. //if run from command line then return true
  115. if (defined('STDIN')) {
  116. return true;
  117. }
  118. //search for the permission
  119. if (!empty($permission_name)) {
  120. return isset($this->permissions[$permission_name]);
  121. }
  122. return false;
  123. }
  124. /**
  125. * get the assigned permissions
  126. * @var array $groups
  127. */
  128. private function assigned() {
  129. //define the array
  130. $permissions = [];
  131. $parameter_names = [];
  132. //return empty array if there are no groups
  133. if (empty($this->groups)) {
  134. return [];
  135. }
  136. //prepare the parameters
  137. $x = 0;
  138. foreach ($this->groups as $field) {
  139. if (!empty($field['group_name'])) {
  140. $parameter_names[] = ":group_name_".$x;
  141. $parameters['group_name_'.$x] = $field['group_name'];
  142. $x++;
  143. }
  144. }
  145. //get the permissions assigned to the user through the assigned groups
  146. $sql = "select distinct(permission_name) from v_group_permissions ";
  147. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  148. $sql .= "and group_name in (".implode(", ", $parameter_names).") \n";
  149. $sql .= "and permission_assigned = 'true' ";
  150. $parameters['domain_uuid'] = $this->domain_uuid;
  151. $group_permissions = $this->database->select($sql, $parameters, 'all');
  152. //format the permission array
  153. foreach ($group_permissions as $row) {
  154. $permissions[$row['permission_name']] = 1;
  155. }
  156. //save permissions to this object
  157. $this->permissions = $permissions;
  158. }
  159. /**
  160. * save the assigned permissions to a session
  161. */
  162. public function session() {
  163. if (!empty($this->permissions)) {
  164. foreach ($this->permissions as $permission_name => $row) {
  165. $_SESSION['permissions'][$permission_name] = true;
  166. $_SESSION["user"]["permissions"][$permission_name] = true;
  167. }
  168. }
  169. }
  170. /**
  171. * Returns a new permission object
  172. */
  173. public static function new($database = null, $domain_uuid = null, $user_uuid = null) {
  174. if (self::$permission === null) {
  175. self::$permission = new permissions($database, $domain_uuid, $user_uuid);
  176. }
  177. return self::$permission;
  178. }
  179. }
  180. }
  181. //examples
  182. /*
  183. //add the permission
  184. $p = permissions::new();
  185. $p->add($permission);
  186. //delete the permission
  187. $p = permissions::new();
  188. $p->delete($permission);
  189. */