diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php index 323c8d609c..2ee3a185da 100644 --- a/apps/provisioning_api/appinfo/routes.php +++ b/apps/provisioning_api/appinfo/routes.php @@ -21,30 +21,45 @@ * */ -// Users +namespace OCA\Provisioning_API\AppInfo; + use OCP\API; -API::register('get', '/cloud/users', array('OCA\Provisioning_API\Users', 'getUsers'), 'provisioning_api', API::ADMIN_AUTH); -API::register('post', '/cloud/users', array('OCA\Provisioning_API\Users', 'addUser'), 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/users/{userid}', array('OCA\Provisioning_API\Users', 'getUser'), 'provisioning_api', API::USER_AUTH); -API::register('put', '/cloud/users/{userid}', array('OCA\Provisioning_API\Users', 'editUser'), 'provisioning_api', API::USER_AUTH); -API::register('delete', '/cloud/users/{userid}', array('OCA\Provisioning_API\Users', 'deleteUser'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('get', '/cloud/users/{userid}/groups', array('OCA\Provisioning_API\Users', 'getUsersGroups'), 'provisioning_api', API::USER_AUTH); -API::register('post', '/cloud/users/{userid}/groups', array('OCA\Provisioning_API\Users', 'addToGroup'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('delete', '/cloud/users/{userid}/groups', array('OCA\Provisioning_API\Users', 'removeFromGroup'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('post', '/cloud/users/{userid}/subadmins', array('OCA\Provisioning_API\Users', 'addSubAdmin'), 'provisioning_api', API::ADMIN_AUTH); -API::register('delete', '/cloud/users/{userid}/subadmins', array('OCA\Provisioning_API\Users', 'removeSubAdmin'), 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/users/{userid}/subadmins', array('OCA\Provisioning_API\Users', 'getUserSubAdminGroups'), 'provisioning_api', API::ADMIN_AUTH); +// Users +$users = new \OCA\Provisioning_API\Users( + \OC::$server->getUserManager(), + \OC::$server->getConfig(), + \OC::$server->getGroupManager(), + \OC::$server->getUserSession() +); +API::register('get', '/cloud/users', [$users, 'getUsers'], 'provisioning_api', API::ADMIN_AUTH); +API::register('post', '/cloud/users', [$users, 'addUser'], 'provisioning_api', API::ADMIN_AUTH); +API::register('get', '/cloud/users/{userid}', [$users, 'getUser'], 'provisioning_api', API::USER_AUTH); +API::register('put', '/cloud/users/{userid}', [$users, 'editUser'], 'provisioning_api', API::USER_AUTH); +API::register('delete', '/cloud/users/{userid}', [$users, 'deleteUser'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('get', '/cloud/users/{userid}/groups', [$users, 'getUsersGroups'], 'provisioning_api', API::USER_AUTH); +API::register('post', '/cloud/users/{userid}/groups', [$users, 'addToGroup'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('delete', '/cloud/users/{userid}/groups', [$users, 'removeFromGroup'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('post', '/cloud/users/{userid}/subadmins', [$users, 'addSubAdmin'], 'provisioning_api', API::ADMIN_AUTH); +API::register('delete', '/cloud/users/{userid}/subadmins', [$users, 'removeSubAdmin'], 'provisioning_api', API::ADMIN_AUTH); +API::register('get', '/cloud/users/{userid}/subadmins', [$users, 'getUserSubAdminGroups'], 'provisioning_api', API::ADMIN_AUTH); // Groups -API::register('get', '/cloud/groups', array('OCA\Provisioning_API\Groups', 'getGroups'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('post', '/cloud/groups', array('OCA\Provisioning_API\Groups', 'addGroup'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('get', '/cloud/groups/{groupid}', array('OCA\Provisioning_API\Groups', 'getGroup'), 'provisioning_api', API::SUBADMIN_AUTH); -API::register('delete', '/cloud/groups/{groupid}', array('OCA\Provisioning_API\Groups', 'deleteGroup'), 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/groups/{groupid}/subadmins', array('OCA\Provisioning_API\Groups', 'getSubAdminsOfGroup'), 'provisioning_api', API::ADMIN_AUTH); +$groups = new \OCA\Provisioning_API\Groups( + \OC::$server->getGroupManager(), + \OC::$server->getUserSession() +); +API::register('get', '/cloud/groups', [$groups, 'getGroups'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('post', '/cloud/groups', [$groups, 'addGroup'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('get', '/cloud/groups/{groupid}', [$groups, 'getGroup'], 'provisioning_api', API::SUBADMIN_AUTH); +API::register('delete', '/cloud/groups/{groupid}', [$groups, 'deleteGroup'], 'provisioning_api', API::ADMIN_AUTH); +API::register('get', '/cloud/groups/{groupid}/subadmins', [$groups, 'getSubAdminsOfGroup'], 'provisioning_api', API::ADMIN_AUTH); // Apps -API::register('get', '/cloud/apps', array('OCA\Provisioning_API\Apps', 'getApps'), 'provisioning_api', API::ADMIN_AUTH); -API::register('get', '/cloud/apps/{appid}', array('OCA\Provisioning_API\Apps', 'getAppInfo'), 'provisioning_api', API::ADMIN_AUTH); -API::register('post', '/cloud/apps/{appid}', array('OCA\Provisioning_API\Apps', 'enable'), 'provisioning_api', API::ADMIN_AUTH); -API::register('delete', '/cloud/apps/{appid}', array('OCA\Provisioning_API\Apps', 'disable'), 'provisioning_api', API::ADMIN_AUTH); +$apps = new \OCA\Provisioning_API\Apps( + \OC::$server->getAppManager() +); +API::register('get', '/cloud/apps', [$apps, 'getApps'], 'provisioning_api', API::ADMIN_AUTH); +API::register('get', '/cloud/apps/{appid}', [$apps, 'getAppInfo'], 'provisioning_api', API::ADMIN_AUTH); +API::register('post', '/cloud/apps/{appid}', [$apps, 'enable'], 'provisioning_api', API::ADMIN_AUTH); +API::register('delete', '/cloud/apps/{appid}', [$apps, 'disable'], 'provisioning_api', API::ADMIN_AUTH); diff --git a/apps/provisioning_api/lib/apps.php b/apps/provisioning_api/lib/apps.php index 22713865c1..168f6f3cad 100644 --- a/apps/provisioning_api/lib/apps.php +++ b/apps/provisioning_api/lib/apps.php @@ -28,7 +28,14 @@ use \OC_App; class Apps { - public static function getApps($parameters){ + /** @var \OCP\App\IAppManager */ + private $appManager; + + public function __construct(\OCP\App\IAppManager $appManager) { + $this->appManager = $appManager; + } + + public function getApps($parameters){ $apps = OC_App::listAllApps(); $list = array(); foreach($apps as $app) { @@ -55,9 +62,9 @@ class Apps { } } - public static function getAppInfo($parameters){ + public function getAppInfo($parameters){ $app = $parameters['appid']; - $info = OC_App::getAppInfo($app); + $info = \OCP\App::getAppInfo($app); if(!is_null($info)) { return new OC_OCS_Result(OC_App::getAppInfo($app)); } else { @@ -65,15 +72,15 @@ class Apps { } } - public static function enable($parameters){ + public function enable($parameters){ $app = $parameters['appid']; - OC_App::enable($app); + $this->appManager->enableApp($app); return new OC_OCS_Result(null, 100); } - public static function disable($parameters){ + public function disable($parameters){ $app = $parameters['appid']; - OC_App::disable($app); + $this->appManager->disableApp($app); return new OC_OCS_Result(null, 100); } diff --git a/apps/provisioning_api/lib/groups.php b/apps/provisioning_api/lib/groups.php index 81a5a6e5c3..91d0a1c634 100644 --- a/apps/provisioning_api/lib/groups.php +++ b/apps/provisioning_api/lib/groups.php @@ -24,33 +24,65 @@ namespace OCA\Provisioning_API; use \OC_OCS_Result; -use \OC_Group; use \OC_SubAdmin; class Groups{ + /** @var \OCP\IGroupManager */ + private $groupManager; + + /** @var \OCP\IUserSession */ + private $userSession; + + /** + * @param \OCP\IGroupManager $groupManager + * @param \OCP\IUserSession $userSession + */ + public function __construct(\OCP\IGroupManager $groupManager, + \OCP\IUserSession $userSession) { + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + /** * returns a list of groups */ - public static function getGroups($parameters){ + public function getGroups($parameters){ $search = !empty($_GET['search']) ? $_GET['search'] : ''; $limit = !empty($_GET['limit']) ? $_GET['limit'] : null; $offset = !empty($_GET['offset']) ? $_GET['offset'] : null; - return new OC_OCS_Result(array('groups' => OC_Group::getGroups($search, $limit, $offset))); + + $groups = $this->groupManager->search($search, $limit, $offset); + $groups = array_map(function($group) { + return $group->getGID(); + }, $groups); + + return new OC_OCS_Result(['groups' => $groups]); } /** * returns an array of users in the group specified */ - public static function getGroup($parameters){ + public function getGroup($parameters) { + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + // Check the group exists - if(!OC_Group::groupExists($parameters['groupid'])){ + if(!$this->groupManager->groupExists($parameters['groupid'])){ return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested group could not be found'); } // Check subadmin has access to this group - if(\OC_User::isAdminUser(\OC_User::getUser()) - || in_array($parameters['groupid'], \OC_SubAdmin::getSubAdminsGroups(\OC_User::getUser()))){ - return new OC_OCS_Result(array('users' => OC_Group::usersInGroup($parameters['groupid']))); + if($this->groupManager->isAdmin($user->getUID()) + || in_array($parameters['groupid'], \OC_SubAdmin::getSubAdminsGroups($user->getUID()))){ + $users = $this->groupManager->get($parameters['groupid'])->getUsers(); + $users = array_map(function($user) { + return $user->getUID(); + }, $users); + $users = array_values($users); + return new OC_OCS_Result(['users' => $users]); } else { return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED, 'User does not have access to specified group'); } @@ -59,7 +91,7 @@ class Groups{ /** * creates a new group */ - public static function addGroup($parameters){ + public function addGroup($parameters){ // Validate name $groupid = isset($_POST['groupid']) ? $_POST['groupid'] : ''; if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $groupid ) || empty($groupid)){ @@ -67,21 +99,18 @@ class Groups{ return new OC_OCS_Result(null, 101, 'Invalid group name'); } // Check if it exists - if(OC_Group::groupExists($groupid)){ + if($this->groupManager->groupExists($groupid)){ return new OC_OCS_Result(null, 102); } - if(OC_Group::createGroup($groupid)){ - return new OC_OCS_Result(null, 100); - } else { - return new OC_OCS_Result(null, 103); - } + $this->groupManager->createGroup($groupid); + return new OC_OCS_Result(null, 100); } - public static function deleteGroup($parameters){ + public function deleteGroup($parameters){ // Check it exists - if(!OC_Group::groupExists($parameters['groupid'])){ + if(!$this->groupManager->groupExists($parameters['groupid'])){ return new OC_OCS_Result(null, 101); - } else if($parameters['groupid'] == 'admin' || !OC_Group::deleteGroup($parameters['groupid'])){ + } else if($parameters['groupid'] === 'admin' || !$this->groupManager->get($parameters['groupid'])->delete()){ // Cannot delete admin group return new OC_OCS_Result(null, 102); } else { @@ -89,10 +118,10 @@ class Groups{ } } - public static function getSubAdminsOfGroup($parameters) { + public function getSubAdminsOfGroup($parameters) { $group = $parameters['groupid']; // Check group exists - if(!OC_Group::groupExists($group)) { + if(!$this->groupManager->groupExists($group)) { return new OC_OCS_Result(null, 101, 'Group does not exist'); } // Go diff --git a/apps/provisioning_api/lib/users.php b/apps/provisioning_api/lib/users.php index fada85b293..f5b201a55e 100644 --- a/apps/provisioning_api/lib/users.php +++ b/apps/provisioning_api/lib/users.php @@ -27,32 +27,64 @@ namespace OCA\Provisioning_API; use \OC_OCS_Result; use \OC_SubAdmin; -use \OC_User; -use \OC_Group; use \OC_Helper; use OCP\Files\NotFoundException; class Users { + /** @var \OCP\IUserManager */ + private $userManager; + + /** @var \OCP\IConfig */ + private $config; + + /** @var \OCP\IGroupManager */ + private $groupManager; + + /** @var \OCP\IUserSession */ + private $userSession; + + /** + * @param \OCP\IUserManager $userManager + * @param \OCP\IConfig $config + * @param \OCP\IGroupManager $groupManager + * @param \OCP\IUserSession $user + */ + public function __construct(\OCP\IUserManager $userManager, + \OCP\IConfig $config, + \OCP\IGroupManager $groupManager, + \OCP\IUserSession $userSession) { + $this->userManager = $userManager; + $this->config = $config; + $this->groupManager = $groupManager; + $this->userSession = $userSession; + } + /** * returns a list of users */ - public static function getUsers(){ + public function getUsers(){ $search = !empty($_GET['search']) ? $_GET['search'] : ''; $limit = !empty($_GET['limit']) ? $_GET['limit'] : null; $offset = !empty($_GET['offset']) ? $_GET['offset'] : null; - return new OC_OCS_Result(array('users' => OC_User::getUsers($search, $limit, $offset))); + + $users = $this->userManager->search($search, $limit, $offset); + $users = array_keys($users); + + return new OC_OCS_Result([ + 'users' => $users + ]); } - public static function addUser(){ + public function addUser(){ $userId = isset($_POST['userid']) ? $_POST['userid'] : null; $password = isset($_POST['password']) ? $_POST['password'] : null; - if(OC_User::userExists($userId)) { + if($this->userManager->userExists($userId)) { \OCP\Util::writeLog('ocs_api', 'Failed addUser attempt: User already exists.', \OCP\Util::ERROR); return new OC_OCS_Result(null, 102, 'User already exists'); } else { try { - OC_User::createUser($userId, $password); + $this->userManager->createUser($userId, $password); \OCP\Util::writeLog('ocs_api', 'Successful addUser call with userid: '.$_POST['userid'], \OCP\Util::INFO); return new OC_OCS_Result(null, 100); } catch (\Exception $e) { @@ -65,25 +97,32 @@ class Users { /** * gets user info */ - public static function getUser($parameters){ + public function getUser($parameters){ $userId = $parameters['userid']; + + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + // Admin? Or SubAdmin? - if(OC_User::isAdminUser(OC_User::getUser()) || OC_SubAdmin::isUserAccessible(OC_User::getUser(), $userId)) { + if($this->groupManager->isAdmin($user->getUID()) || OC_SubAdmin::isUserAccessible($user->getUID(), $userId)) { // Check they exist - if(!OC_User::userExists($userId)) { + if(!$this->userManager->userExists($userId)) { return new OC_OCS_Result(null, \OCP\API::RESPOND_NOT_FOUND, 'The requested user could not be found'); } // Show all - $return = array( + $return = [ 'email', 'enabled', - ); - if(OC_User::getUser() != $userId) { + ]; + if($user->getUID() !== $userId) { $return[] = 'quota'; } } else { // Check they are looking up themselves - if(OC_User::getUser() != $userId) { + if($user->getUID() !== $userId) { return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); } // Return some additional information compared to the core route @@ -93,14 +132,12 @@ class Users { ); } - $config = \OC::$server->getConfig(); - // Find the data $data = []; $data = self::fillStorageInfo($userId, $data); - $data['enabled'] = $config->getUserValue($userId, 'core', 'enabled', 'true'); - $data['email'] = $config->getUserValue($userId, 'settings', 'email'); - $data['displayname'] = OC_User::getDisplayName($parameters['userid']); + $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true'); + $data['email'] = $this->config->getUserValue($userId, 'settings', 'email'); + $data['displayname'] = $this->userManager->get($parameters['userid'])->getDisplayName(); // Return the appropriate data $responseData = array(); @@ -114,21 +151,28 @@ class Users { /** * edit users */ - public static function editUser($parameters){ + public function editUser($parameters){ $userId = $parameters['userid']; - if($userId === OC_User::getUser()) { + + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + + if($userId === $user->getUID()) { // Editing self (display, email) $permittedFields[] = 'display'; $permittedFields[] = 'email'; $permittedFields[] = 'password'; // If admin they can edit their own quota - if(OC_User::isAdminUser(OC_User::getUser())) { + if($this->groupManager->isAdmin($user->getUID())) { $permittedFields[] = 'quota'; } } else { // Check if admin / subadmin - if(OC_SubAdmin::isUserAccessible(OC_User::getUser(), $userId) - || OC_User::isAdminUser(OC_User::getUser())) { + if(OC_SubAdmin::isUserAccessible($user->getUID(), $userId) + || $this->groupManager->isAdmin($user->getUID())) { // They have permissions over the user $permittedFields[] = 'display'; $permittedFields[] = 'quota'; @@ -146,7 +190,7 @@ class Users { // Process the edit switch($parameters['_put']['key']){ case 'display': - OC_User::setDisplayName($userId, $parameters['_put']['value']); + $this->userManager->get($userId)->setDisplayName($parameters['_put']['value']); break; case 'quota': $quota = $parameters['_put']['value']; @@ -154,27 +198,27 @@ class Users { if (is_numeric($quota)) { $quota = floatval($quota); } else { - $quota = OC_Helper::computerFileSize($quota); + $quota = \OCP\Util::computerFileSize($quota); } if ($quota === false) { return new OC_OCS_Result(null, 103, "Invalid quota value {$parameters['_put']['value']}"); } - if($quota == 0) { + if($quota === 0) { $quota = 'default'; - }else if($quota == -1){ + }else if($quota === -1){ $quota = 'none'; } else { - $quota = OC_Helper::humanFileSize($quota); + $quota = \OCP\Util::humanFileSize($quota); } } - \OC::$server->getConfig()->setUserValue($userId, 'files', 'quota', $quota); + $this->config->setUserValue($userId, 'files', 'quota', $quota); break; case 'password': - OC_User::setPassword($userId, $parameters['_put']['value']); + $this->userManager->get($userId)->setPassword($parameters['_put']['value']); break; case 'email': if(filter_var($parameters['_put']['value'], FILTER_VALIDATE_EMAIL)) { - \OC::$server->getConfig()->setUserValue($userId, 'settings', 'email', $parameters['_put']['value']); + $this->config->setUserValue($userId, 'settings', 'email', $parameters['_put']['value']); } else { return new OC_OCS_Result(null, 102); } @@ -186,32 +230,53 @@ class Users { return new OC_OCS_Result(null, 100); } - public static function deleteUser($parameters){ - if(!OC_User::userExists($parameters['userid']) - || $parameters['userid'] === OC_User::getUser()) { + public function deleteUser($parameters){ + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + + if(!$this->userManager->userExists($parameters['userid']) + || $parameters['userid'] === $user->getUID()) { return new OC_OCS_Result(null, 101); } // If not permitted - if(!OC_User::isAdminUser(OC_User::getUser()) && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $parameters['userid'])) { + if(!$this->groupManager->isAdmin($user->getUID()) && !OC_SubAdmin::isUserAccessible($user->getUID(), $parameters['userid'])) { return new OC_OCS_Result(null, 997); } // Go ahead with the delete - if(OC_User::deleteUser($parameters['userid'])) { + if($this->userManager->get($parameters['userid'])->delete()) { return new OC_OCS_Result(null, 100); } else { return new OC_OCS_Result(null, 101); } } - public static function getUsersGroups($parameters){ - if($parameters['userid'] === OC_User::getUser() || OC_User::isAdminUser(OC_User::getUser())) { + public function getUsersGroups($parameters) { + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + + if($parameters['userid'] === $user->getUID() || $this->groupManager->isAdmin($user->getUID())) { // Self lookup or admin lookup - return new OC_OCS_Result(array('groups' => OC_Group::getUserGroups($parameters['userid']))); + return new OC_OCS_Result([ + 'groups' => $this->groupManager->getUserGroupIds( + $this->userManager->get($parameters['userid']) + ) + ]); } else { // Looking up someone else - if(OC_SubAdmin::isUserAccessible(OC_User::getUser(), $parameters['userid'])) { + if(OC_SubAdmin::isUserAccessible($user->getUID(), $parameters['userid'])) { // Return the group that the method caller is subadmin of for the user in question - $groups = array_intersect(OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()), OC_Group::getUserGroups($parameters['userid'])); + $groups = array_intersect( + OC_SubAdmin::getSubAdminsGroups($user->getUID()), + $this->groupManager->getUserGroupIds( + $this->userManager->get($parameters['userid']) + ) + ); return new OC_OCS_Result(array('groups' => $groups)); } else { // Not permitted @@ -221,78 +286,96 @@ class Users { } - public static function addToGroup($parameters){ + public function addToGroup($parameters){ + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + $group = !empty($_POST['groupid']) ? $_POST['groupid'] : null; if(is_null($group)){ return new OC_OCS_Result(null, 101); } // Check they're an admin - if(!OC_Group::inGroup(OC_User::getUser(), 'admin')){ + if(!$this->groupManager->isInGroup($user->getUID(), 'admin')){ // This user doesn't have rights to add a user to this group return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); } // Check if the group exists - if(!OC_Group::groupExists($group)){ + if(!$this->groupManager->groupExists($group)){ return new OC_OCS_Result(null, 102); } // Check if the user exists - if(!OC_User::userExists($parameters['userid'])){ + if(!$this->userManager->userExists($parameters['userid'])){ return new OC_OCS_Result(null, 103); } // Add user to group - return OC_Group::addToGroup($parameters['userid'], $group) ? new OC_OCS_Result(null, 100) : new OC_OCS_Result(null, 105); + $this->groupManager->get($group)->addUser( + $this->userManager->get($parameters['userid']) + ); + return new OC_OCS_Result(null, 100); } - public static function removeFromGroup($parameters){ + public function removeFromGroup($parameters) { + // Check if user is logged in + $user = $this->userSession->getUser(); + if ($user === null) { + return new OC_OCS_Result(null, \OCP\API::RESPOND_UNAUTHORISED); + } + $group = !empty($parameters['_delete']['groupid']) ? $parameters['_delete']['groupid'] : null; if(is_null($group)){ return new OC_OCS_Result(null, 101); } // If they're not an admin, check they are a subadmin of the group in question - if(!OC_Group::inGroup(OC_User::getUser(), 'admin') && !OC_SubAdmin::isSubAdminofGroup(OC_User::getUser(), $group)){ + if(!$this->groupManager->isInGroup($user->getUID(), 'admin') && !OC_SubAdmin::isSubAdminofGroup($user->getUID(), $group)){ return new OC_OCS_Result(null, 104); } // Check they aren't removing themselves from 'admin' or their 'subadmin; group - if($parameters['userid'] === OC_User::getUser()){ - if(OC_Group::inGroup(OC_User::getUser(), 'admin')){ + if($parameters['userid'] === $user->getUID()){ + if($this->groupManager->isInGroup($user->getUID(), 'admin')){ if($group === 'admin'){ return new OC_OCS_Result(null, 105, 'Cannot remove yourself from the admin group'); } } else { // Not an admin, check they are not removing themself from their subadmin group - if(in_array($group, OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()))){ + if(in_array($group, OC_SubAdmin::getSubAdminsGroups($user->getUID()))){ return new OC_OCS_Result(null, 105, 'Cannot remove yourself from this group as you are a SubAdmin'); } } } // Check if the group exists - if(!OC_Group::groupExists($group)){ + if(!$this->groupManager->groupExists($group)){ return new OC_OCS_Result(null, 102); } // Check if the user exists - if(!OC_User::userExists($parameters['userid'])){ + if(!$this->userManager->userExists($parameters['userid'])){ return new OC_OCS_Result(null, 103); } // Remove user from group - return OC_Group::removeFromGroup($parameters['userid'], $group) ? new OC_OCS_Result(null, 100) : new OC_OCS_Result(null, 105); + $this->groupManager->get($group)->removeUser( + $this->userManager->get($parameters['userid']) + ); + return new OC_OCS_Result(null, 100); } /** * Creates a subadmin */ - public static function addSubAdmin($parameters) { + public function addSubAdmin($parameters) { $group = $_POST['groupid']; $user = $parameters['userid']; // Check if the user exists - if(!OC_User::userExists($user)) { + if(!$this->userManager->userExists($user)) { return new OC_OCS_Result(null, 101, 'User does not exist'); } // Check if group exists - if(!OC_Group::groupExists($group)) { + if(!$this->groupManager->groupExists($group)) { return new OC_OCS_Result(null, 102, 'Group:'.$group.' does not exist'); } // Check if trying to make subadmin of admin group - if(strtolower($group) == 'admin') { + if(strtolower($group) === 'admin') { return new OC_OCS_Result(null, 103, 'Cannot create subadmins for admin group'); } // We cannot be subadmin twice @@ -311,11 +394,11 @@ class Users { /** * Removes a subadmin from a group */ - public static function removeSubAdmin($parameters) { + public function removeSubAdmin($parameters) { $group = $parameters['_delete']['groupid']; $user = $parameters['userid']; // Check if the user exists - if(!OC_User::userExists($user)) { + if(!$this->userManager->userExists($user)) { return new OC_OCS_Result(null, 101, 'User does not exist'); } // Check if they are a subadmin of this said group @@ -333,10 +416,10 @@ class Users { /** * @Get the groups a user is a subadmin of */ - public static function getUserSubAdminGroups($parameters) { + public function getUserSubAdminGroups($parameters) { $user = $parameters['userid']; // Check if the user exists - if(!OC_User::userExists($user)) { + if(!$this->userManager->userExists($user)) { return new OC_OCS_Result(null, 101, 'User does not exist'); } // Get the subadmin groups diff --git a/apps/provisioning_api/tests/appstest.php b/apps/provisioning_api/tests/appstest.php index c4298f017f..f2a3977eac 100644 --- a/apps/provisioning_api/tests/appstest.php +++ b/apps/provisioning_api/tests/appstest.php @@ -25,8 +25,17 @@ namespace OCA\Provisioning_API\Tests; class AppsTest extends TestCase { + + public function setup() { + parent::setup(); + $this->appManager = \OC::$server->getAppManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->api = new \OCA\Provisioning_API\Apps($this->appManager); + } + public function testGetAppInfo() { - $result = \OCA\provisioning_API\Apps::getAppInfo(array('appid' => 'provisioning_api')); + $result = $this->api->getAppInfo(['appid' => 'provisioning_api']); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); @@ -34,7 +43,7 @@ class AppsTest extends TestCase { public function testGetAppInfoOnBadAppID() { - $result = \OCA\provisioning_API\Apps::getAppInfo(array('appid' => 'not_provisioning_api')); + $result = $this->api->getAppInfo(['appid' => 'not_provisioning_api']); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode()); @@ -44,10 +53,10 @@ class AppsTest extends TestCase { public function testGetApps() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); - $result = \OCA\provisioning_API\Apps::getApps(array()); + $result = $this->api->getApps([]); $this->assertTrue($result->succeeded()); $data = $result->getData(); @@ -58,7 +67,7 @@ class AppsTest extends TestCase { public function testGetAppsEnabled() { $_GET['filter'] = 'enabled'; - $result = \OCA\provisioning_API\Apps::getApps(array('filter' => 'enabled')); + $result = $this->api->getApps(['filter' => 'enabled']); $this->assertTrue($result->succeeded()); $data = $result->getData(); $this->assertEquals(count(\OC_App::getEnabledApps()), count($data['apps'])); @@ -68,7 +77,7 @@ class AppsTest extends TestCase { public function testGetAppsDisabled() { $_GET['filter'] = 'disabled'; - $result = \OCA\provisioning_API\Apps::getApps(array('filter' => 'disabled')); + $result = $this->api->getApps(['filter' => 'disabled']); $this->assertTrue($result->succeeded()); $data = $result->getData(); $apps = \OC_App::listAllApps(); @@ -78,6 +87,12 @@ class AppsTest extends TestCase { } $disabled = array_diff($list, \OC_App::getEnabledApps()); $this->assertEquals(count($disabled), count($data['apps'])); + } + public function testGetAppsInvalidFilter() { + $_GET['filter'] = 'foo'; + $result = $this->api->getApps([]); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); } } diff --git a/apps/provisioning_api/tests/groupstest.php b/apps/provisioning_api/tests/groupstest.php index b8b0279069..73044e3312 100644 --- a/apps/provisioning_api/tests/groupstest.php +++ b/apps/provisioning_api/tests/groupstest.php @@ -24,18 +24,79 @@ namespace OCA\Provisioning_API\Tests; +use OCP\IUserManager; +use OCP\IGroupManager; +use OCP\IUserSession; + class GroupsTest extends TestCase { + + /** @var IUserManager */ + protected $userManager; + + /** @var IGroupManager */ + protected $groupManager; + + /** @var IUserSession */ + protected $userSession; + + protected function setup() { + parent::setup(); + + $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->api = new \OCA\Provisioning_API\Groups( + $this->groupManager, + $this->userSession + ); + } + + public function testGetGroups() { + $groups = []; + $id = $this->getUniqueID(); + + for ($i=0; $i < 10; $i++) { + $groups[] = $this->groupManager->createGroup($id . '_' . $i); + } + + $_GET = []; + $result = $this->api->getGroups([]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + $this->assertCount(count($this->groupManager->search('')), $result->getData()['groups']); + $this->assertContains('admin', $result->getData()['groups']); + foreach ($groups as $group) { + $this->assertContains($group->getGID(), $result->getData()['groups']); + } + + $_GET = [ + 'search' => $id, + 'limit' => 5, + 'offset' => 2 + ]; + $result = $this->api->getGroups([]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + $this->assertCount(5, $result->getData()['groups']); + foreach (array_splice($groups, 2, 5) as $group) { + $this->assertContains($group->getGID(), $result->getData()['groups']); + } + + foreach ($groups as $group) { + $group->delete(); + } + } + public function testGetGroupAsUser() { $users = $this->generateUsers(2); - self::loginAsUser($users[0]); + $this->userSession->setUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($users[1], $group); + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($users[1]); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, + $result = $this->api->getGroup(array( + 'groupid' => $group->getGID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -47,18 +108,17 @@ class GroupsTest extends TestCase { public function testGetGroupAsSubadmin() { $users = $this->generateUsers(2); - self::loginAsUser($users[0]); + $this->userSession->setUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($users[0], $group); - \OC_Group::addToGroup($users[1], $group); + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($users[0]); + $group->addUser($users[1]); - \OC_SubAdmin::createSubAdmin($users[0], $group); + \OC_SubAdmin::createSubAdmin($users[0]->getUID(), $group->getGID()); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, - )); + $result = $this->api->getGroup([ + 'groupid' => $group->getGID(), + ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); @@ -67,6 +127,10 @@ class GroupsTest extends TestCase { $resultData = $result->getData(); $resultData = $resultData['users']; + $users = array_map(function($user) { + return $user->getUID(); + }, $users); + sort($users); sort($resultData); $this->assertEquals($users, $resultData); @@ -76,20 +140,18 @@ class GroupsTest extends TestCase { public function testGetGroupAsIrrelevantSubadmin() { $users = $this->generateUsers(2); - self::loginAsUser($users[0]); + $this->userSession->setUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($users[1], $group); - \OC_Group::addToGroup($users[0], $group2); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); + $group2 = $this->groupManager->createGroup($this->getUniqueID()); + $group1->addUser($users[1]); + $group2->addUser($users[0]); - \OC_SubAdmin::createSubAdmin($users[0], $group2); + \OC_SubAdmin::createSubAdmin($users[0]->getUID(), $group2->getGID()); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, - )); + $result = $this->api->getGroup([ + 'groupid' => $group1->getGID(), + ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); @@ -100,49 +162,129 @@ class GroupsTest extends TestCase { public function testGetGroupAsAdmin() { $users = $this->generateUsers(2); - self::loginAsUser($users[0]); + $this->userSession->setUser($users[0]); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); + $group = $this->groupManager->createGroup($this->getUniqueID()); - \OC_Group::addToGroup($users[1], $group); - \OC_Group::addToGroup($users[0], 'admin'); + $group->addUser($users[1]); + $this->groupManager->get('admin')->addUser($users[0]); - $result = \OCA\provisioning_api\Groups::getGroup(array( - 'groupid' => $group, - )); + $result = $this->api->getGroup([ + 'groupid' => $group->getGID(), + ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertEquals(array('users' => array($users[1])), $result->getData()); + $this->assertEquals(['users' => [$users[1]->getUID()]], $result->getData()); } + public function testGetGroupNonExisting() { + $result = $this->api->getGroup([ + 'groupid' => $this->getUniqueId() + ]); + + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode()); + $this->assertEquals('The requested group could not be found', $result->getMeta()['message']); + } + public function testGetSubAdminsOfGroup() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_SubAdmin::createSubAdmin($user2, $group1); - $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array( - 'groupid' => $group1, - )); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); + \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID()); + $result = $this->api->getSubAdminsOfGroup([ + 'groupid' => $group1->getGID(), + ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($user2, reset($data)); - \OC_Group::deleteGroup($group1); + $this->assertEquals($user2->getUID(), reset($data)); + $group1->delete(); $user1 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $result = \OCA\provisioning_api\Groups::getSubAdminsOfGroup(array( + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $result = $this->api->getSubAdminsOfGroup([ 'groupid' => $this->getUniqueID(), - )); + ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(101, $result->getStatusCode()); } + + public function testAddGroupEmptyGroup() { + $_POST = []; + $result = $this->api->addGroup([]); + + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + $this->assertEquals('Invalid group name', $result->getMeta()['message']); + } + + public function testAddGroupExistingGroup() { + $group = $this->groupManager->createGroup($this->getUniqueID()); + + $_POST = [ + 'groupid' => $group->getGID() + ]; + $result = $this->api->addGroup([]); + + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); + + $group->delete(); + } + + public function testAddGroup() { + $group = $this->getUniqueId(); + + $_POST = [ + 'groupid' => $group + ]; + + $result = $this->api->addGroup([]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + $this->assertTrue($this->groupManager->groupExists($group)); + + $this->groupManager->get($group)->delete(); + } + + public function testDeleteGroupNonExisting() { + $group = $this->getUniqueId(); + + $result = $this->api->deleteGroup([ + 'groupid' => $group + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + } + + public function testDeleteAdminGroup() { + $result = $this->api->deleteGroup([ + 'groupid' => 'admin' + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); + } + + public function testDeleteGroup() { + $group = $this->groupManager->createGroup($this->getUniqueId()); + + $result = $this->api->deleteGroup([ + 'groupid' => $group->getGID() + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + $this->assertFalse($this->groupManager->groupExists($group->getGID())); + } } diff --git a/apps/provisioning_api/tests/testcase.php b/apps/provisioning_api/tests/testcase.php index 3d0468daa1..ee7eb2a5a9 100644 --- a/apps/provisioning_api/tests/testcase.php +++ b/apps/provisioning_api/tests/testcase.php @@ -22,12 +22,24 @@ namespace OCA\Provisioning_API\Tests; +use OCP\IUserManager; +use OCP\IGroupManager; + abstract class TestCase extends \Test\TestCase { protected $users = array(); + /** @var IUserManager */ + protected $userManager; + + /** @var IGroupManager */ + protected $groupManager; + protected function setUp() { parent::setUp(); - \OC_Group::createGroup('admin'); + + $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->groupManager->createGroup('admin'); } /** @@ -38,8 +50,7 @@ abstract class TestCase extends \Test\TestCase { protected function generateUsers($num = 1) { $users = array(); for ($i = 0; $i < $num; $i++) { - $user = $this->getUniqueID(); - \OC_User::createUser($user, 'password'); + $user = $this->userManager->createUser($this->getUniqueID(), 'password'); $this->users[] = $user; $users[] = $user; } @@ -48,11 +59,10 @@ abstract class TestCase extends \Test\TestCase { protected function tearDown() { foreach($this->users as $user) { - \OC_User::deleteUser($user); + $user->delete(); } - \OC_Group::deleteGroup('admin'); - + $this->groupManager->get('admin')->delete(); parent::tearDown(); } } diff --git a/apps/provisioning_api/tests/userstest.php b/apps/provisioning_api/tests/userstest.php index f286256503..350586f833 100644 --- a/apps/provisioning_api/tests/userstest.php +++ b/apps/provisioning_api/tests/userstest.php @@ -26,34 +26,67 @@ namespace OCA\Provisioning_API\Tests; +use OCP\IUserManager; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUserSession; + class UsersTest extends TestCase { + + /** @var IUserManager */ + protected $userManager; + + /** @var IConfig */ + protected $config; + + /** @var IGroupManager */ + protected $groupManager; + + /** @var IUserSession */ + protected $userSession; + protected function resetParams() { $_GET = null; $_POST = null; } + protected function setup() { + parent::setup(); + + $this->userManager = \OC::$server->getUserManager(); + $this->config = \OC::$server->getConfig(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->api = new \OCA\Provisioning_Api\Users( + $this->userManager, + $this->config, + $this->groupManager, + $this->userSession + ); + } + // Test getting the list of users public function testGetUsers() { - $result = \OCA\provisioning_API\Users::getUsers(array()); + $result = $this->api->getUsers(); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $count = $result->getData(); $count = count($count['users']); - $this->assertEquals(count(\OC_User::getUsers()), $count); + $this->assertEquals(count($this->userManager->search('', null, null)), $count); $user = $this->generateUsers(); - $_GET['search'] = $user; - $result = \OCA\provisioning_API\Users::getUsers(array()); + $_GET['search'] = $user->getUID(); + $result = $this->api->getUsers(); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($user, reset($data['users'])); + $this->assertEquals($user->getUID(), reset($data['users'])); // Add several users $this->generateUsers(10); $this->resetParams(); $_GET['limit'] = 2; - $result = \OCA\provisioning_API\Users::getUsers(array()); + $result = $this->api->getUsers(); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $count = $result->getData(); @@ -63,45 +96,93 @@ class UsersTest extends TestCase { $this->resetParams(); $_GET['limit'] = 1; $_GET['offset'] = 1; - $result = \OCA\provisioning_API\Users::getUsers(array()); + $result = $this->api->getUsers(array()); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals(\OC_User::getUsers('', 1, 1), $data['users']); + $this->assertEquals(array_keys($this->userManager->search('', 1, 1)), $data['users']); } public function testAddUser() { $this->resetParams(); $_POST['userid'] = $this->getUniqueID(); $_POST['password'] = 'password'; - $result = \OCA\provisioning_API\Users::addUser(array()); + $result = $this->api->addUser(); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertTrue(\OC_User::userExists($_POST['userid'])); - $this->assertEquals($_POST['userid'], \OC_User::checkPassword($_POST['userid'], $_POST['password'])); - $this->users[] = $_POST['userid']; + $this->assertTrue($this->userManager->userExists($_POST['userid'])); + $this->assertEquals($_POST['userid'], $this->userManager->checkPassword($_POST['userid'], $_POST['password'])->getUID()); + $this->users[] = $this->userManager->get($_POST['userid']); + } + + public function testAddUserTwice() { + $this->resetParams(); + $_POST['userid'] = $this->getUniqueID(); + $_POST['password'] = 'password'; + $this->api->addUser(); + $result = $this->api->addUser(); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); + $this->assertEquals('User already exists', $result->getMeta()['message']); + } + + public function testAddUserFails() { + $uid = $this->getUniqueID(); + + $userManager = $this->getMockBuilder('\OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + + $userManager->expects($this->once()) + ->method('userExists') + ->with($uid) + ->willReturn(false); + $userManager->expects($this->once()) + ->method('createUser') + ->with($uid, 'password') + ->will($this->throwException(new \Exception)); + + $api = new \OCA\Provisioning_Api\Users( + $userManager, + $this->config, + $this->groupManager, + $this->userSession + ); + + $this->resetParams(); + $_POST['userid'] = $uid; + $_POST['password'] = 'password'; + $result = $api->addUser(); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + $this->assertEquals('Bad request', $result->getMeta()['message']); } public function testGetUserOnSelf() { $user = $this->generateUsers(); - self::loginAsUser($user); - $params['userid'] = $user; - $result = \OCA\provisioning_API\Users::getUser($params); + $user->setDisplayName('foobar'); + $this->userSession->setUser($user); + $params['userid'] = $user->getUID(); + $result = $this->api->getUser($params); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); + + $this->assertEquals('foobar', $data['displayname']); } public function testGetUserOnNonExistingUser() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); $params = array(); $params['userid'] = $this->getUniqueID(); - while(\OC_User::userExists($params['userid'])) { + while($this->userManager->userExists($params['userid'])) { $params['userid'] = $this->getUniqueID(); } - $result = \OCA\provisioning_API\Users::getUser($params); + $result = $this->api->getUser($params); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(\OCP\API::RESPOND_NOT_FOUND, $result->getStatusCode()); @@ -111,33 +192,32 @@ class UsersTest extends TestCase { public function testGetUserOnOtherUser() { $users = $this->generateUsers(2); $params['userid'] = $users[0]; - self::loginAsUser($users[1]); - $result = \OCA\provisioning_API\Users::getUser($params); + $this->userSession->setUser($users[1]); + $result = $this->api->getUser($params); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); // Now as as admin $users = $this->generateUsers(2); - $params['userid'] = $users[0]; + $params['userid'] = $users[0]->getUID(); // login to generate home - self::loginAsUser($users[0]); - \OC_Group::addToGroup($users[1], 'admin'); - self::loginAsUser($users[1]); - $result = \OCA\provisioning_API\Users::getUser($params); + $this->userSession->setUser($users[0]); + $this->groupManager->get('admin')->addUser($users[1]); + $this->userSession->setUser($users[1]); + $result = $this->api->getUser($params); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals(\OC::$server->getConfig()->getUserValue($users[0], 'core', 'enabled', 'true'), $data['enabled']); + $this->assertEquals(\OC::$server->getConfig()->getUserValue($users[0]->getUID(), 'core', 'enabled', 'true'), $data['enabled']); } public function testEditOwnDisplayName() { - // Test editing own name $user = $this->generateUsers(); - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::editUser( + $this->userSession->setUser($user); + $result = $this->api->editUser( array( - 'userid' => $user, + 'userid' => $user->getUID(), '_put' => array( 'key' => 'display', 'value' => 'newname', @@ -146,41 +226,39 @@ class UsersTest extends TestCase { ); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertEquals('newname', \OC_User::getDisplayName($user)); + $this->assertEquals('newname', $user->getDisplayName()); } public function testAdminEditDisplayNameOfUser() { - // Test admin editing users name $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::editUser( - array( - 'userid' => $user2, - '_put' => array( + $result = $this->api->editUser( + [ + 'userid' => $user2->getUID(), + '_put' => [ 'key' => 'display', 'value' => 'newname', - ), - ) + ], + ] ); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertEquals('newname', \OC_User::getDisplayName($user2)); + $this->assertEquals('newname', $user2->getDisplayName()); } public function testUserEditOtherUserDisplayName() { - // Test editing other users name $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::editUser( + $result = $this->api->editUser( array( - 'userid' => $user2, + 'userid' => $user2->getUID(), '_put' => array( 'key' => 'display', 'value' => 'newname', @@ -199,11 +277,33 @@ class UsersTest extends TestCase { */ public function testEditOwnQuota($expected, $quota) { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::editUser( + $this->userSession->setUser($user); + $result = $this->api->editUser( [ - 'userid' => $user, + 'userid' => $user->getUID(), + '_put' => [ + 'key' => 'quota', + 'value' => $quota, + ], + ] + ); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(997, $result->getStatusCode()); + } + + /** + * @dataProvider providesQuotas + * @param $expected + * @param $quota + */ + public function testEditOwnQuotaAsAdmin($expected, $quota) { + $user = $this->generateUsers(); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + $result = $this->api->editUser( + [ + 'userid' => $user->getUID(), '_put' => [ 'key' => 'quota', 'value' => $quota, @@ -221,16 +321,18 @@ class UsersTest extends TestCase { [true, 'none'], [true, 'default'], [false, 'qwertzu'], + [true, 0], + [true, -1] ]; } public function testAdminEditOwnQuota() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::editUser( + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + $result = $this->api->editUser( array( - 'userid' => $user, + 'userid' => $user->getUID(), '_put' => array( 'key' => 'quota', 'value' => '20G', @@ -243,12 +345,12 @@ class UsersTest extends TestCase { public function testAdminEditOtherUserQuota() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::editUser( + $result = $this->api->editUser( array( - 'userid' => $user2, + 'userid' => $user2->getUID(), '_put' => array( 'key' => 'quota', 'value' => '20G', @@ -261,11 +363,11 @@ class UsersTest extends TestCase { public function testUserEditOtherUserQuota() { $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::editUser( + $result = $this->api->editUser( array( - 'userid' => $user2, + 'userid' => $user2->getUID(), '_put' => array( 'key' => 'quota', 'value' => '20G', @@ -279,10 +381,10 @@ class UsersTest extends TestCase { public function testUserEditOwnEmail() { $user = $this->generateUsers(); $email = 'test@example.com'; - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::editUser( + $this->userSession->setUser($user); + $result = $this->api->editUser( array( - 'userid' => $user, + 'userid' => $user->getUID(), '_put' => array( 'key' => 'email', 'value' => $email, @@ -291,16 +393,32 @@ class UsersTest extends TestCase { ); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($user, 'settings', 'email', null)); + $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($user->getUID(), 'settings', 'email', null)); + } + + public function testUserEditOwnEmailInvalid() { + $user = $this->generateUsers(); + $email = 'test@example'; + $this->userSession->setUser($user); + $result = $this->api->editUser([ + 'userid' => $user->getUID(), + '_put' => [ + 'key' => 'email', + 'value' => $email, + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); } public function testUserEditOtherUserEmailAsUser() { $users = $this->generateUsers(2); $email = 'test@example.com'; - self::loginAsUser($users[0]); - $result = \OCA\provisioning_API\Users::editUser( + $this->userSession->setUser($users[0]); + $result = $this->api->editUser( array( - 'userid' => $users[1], + 'userid' => $users[1]->getUID(), '_put' => array( 'key' => 'email', 'value' => $email, @@ -314,11 +432,11 @@ class UsersTest extends TestCase { public function testUserEditOtherUserEmailAsAdmin() { $users = $this->generateUsers(2); $email = 'test@example.com'; - self::loginAsUser($users[0]); - \OC_Group::addToGroup($users[0], 'admin'); - $result = \OCA\provisioning_API\Users::editUser( + $this->userSession->setUser($users[0]); + $this->groupManager->get('admin')->addUser($users[0]); + $result = $this->api->editUser( array( - 'userid' => $users[1], + 'userid' => $users[1]->getUID(), '_put' => array( 'key' => 'email', 'value' => $email, @@ -327,14 +445,60 @@ class UsersTest extends TestCase { ); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($users[1], 'settings', 'email', null)); + $this->assertEquals($email, \OC::$server->getConfig()->getUserValue($users[1]->getUID(), 'settings', 'email', null)); + } + + public function testUserEditOwnPassword() { + $user = $this->generateUsers(); + $password = 'foo'; + $this->userSession->setUser($user); + $result = $this->api->editUser([ + 'userid' => $user->getUID(), + '_put' => [ + 'key' => 'password', + 'value' => $password, + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + } + + public function testUserEditOtherUserPasswordAsUser() { + $users = $this->generateUsers(2); + $password = 'foo'; + $this->userSession->setUser($users[0]); + $result = $this->api->editUser([ + 'userid' => $users[1]->getUID(), + '_put' => [ + 'key' => 'password', + 'value' => $password, + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + } + + public function testUserEditOtherUserPasswordAsAdmin() { + $users = $this->generateUsers(2); + $password = 'foo'; + $this->userSession->setUser($users[0]); + $this->groupManager->get('admin')->addUser($users[0]); + $result = $this->api->editUser([ + 'userid' => $users[1]->getUID(), + '_put' => [ + 'key' => 'password', + 'value' => $password, + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); } public function testDeleteSelf() { $user = $this->generateUsers(); - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user, + $this->userSession->setUser($user); + $result = $this->api->deleteUser(array( + 'userid' => $user->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); @@ -342,10 +506,10 @@ class UsersTest extends TestCase { public function testDeleteOtherAsUser() { $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user2, + $result = $this->api->deleteUser(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); @@ -353,48 +517,45 @@ class UsersTest extends TestCase { public function testDeleteOtherAsSubAdmin() { $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($user, $group); - \OC_Group::addToGroup($user2, $group); - \OC_SubAdmin::createSubAdmin($user, $group); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user2, + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($user); + $group->addUser($user2); + \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID()); + $result = $this->api->deleteUser(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - \OC_Group::deleteGroup($group); + $group->delete(); } public function testDeleteOtherAsIrelevantSubAdmin() { $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $group = $this->getUniqueID(); - $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($user, $group); - \OC_Group::addToGroup($user2, $group2); - \OC_SubAdmin::createSubAdmin($user, $group); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user2, - )); + $group = $this->groupManager->createGroup($this->getUniqueID()); + $group2 = $this->groupManager->createGroup($this->getUniqueID()); + $group->addUser($user); + $group2->addUser($user2); + \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID()); + $result = $this->api->deleteUser(array( + 'userid' => $user2->getUID(), + )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - \OC_Group::deleteGroup($group); - \OC_Group::deleteGroup($group2); + $group->delete(); + $group2->delete(); } public function testDeleteOtherAsAdmin() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); $user2 = $this->generateUsers(); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user2, + $result = $this->api->deleteUser(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); @@ -402,288 +563,485 @@ class UsersTest extends TestCase { public function testDeleteSelfAsAdmin() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); - self::loginAsUser($user); - $result = \OCA\provisioning_API\Users::deleteUser(array( - 'userid' => $user, + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + $result = $this->api->deleteUser(array( + 'userid' => $user->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); } + public function testDeleteFails() { + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $user->expects($this->once()) + ->method('delete') + ->willReturn(false); + + $user2 = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $user2->expects($this->any()) + ->method('getUID') + ->willReturn('user2'); + + $userManager = $this->getMockBuilder('\OCP\IUserManager') + ->disableOriginalConstructor() + ->getMock(); + $userManager->expects($this->once()) + ->method('userExists') + ->with('user') + ->willReturn(true); + $userManager->expects($this->once()) + ->method('get') + ->with('user') + ->willReturn($user); + + $userSession = $this->getMockBuilder('\OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user2); + + $groupManager = $this->getMockBuilder('\OCP\IGroupManager') + ->disableOriginalConstructor() + ->getMock(); + $groupManager->expects($this->once()) + ->method('isAdmin') + ->with('user2') + ->willReturn(true); + + $api = new \OCA\Provisioning_Api\Users( + $userManager, + $this->config, + $groupManager, + $userSession + ); + + $result = $api->deleteUser([ + 'userid' => 'user', + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + } + public function testGetUsersGroupsOnSelf() { $user = $this->generateUsers(); - self::loginAsUser($user); + $this->userSession->setUser($user); $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($user, $group); - $result = \OCA\provisioning_API\Users::getUsersGroups(array( - 'userid' => $user, + $group = $this->groupManager->createGroup($group); + $group->addUser($user); + $result = $this->api->getUsersGroups(array( + 'userid' => $user->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($group, reset($data['groups'])); + $this->assertEquals($group->getGID(), reset($data['groups'])); $this->assertEquals(1, count($data['groups'])); - \OC_Group::deleteGroup($group); + $group->delete(); } public function testGetUsersGroupOnOther() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($user2, $group); - $result = \OCA\provisioning_API\Users::getUsersGroups(array( - 'userid' => $user2, + $group = $this->groupManager->createGroup($group); + $group->addUser($user2); + $result = $this->api->getUsersGroups(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - \OC_Group::deleteGroup($group); + $group->delete(); } public function testGetUsersGroupOnOtherAsAdmin() { $user1 = $this->generateUsers(); - \OC_Group::addToGroup($user1, 'admin'); + $this->groupManager->get('admin')->addUser($user1); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - \OC_Group::addToGroup($user2, $group); - $result = \OCA\provisioning_API\Users::getUsersGroups(array( - 'userid' => $user2, + $group = $this->groupManager->createGroup($group); + $group->addUser($user2); + $result = $this->api->getUsersGroups(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($group, reset($data['groups'])); + $this->assertEquals($group->getGID(), reset($data['groups'])); $this->assertEquals(1, count($data['groups'])); - \OC_Group::deleteGroup($group); + $group->delete(); } public function testGetUsersGroupsOnOtherAsSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($user2, $group1); - \OC_Group::addToGroup($user2, $group2); - \OC_Group::addToGroup($user1, $group1); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $result = \OCA\provisioning_API\Users::getUsersGroups(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group2 = $this->groupManager->createGroup($group2); + $group1->addUser($user2); + $group2->addUser($user2); + $group1->addUser($user1); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $result = $this->api->getUsersGroups(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($group1, reset($data['groups'])); + $this->assertEquals($group1->getGID(), reset($data['groups'])); $this->assertEquals(1, count($data['groups'])); - \OC_Group::deleteGroup($group1); - \OC_Group::deleteGroup($group2); + $group1->delete(); + $group2->delete(); } public function testGetUsersGroupsOnOtherAsIrelevantSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($user2, $group2); - \OC_Group::addToGroup($user1, $group1); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $result = \OCA\provisioning_API\Users::getUsersGroups(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group2 = $this->groupManager->createGroup($group2); + $group2->addUser($user2); + $group1->addUser($user1); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $result = $this->api->getUsersGroups(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - \OC_Group::deleteGroup($group1); - \OC_Group::deleteGroup($group2); + $group1->delete(); + $group2->delete(); } public function testAddToGroup() { $user = $this->generateUsers(); $group = $this->getUniqueID(); - \OC_Group::createGroup($group); - self::loginAsUser($user); - $_POST['groupid'] = $group; - $result = \OCA\provisioning_API\Users::addToGroup(array( - 'userid' => $user, + $group = $this->groupManager->createGroup($group); + $this->userSession->setUser($user); + $_POST['groupid'] = $group->getGID(); + $result = $this->api->addToGroup(array( + 'userid' => $user->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - $this->assertFalse(\OC_Group::inGroup($user, $group)); - \OC_Group::deleteGroup($group); + $this->assertFalse($group->inGroup($user)); + $group->delete(); } public function testAddToGroupAsAdmin() { $user = $this->generateUsers(); - \OC_Group::addToGroup($user, 'admin'); + $this->groupManager->get('admin')->addUser($user); $group = $this->getUniqueID(); - \OC_Group::createGroup($group); + $group = $this->groupManager->createGroup($group); $user2 = $this->generateUsers(); - self::loginAsUser($user); - $_POST['groupid'] = $group; - $result = \OCA\provisioning_API\Users::addToGroup(array( - 'userid' => $user2, + $this->userSession->setUser($user); + $_POST['groupid'] = $group->getGID(); + $result = $this->api->addToGroup(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertTrue(\OC_Group::inGroup($user2, $group)); - \OC_Group::deleteGroup($group); + $this->assertTrue($group->inGroup($user2)); + $group->delete(); } public function testAddToGroupAsSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_API\Users::addToGroup(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->addToGroup(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - $this->assertFalse(\OC_Group::inGroup($user2, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertFalse($group1->inGroup($user2)); + $group1->delete(); } public function testAddToGroupAsIrelevantSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::createGroup($group2); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $_POST['groupid'] = $group2; - $result = \OCA\provisioning_API\Users::addToGroup(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group2 = $this->groupManager->createGroup($group2); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $_POST['groupid'] = $group2->getGID(); + $result = $this->api->addToGroup(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - $this->assertFalse(\OC_Group::inGroup($user2, $group2)); - \OC_Group::deleteGroup($group1); - \OC_Group::deleteGroup($group2); + $this->assertFalse($group2->inGroup($user2)); + $group1->delete(); + $group2->delete(); + } + + public function testAddToGroupNoGroupId() { + $_POST['groupid'] = ''; + $result = $this->api->addToGroup([ + 'userid' => $this->getUniqueID(), + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + } + + public function testAddToNonExistingGroup() { + $user = $this->generateUsers(); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + + $group = $this->groupManager->createGroup($this->getUniqueID()); + $_POST['groupid'] = $group->getGID(); + $result = $this->api->addToGroup([ + 'userid' => $this->getUniqueID(), + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(103, $result->getStatusCode()); + } + + public function testAddNonExistingUserToGroup() { + $user = $this->generateUsers(); + $this->groupManager->get('admin')->addUser($user); + $this->userSession->setUser($user); + + $_POST['groupid'] = $this->getUniqueID(); + $result = $this->api->addToGroup([ + 'userid' => $this->getUniqueID(), + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); } // test delete /cloud/users/{userid}/groups public function testRemoveFromGroupAsSelf() { $user1 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::addToGroup($user1, $group1); - $result = \OCA\provisioning_api\Users::removeFromGroup(array( - 'userid' => $user1, + $group1 = $this->groupManager->createGroup($group1); + $group1->addUser($user1); + $result = $this->api->removeFromGroup(array( + 'userid' => $user1->getUID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - $this->assertTrue(\OC_Group::inGroup($user1, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertTrue($group1->inGroup($user1)); + $group1->delete(); } public function testRemoveFromGroupAsAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::addToGroup($user2, $group1); - \OC_Group::addToGroup($user1, 'admin'); - $result = \OCA\provisioning_api\Users::removeFromGroup(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group1->addUser($user2); + $this->groupManager->get('admin')->addUser($user1); + $result = $this->api->removeFromGroup(array( + 'userid' => $user2->getUID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertFalse(\OC_Group::inGroup($user2, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertFalse($group1->inGroup($user2)); + $group1->delete(); + } + + public function testRemoveSelfFromGroupAsAdmin() { + $user1 = $this->generateUsers(); + $this->userSession->setUser($user1); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); + $group1->addUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $result = $this->api->removeFromGroup([ + 'userid' => $user1->getUID(), + '_delete' => [ + 'groupid' => $group1->getGID(), + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertTrue($result->succeeded()); + $this->assertFalse($group1->inGroup($user1)); + $group1->delete(); } public function testRemoveFromGroupAsSubAdmin() { $user1 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $user2 = $this->generateUsers(); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::addToGroup($user1, $group1); - \OC_Group::addToGroup($user2, $group1); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $result = \OCA\provisioning_api\Users::removeFromGroup(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group1->addUser($user1); + $group1->addUser($user2); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $result = $this->api->removeFromGroup(array( + 'userid' => $user2->getUID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertFalse(\OC_Group::inGroup($user2, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertFalse($group1->inGroup($user2)); + $group1->delete(); } public function testRemoveFromGroupAsIrelevantSubAdmin() { $user1 = $this->generateUsers(); - self::loginAsUser($user1); + $this->userSession->setUser($user1); $user2 = $this->generateUsers(); $group1 = $this->getUniqueID(); $group2 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_Group::createGroup($group2); - \OC_Group::addToGroup($user1, $group1); - \OC_Group::addToGroup($user2, $group2); - \OC_SubAdmin::createSubAdmin($user1, $group1); - $result = \OCA\provisioning_api\Users::removeFromGroup(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $group2 = $this->groupManager->createGroup($group2); + $group1->addUser($user1); + $group2->addUser($user2); + \OC_SubAdmin::createSubAdmin($user1->getUID(), $group1->getGID()); + $result = $this->api->removeFromGroup(array( + 'userid' => $user2->getUID(), '_delete' => array( - 'groupid' => $group2, + 'groupid' => $group2->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); - $this->assertTrue(\OC_Group::inGroup($user2, $group2)); - \OC_Group::deleteGroup($group1); - \OC_Group::deleteGroup($group2); + $this->assertTrue($group2->inGroup($user2)); + $group1->delete(); + $group2->delete(); } + public function testRemoveFromGroupNoGroupId() { + $result = $this->api->removeFromGroup([ + '_delete' => [ + 'groupid' => '' + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(101, $result->getStatusCode()); + } + + public function testRemoveSelfFromAdminAsAdmin() { + $user = $this->generateUsers(); + $this->userSession->setUser($user); + $this->groupManager->get('admin')->addUser($user); + + $result = $this->api->removeFromGroup([ + 'userid' => $user->getUID(), + '_delete' => [ + 'groupid' => 'admin' + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(105, $result->getStatusCode()); + $this->assertEquals('Cannot remove yourself from the admin group', $result->getMeta()['message']); + } + + public function testRemoveSelfFromSubAdminGroupAsSubAdmin() { + $user = $this->generateUsers(); + $this->userSession->setUser($user); + $group = $this->groupManager->createGroup($this->getUniqueID()); + \OC_SubAdmin::createSubAdmin($user->getUID(), $group->getGID()); + + $result = $this->api->removeFromGroup([ + 'userid' => $user->getUID(), + '_delete' => [ + 'groupid' => $group->getGID() + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(105, $result->getStatusCode()); + $this->assertEquals('Cannot remove yourself from this group as you are a SubAdmin', $result->getMeta()['message']); + $group->delete(); + } + + public function testRemoveFromNonExistingGroup() { + $user1 = $this->generateUsers(); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + + $user2 = $this->generateUsers(); + $result = $this->api->removeFromGroup([ + 'userid' => $user2->getUID(), + '_delete' => [ + 'groupid' => $this->getUniqueID() + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); + } + + public function testRemoveFromNonGroupNonExistingUser() { + $user = $this->generateUsers(); + $this->userSession->setUser($user); + $this->groupManager->get('admin')->addUser($user); + + $group = $this->groupManager->createGroup($this->getUniqueID()); + + $result = $this->api->removeFromGroup([ + 'userid' => $this->getUniqueID(), + '_delete' => [ + 'groupid' => $group->getGID() + ], + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(103, $result->getStatusCode()); + } + + public function testCreateSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_api\Users::addSubAdmin(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->addSubAdmin(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertTrue(\OC_SubAdmin::isSubAdminofGroup($user2, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertTrue(\OC_SubAdmin::isSubAdminofGroup($user2->getUID(), $group1->getGID())); + $group1->delete(); $this->resetParams(); $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $_POST['groupid'] = 'admin'; - $result = \OCA\provisioning_api\Users::addSubAdmin(array( - 'userid' => $user2, + $result = $this->api->addSubAdmin(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertEquals(103, $result->getStatusCode()); @@ -692,46 +1050,58 @@ class UsersTest extends TestCase { $this->resetParams(); $user1 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_api\Users::addSubAdmin(array( + $group1 = $this->groupManager->createGroup($group1); + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->addSubAdmin(array( 'userid' => $this->getUniqueID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(101, $result->getStatusCode()); - \OC_Group::deleteGroup($group1); + $group1->delete(); + + $user1 = $this->generateUsers(); + $this->userSession->setUser($user1); + $group = $this->getUniqueID(); + $_POST['groupid'] = $group; + $result = $this->api->addSubAdmin([ + 'userid' => $user1->getUID() + ]); + $this->assertInstanceOf('OC_OCS_Result', $result); + $this->assertFalse($result->succeeded()); + $this->assertEquals(102, $result->getStatusCode()); + $this->assertEquals('Group:'.$group.' does not exist', $result->getMeta()['message']); } public function testRemoveSubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_SubAdmin::createSubAdmin($user2, $group1); - $result = \OCA\provisioning_api\Users::removeSubAdmin(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID()); + $result = $this->api->removeSubAdmin(array( + 'userid' => $user2->getUID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); - $this->assertTrue(!\OC_SubAdmin::isSubAdminofGroup($user2, $group1)); - \OC_Group::deleteGroup($group1); + $this->assertTrue(!\OC_SubAdmin::isSubAdminofGroup($user2->getUID(), $group1->getGID())); + $group1->delete(); $user1 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $result = \OCA\provisioning_api\Users::removeSubAdmin(array( + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $result = $this->api->removeSubAdmin(array( 'userid' => $this->getUniqueID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -742,45 +1112,44 @@ class UsersTest extends TestCase { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_api\Users::removeSubAdmin(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->removeSubAdmin(array( + 'userid' => $user2->getUID(), '_delete' => array( - 'groupid' => $group1, + 'groupid' => $group1->getGID(), ), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertFalse($result->succeeded()); $this->assertEquals(102, $result->getStatusCode()); - \OC_Group::deleteGroup($group1); + $group1->delete(); } public function testGetSubAdminGroups() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); - \OC_SubAdmin::createSubAdmin($user2, $group1); - $result = \OCA\provisioning_api\Users::getUserSubAdminGroups(array( - 'userid' => $user2, + $group1 = $this->groupManager->createGroup($group1); + \OC_SubAdmin::createSubAdmin($user2->getUID(), $group1->getGID()); + $result = $this->api->getUserSubAdminGroups(array( + 'userid' => $user2->getUID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); $data = $result->getData(); - $this->assertEquals($group1, reset($data)); - \OC_Group::deleteGroup($group1); + $this->assertEquals($group1->getGID(), reset($data)); + $group1->delete(); $user1 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $group1 = $this->getUniqueID(); - $result = \OCA\provisioning_api\Users::getUserSubAdminGroups(array( + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $result = $this->api->getUserSubAdminGroups(array( 'userid' => $this->getUniqueID(), )); $this->assertInstanceOf('OC_OCS_Result', $result); @@ -791,25 +1160,25 @@ class UsersTest extends TestCase { public function testSubAdminOfGroupAlreadySubAdmin() { $user1 = $this->generateUsers(); $user2 = $this->generateUsers(); - self::loginAsUser($user1); - \OC_Group::addToGroup($user1, 'admin'); - $group1 = $this->getUniqueID(); - \OC_Group::createGroup($group1); + $this->userSession->setUser($user1); + $this->groupManager->get('admin')->addUser($user1); + $group1 = $this->groupManager->createGroup($this->getUniqueID()); //Make user2 subadmin of group1 - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_api\Users::addSubAdmin([ - 'userid' => $user2, + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->addSubAdmin([ + 'userid' => $user2->getUID(), ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); //Make user2 subadmin of group1 again - $_POST['groupid'] = $group1; - $result = \OCA\provisioning_api\Users::addSubAdmin([ - 'userid' => $user2, + $_POST['groupid'] = $group1->getGID(); + $result = $this->api->addSubAdmin([ + 'userid' => $user2->getUID(), ]); $this->assertInstanceOf('OC_OCS_Result', $result); $this->assertTrue($result->succeeded()); + $group1->delete(); } }