diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2015-08-13 21:31:38 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-10-21 11:32:38 +0200 |
commit | 0e9ab139432b562f442e01a983b7dcfbd6499115 (patch) | |
tree | 1a6c6a6d9e37de0e02f2dcdf8856ba839079f6f7 /lib/private/subadmin.php | |
parent | d0aeb268d687f803e62069c852bcd7a1a86eadee (diff) | |
download | nextcloud-server-0e9ab139432b562f442e01a983b7dcfbd6499115.tar.gz nextcloud-server-0e9ab139432b562f442e01a983b7dcfbd6499115.zip |
New \OC\SubAdmin class
* DI
* Tests
* moved OC_SubAdmin to legacy
* Added to private OC\GroupManager
Diffstat (limited to 'lib/private/subadmin.php')
-rw-r--r-- | lib/private/subadmin.php | 251 |
1 files changed, 157 insertions, 94 deletions
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; } } |