subadmin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @brief add a SubAdmin
  34. * @param $uid uid of the SubAdmin
  35. * @param $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. * @brief delete a SubAdmin
  46. * @param $uid uid of the SubAdmin
  47. * @param $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. * @brief get groups of a SubAdmin
  58. * @param $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. * @brief get SubAdmins of a group
  72. * @param $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. * @brief 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. * @brief checks if a user is a SubAdmin of a group
  99. * @param $uid uid of the subadmin
  100. * @param $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. * @brief checks if a user is a SubAdmin
  114. * @param $uid uid of the subadmin
  115. * @return bool
  116. */
  117. public static function isSubAdmin($uid) {
  118. $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
  119. $result = $stmt->execute(array($uid));
  120. $result = $result->fetchRow();
  121. if($result['count'] > 0) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /**
  127. * @brief checks if a user is a accessible by a subadmin
  128. * @param $subadmin uid of the subadmin
  129. * @param $user uid of the user
  130. * @return bool
  131. */
  132. public static function isUserAccessible($subadmin, $user) {
  133. if(!self::isSubAdmin($subadmin)) {
  134. return false;
  135. }
  136. if(OC_Group::inGroup($user, 'admin')) {
  137. return false;
  138. }
  139. $accessiblegroups = self::getSubAdminsGroups($subadmin);
  140. foreach($accessiblegroups as $accessiblegroup) {
  141. if(OC_Group::inGroup($user, $accessiblegroup)) {
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. /*
  148. * @brief alias for self::isSubAdminofGroup()
  149. */
  150. public static function isGroupAccessible($subadmin, $group) {
  151. return self::isSubAdminofGroup($subadmin, $group);
  152. }
  153. /**
  154. * @brief delete all SubAdmins by uid
  155. * @param $parameters
  156. * @return boolean
  157. */
  158. public static function post_deleteUser($parameters) {
  159. $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `uid` = ?');
  160. $result = $stmt->execute(array($parameters['uid']));
  161. return true;
  162. }
  163. /**
  164. * @brief delete all SubAdmins by gid
  165. * @param $parameters
  166. * @return boolean
  167. */
  168. public static function post_deleteGroup($parameters) {
  169. $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?');
  170. $result = $stmt->execute(array($parameters['gid']));
  171. return true;
  172. }
  173. }