permission.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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) 2013-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //define the permission class
  22. class permission {
  23. //delete the permissions
  24. function delete() {
  25. //get the $apps array from the installed apps from the core and mod directories
  26. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  27. $x = 0;
  28. foreach ($config_list as $config_path) {
  29. include($config_path);
  30. $x++;
  31. }
  32. //initialize array
  33. $group_name_array = array();
  34. //restore default permissions
  35. $x = 0;
  36. foreach ($apps as $row) {
  37. if (!empty($row['permissions']) && is_array($row['permissions']) && @sizeof($row['permissions']) != 0) {
  38. foreach ($row['permissions'] as $permission) {
  39. if (!empty($permission['groups']) && is_array($permission['groups'])) {
  40. foreach ($permission['groups'] as $group_name) {
  41. if (is_array($group_name_array) || !in_array($group_name, $group_name_array)) {
  42. $group_name_array[] = $group_name;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. $group_names = "'".implode("','", $group_name_array)."'";
  50. //delete unprotected permissions
  51. $sql = "delete from v_group_permissions as p ";
  52. $sql .= "where group_name in ( ";
  53. $sql .= " select group_name ";
  54. $sql .= " from v_groups ";
  55. $sql .= " where group_protected <> 'true' ";
  56. $sql .= " and group_name in (".$group_names.") ";
  57. $sql .= ")";
  58. $sql .= "and (permission_protected <> 'true' or permission_protected is null)";
  59. $database = new database;
  60. $result = $database->select($sql);
  61. //get the group_permissons
  62. /*
  63. $sql = "select * from v_group_permissions as p ";
  64. $sql .= "where group_name in ( ";
  65. $sql .= " select group_name ";
  66. $sql .= " from v_groups ";
  67. $sql .= " where group_protected <> 'true' ";
  68. $sql .= " and group_name in (".$group_names.") ";
  69. $sql .= ");";
  70. $database = new database;
  71. $group_permissions = $database->select($sql, null, 'all');
  72. */
  73. //delete unprotected group permissions
  74. /*
  75. if (is_array($group_permissions) && sizeof($group_permissions) > 0) {
  76. $x = 0;
  77. foreach ($group_permissions as $row) {
  78. //build delete array
  79. $array['group_permissions'][$x]['group_permission_uuid'] = $row['group_permission_uuid'];
  80. $array['group_permissions'][$x]['domain_uuid'] = ($row['domain_uuid'] != '') ? $row['domain_uuid'] : null;
  81. $x++;
  82. }
  83. if (is_array($array) && @sizeof($array) != 0) {
  84. //grant temporary permissions
  85. $p = permissions::new();
  86. $p->add('group_permission_delete', 'temp');
  87. //execute delete
  88. $database = new database;
  89. $database->app_name = 'groups';
  90. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  91. $database->delete($array);
  92. unset($array);
  93. //revoke temporary permissions
  94. $p->delete('group_permission_delete', 'temp');
  95. }
  96. }
  97. */
  98. }
  99. //restore the permissions
  100. function restore() {
  101. //if the are no groups add the default groups
  102. $sql = "select * from v_groups ";
  103. $sql .= "where domain_uuid is null ";
  104. $database = new database;
  105. $groups = $database->select($sql, null, 'all');
  106. //delete the group permissions
  107. $this->delete();
  108. //get the remaining group permissions
  109. $sql = "select permission_name, group_name from v_group_permissions ";
  110. $database = new database;
  111. $database_group_permissions = $database->select($sql, null, 'all');
  112. //get the $apps array from the installed apps from the core and mod directories
  113. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  114. $x = 0;
  115. foreach ($config_list as $config_path) {
  116. include($config_path);
  117. $x++;
  118. }
  119. //restore default permissions
  120. $x = 0;
  121. foreach ($apps as $row) {
  122. if (!empty($row['permissions']) && is_array($row['permissions']) && @sizeof($row['permissions']) != 0) {
  123. foreach ($row['permissions'] as $permission) {
  124. //set the variables
  125. if (!empty($permission['groups'])) {
  126. foreach ($permission['groups'] as $group_name) {
  127. //check group protection
  128. $group_uuid = null;
  129. $group_protected = null;
  130. if (is_array($groups)) {
  131. foreach ($groups as $group) {
  132. if ($group['group_name'] == $group_name) {
  133. $group_uuid = $group['group_uuid'];
  134. $group_protected = $group['group_protected'] == 'true' ? true : false;
  135. break;
  136. }
  137. }
  138. }
  139. if (!$group_protected) {
  140. // check if the item is not currently in the database
  141. $exists = false;
  142. foreach ($database_group_permissions as $i => $group_permission) {
  143. if ($group_permission['permission_name'] == $permission['name']) {
  144. if ($group_permission['group_name'] == $group_name) {
  145. $exists = true;
  146. break;
  147. }
  148. }
  149. }
  150. if (!$exists) {
  151. //build default permissions insert array
  152. $array['group_permissions'][$x]['group_permission_uuid'] = uuid();
  153. $array['group_permissions'][$x]['permission_name'] = $permission['name'];
  154. $array['group_permissions'][$x]['permission_protected'] = 'false';
  155. $array['group_permissions'][$x]['permission_assigned'] = 'true';
  156. $array['group_permissions'][$x]['group_name'] = $group_name;
  157. $array['group_permissions'][$x]['group_uuid'] = $group_uuid;
  158. $x++;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. if (is_array($array) && @sizeof($array)) {
  167. //grant temporary permissions
  168. $p = permissions::new();
  169. $p->add('group_permission_add', 'temp');
  170. //execute insert
  171. $database = new database;
  172. $database->app_name = 'groups';
  173. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  174. $database->save($array);
  175. unset($array);
  176. //revoke temporary permissions
  177. $p->delete('group_permission_add', 'temp');
  178. }
  179. }
  180. }
  181. ?>