123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace OC;
- use OC\Hooks\PublicEmitter;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Group\Events\SubAdminAddedEvent;
- use OCP\Group\Events\SubAdminRemovedEvent;
- use OCP\Group\ISubAdmin;
- use OCP\IDBConnection;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IUser;
- use OCP\IUserManager;
- class SubAdmin extends PublicEmitter implements ISubAdmin {
-
- private $userManager;
-
- private $groupManager;
-
- private $dbConn;
-
- private $eventDispatcher;
-
- public function __construct(IUserManager $userManager,
- IGroupManager $groupManager,
- IDBConnection $dbConn,
- IEventDispatcher $eventDispatcher) {
- $this->userManager = $userManager;
- $this->groupManager = $groupManager;
- $this->dbConn = $dbConn;
- $this->eventDispatcher = $eventDispatcher;
- $this->userManager->listen('\OC\User', 'postDelete', function ($user) {
- $this->post_deleteUser($user);
- });
- $this->groupManager->listen('\OC\Group', 'postDelete', function ($group) {
- $this->post_deleteGroup($group);
- });
- }
-
- public function createSubAdmin(IUser $user, IGroup $group): void {
- $qb = $this->dbConn->getQueryBuilder();
- $qb->insert('group_admin')
- ->values([
- 'gid' => $qb->createNamedParameter($group->getGID()),
- 'uid' => $qb->createNamedParameter($user->getUID())
- ])
- ->execute();
-
- $this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
- $event = new SubAdminAddedEvent($group, $user);
- $this->eventDispatcher->dispatchTyped($event);
- }
-
- public function deleteSubAdmin(IUser $user, IGroup $group): void {
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
- ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
- ->execute();
-
- $this->emit('\OC\SubAdmin', 'postDeleteSubAdmin', [$user, $group]);
- $event = new SubAdminRemovedEvent($group, $user);
- $this->eventDispatcher->dispatchTyped($event);
- }
-
- public function getSubAdminsGroups(IUser $user): array {
- $groupIds = $this->getSubAdminsGroupIds($user);
- $groups = [];
- foreach ($groupIds as $groupId) {
- $group = $this->groupManager->get($groupId);
- if ($group !== null) {
- $groups[$group->getGID()] = $group;
- }
- }
- return $groups;
- }
-
- public function getSubAdminsGroupIds(IUser $user): array {
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select('gid')
- ->from('group_admin')
- ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
- ->execute();
- $groups = [];
- while ($row = $result->fetch()) {
- $groups[] = $row['gid'];
- }
- $result->closeCursor();
- return $groups;
- }
-
- public function getSubAdminsGroupsName(IUser $user): array {
- return array_map(function ($group) {
- return ['displayName' => $group->getDisplayName()];
- }, $this->getSubAdminsGroups($user));
- }
-
- public function getGroupsSubAdmins(IGroup $group): array {
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select('uid')
- ->from('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
- ->execute();
- $users = [];
- while ($row = $result->fetch()) {
- $user = $this->userManager->get($row['uid']);
- if (!is_null($user)) {
- $users[] = $user;
- }
- }
- $result->closeCursor();
- return $users;
- }
-
- public function getAllSubAdmins(): array {
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select('*')
- ->from('group_admin')
- ->execute();
- $subadmins = [];
- while ($row = $result->fetch()) {
- $user = $this->userManager->get($row['uid']);
- $group = $this->groupManager->get($row['gid']);
- if (!is_null($user) && !is_null($group)) {
- $subadmins[] = [
- 'user' => $user,
- 'group' => $group
- ];
- }
- }
- $result->closeCursor();
- return $subadmins;
- }
-
- public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {
- $qb = $this->dbConn->getQueryBuilder();
-
- $result = $qb->select('*')
- ->from('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
- ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
- ->execute();
- $fetch = $result->fetch();
- $result->closeCursor();
- $result = !empty($fetch) ? true : false;
- return $result;
- }
-
- public function isSubAdmin(IUser $user): bool {
-
- if ($this->groupManager->isAdmin($user->getUID())) {
- return true;
- }
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select('gid')
- ->from('group_admin')
- ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
- ->setMaxResults(1)
- ->execute();
- $isSubAdmin = $result->fetch();
- $result->closeCursor();
- return $isSubAdmin !== false;
- }
-
- public function isUserAccessible(IUser $subadmin, IUser $user): bool {
- if (!$this->isSubAdmin($subadmin)) {
- return false;
- }
- if ($this->groupManager->isAdmin($user->getUID())) {
- return false;
- }
- $accessibleGroups = $this->getSubAdminsGroupIds($subadmin);
- $userGroups = $this->groupManager->getUserGroupIds($user);
- return !empty(array_intersect($accessibleGroups, $userGroups));
- }
-
- private function post_deleteUser(IUser $user) {
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_admin')
- ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
- ->execute();
- }
-
- private function post_deleteGroup(IGroup $group) {
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
- ->execute();
- }
- }
|