diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2013-09-25 15:36:30 +0400 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2013-09-30 18:36:59 +0400 |
commit | 9c9dc276b7a1d2592c4fb0a887888632dc1f1e29 (patch) | |
tree | bbe3aed3e09c31c68806bdb8acffef70ba08f51c /lib/private/group | |
parent | a711399e62d5a9f14d4b748efe4354ee37e61f13 (diff) |
move the private namespace OC into lib/private - OCP will stay in lib/public
Conflicts:
lib/private/vcategories.php
Diffstat (limited to 'lib/private/group')
-rw-r--r-- | lib/private/group/backend.php | 157 | ||||
-rw-r--r-- | lib/private/group/database.php | 239 | ||||
-rw-r--r-- | lib/private/group/dummy.php | 160 | ||||
-rw-r--r-- | lib/private/group/example.php | 109 | ||||
-rw-r--r-- | lib/private/group/group.php | 248 | ||||
-rw-r--r-- | lib/private/group/interface.php | 83 | ||||
-rw-r--r-- | lib/private/group/manager.php | 169 |
7 files changed, 1165 insertions, 0 deletions
diff --git a/lib/private/group/backend.php b/lib/private/group/backend.php new file mode 100644 index 00000000000..2e17b5d0b7f --- /dev/null +++ b/lib/private/group/backend.php @@ -0,0 +1,157 @@ +<?php + +/** +* ownCloud +* +* @author Frank Karlitschek +* @copyright 2012 Frank Karlitschek frank@owncloud.org +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/** + * error code for functions not provided by the group backend + */ +define('OC_GROUP_BACKEND_NOT_IMPLEMENTED', -501); + +/** + * actions that user backends can define + */ +define('OC_GROUP_BACKEND_CREATE_GROUP', 0x00000001); +define('OC_GROUP_BACKEND_DELETE_GROUP', 0x00000010); +define('OC_GROUP_BACKEND_ADD_TO_GROUP', 0x00000100); +define('OC_GROUP_BACKEND_REMOVE_FROM_GOUP', 0x00001000); +define('OC_GROUP_BACKEND_GET_DISPLAYNAME', 0x00010000); + +/** + * Abstract base class for user management + */ +abstract class OC_Group_Backend implements OC_Group_Interface { + protected $possibleActions = array( + OC_GROUP_BACKEND_CREATE_GROUP => 'createGroup', + OC_GROUP_BACKEND_DELETE_GROUP => 'deleteGroup', + OC_GROUP_BACKEND_ADD_TO_GROUP => 'addToGroup', + OC_GROUP_BACKEND_REMOVE_FROM_GOUP => 'removeFromGroup', + OC_GROUP_BACKEND_GET_DISPLAYNAME => 'displayNamesInGroup', + ); + + /** + * @brief Get all supported actions + * @return int bitwise-or'ed actions + * + * Returns the supported actions as int to be + * compared with OC_USER_BACKEND_CREATE_USER etc. + */ + public function getSupportedActions() { + $actions = 0; + foreach($this->possibleActions AS $action => $methodName) { + if(method_exists($this, $methodName)) { + $actions |= $action; + } + } + + return $actions; + } + + /** + * @brief Check if backend implements actions + * @param int $actions bitwise-or'ed actions + * @return boolean + * + * Returns the supported actions as int to be + * compared with OC_GROUP_BACKEND_CREATE_GROUP etc. + */ + public function implementsActions($actions) { + return (bool)($this->getSupportedActions() & $actions); + } + + /** + * @brief is user in group? + * @param string $uid uid of the user + * @param string $gid gid of the group + * @return bool + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid) { + return in_array($gid, $this->getUserGroups($uid)); + } + + /** + * @brief Get all groups a user belongs to + * @param string $uid Name of the user + * @return array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid) { + return array(); + } + + /** + * @brief get a list of all groups + * @param string $search + * @param int $limit + * @param int $offset + * @return array with group names + * + * Returns a list with all groups + */ + + public function getGroups($search = '', $limit = -1, $offset = 0) { + return array(); + } + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + return in_array($gid, $this->getGroups($gid, 1)); + } + + /** + * @brief get a list of all users in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + return array(); + } + + /** + * @brief get a list of all display names in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with display names (value) and user ids (key) + */ + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { + $displayNames = array(); + $users = $this->usersInGroup($gid, $search, $limit, $offset); + foreach ($users as $user) { + $displayNames[$user] = $user; + } + + return $displayNames; + } + +} diff --git a/lib/private/group/database.php b/lib/private/group/database.php new file mode 100644 index 00000000000..d0974685ff6 --- /dev/null +++ b/lib/private/group/database.php @@ -0,0 +1,239 @@ +<?php + +/** + * ownCloud + * + * @author Frank Karlitschek + * @copyright 2012 Frank Karlitschek frank@owncloud.org + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ +/* + * + * The following SQL statement is just a help for developers and will not be + * executed! + * + * CREATE TABLE `groups` ( + * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL, + * PRIMARY KEY (`gid`) + * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + * + * CREATE TABLE `group_user` ( + * `gid` varchar(64) COLLATE utf8_unicode_ci NOT NULL, + * `uid` varchar(64) COLLATE utf8_unicode_ci NOT NULL + * ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + * + */ + +/** + * Class for group management in a SQL Database (e.g. MySQL, SQLite) + */ +class OC_Group_Database extends OC_Group_Backend { + + /** + * @brief Try to create a new group + * @param string $gid The name of the group to create + * @return bool + * + * Tries to create a new group. If the group name already exists, false will + * be returned. + */ + public function createGroup( $gid ) { + // Check for existence + $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" ); + $result = $stmt->execute( array( $gid )); + + if( $result->fetchRow() ) { + // Can not add an existing group + return false; + } + else{ + // Add group and exit + $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" ); + $result = $stmt->execute( array( $gid )); + + return $result ? true : false; + } + } + + /** + * @brief delete a group + * @param string $gid gid of the group to delete + * @return bool + * + * Deletes a group and removes it from the group_user-table + */ + public function deleteGroup( $gid ) { + // Delete the group + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*groups` WHERE `gid` = ?" ); + $stmt->execute( array( $gid )); + + // Delete the group-user relation + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `gid` = ?" ); + $stmt->execute( array( $gid )); + + return true; + } + + /** + * @brief is user in group? + * @param string $uid uid of the user + * @param string $gid gid of the group + * @return bool + * + * Checks whether the user is member of a group or not. + */ + public function inGroup( $uid, $gid ) { + // check + $stmt = OC_DB::prepare( "SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` = ?" ); + $result = $stmt->execute( array( $gid, $uid )); + + return $result->fetchRow() ? true : false; + } + + /** + * @brief Add a user to a group + * @param string $uid Name of the user to add to group + * @param string $gid Name of the group in which add the user + * @return bool + * + * Adds a user to a group. + */ + public function addToGroup( $uid, $gid ) { + // No duplicate entries! + if( !$this->inGroup( $uid, $gid )) { + $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*group_user` ( `uid`, `gid` ) VALUES( ?, ? )" ); + $stmt->execute( array( $uid, $gid )); + return true; + }else{ + return false; + } + } + + /** + * @brief Removes a user from a group + * @param string $uid Name of the user to remove from group + * @param string $gid Name of the group from which remove the user + * @return bool + * + * removes the user from a group. + */ + public function removeFromGroup( $uid, $gid ) { + $stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_user` WHERE `uid` = ? AND `gid` = ?" ); + $stmt->execute( array( $uid, $gid )); + + return true; + } + + /** + * @brief Get all groups a user belongs to + * @param string $uid Name of the user + * @return array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups( $uid ) { + // No magic! + $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" ); + $result = $stmt->execute( array( $uid )); + + $groups = array(); + while( $row = $result->fetchRow()) { + $groups[] = $row["gid"]; + } + + return $groups; + } + + /** + * @brief get a list of all groups + * @param string $search + * @param int $limit + * @param int $offset + * @return array with group names + * + * Returns a list with all groups + */ + public function getGroups($search = '', $limit = null, $offset = null) { + $stmt = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` LIKE ?', $limit, $offset); + $result = $stmt->execute(array($search.'%')); + $groups = array(); + while ($row = $result->fetchRow()) { + $groups[] = $row['gid']; + } + return $groups; + } + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + $query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?'); + $result = $query->execute(array($gid))->fetchOne(); + if ($result) { + return true; + } + return false; + } + + /** + * @brief get a list of all users in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { + $stmt = OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ? AND `uid` LIKE ?', + $limit, + $offset); + $result = $stmt->execute(array($gid, $search.'%')); + $users = array(); + while ($row = $result->fetchRow()) { + $users[] = $row['uid']; + } + return $users; + } + + /** + * @brief get a list of all display names in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with display names (value) and user ids (key) + */ + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { + $displayNames = array(); + + $stmt = OC_DB::prepare('SELECT `*PREFIX*users`.`uid`, `*PREFIX*users`.`displayname`' + .' FROM `*PREFIX*users`' + .' INNER JOIN `*PREFIX*group_user` ON `*PREFIX*group_user`.`uid` = `*PREFIX*users`.`uid`' + .' WHERE `gid` = ? AND `*PREFIX*group_user`.`uid` LIKE ?', + $limit, + $offset); + $result = $stmt->execute(array($gid, $search.'%')); + $users = array(); + while ($row = $result->fetchRow()) { + $displayName = trim($row['displayname'], ' '); + $displayNames[$row['uid']] = empty($displayName) ? $row['uid'] : $displayName; + } + return $displayNames; + } +} diff --git a/lib/private/group/dummy.php b/lib/private/group/dummy.php new file mode 100644 index 00000000000..9516fd52ff8 --- /dev/null +++ b/lib/private/group/dummy.php @@ -0,0 +1,160 @@ +<?php + +/** +* ownCloud +* +* @author Frank Karlitschek +* @copyright 2012 Frank Karlitschek frank@owncloud.org +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/** + * dummy group backend, does not keep state, only for testing use + */ +class OC_Group_Dummy extends OC_Group_Backend { + private $groups=array(); + /** + * @brief Try to create a new group + * @param $gid The name of the group to create + * @returns true/false + * + * Trys to create a new group. If the group name already exists, false will + * be returned. + */ + public function createGroup($gid) { + if(!isset($this->groups[$gid])) { + $this->groups[$gid]=array(); + return true; + }else{ + return false; + } + } + + /** + * @brief delete a group + * @param $gid gid of the group to delete + * @returns true/false + * + * Deletes a group and removes it from the group_user-table + */ + public function deleteGroup($gid) { + if(isset($this->groups[$gid])) { + unset($this->groups[$gid]); + return true; + }else{ + return false; + } + } + + /** + * @brief is user in group? + * @param $uid uid of the user + * @param $gid gid of the group + * @returns true/false + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + return (array_search($uid, $this->groups[$gid])!==false); + }else{ + return false; + } + } + + /** + * @brief Add a user to a group + * @param $uid Name of the user to add to group + * @param $gid Name of the group in which add the user + * @returns true/false + * + * Adds a user to a group. + */ + public function addToGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + if(array_search($uid, $this->groups[$gid])===false) { + $this->groups[$gid][]=$uid; + return true; + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * @brief Removes a user from a group + * @param $uid NameUSER of the user to remove from group + * @param $gid Name of the group from which remove the user + * @returns true/false + * + * removes the user from a group. + */ + public function removeFromGroup($uid, $gid) { + if(isset($this->groups[$gid])) { + if(($index=array_search($uid, $this->groups[$gid]))!==false) { + unset($this->groups[$gid][$index]); + }else{ + return false; + } + }else{ + return false; + } + } + + /** + * @brief Get all groups a user belongs to + * @param $uid Name of the user + * @returns array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid) { + $groups=array(); + $allGroups=array_keys($this->groups); + foreach($allGroups as $group) { + if($this->inGroup($uid, $group)) { + $groups[]=$group; + } + } + return $groups; + } + + /** + * @brief get a list of all groups + * @returns array with group names + * + * Returns a list with all groups + */ + public function getGroups($search = '', $limit = -1, $offset = 0) { + return array_keys($this->groups); + } + + /** + * @brief get a list of all users in a group + * @returns array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { + if(isset($this->groups[$gid])) { + return $this->groups[$gid]; + }else{ + return array(); + } + } + +} diff --git a/lib/private/group/example.php b/lib/private/group/example.php new file mode 100644 index 00000000000..3519b9ed92f --- /dev/null +++ b/lib/private/group/example.php @@ -0,0 +1,109 @@ +<?php + +/** +* ownCloud +* +* @author Frank Karlitschek +* @copyright 2012 Frank Karlitschek frank@owncloud.org +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +/** + * abstract reference class for group management + * this class should only be used as a reference for method signatures and their descriptions + */ +abstract class OC_Group_Example { + /** + * @brief Try to create a new group + * @param $gid The name of the group to create + * @returns true/false + * + * Trys to create a new group. If the group name already exists, false will + * be returned. + */ + abstract public static function createGroup($gid); + + /** + * @brief delete a group + * @param $gid gid of the group to delete + * @returns true/false + * + * Deletes a group and removes it from the group_user-table + */ + abstract public static function deleteGroup($gid); + + /** + * @brief is user in group? + * @param $uid uid of the user + * @param $gid gid of the group + * @returns true/false + * + * Checks whether the user is member of a group or not. + */ + abstract public static function inGroup($uid, $gid); + + /** + * @brief Add a user to a group + * @param $uid Name of the user to add to group + * @param $gid Name of the group in which add the user + * @returns true/false + * + * Adds a user to a group. + */ + abstract public static function addToGroup($uid, $gid); + + /** + * @brief Removes a user from a group + * @param $uid NameUSER of the user to remove from group + * @param $gid Name of the group from which remove the user + * @returns true/false + * + * removes the user from a group. + */ + abstract public static function removeFromGroup($uid, $gid); + + /** + * @brief Get all groups a user belongs to + * @param $uid Name of the user + * @returns array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + abstract public static function getUserGroups($uid); + + /** + * @brief get a list of all groups + * @returns array with group names + * + * Returns a list with all groups + */ + abstract public static function getGroups($search = '', $limit = -1, $offset = 0); + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + abstract public function groupExists($gid); + + /** + * @brief get a list of all users in a group + * @returns array with user ids + */ + abstract public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); + +} diff --git a/lib/private/group/group.php b/lib/private/group/group.php new file mode 100644 index 00000000000..bcd2419b309 --- /dev/null +++ b/lib/private/group/group.php @@ -0,0 +1,248 @@ +<?php + +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Group; + +class Group { + /** + * @var string $id + */ + private $gid; + + /** + * @var \OC\User\User[] $users + */ + private $users; + + /** + * @var \OC_Group_Backend[] | \OC_Group_Database[] $backend + */ + private $backends; + + /** + * @var \OC\Hooks\PublicEmitter $emitter; + */ + private $emitter; + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @param string $gid + * @param \OC_Group_Backend[] $backends + * @param \OC\User\Manager $userManager + * @param \OC\Hooks\PublicEmitter $emitter + */ + public function __construct($gid, $backends, $userManager, $emitter = null) { + $this->gid = $gid; + $this->backends = $backends; + $this->userManager = $userManager; + $this->emitter = $emitter; + } + + public function getGID() { + return $this->gid; + } + + /** + * get all users in the group + * + * @return \OC\User\User[] + */ + public function getUsers() { + if ($this->users) { + return $this->users; + } + + $userIds = array(); + foreach ($this->backends as $backend) { + $diff = array_diff( + $backend->usersInGroup($this->gid), + $userIds + ); + if ($diff) { + $userIds = array_merge($userIds, $diff); + } + } + + $this->users = $this->getVerifiedUsers($userIds); + return $this->users; + } + + /** + * check if a user is in the group + * + * @param \OC\User\User $user + * @return bool + */ + public function inGroup($user) { + foreach ($this->backends as $backend) { + if ($backend->inGroup($user->getUID(), $this->gid)) { + return true; + } + } + return false; + } + + /** + * add a user to the group + * + * @param \OC\User\User $user + */ + public function addUser($user) { + if ($this->inGroup($user)) { + return; + } + + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) { + $backend->addToGroup($user->getUID(), $this->gid); + if ($this->users) { + $this->users[$user->getUID()] = $user; + } + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); + } + return; + } + } + } + + /** + * remove a user from the group + * + * @param \OC\User\User $user + */ + public function removeUser($user) { + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { + $backend->removeFromGroup($user->getUID(), $this->gid); + $result = true; + } + } + if ($result) { + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); + } + if ($this->users) { + foreach ($this->users as $index => $groupUser) { + if ($groupUser->getUID() === $user->getUID()) { + unset($this->users[$index]); + return; + } + } + } + } + } + + /** + * search for users in the group by userid + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchUsers($search, $limit = null, $offset = null) { + $users = array(); + foreach ($this->backends as $backend) { + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); + if (!is_null($limit)) { + $limit -= count($userIds); + } + if (!is_null($offset)) { + $offset -= count($userIds); + } + $users += $this->getVerifiedUsers($userIds); + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * search for users in the group by displayname + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchDisplayName($search, $limit = null, $offset = null) { + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { + $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset)); + } else { + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); + } + if (!is_null($limit)) { + $limit -= count($userIds); + } + if (!is_null($offset)) { + $offset -= count($userIds); + } + $users = $this->getVerifiedUsers($userIds); + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * delete the group + * + * @return bool + */ + public function delete() { + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) { + $result = true; + $backend->deleteGroup($this->gid); + } + } + if ($result and $this->emitter) { + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); + } + return $result; + } + + /** + * @brief returns all the Users from an array that really exists + * @param $userIds an array containing user IDs + * @return an Array with the userId as Key and \OC\User\User as value + */ + private function getVerifiedUsers($userIds) { + if(!is_array($userIds)) { + return array(); + } + $users = array(); + foreach ($userIds as $userId) { + $user = $this->userManager->get($userId); + if(!is_null($user)) { + $users[$userId] = $user; + } + } + return $users; + } +} diff --git a/lib/private/group/interface.php b/lib/private/group/interface.php new file mode 100644 index 00000000000..4ef3663837f --- /dev/null +++ b/lib/private/group/interface.php @@ -0,0 +1,83 @@ +<?php + +/** + * ownCloud - group interface + * + * @author Arthur Schiwon + * @copyright 2012 Arthur Schiwon blizzz@owncloud.org + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +interface OC_Group_Interface { + /** + * @brief Check if backend implements actions + * @param int $actions bitwise-or'ed actions + * @return boolean + * + * Returns the supported actions as int to be + * compared with OC_GROUP_BACKEND_CREATE_GROUP etc. + */ + public function implementsActions($actions); + + /** + * @brief is user in group? + * @param string $uid uid of the user + * @param string $gid gid of the group + * @return bool + * + * Checks whether the user is member of a group or not. + */ + public function inGroup($uid, $gid); + + /** + * @brief Get all groups a user belongs to + * @param string $uid Name of the user + * @return array with group names + * + * This function fetches all groups a user belongs to. It does not check + * if the user exists at all. + */ + public function getUserGroups($uid); + + /** + * @brief get a list of all groups + * @param string $search + * @param int $limit + * @param int $offset + * @return array with group names + * + * Returns a list with all groups + */ + public function getGroups($search = '', $limit = -1, $offset = 0); + + /** + * check if a group exists + * @param string $gid + * @return bool + */ + public function groupExists($gid); + + /** + * @brief get a list of all users in a group + * @param string $gid + * @param string $search + * @param int $limit + * @param int $offset + * @return array with user ids + */ + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0); + +} diff --git a/lib/private/group/manager.php b/lib/private/group/manager.php new file mode 100644 index 00000000000..bf469d51d12 --- /dev/null +++ b/lib/private/group/manager.php @@ -0,0 +1,169 @@ +<?php + +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Group; + +use OC\Hooks\PublicEmitter; + +/** + * Class Manager + * + * Hooks available in scope \OC\Group: + * - preAddUser(\OC\Group\Group $group, \OC\User\User $user) + * - postAddUser(\OC\Group\Group $group, \OC\User\User $user) + * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user) + * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user) + * - preDelete(\OC\Group\Group $group) + * - postDelete(\OC\Group\Group $group) + * - preCreate(string $groupId) + * - postCreate(\OC\Group\Group $group) + * + * @package OC\Group + */ +class Manager extends PublicEmitter { + /** + * @var \OC_Group_Backend[] | \OC_Group_Database[] $backends + */ + private $backends = array(); + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @var \OC\Group\Group[] + */ + private $cachedGroups; + + /** + * @param \OC\User\Manager $userManager + */ + public function __construct($userManager) { + $this->userManager = $userManager; + $cache = & $this->cachedGroups; + $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cache) { + /** + * @var \OC\Group\Group $group + */ + unset($cache[$group->getGID()]); + }); + } + + /** + * @param \OC_Group_Backend $backend + */ + public function addBackend($backend) { + $this->backends[] = $backend; + } + + public function clearBackends() { + $this->backends = array(); + $this->cachedGroups = array(); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function get($gid) { + if (isset($this->cachedGroups[$gid])) { + return $this->cachedGroups[$gid]; + } + foreach ($this->backends as $backend) { + if ($backend->groupExists($gid)) { + return $this->getGroupObject($gid); + } + } + return null; + } + + protected function getGroupObject($gid) { + $backends = array(); + foreach ($this->backends as $backend) { + if ($backend->groupExists($gid)) { + $backends[] = $backend; + } + } + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); + return $this->cachedGroups[$gid]; + } + + /** + * @param string $gid + * @return bool + */ + public function groupExists($gid) { + return !is_null($this->get($gid)); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function createGroup($gid) { + if (!$gid) { + return false; + } else if ($this->groupExists($gid)) { + return $this->get($gid); + } else { + $this->emit('\OC\Group', 'preCreate', array($gid)); + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) { + $backend->createGroup($gid); + $group = $this->getGroupObject($gid); + $this->emit('\OC\Group', 'postCreate', array($group)); + return $group; + } + } + return null; + } + } + + /** + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\Group\Group[] + */ + public function search($search, $limit = null, $offset = null) { + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getGroups($search, $limit, $offset); + if (!is_null($limit)) { + $limit -= count($groupIds); + } + if (!is_null($offset)) { + $offset -= count($groupIds); + } + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->getGroupObject($groupId); + } + if (!is_null($limit) and $limit <= 0) { + return array_values($groups); + } + } + return array_values($groups); + } + + /** + * @param \OC\User\User $user + * @return \OC\Group\Group[] + */ + public function getUserGroups($user) { + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getUserGroups($user->getUID()); + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->getGroupObject($groupId); + } + } + return array_values($groups); + } +} |