subadmin.php 5.2 KB

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