Browse Source
Add REST route for user & group management
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.remotes/origin/fix-10825
25 changed files with 1222 additions and 340 deletions
-
22lib/private/group.php
-
5lib/private/group/group.php
-
7lib/private/group/metadata.php
-
22lib/private/server.php
-
25lib/private/user.php
-
2lib/private/user/manager.php
-
18lib/private/user/user.php
-
21settings/ajax/creategroup.php
-
59settings/ajax/createuser.php
-
46settings/ajax/grouplist.php
-
14settings/ajax/removegroup.php
-
26settings/ajax/removeuser.php
-
92settings/ajax/userlist.php
-
71settings/application.php
-
140settings/controller/groupscontroller.php
-
251settings/controller/userscontroller.php
-
2settings/js/settings.js
-
5settings/js/users/deleteHandler.js
-
6settings/js/users/groups.js
-
6settings/js/users/users.js
-
65settings/middleware/subadminmiddleware.php
-
39settings/routes.php
-
217tests/settings/controller/groupscontrollertest.php
-
310tests/settings/controller/userscontrollertest.php
-
91tests/settings/middleware/subadminmiddlewaretest.php
@ -1,21 +0,0 @@ |
|||
<?php |
|||
|
|||
OCP\JSON::callCheck(); |
|||
OC_JSON::checkAdminUser(); |
|||
|
|||
$groupname = $_POST["groupname"]; |
|||
$l = \OC::$server->getL10N('settings'); |
|||
|
|||
// Does the group exist?
|
|||
if( in_array( $groupname, OC_Group::getGroups())) { |
|||
OC_JSON::error(array("data" => array( "message" => $l->t("Group already exists") ))); |
|||
exit(); |
|||
} |
|||
|
|||
// Return Success story
|
|||
if( OC_Group::createGroup( $groupname )) { |
|||
OC_JSON::success(array("data" => array( "groupname" => $groupname ))); |
|||
} |
|||
else{ |
|||
OC_JSON::error(array("data" => array( "message" => $l->t("Unable to add group") ))); |
|||
} |
|||
@ -1,59 +0,0 @@ |
|||
<?php |
|||
|
|||
OCP\JSON::callCheck(); |
|||
OC_JSON::checkSubAdminUser(); |
|||
|
|||
if(OC_User::isAdminUser(OC_User::getUser())) { |
|||
$groups = array(); |
|||
if (!empty($_POST["groups"])) { |
|||
$groups = $_POST["groups"]; |
|||
} |
|||
}else{ |
|||
if (isset($_POST["groups"])) { |
|||
$groups = array(); |
|||
if (!empty($_POST["groups"])) { |
|||
foreach ($_POST["groups"] as $group) { |
|||
if (OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group)) { |
|||
$groups[] = $group; |
|||
} |
|||
} |
|||
} |
|||
if (empty($groups)) { |
|||
$groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); |
|||
} |
|||
} else { |
|||
$groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); |
|||
} |
|||
} |
|||
$username = $_POST["username"]; |
|||
$password = $_POST["password"]; |
|||
|
|||
// Return Success story
|
|||
try { |
|||
// check whether the user's files home exists
|
|||
$userDirectory = OC_User::getHome($username) . '/files/'; |
|||
$homeExists = file_exists($userDirectory); |
|||
|
|||
if (!OC_User::createUser($username, $password)) { |
|||
OC_JSON::error(array('data' => array( 'message' => 'User creation failed for '.$username ))); |
|||
exit(); |
|||
} |
|||
foreach( $groups as $i ) { |
|||
if(!OC_Group::groupExists($i)) { |
|||
OC_Group::createGroup($i); |
|||
} |
|||
OC_Group::addToGroup( $username, $i ); |
|||
} |
|||
|
|||
$userManager = \OC_User::getManager(); |
|||
$user = $userManager->get($username); |
|||
OCP\JSON::success(array("data" => |
|||
array( |
|||
// returns whether the home already existed
|
|||
"homeExists" => $homeExists, |
|||
"username" => $username, |
|||
"groups" => OC_Group::getUserGroups( $username ), |
|||
'storageLocation' => $user->getHome()))); |
|||
} catch (Exception $exception) { |
|||
OCP\JSON::error(array("data" => array( "message" => $exception->getMessage()))); |
|||
} |
|||
@ -1,46 +0,0 @@ |
|||
<?php |
|||
/** |
|||
* ownCloud |
|||
* |
|||
* @author Arthur Schiwon |
|||
* @copyright 2014 Arthur Schiwon <blizzz@owncloud.com> |
|||
* |
|||
* This library is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|||
* License as published by the Free Software Foundation; either |
|||
* version 3 of the License, or any later version. |
|||
* |
|||
* This library is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public |
|||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
OC_JSON::callCheck(); |
|||
OC_JSON::checkSubAdminUser(); |
|||
if (isset($_GET['pattern']) && !empty($_GET['pattern'])) { |
|||
$pattern = $_GET['pattern']; |
|||
} else { |
|||
$pattern = ''; |
|||
} |
|||
if (isset($_GET['filterGroups']) && !empty($_GET['filterGroups'])) { |
|||
$filterGroups = intval($_GET['filterGroups']) === 1; |
|||
} else { |
|||
$filterGroups = false; |
|||
} |
|||
$groupPattern = $filterGroups ? $pattern : ''; |
|||
$groups = array(); |
|||
$adminGroups = array(); |
|||
$groupManager = \OC_Group::getManager(); |
|||
$isAdmin = OC_User::isAdminUser(OC_User::getUser()); |
|||
|
|||
$groupsInfo = new \OC\Group\MetaData(OC_User::getUser(), $isAdmin, $groupManager); |
|||
$groupsInfo->setSorting($groupsInfo::SORT_USERCOUNT); |
|||
list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern); |
|||
|
|||
OC_JSON::success( |
|||
array('data' => array('adminGroups' => $adminGroups, 'groups' => $groups))); |
|||
@ -1,14 +0,0 @@ |
|||
<?php |
|||
|
|||
OC_JSON::checkAdminUser(); |
|||
OCP\JSON::callCheck(); |
|||
|
|||
$name = $_POST["groupname"]; |
|||
|
|||
// Return Success story
|
|||
if( OC_Group::deleteGroup( $name )) { |
|||
OC_JSON::success(array("data" => array( "groupname" => $name ))); |
|||
} |
|||
else{ |
|||
OC_JSON::error(array("data" => array( "message" => $l->t("Unable to delete group") ))); |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
<?php |
|||
|
|||
OC_JSON::checkSubAdminUser(); |
|||
OCP\JSON::callCheck(); |
|||
|
|||
$username = $_POST["username"]; |
|||
|
|||
// A user shouldn't be able to delete his own account
|
|||
if(OC_User::getUser() === $username) { |
|||
exit; |
|||
} |
|||
|
|||
if(!OC_User::isAdminUser(OC_User::getUser()) && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) { |
|||
$l = \OC::$server->getL10N('core'); |
|||
OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') ))); |
|||
exit(); |
|||
} |
|||
|
|||
// Return Success story
|
|||
if( OC_User::deleteUser( $username )) { |
|||
OC_JSON::success(array("data" => array( "username" => $username ))); |
|||
} |
|||
else{ |
|||
$l = \OC::$server->getL10N('core'); |
|||
OC_JSON::error(array("data" => array( "message" => $l->t("Unable to delete user") ))); |
|||
} |
|||
@ -1,92 +0,0 @@ |
|||
<?php |
|||
/** |
|||
* ownCloud |
|||
* |
|||
* @author Michael Gapczynski |
|||
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com |
|||
* |
|||
* This library is free software; you can redistribute it and/or |
|||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|||
* License as published by the Free Software Foundation; either |
|||
* version 3 of the License, or any later version. |
|||
* |
|||
* This library is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|||
* |
|||
* You should have received a copy of the GNU Affero General Public |
|||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|||
* |
|||
*/ |
|||
|
|||
OC_JSON::callCheck(); |
|||
OC_JSON::checkSubAdminUser(); |
|||
if (isset($_GET['offset'])) { |
|||
$offset = $_GET['offset']; |
|||
} else { |
|||
$offset = 0; |
|||
} |
|||
if (isset($_GET['limit'])) { |
|||
$limit = $_GET['limit']; |
|||
} else { |
|||
$limit = 10; |
|||
} |
|||
if (isset($_GET['gid']) && !empty($_GET['gid'])) { |
|||
$gid = $_GET['gid']; |
|||
if ($gid === '_everyone') { |
|||
$gid = false; |
|||
} |
|||
} else { |
|||
$gid = false; |
|||
} |
|||
if (isset($_GET['pattern']) && !empty($_GET['pattern'])) { |
|||
$pattern = $_GET['pattern']; |
|||
} else { |
|||
$pattern = ''; |
|||
} |
|||
$users = array(); |
|||
$userManager = \OC_User::getManager(); |
|||
if (OC_User::isAdminUser(OC_User::getUser())) { |
|||
if($gid !== false) { |
|||
$batch = OC_Group::displayNamesInGroup($gid, $pattern, $limit, $offset); |
|||
} else { |
|||
$batch = OC_User::getDisplayNames($pattern, $limit, $offset); |
|||
} |
|||
foreach ($batch as $uid => $displayname) { |
|||
$user = $userManager->get($uid); |
|||
$users[] = array( |
|||
'name' => $uid, |
|||
'displayname' => $displayname, |
|||
'groups' => OC_Group::getUserGroups($uid), |
|||
'subadmin' => OC_SubAdmin::getSubAdminsGroups($uid), |
|||
'quota' => OC_Preferences::getValue($uid, 'files', 'quota', 'default'), |
|||
'storageLocation' => $user->getHome(), |
|||
'lastLogin' => $user->getLastLogin(), |
|||
); |
|||
} |
|||
} else { |
|||
$groups = OC_SubAdmin::getSubAdminsGroups(OC_User::getUser()); |
|||
if($gid !== false && in_array($gid, $groups)) { |
|||
$groups = array($gid); |
|||
} elseif($gid !== false) { |
|||
//don't you try to investigate loops you must not know about
|
|||
$groups = array(); |
|||
} |
|||
$batch = OC_Group::usersInGroups($groups, $pattern, $limit, $offset); |
|||
foreach ($batch as $uid) { |
|||
$user = $userManager->get($uid); |
|||
|
|||
// Only add the groups, this user is a subadmin of
|
|||
$userGroups = array_intersect(OC_Group::getUserGroups($uid), OC_SubAdmin::getSubAdminsGroups(OC_User::getUser())); |
|||
$users[] = array( |
|||
'name' => $uid, |
|||
'displayname' => $user->getDisplayName(), |
|||
'groups' => $userGroups, |
|||
'quota' => OC_Preferences::getValue($uid, 'files', 'quota', 'default'), |
|||
'storageLocation' => $user->getHome(), |
|||
'lastLogin' => $user->getLastLogin(), |
|||
); |
|||
} |
|||
} |
|||
OC_JSON::success(array('data' => $users)); |
|||
@ -0,0 +1,140 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
|
|||
namespace OC\Settings\Controller; |
|||
|
|||
use \OCP\AppFramework\Controller; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
use OCP\IGroupManager; |
|||
use OCP\IL10N; |
|||
use OCP\IRequest; |
|||
use OCP\IUserSession; |
|||
|
|||
/** |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class GroupsController extends Controller { |
|||
/** @var IGroupManager */ |
|||
private $groupManager; |
|||
/** @var IL10N */ |
|||
private $l10n; |
|||
/** @var IUserSession */ |
|||
private $userSession; |
|||
/** @var bool */ |
|||
private $isAdmin; |
|||
|
|||
/** |
|||
* @param string $appName |
|||
* @param IRequest $request |
|||
* @param IGroupManager $groupManager |
|||
* @param IUserSession $userSession |
|||
* @param bool $isAdmin |
|||
* @param IL10N $l10n |
|||
*/ |
|||
public function __construct($appName, |
|||
IRequest $request, |
|||
IGroupManager $groupManager, |
|||
IUserSession $userSession, |
|||
$isAdmin, |
|||
IL10N $l10n) { |
|||
parent::__construct($appName, $request); |
|||
$this->groupManager = $groupManager; |
|||
$this->userSession = $userSession; |
|||
$this->isAdmin = $isAdmin; |
|||
$this->l10n = $l10n; |
|||
} |
|||
|
|||
/** |
|||
* @NoAdminRequired |
|||
* |
|||
* @param string $pattern |
|||
* @param bool $filterGroups |
|||
* @return DataResponse |
|||
*/ |
|||
public function index($pattern = '', $filterGroups = false) { |
|||
$groupPattern = $filterGroups ? $pattern : ''; |
|||
|
|||
$groupsInfo = new \OC\Group\MetaData($this->userSession->getUser()->getUID(), |
|||
$this->isAdmin, $this->groupManager); |
|||
$groupsInfo->setSorting($groupsInfo::SORT_USERCOUNT); |
|||
list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern); |
|||
|
|||
return new DataResponse( |
|||
array( |
|||
'data' => array('adminGroups' => $adminGroups, 'groups' => $groups) |
|||
) |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* @param string $id |
|||
* @return DataResponse |
|||
*/ |
|||
public function create($id) { |
|||
if($this->groupManager->groupExists($id)) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Group already exists.') |
|||
) |
|||
) |
|||
); |
|||
} |
|||
if($this->groupManager->createGroup($id)) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'groupname' => $id |
|||
) |
|||
) |
|||
); |
|||
} |
|||
|
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Unable to add group.') |
|||
) |
|||
) |
|||
); |
|||
} |
|||
|
|||
/** |
|||
* @param string $id |
|||
* @return DataResponse |
|||
*/ |
|||
public function destroy($id) { |
|||
$group = $this->groupManager->get($id); |
|||
if ($group) { |
|||
if ($group->delete()) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'groupname' => $id |
|||
) |
|||
) |
|||
); |
|||
} |
|||
} |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Unable to delete group.') |
|||
) |
|||
) |
|||
); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,251 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
|
|||
namespace OC\Settings\Controller; |
|||
|
|||
use OC\User\User; |
|||
use \OCP\AppFramework\Controller; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
use OCP\IConfig; |
|||
use OCP\IGroupManager; |
|||
use OCP\IL10N; |
|||
use OCP\IRequest; |
|||
use OCP\IUserManager; |
|||
use OCP\IUserSession; |
|||
|
|||
/** |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class UsersController extends Controller { |
|||
/** @var IL10N */ |
|||
private $l10n; |
|||
/** @var IUserSession */ |
|||
private $userSession; |
|||
/** @var bool */ |
|||
private $isAdmin; |
|||
/** @var IUserManager */ |
|||
private $userManager; |
|||
/** @var IGroupManager */ |
|||
private $groupManager; |
|||
/** @var IConfig */ |
|||
private $config; |
|||
|
|||
/** |
|||
* @param string $appName |
|||
* @param IRequest $request |
|||
* @param IUserManager $userManager |
|||
* @param IGroupManager $groupManager |
|||
* @param IUserSession $userSession |
|||
* @param IConfig $config |
|||
* @param bool $isAdmin |
|||
* @param IL10N $l10n |
|||
*/ |
|||
public function __construct($appName, |
|||
IRequest $request, |
|||
IUserManager $userManager, |
|||
IGroupManager $groupManager, |
|||
IUserSession $userSession, |
|||
IConfig $config, |
|||
$isAdmin, |
|||
IL10N $l10n) { |
|||
parent::__construct($appName, $request); |
|||
$this->userManager = $userManager; |
|||
$this->groupManager = $groupManager; |
|||
$this->userSession = $userSession; |
|||
$this->config = $config; |
|||
$this->isAdmin = $isAdmin; |
|||
$this->l10n = $l10n; |
|||
} |
|||
|
|||
/** |
|||
* @NoAdminRequired |
|||
* @NoCSRFRequired |
|||
* @param int $offset |
|||
* @param int $limit |
|||
* @param string $gid |
|||
* @param string $pattern |
|||
* @return DataResponse |
|||
* |
|||
* TODO: Tidy up and write unit tests - code is mainly static method calls |
|||
*/ |
|||
public function index($offset = 0, $limit = 10, $gid = '', $pattern = '') { |
|||
// FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
|
|||
if($gid === '_everyone') { |
|||
$gid = ''; |
|||
} |
|||
$users = array(); |
|||
if ($this->isAdmin) { |
|||
if($gid !== '') { |
|||
$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset); |
|||
} else { |
|||
// FIXME: Remove static method call
|
|||
$batch = \OC_User::getDisplayNames($pattern, $limit, $offset); |
|||
} |
|||
|
|||
foreach ($batch as $uid => $displayname) { |
|||
$user = $this->userManager->get($uid); |
|||
$users[] = array( |
|||
'name' => $uid, |
|||
'displayname' => $displayname, |
|||
'groups' => $this->groupManager->getUserGroupIds($user), |
|||
'subadmin' => \OC_SubAdmin::getSubAdminsGroups($uid), |
|||
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'), |
|||
'storageLocation' => $user->getHome(), |
|||
'lastLogin' => $user->getLastLogin(), |
|||
); |
|||
} |
|||
} else { |
|||
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()); |
|||
if($gid !== '' && in_array($gid, $groups)) { |
|||
$groups = array($gid); |
|||
} elseif($gid !== '') { |
|||
//don't you try to investigate loops you must not know about
|
|||
$groups = array(); |
|||
} |
|||
$batch = \OC_Group::usersInGroups($groups, $pattern, $limit, $offset); |
|||
foreach ($batch as $uid) { |
|||
$user = $this->userManager->get($uid); |
|||
|
|||
// Only add the groups, this user is a subadmin of
|
|||
$userGroups = array_intersect($this->groupManager->getUserGroupIds($user), \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID())); |
|||
$users[] = array( |
|||
'name' => $uid, |
|||
'displayname' => $user->getDisplayName(), |
|||
'groups' => $userGroups, |
|||
'quota' => $this->config->getUserValue($uid, 'files', 'quota', 'default'), |
|||
'storageLocation' => $user->getHome(), |
|||
'lastLogin' => $user->getLastLogin(), |
|||
); |
|||
} |
|||
} |
|||
|
|||
// FIXME: That assignment on "data" is uneeded here - JS should be adjusted
|
|||
return new DataResponse(array('data' => $users, 'status' => 'success')); |
|||
} |
|||
|
|||
/** |
|||
* @NoAdminRequired |
|||
* |
|||
* @param string $username |
|||
* @param string $password |
|||
* @param array $groups |
|||
* @return DataResponse |
|||
* |
|||
* TODO: Tidy up and write unit tests - code is mainly static method calls |
|||
*/ |
|||
public function create($username, $password, array $groups) { |
|||
|
|||
if (!$this->isAdmin) { |
|||
if (!empty($groups)) { |
|||
foreach ($groups as $key => $group) { |
|||
if (!\OC_SubAdmin::isGroupAccessible($this->userSession->getUser()->getUID(), $group)) { |
|||
unset($groups[$key]); |
|||
} |
|||
} |
|||
} |
|||
if (empty($groups)) { |
|||
$groups = \OC_SubAdmin::getSubAdminsGroups($this->userSession->getUser()->getUID()); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
$user = $this->userManager->createUser($username, $password); |
|||
} catch (\Exception $exception) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Unable to create user.') |
|||
) |
|||
) |
|||
); |
|||
} |
|||
|
|||
if($user instanceof User) { |
|||
foreach( $groups as $groupName ) { |
|||
$group = $this->groupManager->get($groupName); |
|||
|
|||
if(empty($group)) { |
|||
$group = $this->groupManager->createGroup($groupName); |
|||
} |
|||
$group->addUser($user); |
|||
} |
|||
} |
|||
|
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'username' => $username, |
|||
'groups' => $this->groupManager->getUserGroupIds($user), |
|||
'storageLocation' => $user->getHome() |
|||
) |
|||
) |
|||
); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* @NoAdminRequired |
|||
* |
|||
* @param string $id |
|||
* @return DataResponse |
|||
* |
|||
* TODO: Tidy up and write unit tests - code is mainly static method calls |
|||
*/ |
|||
public function destroy($id) { |
|||
if($this->userSession->getUser()->getUID() === $id) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Unable to delete user.') |
|||
) |
|||
) |
|||
); |
|||
} |
|||
|
|||
// FIXME: Remove this static function call at some point…
|
|||
if(!$this->isAdmin && !\OC_SubAdmin::isUserAccessible($this->userSession->getUser()->getUID(), $id)) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Authentication error')) |
|||
) |
|||
); |
|||
} |
|||
|
|||
$user = $this->userManager->get($id); |
|||
if($user) { |
|||
if($user->delete()) { |
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'username' => $id |
|||
) |
|||
) |
|||
); |
|||
} |
|||
} |
|||
|
|||
return new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => (string)$this->l10n->t('Unable to delete user.') |
|||
) |
|||
) |
|||
); |
|||
|
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
|
|||
namespace OC\Settings\Middleware; |
|||
|
|||
use OC\AppFramework\Http; |
|||
use OC\AppFramework\Utility\ControllerMethodReflector; |
|||
use OCP\AppFramework\Http\TemplateResponse; |
|||
use OCP\AppFramework\Middleware; |
|||
|
|||
/** |
|||
* Verifies whether an user has at least subadmin rights. |
|||
* To bypass use the `@NoSubadminRequired` annotation |
|||
* |
|||
* @package OC\Settings\Middleware |
|||
*/ |
|||
class SubadminMiddleware extends Middleware { |
|||
/** @var bool */ |
|||
protected $isSubAdmin; |
|||
/** @var ControllerMethodReflector */ |
|||
protected $reflector; |
|||
|
|||
/** |
|||
* @param ControllerMethodReflector $reflector |
|||
* @param bool $isSubAdmin |
|||
*/ |
|||
public function __construct(ControllerMethodReflector $reflector, |
|||
$isSubAdmin) { |
|||
$this->reflector = $reflector; |
|||
$this->isSubAdmin = $isSubAdmin; |
|||
} |
|||
|
|||
/** |
|||
* Check if sharing is enabled before the controllers is executed |
|||
* @param \OCP\AppFramework\Controller $controller |
|||
* @param string $methodName |
|||
* @throws \Exception |
|||
*/ |
|||
public function beforeController($controller, $methodName) { |
|||
if(!$this->reflector->hasAnnotation('NoSubadminRequired')) { |
|||
if(!$this->isSubAdmin) { |
|||
throw new \Exception('Logged in user must be a subadmin'); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Return 403 page in case of an exception |
|||
* @param \OCP\AppFramework\Controller $controller |
|||
* @param string $methodName |
|||
* @param \Exception $exception |
|||
* @return TemplateResponse |
|||
*/ |
|||
public function afterException($controller, $methodName, \Exception $exception) { |
|||
return new TemplateResponse('core', '403', array(), 'guest'); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,217 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
namespace OC\Settings\Controller; |
|||
|
|||
use OC\Group\Group; |
|||
use \OC\Settings\Application; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
|
|||
/** |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class GroupsControllerTest extends \Test\TestCase { |
|||
|
|||
/** @var \OCP\AppFramework\IAppContainer */ |
|||
private $container; |
|||
|
|||
/** @var GroupsController */ |
|||
private $groupsController; |
|||
|
|||
protected function setUp() { |
|||
$app = new Application(); |
|||
$this->container = $app->getContainer(); |
|||
$this->container['AppName'] = 'settings'; |
|||
$this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['IsAdmin'] = true; |
|||
$this->container['L10N'] |
|||
->expects($this->any()) |
|||
->method('t') |
|||
->will($this->returnCallback(function($text, $parameters = array()) { |
|||
return vsprintf($text, $parameters); |
|||
})); |
|||
$this->groupsController = $this->container['GroupsController']; |
|||
|
|||
} |
|||
|
|||
/** |
|||
* TODO: Since GroupManager uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testIndex() { |
|||
$firstGroup = $this->getMockBuilder('\OC\Group\Group') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$firstGroup |
|||
->method('getGID') |
|||
->will($this->returnValue('firstGroup')); |
|||
$firstGroup |
|||
->method('count') |
|||
->will($this->returnValue(12)); |
|||
$secondGroup = $this->getMockBuilder('\OC\Group\Group') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$secondGroup |
|||
->method('getGID') |
|||
->will($this->returnValue('secondGroup')); |
|||
$secondGroup |
|||
->method('count') |
|||
->will($this->returnValue(25)); |
|||
$thirdGroup = $this->getMockBuilder('\OC\Group\Group') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$thirdGroup |
|||
->method('getGID') |
|||
->will($this->returnValue('thirdGroup')); |
|||
$thirdGroup |
|||
->method('count') |
|||
->will($this->returnValue(14)); |
|||
$fourthGroup = $this->getMockBuilder('\OC\Group\Group') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$fourthGroup |
|||
->method('getGID') |
|||
->will($this->returnValue('admin')); |
|||
$fourthGroup |
|||
->method('count') |
|||
->will($this->returnValue(18)); |
|||
/** @var \OC\Group\Group[] $groups */ |
|||
$groups = array(); |
|||
$groups[] = $firstGroup; |
|||
$groups[] = $secondGroup; |
|||
$groups[] = $thirdGroup; |
|||
$groups[] = $fourthGroup; |
|||
|
|||
$user = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['UserSession'] |
|||
->expects($this->once()) |
|||
->method('getUser') |
|||
->will($this->returnValue($user)); |
|||
$user |
|||
->expects($this->once()) |
|||
->method('getUID') |
|||
->will($this->returnValue('MyAdminUser')); |
|||
$this->container['GroupManager'] |
|||
->method('search') |
|||
->will($this->returnValue($groups)); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'data' => array( |
|||
'adminGroups' => array( |
|||
0 => array( |
|||
'id' => 'admin', |
|||
'name' => 'admin', |
|||
'usercount' => 18 |
|||
) |
|||
), |
|||
'groups' => |
|||
array( |
|||
0 => array( |
|||
'id' => 'secondGroup', |
|||
'name' => 'secondGroup', |
|||
'usercount' => 25 |
|||
), |
|||
1 => array( |
|||
'id' => 'thirdGroup', |
|||
'name' => 'thirdGroup', |
|||
'usercount' => 14 |
|||
), |
|||
2 => array( |
|||
'id' => 'firstGroup', |
|||
'name' => 'firstGroup', |
|||
'usercount' => 12 |
|||
) |
|||
) |
|||
) |
|||
) |
|||
); |
|||
$response = $this->groupsController->index(); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
public function testCreateWithExistingGroup() { |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('groupExists') |
|||
->with('ExistingGroup') |
|||
->will($this->returnValue(true)); |
|||
|
|||
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Group already exists.'))); |
|||
$response = $this->groupsController->create('ExistingGroup'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
public function testCreateSuccessful() { |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('groupExists') |
|||
->with('NewGroup') |
|||
->will($this->returnValue(false)); |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('createGroup') |
|||
->with('NewGroup') |
|||
->will($this->returnValue(true)); |
|||
|
|||
$expectedResponse = new DataResponse(array('status' => 'success', 'data' => array('groupname' => 'NewGroup'))); |
|||
$response = $this->groupsController->create('NewGroup'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
public function testCreateUnsuccessful() { |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('groupExists') |
|||
->with('NewGroup') |
|||
->will($this->returnValue(false)); |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('createGroup') |
|||
->with('NewGroup') |
|||
->will($this->returnValue(false)); |
|||
|
|||
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Unable to add group.'))); |
|||
$response = $this->groupsController->create('NewGroup'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
public function testDestroySuccessful() { |
|||
$group = $this->getMockBuilder('\OC\Group\Group') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('get') |
|||
->with('ExistingGroup') |
|||
->will($this->returnValue($group)); |
|||
$group |
|||
->expects($this->once()) |
|||
->method('delete') |
|||
->will($this->returnValue(true)); |
|||
|
|||
$expectedResponse = new DataResponse(array('status' => 'success', 'data' => array('groupname' => 'ExistingGroup'))); |
|||
$response = $this->groupsController->destroy('ExistingGroup'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
public function testDestroyUnsuccessful() { |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('get') |
|||
->with('ExistingGroup') |
|||
->will($this->returnValue(null)); |
|||
|
|||
$expectedResponse = new DataResponse(array('status' => 'error', 'data' => array('message' => 'Unable to delete group.'))); |
|||
$response = $this->groupsController->destroy('ExistingGroup'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,310 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
namespace OC\Settings\Controller; |
|||
|
|||
use \OC\Settings\Application; |
|||
use OCP\AppFramework\Http\DataResponse; |
|||
|
|||
/** |
|||
* @package OC\Settings\Controller |
|||
*/ |
|||
class UsersControllerTest extends \Test\TestCase { |
|||
|
|||
/** @var \OCP\AppFramework\IAppContainer */ |
|||
private $container; |
|||
|
|||
/** @var UsersController */ |
|||
private $usersController; |
|||
|
|||
protected function setUp() { |
|||
$app = new Application(); |
|||
$this->container = $app->getContainer(); |
|||
$this->container['AppName'] = 'settings'; |
|||
$this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['Config'] = $this->getMockBuilder('\OCP\IConfig') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->container['IsAdmin'] = true; |
|||
$this->container['L10N'] |
|||
->expects($this->any()) |
|||
->method('t') |
|||
->will($this->returnCallback(function($text, $parameters = array()) { |
|||
return vsprintf($text, $parameters); |
|||
})); |
|||
$this->usersController = $this->container['UsersController']; |
|||
|
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testIndex() { |
|||
$admin = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$admin |
|||
->method('getLastLogin') |
|||
->will($this->returnValue(12)); |
|||
$admin |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/admin')); |
|||
$foo = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$foo |
|||
->method('getLastLogin') |
|||
->will($this->returnValue(500)); |
|||
$foo |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/foo')); |
|||
$bar = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$bar |
|||
->method('getLastLogin') |
|||
->will($this->returnValue(3999)); |
|||
$bar |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/bar')); |
|||
|
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('displayNamesInGroup') |
|||
->will($this->returnValue(array('foo' => 'M. Foo', 'admin' => 'S. Admin', 'bar' => 'B. Ar'))); |
|||
$this->container['GroupManager'] |
|||
->expects($this->exactly(3)) |
|||
->method('getUserGroupIds') |
|||
->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users'))); |
|||
$this->container['UserManager'] |
|||
->expects($this->exactly(3)) |
|||
->method('get') |
|||
->will($this->onConsecutiveCalls($foo, $admin, $bar)); |
|||
$this->container['Config'] |
|||
->expects($this->exactly(3)) |
|||
->method('getUserValue') |
|||
->will($this->onConsecutiveCalls(1024, 404, 2323)); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
0 => array( |
|||
'name' => 'foo', |
|||
'displayname' => 'M. Foo', |
|||
'groups' => array('Users', 'Support'), |
|||
'subadmin' => array(), |
|||
'quota' => 1024, |
|||
'storageLocation' => '/home/foo', |
|||
'lastLogin' => 500 |
|||
), |
|||
1 => array( |
|||
'name' => 'admin', |
|||
'displayname' => 'S. Admin', |
|||
'groups' => array('admins', 'Support'), |
|||
'subadmin' => array(), |
|||
'quota' => 404, |
|||
'storageLocation' => '/home/admin', |
|||
'lastLogin' => 12 |
|||
), |
|||
2 => array( |
|||
'name' => 'bar', |
|||
'displayname' => 'B. Ar', |
|||
'groups' => array('External Users'), |
|||
'subadmin' => array(), |
|||
'quota' => 2323, |
|||
'storageLocation' => '/home/bar', |
|||
'lastLogin' => 3999 |
|||
), |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->index(0, 10, 'pattern'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testCreateSuccessfulWithoutGroup() { |
|||
$user = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$user |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/user')); |
|||
|
|||
$this->container['UserManager'] |
|||
->expects($this->once()) |
|||
->method('createUser') |
|||
->will($this->onConsecutiveCalls($user)); |
|||
|
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'username' => 'foo', |
|||
'groups' => null, |
|||
'storageLocation' => '/home/user' |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->create('foo', 'password', array()); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testCreateSuccessfulWithGroup() { |
|||
$user = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$user |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/user')); |
|||
$user |
|||
->method('getHome') |
|||
->will($this->returnValue('/home/user')); |
|||
$existingGroup = $this->getMockBuilder('\OCP\IGroup') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$existingGroup |
|||
->expects($this->once()) |
|||
->method('addUser') |
|||
->with($user); |
|||
$newGroup = $this->getMockBuilder('\OCP\IGroup') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$newGroup |
|||
->expects($this->once()) |
|||
->method('addUser') |
|||
->with($user); |
|||
|
|||
$this->container['UserManager'] |
|||
->expects($this->once()) |
|||
->method('createUser') |
|||
->will($this->onConsecutiveCalls($user)); |
|||
$this->container['GroupManager'] |
|||
->expects($this->exactly(2)) |
|||
->method('get') |
|||
->will($this->onConsecutiveCalls(null, $existingGroup)); |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('createGroup') |
|||
->with('NewGroup') |
|||
->will($this->onConsecutiveCalls($newGroup)); |
|||
$this->container['GroupManager'] |
|||
->expects($this->once()) |
|||
->method('getUserGroupIds') |
|||
->with($user) |
|||
->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup'))); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'username' => 'foo', |
|||
'groups' => array('NewGroup', 'ExistingGroup'), |
|||
'storageLocation' => '/home/user' |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->create('foo', 'password', array('NewGroup', 'ExistingGroup')); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testCreateUnsuccessful() { |
|||
$this->container['UserManager'] |
|||
->method('createUser') |
|||
->will($this->throwException(new \Exception())); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => 'Unable to create user.' |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->create('foo', 'password', array()); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testDestroySelf() { |
|||
$user = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$user |
|||
->expects($this->once()) |
|||
->method('getUID') |
|||
->will($this->returnValue('myself')); |
|||
$this->container['UserSession'] |
|||
->method('getUser') |
|||
->will($this->returnValue($user)); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'error', |
|||
'data' => array( |
|||
'message' => 'Unable to delete user.' |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->destroy('myself'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
|
|||
/** |
|||
* TODO: Since the function uses the static OC_Subadmin class it can't be mocked |
|||
* to test for subadmins. Thus the test always assumes you have admin permissions... |
|||
*/ |
|||
public function testDestroy() { |
|||
$user = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$user |
|||
->expects($this->once()) |
|||
->method('getUID') |
|||
->will($this->returnValue('Admin')); |
|||
$toDeleteUser = $this->getMockBuilder('\OC\User\User') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$toDeleteUser |
|||
->expects($this->once()) |
|||
->method('delete') |
|||
->will($this->returnValue(true)); |
|||
$this->container['UserSession'] |
|||
->method('getUser') |
|||
->will($this->returnValue($user)); |
|||
$this->container['UserManager'] |
|||
->method('get') |
|||
->with('UserToDelete') |
|||
->will($this->returnValue($toDeleteUser)); |
|||
|
|||
$expectedResponse = new DataResponse( |
|||
array( |
|||
'status' => 'success', |
|||
'data' => array( |
|||
'username' => 'UserToDelete' |
|||
) |
|||
) |
|||
); |
|||
$response = $this->usersController->destroy('UserToDelete'); |
|||
$this->assertEquals($expectedResponse, $response); |
|||
} |
|||
} |
|||
@ -0,0 +1,91 @@ |
|||
<?php |
|||
/** |
|||
* @author Lukas Reschke |
|||
* @copyright 2014 Lukas Reschke lukas@owncloud.com |
|||
* |
|||
* This file is licensed under the Affero General Public License version 3 or |
|||
* later. |
|||
* See the COPYING-README file. |
|||
*/ |
|||
|
|||
namespace OC\Settings\Middleware; |
|||
|
|||
use OC\AppFramework\Utility\ControllerMethodReflector; |
|||
use OCP\AppFramework\Controller; |
|||
use OCP\AppFramework\Http\TemplateResponse; |
|||
|
|||
/** |
|||
* Verifies whether an user has at least subadmin rights. |
|||
* To bypass use the `@NoSubadminRequired` annotation |
|||
* |
|||
* @package OC\Settings\Middleware |
|||
*/ |
|||
class SubadminMiddlewareTest extends \Test\TestCase { |
|||
/** @var SubadminMiddleware */ |
|||
private $subadminMiddlewareAsSubAdmin; |
|||
/** @var SubadminMiddleware */ |
|||
private $subadminMiddleware; |
|||
/** @var ControllerMethodReflector */ |
|||
private $reflector; |
|||
/** @var Controller */ |
|||
private $controller; |
|||
|
|||
protected function setUp() { |
|||
$this->reflector = $this->getMockBuilder('\OC\AppFramework\Utility\ControllerMethodReflector') |
|||
->disableOriginalConstructor()->getMock(); |
|||
$this->controller = $this->getMockBuilder('\OCP\AppFramework\Controller') |
|||
->disableOriginalConstructor()->getMock(); |
|||
|
|||
$this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true); |
|||
$this->subadminMiddleware = new SubadminMiddleware($this->reflector, false); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException \Exception |
|||
* @expectedExceptionMessage Logged in user must be a subadmin |
|||
*/ |
|||
public function testBeforeControllerAsUserWithExemption() { |
|||
$this->reflector |
|||
->expects($this->once()) |
|||
->method('hasAnnotation') |
|||
->with('NoSubadminRequired') |
|||
->will($this->returnValue(false)); |
|||
$this->subadminMiddleware->beforeController($this->controller, 'foo'); |
|||
} |
|||
|
|||
|
|||
public function testBeforeControllerAsUserWithoutExemption() { |
|||
$this->reflector |
|||
->expects($this->once()) |
|||
->method('hasAnnotation') |
|||
->with('NoSubadminRequired') |
|||
->will($this->returnValue(true)); |
|||
$this->subadminMiddleware->beforeController($this->controller, 'foo'); |
|||
} |
|||
|
|||
public function testBeforeControllerAsSubAdminWithoutExemption() { |
|||
$this->reflector |
|||
->expects($this->once()) |
|||
->method('hasAnnotation') |
|||
->with('NoSubadminRequired') |
|||
->will($this->returnValue(false)); |
|||
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); |
|||
} |
|||
|
|||
public function testBeforeControllerAsSubAdminWithExemption() { |
|||
$this->reflector |
|||
->expects($this->once()) |
|||
->method('hasAnnotation') |
|||
->with('NoSubadminRequired') |
|||
->will($this->returnValue(true)); |
|||
$this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo'); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
public function testAfterException() { |
|||
$expectedResponse = new TemplateResponse('core', '403', array(), 'guest'); |
|||
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new \Exception())); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue