permission.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 (is_array($row['permissions']) && @sizeof($row['permissions']) != 0) {
  38. foreach ($row['permissions'] as $permission) {
  39. if (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 system group 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. $database = new database;
  59. $result = $database->select($sql);
  60. //get the group_permissons
  61. /*
  62. $sql = "select * from v_group_permissions as p ";
  63. $sql .= "where group_name in ( ";
  64. $sql .= " select group_name ";
  65. $sql .= " from v_groups ";
  66. $sql .= " where group_protected <> 'true' ";
  67. $sql .= " and group_name in (".$group_names.") ";
  68. $sql .= ");";
  69. $database = new database;
  70. $group_permissions = $database->select($sql, null, 'all');
  71. */
  72. //delete unprotected group permissions
  73. /*
  74. if (is_array($group_permissions) && sizeof($group_permissions) > 0) {
  75. $x = 0;
  76. foreach ($group_permissions as $row) {
  77. //build delete array
  78. $array['group_permissions'][$x]['group_permission_uuid'] = $row['group_permission_uuid'];
  79. $array['group_permissions'][$x]['domain_uuid'] = ($row['domain_uuid'] != '') ? $row['domain_uuid'] : null;
  80. $x++;
  81. }
  82. if (is_array($array) && @sizeof($array) != 0) {
  83. //grant temporary permissions
  84. $p = new permissions;
  85. $p->add('group_permission_delete', 'temp');
  86. //execute delete
  87. $database = new database;
  88. $database->app_name = 'groups';
  89. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  90. $database->delete($array);
  91. unset($array);
  92. //revoke temporary permissions
  93. $p->delete('group_permission_delete', 'temp');
  94. }
  95. }
  96. */
  97. }
  98. //restore the permissions
  99. function restore() {
  100. //if the are no groups add the default groups
  101. $sql = "select * from v_groups ";
  102. $sql .= "where domain_uuid is null ";
  103. $database = new database;
  104. $groups = $database->select($sql, null, 'all');
  105. //delete the group permissions
  106. $this->delete();
  107. //get the $apps array from the installed apps from the core and mod directories
  108. $config_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/*/*/app_config.php");
  109. $x = 0;
  110. foreach ($config_list as &$config_path) {
  111. include($config_path);
  112. $x++;
  113. }
  114. //restore default permissions
  115. $x = 0;
  116. foreach ($apps as $row) {
  117. if (is_array($row['permissions']) && @sizeof($row['permissions']) != 0) {
  118. foreach ($row['permissions'] as $permission) {
  119. //set the variables
  120. if ($permission['groups']) {
  121. foreach ($permission['groups'] as $group_name) {
  122. //check group protection
  123. if (is_array($groups)) {
  124. foreach ($groups as $group) {
  125. if ($group['group_name'] == $group_name) {
  126. $group_uuid = $group['group_uuid'];
  127. $group_protected = $group['group_protected'] == 'true' ? true : false;
  128. break;
  129. }
  130. }
  131. }
  132. if (!$group_protected) {
  133. //if the item uuid is not currently in the db then add it
  134. $sql = "select count(*) from v_group_permissions ";
  135. $sql .= "where permission_name = :permission_name ";
  136. $sql .= "and group_name = :group_name ";
  137. $parameters['permission_name'] = $permission['name'];
  138. $parameters['group_name'] = $group_name;
  139. $database = new database;
  140. $num_rows = $database->select($sql, $parameters, 'column');
  141. unset($sql, $parameters);
  142. if ($num_rows == 0) {
  143. //build default permissions insert array
  144. $array['group_permissions'][$x]['group_permission_uuid'] = uuid();
  145. $array['group_permissions'][$x]['permission_name'] = $permission['name'];
  146. $array['group_permissions'][$x]['group_name'] = $group_name;
  147. $array['group_permissions'][$x]['group_uuid'] = $group_uuid;
  148. $x++;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. if (is_array($array) && @sizeof($array)) {
  157. //grant temporary permissions
  158. $p = new permissions;
  159. $p->add('group_permission_add', 'temp');
  160. //execute insert
  161. $database = new database;
  162. $database->app_name = 'groups';
  163. $database->app_uuid = '2caf27b0-540a-43d5-bb9b-c9871a1e4f84';
  164. $database->save($array);
  165. unset($array);
  166. //revoke temporary permissions
  167. $p->delete('group_permission_add', 'temp');
  168. }
  169. }
  170. }
  171. ?>