2012-07-09 23:51:19 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Mikael Hammarin <mikael@try2.se>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2012-07-09 23:51:19 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2012-07-09 23:51:19 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2012-07-09 23:51:19 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-07-09 23:51:19 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2012-07-09 23:51:19 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2012-07-09 23:51:19 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-08-13 22:31:38 +03:00
|
|
|
|
|
|
|
namespace OC;
|
|
|
|
|
|
|
|
use OC\Hooks\PublicEmitter;
|
2019-01-29 12:38:05 +03:00
|
|
|
use OCP\Group\ISubAdmin;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IDBConnection;
|
2015-08-13 22:31:38 +03:00
|
|
|
use OCP\IGroup;
|
|
|
|
use OCP\IGroupManager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2015-08-13 22:31:38 +03:00
|
|
|
|
2019-01-29 12:38:05 +03:00
|
|
|
class SubAdmin extends PublicEmitter implements ISubAdmin {
|
2015-08-13 22:31:38 +03:00
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $dbConn;
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
/**
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IGroupManager $groupManager
|
|
|
|
* @param IDBConnection $dbConn
|
|
|
|
*/
|
2015-08-13 22:31:38 +03:00
|
|
|
public function __construct(IUserManager $userManager,
|
2020-04-09 10:22:29 +03:00
|
|
|
IGroupManager $groupManager,
|
2015-08-13 22:31:38 +03:00
|
|
|
IDBConnection $dbConn) {
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->dbConn = $dbConn;
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->userManager->listen('\OC\User', 'postDelete', function ($user) {
|
2015-08-13 22:31:38 +03:00
|
|
|
$this->post_deleteUser($user);
|
|
|
|
});
|
2020-04-09 14:53:40 +03:00
|
|
|
$this->groupManager->listen('\OC\Group', 'postDelete', function ($group) {
|
2018-03-02 17:21:35 +03:00
|
|
|
$this->post_deleteGroup($group);
|
2015-08-13 22:31:38 +03:00
|
|
|
});
|
|
|
|
}
|
2012-07-09 23:51:19 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* add a SubAdmin
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IUser $user user to be SubAdmin
|
|
|
|
* @param IGroup $group group $user becomes subadmin of
|
2012-07-09 23:51:19 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function createSubAdmin(IUser $user, IGroup $group): void {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$qb->insert('group_admin')
|
2015-08-13 22:31:38 +03:00
|
|
|
->values([
|
|
|
|
'gid' => $qb->createNamedParameter($group->getGID()),
|
|
|
|
'uid' => $qb->createNamedParameter($user->getUID())
|
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->emit('\OC\SubAdmin', 'postCreateSubAdmin', [$user, $group]);
|
|
|
|
\OC_Hook::emit("OC_SubAdmin", "post_createSubAdmin", ["gid" => $group->getGID()]);
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* delete a SubAdmin
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IUser $user the user that is the SubAdmin
|
|
|
|
* @param IGroup $group the group
|
2012-07-09 23:51:19 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function deleteSubAdmin(IUser $user, IGroup $group): void {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$qb->delete('group_admin')
|
2015-08-13 22:31:38 +03:00
|
|
|
->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]);
|
|
|
|
\OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]);
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* get groups of a SubAdmin
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IUser $user the SubAdmin
|
|
|
|
* @return IGroup[]
|
2012-07-09 23:51:19 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function getSubAdminsGroups(IUser $user): array {
|
2020-04-27 09:37:53 +03:00
|
|
|
$groupIds = $this->getSubAdminsGroupIds($user);
|
|
|
|
|
|
|
|
$groups = [];
|
|
|
|
foreach ($groupIds as $groupId) {
|
|
|
|
$group = $this->groupManager->get($groupId);
|
|
|
|
if ($group !== null) {
|
|
|
|
$groups[$group->getGID()] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get group ids of a SubAdmin
|
|
|
|
* @param IUser $user the SubAdmin
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getSubAdminsGroupIds(IUser $user): array {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$result = $qb->select('gid')
|
|
|
|
->from('group_admin')
|
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$groups = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $result->fetch()) {
|
2020-04-27 09:37:53 +03:00
|
|
|
$groups[] = $row['gid'];
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
2015-10-28 11:09:16 +03:00
|
|
|
$result->closeCursor();
|
2015-08-13 22:31:38 +03:00
|
|
|
|
|
|
|
return $groups;
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
|
|
|
|
2018-03-02 17:21:35 +03:00
|
|
|
/**
|
|
|
|
* get an array of groupid and displayName for a user
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array ['displayName' => displayname]
|
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function getSubAdminsGroupsName(IUser $user): array {
|
2020-04-09 14:53:40 +03:00
|
|
|
return array_map(function ($group) {
|
2020-03-26 11:30:18 +03:00
|
|
|
return ['displayName' => $group->getDisplayName()];
|
2018-03-02 17:21:35 +03:00
|
|
|
}, $this->getSubAdminsGroups($user));
|
|
|
|
}
|
|
|
|
|
2012-07-09 23:51:19 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* get SubAdmins of a group
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IGroup $group the group
|
|
|
|
* @return IUser[]
|
2012-07-09 23:51:19 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function getGroupsSubAdmins(IGroup $group): array {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$result = $qb->select('uid')
|
|
|
|
->from('group_admin')
|
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$users = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $result->fetch()) {
|
2015-11-05 13:50:57 +03:00
|
|
|
$user = $this->userManager->get($row['uid']);
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($user)) {
|
2015-11-05 13:50:57 +03:00
|
|
|
$users[] = $user;
|
|
|
|
}
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
2015-10-28 11:09:16 +03:00
|
|
|
$result->closeCursor();
|
2015-08-13 22:31:38 +03:00
|
|
|
|
|
|
|
return $users;
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-09 23:51:19 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* get all SubAdmins
|
2012-07-09 23:51:19 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function getAllSubAdmins(): array {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$result = $qb->select('*')
|
|
|
|
->from('group_admin')
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$subadmins = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
while ($row = $result->fetch()) {
|
2015-11-05 13:50:57 +03:00
|
|
|
$user = $this->userManager->get($row['uid']);
|
|
|
|
$group = $this->groupManager->get($row['gid']);
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!is_null($user) && !is_null($group)) {
|
2015-11-05 13:50:57 +03:00
|
|
|
$subadmins[] = [
|
|
|
|
'user' => $user,
|
|
|
|
'group' => $group
|
|
|
|
];
|
|
|
|
}
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|
2015-10-28 11:09:16 +03:00
|
|
|
$result->closeCursor();
|
|
|
|
|
2012-07-09 23:51:19 +04:00
|
|
|
return $subadmins;
|
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-15 18:31:28 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* checks if a user is a SubAdmin of a group
|
2018-03-02 17:21:35 +03:00
|
|
|
* @param IUser $user
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IGroup $group
|
2012-07-19 18:43:46 +04:00
|
|
|
* @return bool
|
2012-07-15 18:31:28 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2015-10-28 11:09:16 +03:00
|
|
|
/*
|
|
|
|
* Primary key is ('gid', 'uid') so max 1 result possible here
|
|
|
|
*/
|
2015-08-13 22:31:38 +03:00
|
|
|
$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();
|
|
|
|
|
2015-10-28 11:09:16 +03:00
|
|
|
$fetch = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
$result = !empty($fetch) ? true : false;
|
|
|
|
|
|
|
|
return $result;
|
2012-07-15 18:31:28 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-19 18:43:46 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* checks if a user is a SubAdmin
|
2018-03-02 17:21:35 +03:00
|
|
|
* @param IUser $user
|
2012-07-19 18:43:46 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function isSubAdmin(IUser $user): bool {
|
2013-01-14 22:45:17 +04:00
|
|
|
// Check if the user is already an admin
|
2015-08-13 22:31:38 +03:00
|
|
|
if ($this->groupManager->isAdmin($user->getUID())) {
|
2013-01-14 22:45:17 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$result = $qb->select('gid')
|
|
|
|
->from('group_admin')
|
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
|
|
|
|
->setMaxResults(1)
|
2015-10-28 11:09:16 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$isSubAdmin = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2018-01-26 14:36:25 +03:00
|
|
|
return $isSubAdmin !== false;
|
2012-07-15 18:31:28 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-19 18:43:46 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* checks if a user is a accessible by a subadmin
|
2015-08-13 22:31:38 +03:00
|
|
|
* @param IUser $subadmin
|
|
|
|
* @param IUser $user
|
2012-07-19 18:43:46 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
public function isUserAccessible(IUser $subadmin, IUser $user): bool {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!$this->isSubAdmin($subadmin)) {
|
2012-07-19 18:30:58 +04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->groupManager->isAdmin($user->getUID())) {
|
2012-08-26 18:29:10 +04:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-28 17:48:46 +03:00
|
|
|
|
2020-04-27 09:37:53 +03:00
|
|
|
$accessibleGroups = $this->getSubAdminsGroupIds($subadmin);
|
|
|
|
$userGroups = $this->groupManager->getUserGroupIds($user);
|
|
|
|
|
|
|
|
return !empty(array_intersect($accessibleGroups, $userGroups));
|
2012-07-19 18:30:58 +04:00
|
|
|
}
|
2012-08-29 10:38:33 +04:00
|
|
|
|
2012-07-21 18:43:39 +04:00
|
|
|
/**
|
2015-08-13 22:31:38 +03:00
|
|
|
* delete all SubAdmins by $user
|
|
|
|
* @param IUser $user
|
2012-07-21 18:43:39 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
private function post_deleteUser(IUser $user) {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$qb->delete('group_admin')
|
2015-08-13 22:31:38 +03:00
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
|
|
|
|
->execute();
|
2012-07-21 18:43:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-13 22:31:38 +03:00
|
|
|
* delete all SubAdmins by $group
|
|
|
|
* @param IGroup $group
|
2012-07-21 18:43:39 +04:00
|
|
|
*/
|
2019-01-29 12:38:05 +03:00
|
|
|
private function post_deleteGroup(IGroup $group) {
|
2015-08-13 22:31:38 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
|
2015-10-27 16:09:45 +03:00
|
|
|
$qb->delete('group_admin')
|
2015-08-13 22:31:38 +03:00
|
|
|
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
|
|
|
|
->execute();
|
2012-07-21 18:43:39 +04:00
|
|
|
}
|
2012-07-09 23:51:19 +04:00
|
|
|
}
|