subadmin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Georg Ehrke <georg@owncloud.com>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_SubAdmin', 'post_deleteUser');
  28. OC_Hook::connect('OC_User', 'post_deleteGroup', 'OC_SubAdmin', 'post_deleteGroup');
  29. /**
  30. * This class provides all methods needed for managing groups.
  31. *
  32. * Hooks provided:
  33. * post_createSubAdmin($gid)
  34. * post_deleteSubAdmin($gid)
  35. */
  36. class OC_SubAdmin{
  37. /**
  38. * add a SubAdmin
  39. * @param string $uid uid of the SubAdmin
  40. * @param string $gid gid of the group
  41. * @return boolean
  42. */
  43. public static function createSubAdmin($uid, $gid) {
  44. $stmt = OC_DB::prepare('INSERT INTO `*PREFIX*group_admin` (`gid`,`uid`) VALUES(?,?)');
  45. $stmt->execute(array($gid, $uid));
  46. OC_Hook::emit( "OC_SubAdmin", "post_createSubAdmin", array( "gid" => $gid ));
  47. return true;
  48. }
  49. /**
  50. * delete a SubAdmin
  51. * @param string $uid uid of the SubAdmin
  52. * @param string $gid gid of the group
  53. * @return boolean
  54. */
  55. public static function deleteSubAdmin($uid, $gid) {
  56. $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ? AND `uid` = ?');
  57. $stmt->execute(array($gid, $uid));
  58. OC_Hook::emit( "OC_SubAdmin", "post_deleteSubAdmin", array( "gid" => $gid ));
  59. return true;
  60. }
  61. /**
  62. * get groups of a SubAdmin
  63. * @param string $uid uid of the SubAdmin
  64. * @return array
  65. */
  66. public static function getSubAdminsGroups($uid) {
  67. $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
  68. $result = $stmt->execute(array($uid));
  69. $gids = array();
  70. while($row = $result->fetchRow()) {
  71. $gids[] = $row['gid'];
  72. }
  73. return $gids;
  74. }
  75. /**
  76. * get SubAdmins of a group
  77. * @param string $gid gid of the group
  78. * @return array
  79. */
  80. public static function getGroupsSubAdmins($gid) {
  81. $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_admin` WHERE `gid` = ?');
  82. $result = $stmt->execute(array($gid));
  83. $uids = array();
  84. while($row = $result->fetchRow()) {
  85. $uids[] = $row['uid'];
  86. }
  87. return $uids;
  88. }
  89. /**
  90. * get all SubAdmins
  91. * @return array
  92. */
  93. public static function getAllSubAdmins() {
  94. $stmt = OC_DB::prepare('SELECT * FROM `*PREFIX*group_admin`');
  95. $result = $stmt->execute();
  96. $subadmins = array();
  97. while($row = $result->fetchRow()) {
  98. $subadmins[] = $row;
  99. }
  100. return $subadmins;
  101. }
  102. /**
  103. * checks if a user is a SubAdmin of a group
  104. * @param string $uid uid of the subadmin
  105. * @param string $gid gid of the group
  106. * @return bool
  107. */
  108. public static function isSubAdminofGroup($uid, $gid) {
  109. $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ? AND `gid` = ?');
  110. $result = $stmt->execute(array($uid, $gid));
  111. $result = $result->fetchRow();
  112. if($result['count'] >= 1) {
  113. return true;
  114. }
  115. return false;
  116. }
  117. /**
  118. * checks if a user is a SubAdmin
  119. * @param string $uid uid of the subadmin
  120. * @return bool
  121. */
  122. public static function isSubAdmin($uid) {
  123. // Check if the user is already an admin
  124. if(OC_Group::inGroup($uid, 'admin' )) {
  125. return true;
  126. }
  127. $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
  128. $result = $stmt->execute(array($uid));
  129. $result = $result->fetchRow();
  130. if($result['count'] > 0) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * checks if a user is a accessible by a subadmin
  137. * @param string $subadmin uid of the subadmin
  138. * @param string $user uid of the user
  139. * @return bool
  140. */
  141. public static function isUserAccessible($subadmin, $user) {
  142. if(!self::isSubAdmin($subadmin)) {
  143. return false;
  144. }
  145. if(OC_User::isAdminUser($user)) {
  146. return false;
  147. }
  148. $accessiblegroups = self::getSubAdminsGroups($subadmin);
  149. foreach($accessiblegroups as $accessiblegroup) {
  150. if(OC_Group::inGroup($user, $accessiblegroup)) {
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. /*
  157. * alias for self::isSubAdminofGroup()
  158. */
  159. public static function isGroupAccessible($subadmin, $group) {
  160. return self::isSubAdminofGroup($subadmin, $group);
  161. }
  162. /**
  163. * delete all SubAdmins by uid
  164. * @param array $parameters
  165. * @return boolean
  166. */
  167. public static function post_deleteUser($parameters) {
  168. $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `uid` = ?');
  169. $stmt->execute(array($parameters['uid']));
  170. return true;
  171. }
  172. /**
  173. * delete all SubAdmins by gid
  174. * @param array $parameters
  175. * @return boolean
  176. */
  177. public static function post_deleteGroup($parameters) {
  178. $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?');
  179. $stmt->execute(array($parameters['gid']));
  180. return true;
  181. }
  182. }