1
0

SubAdmin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC;
  29. use OC\Hooks\PublicEmitter;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use OCP\IGroup;
  33. use OCP\IGroupManager;
  34. use OCP\IDBConnection;
  35. class SubAdmin extends PublicEmitter {
  36. /** @var IUserManager */
  37. private $userManager;
  38. /** @var IGroupManager */
  39. private $groupManager;
  40. /** @var IDBConnection */
  41. private $dbConn;
  42. /**
  43. * @param IUserManager $userManager
  44. * @param IGroupManager $groupManager
  45. * @param IDBConnection $dbConn
  46. */
  47. public function __construct(IUserManager $userManager,
  48. IGroupManager $groupManager,
  49. IDBConnection $dbConn) {
  50. $this->userManager = $userManager;
  51. $this->groupManager = $groupManager;
  52. $this->dbConn = $dbConn;
  53. $this->userManager->listen('\OC\User', 'postDelete', function($user) {
  54. $this->post_deleteUser($user);
  55. });
  56. $this->groupManager->listen('\OC\Group', 'postDelete', function($group) {
  57. $this->post_deleteGroup($group);
  58. });
  59. }
  60. /**
  61. * add a SubAdmin
  62. * @param IUser $user user to be SubAdmin
  63. * @param IGroup $group group $user becomes subadmin of
  64. * @return bool
  65. */
  66. public function createSubAdmin(IUser $user, IGroup $group) {
  67. $qb = $this->dbConn->getQueryBuilder();
  68. $qb->insert('group_admin')
  69. ->values([
  70. 'gid' => $qb->createNamedParameter($group->getGID()),
  71. 'uid' => $qb->createNamedParameter($user->getUID())
  72. ])
  73. ->execute();
  74. $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
  75. \OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]);
  76. return true;
  77. }
  78. /**
  79. * delete a SubAdmin
  80. * @param IUser $user the user that is the SubAdmin
  81. * @param IGroup $group the group
  82. * @return bool
  83. */
  84. public function deleteSubAdmin(IUser $user, IGroup $group) {
  85. $qb = $this->dbConn->getQueryBuilder();
  86. $qb->delete('group_admin')
  87. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  88. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  89. ->execute();
  90. $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
  91. \OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]);
  92. return true;
  93. }
  94. /**
  95. * get groups of a SubAdmin
  96. * @param IUser $user the SubAdmin
  97. * @return IGroup[]
  98. */
  99. public function getSubAdminsGroups(IUser $user) {
  100. $qb = $this->dbConn->getQueryBuilder();
  101. $result = $qb->select('gid')
  102. ->from('group_admin')
  103. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  104. ->execute();
  105. $groups = [];
  106. while($row = $result->fetch()) {
  107. $group = $this->groupManager->get($row['gid']);
  108. if(!is_null($group)) {
  109. $groups[$group->getGID()] = $group;
  110. }
  111. }
  112. $result->closeCursor();
  113. return $groups;
  114. }
  115. /**
  116. * get an array of groupid and displayName for a user
  117. * @param IUser $user
  118. * @return array ['displayName' => displayname]
  119. */
  120. public function getSubAdminsGroupsName(IUser $user) {
  121. return array_map(function($group) {
  122. return array('displayName' => $group->getDisplayName());
  123. }, $this->getSubAdminsGroups($user));
  124. }
  125. /**
  126. * get SubAdmins of a group
  127. * @param IGroup $group the group
  128. * @return IUser[]
  129. */
  130. public function getGroupsSubAdmins(IGroup $group) {
  131. $qb = $this->dbConn->getQueryBuilder();
  132. $result = $qb->select('uid')
  133. ->from('group_admin')
  134. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  135. ->execute();
  136. $users = [];
  137. while($row = $result->fetch()) {
  138. $user = $this->userManager->get($row['uid']);
  139. if(!is_null($user)) {
  140. $users[] = $user;
  141. }
  142. }
  143. $result->closeCursor();
  144. return $users;
  145. }
  146. /**
  147. * get all SubAdmins
  148. * @return array
  149. */
  150. public function getAllSubAdmins() {
  151. $qb = $this->dbConn->getQueryBuilder();
  152. $result = $qb->select('*')
  153. ->from('group_admin')
  154. ->execute();
  155. $subadmins = [];
  156. while($row = $result->fetch()) {
  157. $user = $this->userManager->get($row['uid']);
  158. $group = $this->groupManager->get($row['gid']);
  159. if(!is_null($user) && !is_null($group)) {
  160. $subadmins[] = [
  161. 'user' => $user,
  162. 'group' => $group
  163. ];
  164. }
  165. }
  166. $result->closeCursor();
  167. return $subadmins;
  168. }
  169. /**
  170. * checks if a user is a SubAdmin of a group
  171. * @param IUser $user
  172. * @param IGroup $group
  173. * @return bool
  174. */
  175. public function isSubAdminOfGroup(IUser $user, IGroup $group) {
  176. $qb = $this->dbConn->getQueryBuilder();
  177. /*
  178. * Primary key is ('gid', 'uid') so max 1 result possible here
  179. */
  180. $result = $qb->select('*')
  181. ->from('group_admin')
  182. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  183. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  184. ->execute();
  185. $fetch = $result->fetch();
  186. $result->closeCursor();
  187. $result = !empty($fetch) ? true : false;
  188. return $result;
  189. }
  190. /**
  191. * checks if a user is a SubAdmin
  192. * @param IUser $user
  193. * @return bool
  194. */
  195. public function isSubAdmin(IUser $user) {
  196. // Check if the user is already an admin
  197. if ($this->groupManager->isAdmin($user->getUID())) {
  198. return true;
  199. }
  200. $qb = $this->dbConn->getQueryBuilder();
  201. $result = $qb->select('gid')
  202. ->from('group_admin')
  203. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  204. ->setMaxResults(1)
  205. ->execute();
  206. $isSubAdmin = $result->fetch();
  207. $result->closeCursor();
  208. return $isSubAdmin !== false;
  209. }
  210. /**
  211. * checks if a user is a accessible by a subadmin
  212. * @param IUser $subadmin
  213. * @param IUser $user
  214. * @return bool
  215. */
  216. public function isUserAccessible($subadmin, $user) {
  217. if(!$this->isSubAdmin($subadmin)) {
  218. return false;
  219. }
  220. if($this->groupManager->isAdmin($user->getUID())) {
  221. return false;
  222. }
  223. $accessibleGroups = $this->getSubAdminsGroups($subadmin);
  224. foreach($accessibleGroups as $accessibleGroup) {
  225. if($accessibleGroup->inGroup($user)) {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. /**
  232. * delete all SubAdmins by $user
  233. * @param IUser $user
  234. * @return boolean
  235. */
  236. private function post_deleteUser($user) {
  237. $qb = $this->dbConn->getQueryBuilder();
  238. $qb->delete('group_admin')
  239. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  240. ->execute();
  241. return true;
  242. }
  243. /**
  244. * delete all SubAdmins by $group
  245. * @param IGroup $group
  246. * @return boolean
  247. */
  248. private function post_deleteGroup($group) {
  249. $qb = $this->dbConn->getQueryBuilder();
  250. $qb->delete('group_admin')
  251. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  252. ->execute();
  253. return true;
  254. }
  255. }