Move login form into controller
First step on getting the authorisation stuff cleaned up. This is only for the login form, all other stuff is still where it is.remotes/origin/index-with-prefix
parent
63a385d2b8
commit
331e4efacb
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Lukas Reschke <lukas@owncloud.com>
|
||||
*
|
||||
* @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\Controller;
|
||||
|
||||
use OC\Setup;
|
||||
use OCP\AppFramework\Controller;
|
||||
use OCP\AppFramework\Http\RedirectResponse;
|
||||
use OCP\AppFramework\Http\TemplateResponse;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\ISession;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
|
||||
class LoginController extends Controller {
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
/** @var ISession */
|
||||
private $session;
|
||||
/** @var IUserSession */
|
||||
private $userSession;
|
||||
|
||||
/**
|
||||
* @param string $appName
|
||||
* @param IRequest $request
|
||||
* @param IUserManager $userManager
|
||||
* @param IConfig $config
|
||||
* @param ISession $session
|
||||
* @param IUserSession $userSession
|
||||
*/
|
||||
function __construct($appName,
|
||||
IRequest $request,
|
||||
IUserManager $userManager,
|
||||
IConfig $config,
|
||||
ISession $session,
|
||||
IUserSession $userSession) {
|
||||
parent::__construct($appName, $request);
|
||||
$this->userManager = $userManager;
|
||||
$this->config = $config;
|
||||
$this->session = $session;
|
||||
$this->userSession = $userSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* @PublicPage
|
||||
* @NoCSRFRequired
|
||||
* @UseSession
|
||||
*
|
||||
* @param string $user
|
||||
* @param string $redirect_url
|
||||
* @param string $remember_login
|
||||
*
|
||||
* @return TemplateResponse
|
||||
*/
|
||||
public function showLoginForm($user,
|
||||
$redirect_url,
|
||||
$remember_login) {
|
||||
if($this->userSession->isLoggedIn()) {
|
||||
return new RedirectResponse(\OC_Util::getDefaultPageUrl());
|
||||
}
|
||||
|
||||
$parameters = array();
|
||||
$loginMessages = $this->session->get('loginMessages');
|
||||
$errors = [];
|
||||
$messages = [];
|
||||
if(is_array($loginMessages)) {
|
||||
list($errors, $messages) = $loginMessages;
|
||||
}
|
||||
$this->session->remove('loginMessages');
|
||||
foreach ($errors as $value) {
|
||||
$parameters[$value] = true;
|
||||
}
|
||||
|
||||
$parameters['messages'] = $messages;
|
||||
if (!empty($user)) {
|
||||
$parameters['username'] = $user;
|
||||
$parameters['user_autofocus'] = false;
|
||||
} else {
|
||||
$parameters['username'] = '';
|
||||
$parameters['user_autofocus'] = true;
|
||||
}
|
||||
if (!empty($redirect_url)) {
|
||||
$parameters['redirect_url'] = $redirect_url;
|
||||
}
|
||||
|
||||
$parameters['canResetPassword'] = true;
|
||||
if (!$this->config->getSystemValue('lost_password_link')) {
|
||||
if (!empty($user)) {
|
||||
$userObj = $this->userManager->get($user);
|
||||
if ($userObj instanceof IUser) {
|
||||
$parameters['canResetPassword'] = $userObj->canChangePassword();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$parameters['alt_login'] = \OC_App::getAlternativeLogIns();
|
||||
$parameters['rememberLoginAllowed'] = \OC_Util::rememberLoginAllowed();
|
||||
$parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0;
|
||||
|
||||
if (!empty($user)) {
|
||||
$parameters['username'] = $user;
|
||||
$parameters['user_autofocus'] = false;
|
||||
} else {
|
||||
$parameters['username'] = '';
|
||||
$parameters['user_autofocus'] = true;
|
||||
}
|
||||
|
||||
return new TemplateResponse(
|
||||
$this->appName,
|
||||
'login',
|
||||
$parameters,
|
||||
'guest'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Lukas Reschke <lukas@owncloud.com>
|
||||
*
|
||||
* @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\Controller;
|
||||
|
||||
use OCP\AppFramework\Http\RedirectResponse;
|
||||
use OCP\AppFramework\Http\TemplateResponse;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\ISession;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
use Test\TestCase;
|
||||
|
||||
class LoginControllerTest extends TestCase {
|
||||
/** @var LoginController */
|
||||
private $loginController;
|
||||
/** @var IRequest */
|
||||
private $request;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
/** @var ISession */
|
||||
private $session;
|
||||
/** @var IUserSession */
|
||||
private $userSession;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->request = $this->getMock('\\OCP\\IRequest');
|
||||
$this->userManager = $this->getMock('\\OCP\\IUserManager');
|
||||
$this->config = $this->getMock('\\OCP\\IConfig');
|
||||
$this->session = $this->getMock('\\OCP\\ISession');
|
||||
$this->userSession = $this->getMock('\\OCP\\IUserSession');
|
||||
|
||||
$this->loginController = new LoginController(
|
||||
'core',
|
||||
$this->request,
|
||||
$this->userManager,
|
||||
$this->config,
|
||||
$this->session,
|
||||
$this->userSession
|
||||
);
|
||||
}
|
||||
|
||||
public function testShowLoginFormForLoggedInUsers() {
|
||||
$this->userSession
|
||||
->expects($this->once())
|
||||
->method('isLoggedIn')
|
||||
->willReturn(true);
|
||||
|
||||
$expectedResponse = new RedirectResponse(\OC_Util::getDefaultPageUrl());
|
||||
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
||||
}
|
||||
|
||||
public function testShowLoginFormWithErrorsInSession() {
|
||||
$this->userSession
|
||||
->expects($this->once())
|
||||
->method('isLoggedIn')
|
||||
->willReturn(false);
|
||||
$this->session
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with('loginMessages')
|
||||
->willReturn(
|
||||
[
|
||||
[
|
||||
'ErrorArray1',
|
||||
'ErrorArray2',
|
||||
],
|
||||
[
|
||||
'MessageArray1',
|
||||
'MessageArray2',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$expectedResponse = new TemplateResponse(
|
||||
'core',
|
||||
'login',
|
||||
[
|
||||
'ErrorArray1' => true,
|
||||
'ErrorArray2' => true,
|
||||
'messages' => [
|
||||
'MessageArray1',
|
||||
'MessageArray2',
|
||||
],
|
||||
'username' => '',
|
||||
'user_autofocus' => true,
|
||||
'canResetPassword' => true,
|
||||
'alt_login' => [],
|
||||
'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(),
|
||||
'rememberLoginState' => 0,
|
||||
],
|
||||
'guest'
|
||||
);
|
||||
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function passwordResetDataProvider() {
|
||||
return [
|
||||
[
|
||||
true,
|
||||
true,
|
||||
],
|
||||
[
|
||||
false,
|
||||
false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider passwordResetDataProvider
|
||||
*/
|
||||
public function testShowLoginFormWithPasswordResetOption($canChangePassword,
|
||||
$expectedResult) {
|
||||
$this->userSession
|
||||
->expects($this->once())
|
||||
->method('isLoggedIn')
|
||||
->willReturn(false);
|
||||
$this->config
|
||||
->expects($this->once())
|
||||
->method('getSystemValue')
|
||||
->with('lost_password_link')
|
||||
->willReturn(false);
|
||||
$user = $this->getMock('\\OCP\\IUser');
|
||||
$user
|
||||
->expects($this->once())
|
||||
->method('canChangePassword')
|
||||
->willReturn($canChangePassword);
|
||||
$this->userManager
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with('LdapUser')
|
||||
->willReturn($user);
|
||||
|
||||
$expectedResponse = new TemplateResponse(
|
||||
'core',
|
||||
'login',
|
||||
[
|
||||
'messages' => [],
|
||||
'username' => 'LdapUser',
|
||||
'user_autofocus' => false,
|
||||
'canResetPassword' => $expectedResult,
|
||||
'alt_login' => [],
|
||||
'rememberLoginAllowed' => \OC_Util::rememberLoginAllowed(),
|
||||
'rememberLoginState' => 0,
|
||||
],
|
||||
'guest'
|
||||
);
|
||||
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', '', ''));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue