SubAdmin.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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;
  110. }
  111. }
  112. $result->closeCursor();
  113. return $groups;
  114. }
  115. /**
  116. * get SubAdmins of a group
  117. * @param IGroup $group the group
  118. * @return IUser[]
  119. */
  120. public function getGroupsSubAdmins(IGroup $group) {
  121. $qb = $this->dbConn->getQueryBuilder();
  122. $result = $qb->select('uid')
  123. ->from('group_admin')
  124. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  125. ->execute();
  126. $users = [];
  127. while($row = $result->fetch()) {
  128. $user = $this->userManager->get($row['uid']);
  129. if(!is_null($user)) {
  130. $users[] = $user;
  131. }
  132. }
  133. $result->closeCursor();
  134. return $users;
  135. }
  136. /**
  137. * get all SubAdmins
  138. * @return array
  139. */
  140. public function getAllSubAdmins() {
  141. $qb = $this->dbConn->getQueryBuilder();
  142. $result = $qb->select('*')
  143. ->from('group_admin')
  144. ->execute();
  145. $subadmins = [];
  146. while($row = $result->fetch()) {
  147. $user = $this->userManager->get($row['uid']);
  148. $group = $this->groupManager->get($row['gid']);
  149. if(!is_null($user) && !is_null($group)) {
  150. $subadmins[] = [
  151. 'user' => $user,
  152. 'group' => $group
  153. ];
  154. }
  155. }
  156. $result->closeCursor();
  157. return $subadmins;
  158. }
  159. /**
  160. * checks if a user is a SubAdmin of a group
  161. * @param IUser $user
  162. * @param IGroup $group
  163. * @return bool
  164. */
  165. public function isSubAdminOfGroup(IUser $user, IGroup $group) {
  166. $qb = $this->dbConn->getQueryBuilder();
  167. /*
  168. * Primary key is ('gid', 'uid') so max 1 result possible here
  169. */
  170. $result = $qb->select('*')
  171. ->from('group_admin')
  172. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  173. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  174. ->execute();
  175. $fetch = $result->fetch();
  176. $result->closeCursor();
  177. $result = !empty($fetch) ? true : false;
  178. return $result;
  179. }
  180. /**
  181. * checks if a user is a SubAdmin
  182. * @param IUser $user
  183. * @return bool
  184. */
  185. public function isSubAdmin(IUser $user) {
  186. // Check if the user is already an admin
  187. if ($this->groupManager->isAdmin($user->getUID())) {
  188. return true;
  189. }
  190. $qb = $this->dbConn->getQueryBuilder();
  191. $result = $qb->select('gid')
  192. ->from('group_admin')
  193. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  194. ->setMaxResults(1)
  195. ->execute();
  196. $isSubAdmin = $result->fetch();
  197. $result->closeCursor();
  198. return $isSubAdmin !== false;
  199. }
  200. /**
  201. * checks if a user is a accessible by a subadmin
  202. * @param IUser $subadmin
  203. * @param IUser $user
  204. * @return bool
  205. */
  206. public function isUserAccessible($subadmin, $user) {
  207. if(!$this->isSubAdmin($subadmin)) {
  208. return false;
  209. }
  210. if($this->groupManager->isAdmin($user->getUID())) {
  211. return false;
  212. }
  213. $accessibleGroups = $this->getSubAdminsGroups($subadmin);
  214. foreach($accessibleGroups as $accessibleGroup) {
  215. if($accessibleGroup->inGroup($user)) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. /**
  222. * delete all SubAdmins by $user
  223. * @param IUser $user
  224. * @return boolean
  225. */
  226. private function post_deleteUser($user) {
  227. $qb = $this->dbConn->getQueryBuilder();
  228. $qb->delete('group_admin')
  229. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
  230. ->execute();
  231. return true;
  232. }
  233. /**
  234. * delete all SubAdmins by $group
  235. * @param IGroup $group
  236. * @return boolean
  237. */
  238. private function post_deleteGroup($group) {
  239. $qb = $this->dbConn->getQueryBuilder();
  240. $qb->delete('group_admin')
  241. ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
  242. ->execute();
  243. return true;
  244. }
  245. }