Skip to content

Commit

Permalink
Add occ commands to enable and disable a user + a disabled user can n…
Browse files Browse the repository at this point in the history
…o longer login - fixes #23838
  • Loading branch information
DeepDiver1975 committed Apr 12, 2016
1 parent 276b8a5 commit e0e2110
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 15 deletions.
64 changes: 64 additions & 0 deletions core/Command/User/Disable.php
Original file line number Diff line number Diff line change
@@ -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>');
}
}
64 changes: 64 additions & 0 deletions core/Command/User/Enable.php
Original file line number Diff line number Diff line change
@@ -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>');
}
}
2 changes: 2 additions & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@

$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()));
Expand Down
14 changes: 6 additions & 8 deletions lib/private/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static function getUserSession() {
return OC::$server->getUserSession();
}

private static $_backends = array();

private static $_usedBackends = array();

private static $_setupedBackends = array();
Expand Down Expand Up @@ -105,7 +103,7 @@ public static function useBackend($backend = 'database') {
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;
Expand Down Expand Up @@ -175,6 +173,7 @@ public static function login($loginname, $password) {
/**
* 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
Expand Down Expand Up @@ -237,6 +236,8 @@ public static function handleApacheAuth() {

/**
* Sets user id for session and triggers emit
*
* @param string $uid
*/
public static function setUserId($uid) {
$userSession = \OC::$server->getUserSession();
Expand Down Expand Up @@ -301,14 +302,11 @@ public static function tryBasicAuthLogin() {
/**
* 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();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/private/user/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace OC\User;

use OC\Hooks\Emitter;
use OCP\IUser;
use OCP\IUserSession;

/**
Expand Down Expand Up @@ -170,7 +171,12 @@ public function getUser() {
* @return bool if logged in
*/
public function isLoggedIn() {
return $this->getUser() !== null;
$user = $this->getUser();
if (is_null($user)) {
return false;
}

return $user->isEnabled();
}

/**
Expand Down Expand Up @@ -229,7 +235,7 @@ public function login($uid, $password) {
throw new LoginException('Login canceled by app');
}
} else {
return false;
throw new LoginException('User disabled');
}
}
} else {
Expand Down
1 change: 0 additions & 1 deletion lib/private/user/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,4 @@ public function triggerChange($feature, $value = null) {
$this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value));
}
}

}
1 change: 1 addition & 0 deletions lib/public/iuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @since 8.0.0
*/
interface IUser {

/**
* get the user id
*
Expand Down
Loading

0 comments on commit e0e2110

Please sign in to comment.