diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-10-21 14:43:09 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-10-21 14:43:09 +0200 |
commit | 33144a5d806bbb01208588b7eaed69c1739e9839 (patch) | |
tree | d850c73ef3e4f91b2362dba830b1da486ccf0490 /lib | |
parent | 036c8a2e56b3b53281561dfc6e20da20258086c9 (diff) | |
parent | 02af4269f27d4d7b4a3a053e23a614738293be2b (diff) | |
download | nextcloud-server-33144a5d806bbb01208588b7eaed69c1739e9839.tar.gz nextcloud-server-33144a5d806bbb01208588b7eaed69c1739e9839.zip |
Merge pull request #19827 from owncloud/subadmin_non_static
Make OC_SubAdmin non static
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/group/manager.php | 19 | ||||
-rw-r--r-- | lib/private/legacy/subadmin.php | 172 | ||||
-rw-r--r-- | lib/private/subadmin.php | 251 |
3 files changed, 347 insertions, 95 deletions
diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php index 65ae49dfcd2..6905dd8f5f2 100644 --- a/lib/private/group/manager.php +++ b/lib/private/group/manager.php @@ -71,11 +71,13 @@ class Manager extends PublicEmitter implements IGroupManager { */ private $cachedUserGroups = array(); + /** @var \OC\SubAdmin */ + private $subAdmin = null; /** * @param \OC\User\Manager $userManager */ - public function __construct($userManager) { + public function __construct(\OC\User\Manager $userManager) { $this->userManager = $userManager; $cachedGroups = & $this->cachedGroups; $cachedUserGroups = & $this->cachedUserGroups; @@ -314,4 +316,19 @@ class Manager extends PublicEmitter implements IGroupManager { } return $matchingUsers; } + + /** + * @return \OC\SubAdmin + */ + public function getSubAdmin() { + if (!$this->subAdmin) { + $this->subAdmin = new \OC\SubAdmin( + $this->userManager, + $this, + \OC::$server->getDatabaseConnection() + ); + } + + return $this->subAdmin; + } } diff --git a/lib/private/legacy/subadmin.php b/lib/private/legacy/subadmin.php new file mode 100644 index 00000000000..d10b6e90bb5 --- /dev/null +++ b/lib/private/legacy/subadmin.php @@ -0,0 +1,172 @@ +<?php +/** + * @author Bart Visscher <bartv@thisnet.nl> + * @author Georg Ehrke <georg@owncloud.com> + * @author Jörn Friedrich Dreyer <jfd@butonic.de> + * @author Lukas Reschke <lukas@owncloud.com> + * @author Morris Jobke <hey@morrisjobke.de> + * @author Robin McCorkell <rmccorkell@karoshi.org.uk> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * 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/> + * + */ + +/** + * This class provides all methods needed for managing groups. + * + * Hooks provided: + * post_createSubAdmin($gid) + * post_deleteSubAdmin($gid) + */ +class OC_SubAdmin{ + + /** + * add a SubAdmin + * @param string $uid uid of the SubAdmin + * @param string $gid gid of the group + * @return boolean + */ + public static function createSubAdmin($uid, $gid) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + return $subAdmin->createSubAdmin($userManager->get($uid), $groupManager->get($gid)); + } + + /** + * delete a SubAdmin + * @param string $uid uid of the SubAdmin + * @param string $gid gid of the group + * @return boolean + */ + public static function deleteSubAdmin($uid, $gid) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + return $subAdmin->deleteSubAdmin($userManager->get($uid), $groupManager->get($gid)); + } + + /** + * get groups of a SubAdmin + * @param string $uid uid of the SubAdmin + * @return array + */ + public static function getSubAdminsGroups($uid) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + $groups = $subAdmin->getSubAdminsGroups($userManager->get($uid)); + + // New class returns IGroup[] so convert back + $gids = []; + foreach ($groups as $group) { + $gids[] = $group->getGID(); + } + return $gids; + } + + /** + * get SubAdmins of a group + * @param string $gid gid of the group + * @return array + */ + public static function getGroupsSubAdmins($gid) { + $groupManager = \OC::$server->getGroupManager(); + $subAdmin = $groupManager->getSubAdmin(); + + $users = $subAdmin->getGroupsSubAdmins($groupManager->get($gid)); + + // New class returns IUser[] so convert back + $uids = []; + foreach ($users as $user) { + $uids[] = $user->getUID(); + } + return $uids; + } + + /** + * get all SubAdmins + * @return array + */ + public static function getAllSubAdmins() { + $groupManager = \OC::$server->getGroupManager(); + $subAdmin = $groupManager->getSubAdmin(); + + $subAdmins = $subAdmin->getAllSubAdmins(); + + // New class returns IUser[] so convert back + $result = []; + foreach ($subAdmins as $subAdmin) { + $result[] = [ + 'gid' => $subAdmin['group']->getGID(), + 'uid' => $subAdmin['user']->getUID(), + ]; + } + return $result; + } + + /** + * checks if a user is a SubAdmin of a group + * @param string $uid uid of the subadmin + * @param string $gid gid of the group + * @return bool + */ + public static function isSubAdminofGroup($uid, $gid) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + return $subAdmin->isSubAdminOfGroup($userManager->get($uid), $groupManager->get($gid)); + } + + /** + * checks if a user is a SubAdmin + * @param string $uid uid of the subadmin + * @return bool + */ + public static function isSubAdmin($uid) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + return $subAdmin->isSubAdmin($userManager->get($uid)); + } + + /** + * checks if a user is a accessible by a subadmin + * @param string $subadmin uid of the subadmin + * @param string $user uid of the user + * @return bool + */ + public static function isUserAccessible($subadmin, $user) { + $groupManager = \OC::$server->getGroupManager(); + $userManager = \OC::$server->getUserManager(); + $subAdmin = $groupManager->getSubAdmin(); + + return $subAdmin->isUserAccessible($userManager->get($subadmin), $userManager->get($user)); + } + + /* + * alias for self::isSubAdminofGroup() + */ + public static function isGroupAccessible($subadmin, $group) { + return self::isSubAdminofGroup($subadmin, $group); + } +} diff --git a/lib/private/subadmin.php b/lib/private/subadmin.php index ec4f9f3b899..0e130a7700a 100644 --- a/lib/private/subadmin.php +++ b/lib/private/subadmin.php @@ -2,10 +2,9 @@ /** * @author Bart Visscher <bartv@thisnet.nl> * @author Georg Ehrke <georg@owncloud.com> - * @author Jörn Friedrich Dreyer <jfd@butonic.de> * @author Lukas Reschke <lukas@owncloud.com> * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin McCorkell <rmccorkell@karoshi.org.uk> + * @author Roeland Jago Douma <roeland@famdouma.nl> * @author Thomas Müller <thomas.mueller@tmit.eu> * * @copyright Copyright (c) 2015, ownCloud, Inc. @@ -24,171 +23,235 @@ * along with this program. If not, see <http://www.gnu.org/licenses/> * */ -OC_Hook::connect('OC_User', 'post_deleteUser', 'OC_SubAdmin', 'post_deleteUser'); -OC_Hook::connect('OC_User', 'post_deleteGroup', 'OC_SubAdmin', 'post_deleteGroup'); -/** - * This class provides all methods needed for managing groups. - * - * Hooks provided: - * post_createSubAdmin($gid) - * post_deleteSubAdmin($gid) - */ -class OC_SubAdmin{ + +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); + }); + } /** * add a SubAdmin - * @param string $uid uid of the SubAdmin - * @param string $gid gid of the group - * @return boolean + * @param IUser $user user to be SubAdmin + * @param IGroup $group group $user becomes subadmin of + * @return bool */ - public static function createSubAdmin($uid, $gid) { - $stmt = OC_DB::prepare('INSERT INTO `*PREFIX*group_admin` (`gid`,`uid`) VALUES(?,?)'); - $stmt->execute(array($gid, $uid)); - OC_Hook::emit( "OC_SubAdmin", "post_createSubAdmin", array( "gid" => $gid )); + 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()]); return true; } /** * delete a SubAdmin - * @param string $uid uid of the SubAdmin - * @param string $gid gid of the group - * @return boolean + * @param IUser $user the user that is the SubAdmin + * @param IGroup $group the group + * @return bool */ - public static function deleteSubAdmin($uid, $gid) { - $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ? AND `uid` = ?'); - $stmt->execute(array($gid, $uid)); - OC_Hook::emit( "OC_SubAdmin", "post_deleteSubAdmin", array( "gid" => $gid )); + 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()]); return true; } /** * get groups of a SubAdmin - * @param string $uid uid of the SubAdmin - * @return array + * @param IUser $user the SubAdmin + * @return IGroup[] */ - public static function getSubAdminsGroups($uid) { - $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*group_admin` WHERE `uid` = ?'); - $result = $stmt->execute(array($uid)); - $gids = array(); - while($row = $result->fetchRow()) { - $gids[] = $row['gid']; + 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']); } - return $gids; + + return $groups; } /** * get SubAdmins of a group - * @param string $gid gid of the group - * @return array + * @param IGroup $group the group + * @return IUser[] */ - public static function getGroupsSubAdmins($gid) { - $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_admin` WHERE `gid` = ?'); - $result = $stmt->execute(array($gid)); - $uids = array(); - while($row = $result->fetchRow()) { - $uids[] = $row['uid']; + 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']); } - return $uids; + + return $users; } /** * get all SubAdmins * @return array */ - public static function getAllSubAdmins() { - $stmt = OC_DB::prepare('SELECT * FROM `*PREFIX*group_admin`'); - $result = $stmt->execute(); - $subadmins = array(); - while($row = $result->fetchRow()) { - $subadmins[] = $row; + 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']) + ]; } return $subadmins; } /** * checks if a user is a SubAdmin of a group - * @param string $uid uid of the subadmin - * @param string $gid gid of the group + * @param IUser $user + * @param IGroup $group * @return bool */ - public static function isSubAdminofGroup($uid, $gid) { - $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ? AND `gid` = ?'); - $result = $stmt->execute(array($uid, $gid)); - $result = $result->fetchRow(); - if($result['count'] >= 1) { - return true; - } - return false; + 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; } /** * checks if a user is a SubAdmin - * @param string $uid uid of the subadmin + * @param IUser $user * @return bool */ - public static function isSubAdmin($uid) { + public function isSubAdmin(IUser $user) { // Check if the user is already an admin - if(OC_Group::inGroup($uid, 'admin' )) { + if ($this->groupManager->isAdmin($user->getUID())) { return true; } - $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?'); - $result = $stmt->execute(array($uid)); - $result = $result->fetchRow(); - if($result['count'] > 0) { - return true; - } - return false; + $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; } /** * checks if a user is a accessible by a subadmin - * @param string $subadmin uid of the subadmin - * @param string $user uid of the user + * @param IUser $subadmin + * @param IUser $user * @return bool */ - public static function isUserAccessible($subadmin, $user) { - if(!self::isSubAdmin($subadmin)) { + public function isUserAccessible($subadmin, $user) { + if(!$this->isSubAdmin($subadmin)) { return false; } - if(OC_User::isAdminUser($user)) { + if($this->groupManager->isAdmin($user->getUID())) { return false; } - $accessiblegroups = self::getSubAdminsGroups($subadmin); + $accessiblegroups = $this->getSubAdminsGroups($subadmin); foreach($accessiblegroups as $accessiblegroup) { - if(OC_Group::inGroup($user, $accessiblegroup)) { + if($accessiblegroup->inGroup($user)) { return true; } } return false; } - /* - * alias for self::isSubAdminofGroup() - */ - public static function isGroupAccessible($subadmin, $group) { - return self::isSubAdminofGroup($subadmin, $group); - } - /** - * delete all SubAdmins by uid - * @param array $parameters + * delete all SubAdmins by $user + * @param IUser $user * @return boolean */ - public static function post_deleteUser($parameters) { - $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `uid` = ?'); - $stmt->execute(array($parameters['uid'])); + private function post_deleteUser($user) { + $qb = $this->dbConn->getQueryBuilder(); + + $result = $qb->delete('group_admin') + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID()))) + ->execute(); + return true; } /** - * delete all SubAdmins by gid - * @param array $parameters + * delete all SubAdmins by $group + * @param IGroup $group * @return boolean */ - public static function post_deleteGroup($parameters) { - $stmt = OC_DB::prepare('DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?'); - $stmt->execute(array($parameters['gid'])); + private function post_deleteGroup($group) { + $qb = $this->dbConn->getQueryBuilder(); + + $result = $qb->delete('group_admin') + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($group->getGID()))) + ->execute(); + return true; } } |