Completed
Push — stable9 ( 212ff8...07c437 )
by Morris
26:13 queued 09:36
created

Controller   B

Complexity

Total Complexity 28

Size/Duplication

Total Lines 136
Duplicated Lines 8.09 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 136
rs 7.8571
wmc 28
lcom 1
cbo 17

2 Methods

Rating   Name   Duplication   Size   Complexity  
C changePersonalPassword() 0 25 7
F changeUserPassword() 11 108 21

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Björn Schießle <[email protected]>
5
 * @author Christopher Schäpers <[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
 *
15
 * @copyright Copyright (c) 2016, ownCloud, Inc.
16
 * @license AGPL-3.0
17
 *
18
 * This code is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License, version 3,
20
 * as published by the Free Software Foundation.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License, version 3,
28
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
29
 *
30
 */
31
namespace OC\Settings\ChangePassword;
32
33
use OC\HintException;
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();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::callCheck() has been deprecated with message: Use annotation based CSRF checks from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
39
		\OC_JSON::checkLoggedIn();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::checkLoggedIn() has been deprecated with message: Use annotation based ACLs from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
41
		$username = \OC_User::getUser();
42
		$password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null;
43
		$oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : '';
44
		$l = new \OC_L10n('settings');
0 ignored issues
show
Deprecated Code introduced by
The class OC_L10N has been deprecated with message: 9.0.0 Use \OC::$server->getL10NFactory()->get() instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
45
46
		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...
47
			\OC_JSON::error(array("data" => array("message" => $l->t("Wrong password")) ));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48
			exit();
49
		}
50
51
		try {
52
			if (!is_null($password) && \OC_User::setPassword($username, $password)) {
53
				\OC_JSON::success(['data' => ['message' => $l->t('Saved')]]);
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::success() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
			} else {
55
				\OC_JSON::error();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
			}
57
		} catch (HintException $e) {
58
			\OC_JSON::error(['data' => ['message' => $e->getHint()]]);
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
		}
60
	}
61
62
	public static function changeUserPassword($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...
63
		// Check if we are an user
64
		\OC_JSON::callCheck();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::callCheck() has been deprecated with message: Use annotation based CSRF checks from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
		\OC_JSON::checkLoggedIn();
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::checkLoggedIn() has been deprecated with message: Use annotation based ACLs from the AppFramework instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
66
67
		$l = new \OC_L10n('settings');
0 ignored issues
show
Deprecated Code introduced by
The class OC_L10N has been deprecated with message: 9.0.0 Use \OC::$server->getL10NFactory()->get() instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
68
		if (isset($_POST['username'])) {
69
			$username = $_POST['username'];
70
		} else {
71
			\OC_JSON::error(array('data' => array('message' => $l->t('No user supplied')) ));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
72
			exit();
73
		}
74
75
		$password = isset($_POST['password']) ? $_POST['password'] : null;
76
		$recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null;
77
78
		$isUserAccessible = false;
79
		$currentUserObject = \OC::$server->getUserSession()->getUser();
80
		$targetUserObject = \OC::$server->getUserManager()->get($username);
81 View Code Duplication
		if($currentUserObject !== null && $targetUserObject !== null) {
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...
82
			$isUserAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isUserAccessible($currentUserObject, $targetUserObject);
83
		}
84
85
		if (\OC_User::isAdminUser(\OC_User::getUser())) {
86
			$userstatus = 'admin';
0 ignored issues
show
Unused Code introduced by
$userstatus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
		} elseif ($isUserAccessible) {
88
			$userstatus = 'subadmin';
0 ignored issues
show
Unused Code introduced by
$userstatus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
		} else {
90
			\OC_JSON::error(array('data' => array('message' => $l->t('Authentication error')) ));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91
			exit();
92
		}
93
94
		if (\OC_App::isEnabled('encryption')) {
95
			//handle the recovery case
96
			$crypt = new \OCA\Encryption\Crypto\Crypt(
97
				\OC::$server->getLogger(),
98
				\OC::$server->getUserSession(),
99
				\OC::$server->getConfig(),
100
				\OC::$server->getL10N('encryption'));
101
			$keyStorage = \OC::$server->getEncryptionKeyStorage();
102
			$util = new \OCA\Encryption\Util(
103
				new \OC\Files\View(),
104
				$crypt,
105
				\OC::$server->getLogger(),
106
				\OC::$server->getUserSession(),
107
				\OC::$server->getConfig(),
108
				\OC::$server->getUserManager());
109
			$keyManager = new \OCA\Encryption\KeyManager(
110
				$keyStorage,
111
				$crypt,
112
				\OC::$server->getConfig(),
113
				\OC::$server->getUserSession(),
114
				new \OCA\Encryption\Session(\OC::$server->getSession()),
115
				\OC::$server->getLogger(),
116
				$util);
117
			$recovery = new \OCA\Encryption\Recovery(
118
				\OC::$server->getUserSession(),
119
				$crypt,
120
				\OC::$server->getSecureRandom(),
121
				$keyManager,
122
				\OC::$server->getConfig(),
123
				$keyStorage,
124
				\OC::$server->getEncryptionFilesHelper(),
125
				new \OC\Files\View());
126
			$recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled();
127
128
			$validRecoveryPassword = false;
129
			$recoveryEnabledForUser = false;
130
			if ($recoveryAdminEnabled) {
131
				$validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword);
132
				$recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username);
133
			}
134
135
			if ($recoveryEnabledForUser && $recoveryPassword === '') {
136
				\OC_JSON::error(array('data' => array(
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
137
					'message' => $l->t('Please provide an admin recovery password, otherwise all user data will be lost')
138
				)));
139 View Code Duplication
			} elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
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...
140
				\OC_JSON::error(array('data' => array(
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
141
					'message' => $l->t('Wrong admin recovery password. Please check the password and try again.')
142
				)));
143
			} else { // now we know that everything is fine regarding the recovery password, let's try to change the password
144
				$result = \OC_User::setPassword($username, $password, $recoveryPassword);
145
				if (!$result && $recoveryEnabledForUser) {
146
					\OC_JSON::error(array(
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
147
						"data" => array(
148
							"message" => $l->t("Backend doesn't support password change, but the user's encryption key was successfully updated.")
149
						)
150
					));
151 View Code Duplication
				} elseif (!$result && !$recoveryEnabledForUser) {
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...
152
					\OC_JSON::error(array("data" => array( "message" => $l->t("Unable to change password" ) )));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
				} else {
154
					\OC_JSON::success(array("data" => array("username" => $username, 'message' => $l->t('Saved'))));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::success() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
155
				}
156
157
			}
158
		} else { // if encryption is disabled, proceed
159
			try {
160
				if (!is_null($password) && \OC_User::setPassword($username, $password)) {
161
					\OC_JSON::success(array('data' => array('username' => $username, 'message' => $l->t('Saved'))));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::success() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
162
				} else {
163
					\OC_JSON::error(array('data' => array('message' => $l->t('Unable to change password'))));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
164
				}
165
			} catch (HintException $e) {
166
				\OC_JSON::error(array('data' => array('message' => $e->getHint())));
0 ignored issues
show
Deprecated Code introduced by
The method OC_JSON::error() has been deprecated with message: Use a AppFramework JSONResponse instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
167
			}
168
		}
169
	}
170
}
171