nextcloud/lib/private/subadmin.php

258 lines
6.5 KiB
PHP
Raw Normal View History

2012-07-09 23:51:19 +04:00
<?php
/**
2015-03-26 13:44:34 +03:00
* @author Bart Visscher <bartv@thisnet.nl>
* @author Georg Ehrke <georg@owncloud.com>
* @author Lukas Reschke <lukas@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Roeland Jago Douma <roeland@famdouma.nl>
2015-03-26 13:44:34 +03:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
2012-07-09 23:51:19 +04:00
*
2015-03-26 13:44:34 +03:00
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2012-07-09 23:51:19 +04:00
*
*/
namespace OC;
use OC\Hooks\PublicEmitter;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IDBConnection;
class SubAdmin extends PublicEmitter {
/** @var IUserManager */
private $userManager;
/** @var IGroupManager */
private $groupManager;
/** @var IDBConnection */
private $dbConn;
public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IDBConnection $dbConn) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->dbConn = $dbConn;
$this->userManager->listen('\OC\User', 'postDelete', function($user) {
$this->post_deleteUser($user);
});
$this->groupManager->listen('\OC\Group', 'postDelete', function($group) {
$this->post_deleteGroup($group);
});
}
2012-07-09 23:51:19 +04:00
/**
* add a SubAdmin
* @param IUser $user user to be SubAdmin
* @param IGroup $group group $user becomes subadmin of
* @return bool
2012-07-09 23:51:19 +04:00
*/
public function createSubAdmin(IUser $user, IGroup $group) {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->insert('group_admin')
->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
return true;
}
/**
* delete a SubAdmin
* @param IUser $user the user that is the SubAdmin
* @param IGroup $group the group
* @return bool
2012-07-09 23:51:19 +04:00
*/
public function deleteSubAdmin(IUser $user, IGroup $group) {
$qb = $this->dbConn->getQueryBuilder();
$result = $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]);
\OC_Hook::emit("OC_SubAdmin", "post_deleteSubAdmin", ["gid" => $group->getGID()]);
2012-07-09 23:51:19 +04:00
return true;
}
/**
* get groups of a SubAdmin
* @param IUser $user the SubAdmin
* @return IGroup[]
2012-07-09 23:51:19 +04:00
*/
public function getSubAdminsGroups(IUser $user) {
$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[] = $this->groupManager->get($row['gid']);
2012-07-09 23:51:19 +04:00
}
return $groups;
2012-07-09 23:51:19 +04:00
}
/**
* get SubAdmins of a group
* @param IGroup $group the group
* @return IUser[]
2012-07-09 23:51:19 +04:00
*/
public function getGroupsSubAdmins(IGroup $group) {
$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()) {
$users[] = $this->userManager->get($row['uid']);
2012-07-09 23:51:19 +04: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
/**
* get all SubAdmins
2012-07-09 23:51:19 +04:00
* @return array
*/
public function getAllSubAdmins() {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->select('*')
->from('group_admin')
->execute();
$subadmins = [];
while($row = $result->fetch()) {
$subadmins[] = [
'user' => $this->userManager->get($row['uid']),
'group' => $this->groupManager->get($row['gid'])
];
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
/**
* checks if a user is a SubAdmin of a group
* @param IUser $user
* @param IGroup $group
2012-07-19 18:43:46 +04:00
* @return bool
2012-07-15 18:31:28 +04:00
*/
public function isSubAdminofGroup(IUser $user, IGroup $group) {
$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();
return !empty($result->fetch()) ? true : 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
/**
* checks if a user is a SubAdmin
* @param IUser $user
2012-07-19 18:43:46 +04:00
* @return bool
*/
public function isSubAdmin(IUser $user) {
// Check if the user is already an admin
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()
->fetch();
return $result === false ? false : true;
2012-07-15 18:31:28 +04:00
}
2012-08-29 10:38:33 +04:00
2012-07-19 18:43:46 +04:00
/**
* checks if a user is a accessible by a subadmin
* @param IUser $subadmin
* @param IUser $user
2012-07-19 18:43:46 +04:00
* @return bool
*/
public function isUserAccessible($subadmin, $user) {
if(!$this->isSubAdmin($subadmin)) {
return false;
}
if($this->groupManager->isAdmin($user->getUID())) {
return false;
}
$accessiblegroups = $this->getSubAdminsGroups($subadmin);
2012-09-07 17:22:01 +04:00
foreach($accessiblegroups as $accessiblegroup) {
if($accessiblegroup->inGroup($user)) {
return true;
}
}
return false;
}
2012-08-29 10:38:33 +04:00
2012-07-21 18:43:39 +04:00
/**
* delete all SubAdmins by $user
* @param IUser $user
2012-07-21 18:43:39 +04:00
* @return boolean
*/
private function post_deleteUser($user) {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->delete('group_admin')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
->execute();
2012-07-21 18:43:39 +04:00
return true;
}
/**
* delete all SubAdmins by $group
* @param IGroup $group
2012-07-21 18:43:39 +04:00
* @return boolean
*/
private function post_deleteGroup($group) {
$qb = $this->dbConn->getQueryBuilder();
$result = $qb->delete('group_admin')
->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID())))
->execute();
2012-07-21 18:43:39 +04:00
return true;
}
2012-07-09 23:51:19 +04:00
}