From fe7d9a7ca07bb21905c6483dee49bf37dd131674 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 4 Dec 2014 14:15:55 +0100 Subject: Add REST route for user & group management First step of a somewhat testable user management. - I know, the JSON returns are in an ugly format but the JS expects it that way. So let's keep it that way until we have time to fix the JS in the future. --- lib/private/group.php | 22 +++++++++------------- lib/private/group/group.php | 5 +++++ lib/private/group/metadata.php | 7 ++++--- lib/private/server.php | 22 ++++++++++++++++++++-- lib/private/user.php | 25 +++++-------------------- lib/private/user/manager.php | 2 +- lib/private/user/user.php | 18 ++++++++++++++++++ 7 files changed, 62 insertions(+), 39 deletions(-) (limited to 'lib') diff --git a/lib/private/group.php b/lib/private/group.php index 49f683c411a..d6e6e17f881 100644 --- a/lib/private/group.php +++ b/lib/private/group.php @@ -37,6 +37,7 @@ class OC_Group { /** * @return \OC\Group\Manager + * @deprecated Use \OC::$server->getGroupManager(); */ public static function getManager() { return \OC::$server->getGroupManager(); @@ -44,6 +45,7 @@ class OC_Group { /** * @return \OC\User\Manager + * @deprecated Use \OC::$server->getUserManager() */ private static function getUserManager() { return \OC::$server->getUserManager(); @@ -73,12 +75,10 @@ class OC_Group { * * Tries to create a new group. If the group name already exists, false will * be returned. Basic checking of Group name + * @deprecated Use \OC::$server->getGroupManager()->createGroup() instead */ public static function createGroup($gid) { - OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid)); - if (self::getManager()->createGroup($gid)) { - OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid)); return true; } else { return false; @@ -91,19 +91,12 @@ class OC_Group { * @return bool * * Deletes a group and removes it from the group_user-table + * @deprecated Use \OC::$server->getGroupManager()->delete() instead */ public static function deleteGroup($gid) { - // Prevent users from deleting group admin - if ($gid == "admin") { - return false; - } - - OC_Hook::emit("OC_Group", "pre_deleteGroup", array("run" => true, "gid" => $gid)); - $group = self::getManager()->get($gid); if ($group) { if ($group->delete()) { - OC_Hook::emit("OC_User", "post_deleteGroup", array("gid" => $gid)); return true; } } @@ -117,6 +110,7 @@ class OC_Group { * @return bool * * Checks whether the user is member of a group or not. + * @deprecated Use \OC::$server->getGroupManager->inGroup($user); */ public static function inGroup($uid, $gid) { $group = self::getManager()->get($gid); @@ -134,14 +128,13 @@ class OC_Group { * @return bool * * Adds a user to a group. + * @deprecated Use \OC::$server->getGroupManager->addUser(); */ public static function addToGroup($uid, $gid) { $group = self::getManager()->get($gid); $user = self::getUserManager()->get($uid); if ($group and $user) { - OC_Hook::emit("OC_Group", "pre_addToGroup", array("run" => true, "uid" => $uid, "gid" => $gid)); $group->addUser($user); - OC_Hook::emit("OC_User", "post_addToGroup", array("uid" => $uid, "gid" => $gid)); return true; } else { return false; @@ -176,6 +169,7 @@ class OC_Group { * * This function fetches all groups a user belongs to. It does not check * if the user exists at all. + * @deprecated Use \OC::$server->getGroupManager->getuserGroupIds($user) */ public static function getUserGroups($uid) { $user = self::getUserManager()->get($uid); @@ -209,6 +203,7 @@ class OC_Group { * * @param string $gid * @return bool + * @deprecated Use \OC::$server->getGroupManager->groupExists($gid) */ public static function groupExists($gid) { return self::getManager()->groupExists($gid); @@ -260,6 +255,7 @@ class OC_Group { * @param int $limit * @param int $offset * @return array an array of display names (value) and user ids(key) + * @deprecated Use \OC::$server->getGroupManager->displayNamesInGroup($gid, $search, $limit, $offset) */ public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { return self::getManager()->displayNamesInGroup($gid, $search, $limit, $offset); diff --git a/lib/private/group/group.php b/lib/private/group/group.php index 6111051ea09..5f439e91cde 100644 --- a/lib/private/group/group.php +++ b/lib/private/group/group.php @@ -229,6 +229,11 @@ class Group implements IGroup { * @return bool */ public function delete() { + // Prevent users from deleting group admin + if ($this->getGID() === 'admin') { + return false; + } + $result = false; if ($this->emitter) { $this->emitter->emit('\OC\Group', 'preDelete', array($this)); diff --git a/lib/private/group/metadata.php b/lib/private/group/metadata.php index 687a735347c..c702c924ff7 100644 --- a/lib/private/group/metadata.php +++ b/lib/private/group/metadata.php @@ -29,7 +29,7 @@ class MetaData { protected $metaData = array(); /** - * @var \OC\Group\Manager $groupManager + * @var \OCP\IGroupManager $groupManager */ protected $groupManager; @@ -41,12 +41,12 @@ class MetaData { /** * @param string $user the uid of the current user * @param bool $isAdmin whether the current users is an admin - * @param \OC\Group\Manager $groupManager + * @param \OCP\IGroupManager $groupManager */ public function __construct( $user, $isAdmin, - \OC\Group\Manager $groupManager + \OCP\IGroupManager $groupManager ) { $this->user = $user; $this->isAdmin = (bool)$isAdmin; @@ -168,6 +168,7 @@ class MetaData { if($this->isAdmin) { return $this->groupManager->search($search); } else { + // FIXME: Remove static method call $groupIds = \OC_SubAdmin::getSubAdminsGroups($this->user); /* \OC_SubAdmin::getSubAdminsGroups() returns an array of GIDs, but this diff --git a/lib/private/server.php b/lib/private/server.php index 7bd7f8ca45d..a08014fa6fa 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -104,8 +104,26 @@ class Server extends SimpleContainer implements IServerContainer { return new \OC\User\Manager($config); }); $this->registerService('GroupManager', function (Server $c) { - $userManager = $c->getUserManager(); - return new \OC\Group\Manager($userManager); + $groupManager = new \OC\Group\Manager($this->getUserManager()); + $groupManager->listen('\OC\Group', 'preCreate', function ($gid) { + \OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid)); + }); + $groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $gid) { + \OC_Hook::emit('OC_User', 'post_createGroup', array('gid' => $gid->getGID())); + }); + $groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) { + \OC_Hook::emit('OC_Group', 'pre_deleteGroup', array('run' => true, 'gid' => $group->getGID())); + }); + $groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) { + \OC_Hook::emit('OC_User', 'post_deleteGroup', array('gid' => $group->getGID())); + }); + $groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) { + \OC_Hook::emit('OC_Group', 'pre_addToGroup', array('run' => true, 'uid' => $user->getUID(), 'gid' => $group->getGID())); + }); + $groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) { + \OC_Hook::emit('OC_Group', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID())); + }); + return $groupManager; }); $this->registerService('UserSession', function (Server $c) { $manager = $c->getUserManager(); diff --git a/lib/private/user.php b/lib/private/user.php index b2a235425c4..f93b76a3a64 100644 --- a/lib/private/user.php +++ b/lib/private/user.php @@ -47,6 +47,7 @@ class OC_User { /** * @return \OC\User\Manager + * @deprecated Use \OC::$server->getUserManager() */ public static function getManager() { return OC::$server->getUserManager(); @@ -179,6 +180,7 @@ class OC_User { * itself, not in its subclasses. * * Allowed characters in the username are: "a-z", "A-Z", "0-9" and "_.@-" + * @deprecated Use \OC::$server->getUserManager->createUser($uid, $password) */ public static function createUser($uid, $password) { return self::getManager()->createUser($uid, $password); @@ -190,30 +192,12 @@ class OC_User { * @return bool * * Deletes a user + * @deprecated Use \OC::$server->getUserManager->delete() */ public static function deleteUser($uid) { $user = self::getManager()->get($uid); if ($user) { - $result = $user->delete(); - - // if delete was successful we clean-up the rest - if ($result) { - - // We have to delete the user from all groups - foreach (OC_Group::getUserGroups($uid) as $i) { - OC_Group::removeFromGroup($uid, $i); - } - // Delete the user's keys in preferences - OC_Preferences::deleteUser($uid); - - // Delete user files in /data/ - OC_Helper::rmdirr(\OC_User::getHome($uid)); - - // Delete the users entry in the storage table - \OC\Files\Cache\Storage::remove('home::' . $uid); - } - - return true; + return $user->delete(); } else { return false; } @@ -525,6 +509,7 @@ class OC_User { * @return string * * returns the path to the users home directory + * @deprecated Use \OC::$server->getUserManager->getHome() */ public static function getHome($uid) { $user = self::getManager()->get($uid); diff --git a/lib/private/user/manager.php b/lib/private/user/manager.php index 0c01f957bd3..2403f45aa2f 100644 --- a/lib/private/user/manager.php +++ b/lib/private/user/manager.php @@ -220,7 +220,7 @@ class Manager extends PublicEmitter implements IUserManager { * @param string $uid * @param string $password * @throws \Exception - * @return bool|\OC\User\User the created user of false + * @return bool|\OC\User\User the created user or false */ public function createUser($uid, $password) { $l = \OC::$server->getL10N('lib'); diff --git a/lib/private/user/user.php b/lib/private/user/user.php index 9ad2f5f0d3a..ad85337f628 100644 --- a/lib/private/user/user.php +++ b/lib/private/user/user.php @@ -153,6 +153,24 @@ class User implements IUser { $this->emitter->emit('\OC\User', 'preDelete', array($this)); } $result = $this->backend->deleteUser($this->uid); + if ($result) { + + // FIXME: Feels like an hack - suggestions? + + // We have to delete the user from all groups + foreach (\OC_Group::getUserGroups($this->uid) as $i) { + \OC_Group::removeFromGroup($this->uid, $i); + } + // Delete the user's keys in preferences + \OC_Preferences::deleteUser($this->uid); + + // Delete user files in /data/ + \OC_Helper::rmdirr(\OC_User::getHome($this->uid)); + + // Delete the users entry in the storage table + \OC\Files\Cache\Storage::remove('home::' . $this->uid); + } + if ($this->emitter) { $this->emitter->emit('\OC\User', 'postDelete', array($this)); } -- cgit v1.2.3