Completed
Push — master ( 3d671c...42e805 )
by Blizzz
48:26 queued 33:21
created

RenewPasswordController::showRenewPasswordForm()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 33
Code Lines 23

Duplication

Lines 9
Ratio 27.27 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 13
nop 1
dl 9
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Roger Szabo <[email protected]>
4
 *
5
 * @author Roger Szabo <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\User_LDAP\Controller;
25
26
use OC\HintException;
27
use OC_Util;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\RedirectResponse;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\ISession;
35
use OCP\IURLGenerator;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
39
class RenewPasswordController extends Controller {
40
	/** @var IUserManager */
41
	private $userManager;
42
	/** @var IConfig */
43
	private $config;
44
	/** @var IL10N */
45
	protected $l10n;
46
	/** @var ISession */
47
	private $session;
48
	/** @var IURLGenerator */
49
	private $urlGenerator;
50
51
	/**
52
	 * @param string $appName
53
	 * @param IRequest $request
54
	 * @param IUserManager $userManager
55
	 * @param IConfig $config
56
	 * @param IURLGenerator $urlGenerator
57
	 */
58 View Code Duplication
	function __construct($appName, IRequest $request, IUserManager $userManager, 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
		IConfig $config, IL10N $l10n, ISession $session, IURLGenerator $urlGenerator) {
60
		parent::__construct($appName, $request);
61
		$this->userManager = $userManager;
62
		$this->config = $config;
63
		$this->l10n = $l10n;
64
		$this->session = $session;
65
		$this->urlGenerator = $urlGenerator;
66
	}
67
68
	/**
69
	 * @PublicPage
70
	 * @NoCSRFRequired
71
	 *
72
	 * @return RedirectResponse
73
	 */
74
	public function cancel() {
75
		return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
76
	}
77
78
	/**
79
	 * @PublicPage
80
	 * @NoCSRFRequired
81
	 * @UseSession
82
	 *
83
	 * @param string $user
84
	 *
85
	 * @return TemplateResponse|RedirectResponse
86
	 */
87
	public function showRenewPasswordForm($user) {
88 View Code Duplication
		if($this->config->getUserValue($user, 'user_ldap', 'needsPasswordReset') !== 'true') {
89
			return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
90
		}
91
		$parameters = [];
92
		$renewPasswordMessages = $this->session->get('renewPasswordMessages');
93
		$errors = [];
94
		$messages = [];
95
		if (is_array($renewPasswordMessages)) {
96
			list($errors, $messages) = $renewPasswordMessages;
97
		}
98
		$this->session->remove('renewPasswordMessages');
99
		foreach ($errors as $value) {
100
			$parameters[$value] = true;
101
		}
102
103
		$parameters['messages'] = $messages;
104
		$parameters['user'] = $user;
105
106
		$parameters['canResetPassword'] = true;
107
		$parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', '');
108 View Code Duplication
		if (!$parameters['resetPasswordLink']) {
109
			$userObj = $this->userManager->get($user);
110
			if ($userObj instanceof IUser) {
111
				$parameters['canResetPassword'] = $userObj->canChangePassword();
112
			}
113
		}
114
		$parameters['cancelLink'] = $this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm');
115
116
		return new TemplateResponse(
117
			$this->appName, 'renewpassword', $parameters, 'guest'
118
		);
119
	}
120
121
	/**
122
	 * @PublicPage
123
	 * @UseSession
124
	 *
125
	 * @param string $user
126
	 * @param string $oldPassword
127
	 * @param string $newPassword
128
	 *
129
	 * @return RedirectResponse
130
	 */
131
	public function tryRenewPassword($user, $oldPassword, $newPassword) {
132 View Code Duplication
		if($this->config->getUserValue($user, 'user_ldap', 'needsPasswordReset') !== 'true') {
133
			return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
134
		}
135
		$args = !is_null($user) ? ['user' => $user] : [];
136
		$loginResult = $this->userManager->checkPassword($user, $oldPassword);
137
		if ($loginResult === false) {
138
			$this->session->set('renewPasswordMessages', [
139
				['invalidpassword'], []
140
			]);
141
			return new RedirectResponse($this->urlGenerator->linkToRoute('user_ldap.renewPassword.showRenewPasswordForm', $args));
142
		}
143
		
144
		try {
145
			if (!is_null($newPassword) && \OC_User::setPassword($user, $newPassword)) {
146
				$this->session->set('loginMessages', [
147
					[], [$this->l10n->t("Please login with the new password")]
148
				]);
149
				$this->session->remove('needPasswordRenewal');
150
				return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
151
			} else {
152
				$this->session->set('renewPasswordMessages', [
153
					['internalexception'], []
154
				]);
155
			}
156
		} catch (HintException $e) {
157
			$this->session->set('renewPasswordMessages', [
158
				[], [$e->getHint()]
159
			]);
160
		}
161
162
		return new RedirectResponse($this->urlGenerator->linkToRoute('user_ldap.renewPassword.showRenewPasswordForm', $args));
163
	}
164
165
	/**
166
	 * @PublicPage
167
	 * @NoCSRFRequired
168
	 * @UseSession
169
	 *
170
	 * @return RedirectResponse
171
	 */
172
	public function showLoginFormInvalidPassword($user) {
173
		$args = !is_null($user) ? ['user' => $user] : [];
174
		$this->session->set('loginMessages', [
175
			['invalidpassword'], []
176
		]);
177
		return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
178
	}
179
180
}
181