diff options
-rw-r--r-- | core/Command/User/Disable.php | 64 | ||||
-rw-r--r-- | core/Command/User/Enable.php | 64 | ||||
-rw-r--r-- | core/register_command.php | 2 | ||||
-rw-r--r-- | lib/private/legacy/user.php | 14 | ||||
-rw-r--r-- | lib/private/user/session.php | 46 | ||||
-rw-r--r-- | lib/private/user/user.php | 1 | ||||
-rw-r--r-- | lib/public/iuser.php | 1 | ||||
-rw-r--r-- | tests/lib/user/session.php | 38 |
8 files changed, 196 insertions, 34 deletions
diff --git a/core/Command/User/Disable.php b/core/Command/User/Disable.php new file mode 100644 index 00000000000..018f11190d6 --- /dev/null +++ b/core/Command/User/Disable.php @@ -0,0 +1,64 @@ +<?php +/** + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputArgument; + +class Disable extends Command { + /** @var IUserManager */ + protected $userManager; + + /** + * @param IUserManager $userManager + */ + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:disable') + ->setDescription('disables the specified user') + ->addArgument( + 'uid', + InputArgument::REQUIRED, + 'the username' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $this->userManager->get($input->getArgument('uid')); + if (is_null($user)) { + $output->writeln('<error>User does not exist</error>'); + return; + } + + $user->setEnabled(false); + $output->writeln('<info>The specified user is disabled</info>'); + } +} diff --git a/core/Command/User/Enable.php b/core/Command/User/Enable.php new file mode 100644 index 00000000000..ffe2e40d654 --- /dev/null +++ b/core/Command/User/Enable.php @@ -0,0 +1,64 @@ +<?php +/** + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Core\Command\User; + +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Input\InputArgument; + +class Enable extends Command { + /** @var IUserManager */ + protected $userManager; + + /** + * @param IUserManager $userManager + */ + public function __construct(IUserManager $userManager) { + $this->userManager = $userManager; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('user:enable') + ->setDescription('enables the specified user') + ->addArgument( + 'uid', + InputArgument::REQUIRED, + 'the username' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $this->userManager->get($input->getArgument('uid')); + if (is_null($user)) { + $output->writeln('<error>User does not exist</error>'); + return; + } + + $user->setEnabled(true); + $output->writeln('<info>The specified user is enabled</info>'); + } +} diff --git a/core/register_command.php b/core/register_command.php index 798497d97d2..3dab37ce5a6 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -121,6 +121,8 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); + $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); + $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager())); $application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager())); $application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager())); $application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager())); diff --git a/lib/private/legacy/user.php b/lib/private/legacy/user.php index 11c35daa0de..34ce68e989f 100644 --- a/lib/private/legacy/user.php +++ b/lib/private/legacy/user.php @@ -63,8 +63,6 @@ class OC_User { return OC::$server->getUserSession(); } - private static $_backends = array(); - private static $_usedBackends = array(); private static $_setupedBackends = array(); @@ -105,7 +103,7 @@ class OC_User { break; default: \OCP\Util::writeLog('core', 'Adding default user backend ' . $backend . '.', \OCP\Util::DEBUG); - $className = 'OC_USER_' . strToUpper($backend); + $className = 'OC_USER_' . strtoupper($backend); self::$_usedBackends[$backend] = new $className(); \OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]); break; @@ -175,6 +173,7 @@ class OC_User { /** * Try to login a user using the magic cookie (remember login) * + * @deprecated use \OCP\IUserSession::loginWithCookie() * @param string $uid The username of the user to log in * @param string $token * @return bool @@ -241,6 +240,8 @@ class OC_User { /** * Sets user id for session and triggers emit + * + * @param string $uid */ public static function setUserId($uid) { $userSession = \OC::$server->getUserSession(); @@ -296,14 +297,11 @@ class OC_User { /** * Check if the user is logged in, considers also the HTTP basic credentials * + * @deprecated use \OC::$server->getUserSession()->isLoggedIn() * @return bool */ public static function isLoggedIn() { - if (\OC::$server->getSession()->get('user_id') !== null && self::$incognitoMode === false) { - return self::userExists(\OC::$server->getSession()->get('user_id')); - } - - return false; + return \OC::$server->getUserSession()->isLoggedIn(); } /** diff --git a/lib/private/user/session.php b/lib/private/user/session.php index 5402c5cf74f..c7f8a6920de 100644 --- a/lib/private/user/session.php +++ b/lib/private/user/session.php @@ -32,6 +32,8 @@ namespace OC\User; use OC\Hooks\Emitter; +use OCP\ISession; +use OCP\IUserManager; use OCP\IUserSession; /** @@ -53,26 +55,20 @@ use OCP\IUserSession; * @package OC\User */ class Session implements IUserSession, Emitter { - /** - * @var \OC\User\Manager $manager - */ + /** @var \OC\User\Manager $manager */ private $manager; - /** - * @var \OC\Session\Session $session - */ + /** @var \OC\Session\Session $session */ private $session; - /** - * @var \OC\User\User $activeUser - */ + /** @var \OC\User\User $activeUser */ protected $activeUser; /** - * @param \OCP\IUserManager $manager - * @param \OCP\ISession $session + * @param IUserManager $manager + * @param ISession $session */ - public function __construct(\OCP\IUserManager $manager, \OCP\ISession $session) { + public function __construct(IUserManager $manager, ISession $session) { $this->manager = $manager; $this->session = $session; } @@ -107,7 +103,7 @@ class Session implements IUserSession, Emitter { /** * get the session object * - * @return \OCP\ISession + * @return ISession */ public function getSession() { return $this->session; @@ -116,10 +112,10 @@ class Session implements IUserSession, Emitter { /** * set the session object * - * @param \OCP\ISession $session + * @param ISession $session */ - public function setSession(\OCP\ISession $session) { - if ($this->session instanceof \OCP\ISession) { + public function setSession(ISession $session) { + if ($this->session instanceof ISession) { $this->session->close(); } $this->session = $session; @@ -170,7 +166,12 @@ class Session implements IUserSession, Emitter { * @return bool if logged in */ public function isLoggedIn() { - return $this->getUser() !== null; + $user = $this->getUser(); + if (is_null($user)) { + return false; + } + + return $user->isEnabled(); } /** @@ -226,15 +227,18 @@ class Session implements IUserSession, Emitter { if ($this->isLoggedIn()) { return true; } else { - throw new LoginException('Login canceled by app'); + // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory + $message = \OC::$server->getL10N('lib')->t('Login canceled by app'); + throw new LoginException($message); } } else { - return false; + // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory + $message = \OC::$server->getL10N('lib')->t('User disabled'); + throw new LoginException($message); } } - } else { - return false; } + return false; } /** diff --git a/lib/private/user/user.php b/lib/private/user/user.php index 3199790dba0..36680436769 100644 --- a/lib/private/user/user.php +++ b/lib/private/user/user.php @@ -417,5 +417,4 @@ class User implements IUser { $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value)); } } - } diff --git a/lib/public/iuser.php b/lib/public/iuser.php index b0dd8dc35b2..16617a2f2f6 100644 --- a/lib/public/iuser.php +++ b/lib/public/iuser.php @@ -32,6 +32,7 @@ namespace OCP; * @since 8.0.0 */ interface IUser { + /** * get the user id * diff --git a/tests/lib/user/session.php b/tests/lib/user/session.php index 1c042dec9f0..5a8ea57cb86 100644 --- a/tests/lib/user/session.php +++ b/tests/lib/user/session.php @@ -11,19 +11,25 @@ namespace Test\User; use OC\Session\Memory; use OC\User\User; +use OCP\ISession; +use OCP\IUserManager; +use OCP\UserInterface; +use Test\TestCase; /** * @group DB * @package Test\User */ -class Session extends \Test\TestCase { +class Session extends TestCase { public function testGetUser() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->once()) ->method('get') ->with('user_id') ->will($this->returnValue('foo')); + /** @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend */ $backend = $this->getMock('\Test\Util\User\Dummy'); $backend->expects($this->once()) ->method('userExists') @@ -39,12 +45,14 @@ class Session extends \Test\TestCase { } public function testIsLoggedIn() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->once()) ->method('get') ->with('user_id') ->will($this->returnValue('foo')); + /** @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend */ $backend = $this->getMock('\Test\Util\User\Dummy'); $backend->expects($this->once()) ->method('userExists') @@ -60,12 +68,14 @@ class Session extends \Test\TestCase { } public function testNotLoggedIn() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->once()) ->method('get') ->with('user_id') ->will($this->returnValue(null)); + /** @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend */ $backend = $this->getMock('\Test\Util\User\Dummy'); $backend->expects($this->never()) ->method('userExists'); @@ -79,15 +89,18 @@ class Session extends \Test\TestCase { } public function testSetUser() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->once()) ->method('set') ->with('user_id', 'foo'); + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager'); $backend = $this->getMock('\Test\Util\User\Dummy'); + /** @var User | \PHPUnit_Framework_MockObject_MockObject $user */ $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); $user->expects($this->once()) ->method('getUID') @@ -98,6 +111,7 @@ class Session extends \Test\TestCase { } public function testLoginValidPasswordEnabled() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->once()) ->method('regenerateId'); @@ -126,12 +140,13 @@ class Session extends \Test\TestCase { unset($managerMethods[$i]); } } + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('\Test\Util\User\Dummy'); $user = $this->getMock('\OC\User\User', array(), array('foo', $backend)); - $user->expects($this->once()) + $user->expects($this->exactly(2)) ->method('isEnabled') ->will($this->returnValue(true)); $user->expects($this->any()) @@ -150,7 +165,12 @@ class Session extends \Test\TestCase { $this->assertEquals($user, $userSession->getUser()); } + /** + * @expectedException \OC\User\LoginException + * @expectedExceptionMessage User disabled + */ public function testLoginValidPasswordDisabled() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->never()) ->method('set'); @@ -167,6 +187,7 @@ class Session extends \Test\TestCase { unset($managerMethods[$i]); } } + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('\Test\Util\User\Dummy'); @@ -188,6 +209,7 @@ class Session extends \Test\TestCase { } public function testLoginInvalidPassword() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->never()) ->method('set'); @@ -204,6 +226,7 @@ class Session extends \Test\TestCase { unset($managerMethods[$i]); } } + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('\Test\Util\User\Dummy'); @@ -224,16 +247,16 @@ class Session extends \Test\TestCase { } public function testLoginNonExisting() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->never()) ->method('set'); $session->expects($this->once()) ->method('regenerateId'); + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager'); - $backend = $this->getMock('\Test\Util\User\Dummy'); - $manager->expects($this->once()) ->method('checkPassword') ->with('foo', 'bar') @@ -244,6 +267,7 @@ class Session extends \Test\TestCase { } public function testRememberLoginValidToken() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->exactly(1)) ->method('set') @@ -290,6 +314,7 @@ class Session extends \Test\TestCase { $token = 'goodToken'; \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); + /** @var \OC\User\Session $userSession */ $userSession = $this->getMock( '\OC\User\Session', //override, otherwise tests will fail because of setcookie() @@ -303,6 +328,7 @@ class Session extends \Test\TestCase { } public function testRememberLoginInvalidToken() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->never()) ->method('set'); @@ -319,6 +345,7 @@ class Session extends \Test\TestCase { unset($managerMethods[$i]); } } + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('\Test\Util\User\Dummy'); @@ -347,6 +374,7 @@ class Session extends \Test\TestCase { } public function testRememberLoginInvalidUser() { + /** @var ISession | \PHPUnit_Framework_MockObject_MockObject $session */ $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->never()) ->method('set'); @@ -363,6 +391,7 @@ class Session extends \Test\TestCase { unset($managerMethods[$i]); } } + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMock('\OC\User\Manager', $managerMethods, array()); $backend = $this->getMock('\Test\Util\User\Dummy'); @@ -395,6 +424,7 @@ class Session extends \Test\TestCase { 'bar' => new User('bar', null) ); + /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $manager */ $manager = $this->getMockBuilder('\OC\User\Manager') ->disableOriginalConstructor() ->getMock(); |