Completed
Push — master ( e7a609...6d1b90 )
by Thomas
31:06 queued 17:33
created

Controller::changeUserPassword()   F

Complexity

Conditions 21
Paths 265

Size

Total Lines 110
Code Lines 86

Duplication

Lines 11
Ratio 10 %

Importance

Changes 0
Metric Value
cc 21
eloc 86
nc 265
nop 1
dl 11
loc 110
rs 3.6155
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Christopher Schäpers <[email protected]>
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Clark Tomlinson <[email protected]>
7
 * @author cmeh <[email protected]>
8
 * @author Florin Peter <[email protected]>
9
 * @author Jakob Sack <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Sam Tuke <[email protected]>
13
 * @author Thomas Müller <[email protected]>
14
 * @author Ujjwal Bhardwaj <[email protected]>
15
 * @author Yarno Boelens <[email protected]>
16
 *
17
 * @copyright Copyright (c) 2018, ownCloud GmbH
18
 * @license AGPL-3.0
19
 *
20
 * This code is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License, version 3,
22
 * as published by the Free Software Foundation.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
 * GNU Affero General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU Affero General Public License, version 3,
30
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
31
 *
32
 */
33
namespace OC\Settings\ChangePassword;
34
35
class Controller {
36
	public static function changePersonalPassword($args) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
		// Check if we are an user
38
		\OC_JSON::callCheck();
39
		\OC_JSON::checkLoggedIn();
40
41
		$username = \OC_User::getUser();
42
		$password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null;
43
		$oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : '';
44
45 View Code Duplication
		if (!\OC_User::checkPassword($username, $oldPassword)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \OC_User::checkPassword($username, $oldPassword) of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
46
			$l = \OC::$server->getL10NFactory()->get('settings');
47
			\OC_JSON::error(["data" => ["message" => $l->t("Wrong password")]]);
48
			exit();
49
		}
50 View Code Duplication
		if ($oldPassword === $password) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
			$l = \OC::$server->getL10NFactory()->get('settings');
52
			\OC_JSON::error(["data" => ["message" => $l->t("The new password can not be the same as the previous one")]]);
53
			exit();
54
	        }
55
		try {
56
			if (!is_null($password) && \OC_User::setPassword($username, $password)) {
57
				\OC::$server->getUserSession()->updateSessionTokenPassword($password);
58
59
				self::sendNotificationMail($username);
60
61
				\OC_JSON::success();
62
			} else {
63
				\OC_JSON::error();
64
			}
65
		} catch (\Exception $e) {
66
			\OC_JSON::error(['data' => ['message' => $e->getMessage()]]);
67
		}
68
	}
69
70
	private static function sendNotificationMail($username) {
71
		$userObject = \OC::$server->getUserManager()->get($username);
72
		$email = $userObject->getEMailAddress();
73
		$defaults = new \OC_Defaults();
74
		$from = \OCP\Util::getDefaultEmailAddress('lostpassword-noreply');
75
		$mailer = \OC::$server->getMailer();
76
		$lion = \OC::$server->getL10N('lib');
77
78
		if ($email !== null && $email !== '') {
79
			$tmpl = new \OC_Template('core', 'lostpassword/notify');
80
			$msg = $tmpl->fetchPage();
81
82
			try {
83
				$message = $mailer->createMessage();
84
				$message->setTo([$email => $username]);
85
				$message->setSubject($lion->t('%s password changed successfully', [$defaults->getName()]));
86
				$message->setPlainBody($msg);
0 ignored issues
show
Bug introduced by
It seems like $msg defined by $tmpl->fetchPage() on line 80 can also be of type boolean; however, OC\Mail\Message::setPlainBody() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
				$message->setFrom([$from => $defaults->getName()]);
88
				$mailer->send($message);
89
			} catch (\Exception $e) {
90
				throw new \Exception($lion->t(
91
					'Couldn\'t send reset email. Please contact your administrator.'
92
				));
93
			}
94
		}
95
	}
96
}
97