Completed
Push — master ( e2a767...40afc9 )
by Henry
16:31 queued 08:12
created

includes/Admin/Controller/User.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Auth;
6
use Redaxscript\Filter;
7
use Redaxscript\Hash;
8
use Redaxscript\Validator;
9
use function json_encode;
10
11
/**
12
 * children class to process the admin user request
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category Controller
18
 * @author Henry Ruhs
19
 */
20
21
class User extends ControllerAbstract
22
{
23
	/**
24
	 * process the class
25
	 *
26
	 * @since 4.0.0
27
	 *
28
	 * @param string $action action to process
29
	 *
30
	 * @return string
31
	 */
32
33 14
	public function process(string $action = null) : string
34
	{
35 14
		$postArray = $this->_normalizePost($this->_sanitizePost());
36 14
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 35 can also be of type null; however, Redaxscript\Admin\Controller\User::_validatePost() does only seem to accept array, 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...
37 14
		$passwordHash = new Hash();
38 14
		$myId = (int)$this->_registry->get('myId');
39
40
		/* validate post */
41
42 14
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
		{
44 10
			return $this->_error(
45
			[
46 10
				'route' => $this->_getErrorRoute($postArray),
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 35 can also be of type null; however, Redaxscript\Admin\Contro...\User::_getErrorRoute() does only seem to accept array, 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...
47 10
				'message' => $validateArray
48
			]);
49
		}
50
51
		/* handle create */
52
53 4
		if ($action === 'create')
54
		{
55 1
			$passwordHash->init($postArray['password']);
56
			$createArray =
57
			[
58 1
				'name' => $postArray['name'],
59 1
				'user' => $postArray['user'],
60 1
				'description' => $postArray['description'],
61 1
				'password' => $passwordHash->getHash(),
62 1
				'email' => $postArray['email'],
63 1
				'language' => $postArray['language'],
64 1
				'status' => $postArray['status'],
65 1
				'groups' => $postArray['groups']
66
			];
67 1
			if ($this->_create($createArray))
68
			{
69 1
				return $this->_success(
70
				[
71 1
					'route' => $this->_getSuccessRoute($postArray),
72 1
					'timeout' => 2
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79 3
		if ($action === 'update')
80
		{
81
			$updateFullArray =
82
			[
83 2
				'name' => $postArray['name'],
84 2
				'description' => $postArray['description'],
85 2
				'email' => $postArray['email'],
86 2
				'language' => $postArray['language'],
87 2
				'status' => $postArray['status'],
88 2
				'groups' => $postArray['groups']
89
			];
90
			$updateLiteArray =
91
			[
92 2
				'name' => $postArray['name'],
93 2
				'description' => $postArray['description'],
94 2
				'email' => $postArray['email'],
95 2
				'language' => $postArray['language']
96
			];
97 2
			if ($postArray['password'])
98
			{
99 2
				$passwordHash->init($postArray['password']);
100 2
				$updateFullArray['password'] = $updateLiteArray['password'] = $passwordHash->getHash();
101
			}
102 2
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
103
			{
104 2
				if ($postArray['id'] === $myId)
105
				{
106 2
					$this->_refresh($postArray);
107
				}
108 2
				return $this->_success(
109
				[
110 2
					'route' => $this->_getSuccessRoute($postArray),
111 2
					'timeout' => 2
112
				]);
113
			}
114
		}
115
116
		/* handle error */
117
118 1
		return $this->_error(
119
		[
120 1
			'route' => $this->_getErrorRoute($postArray)
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 35 can also be of type null; however, Redaxscript\Admin\Contro...\User::_getErrorRoute() does only seem to accept array, 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...
121
		]);
122
	}
123
124
	/**
125
	 * sanitize the post
126
	 *
127
	 * @since 4.0.0
128
	 *
129
	 * @return array
130
	 */
131
132 14
	protected function _sanitizePost() : array
133
	{
134 14
		$emailFilter = new Filter\Email();
135 14
		$numberFilter = new Filter\Number();
136 14
		$passwordFilter = new Filter\Password();
137 14
		$specialFilter = new Filter\Special();
138 14
		$textFilter = new Filter\Text();
139 14
		$toggleFilter = new Filter\Toggle();
140 14
		$userFilter = new Filter\User();
141
142
		/* sanitize post */
143
144
		return
145
		[
146 14
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
0 ignored issues
show
It seems like $this->_request->getPost('id') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147 14
			'name' => $textFilter->sanitize($this->_request->getPost('name')),
0 ignored issues
show
It seems like $this->_request->getPost('name') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
148 14
			'user' => $userFilter->sanitize($this->_request->getPost('user')),
0 ignored issues
show
It seems like $this->_request->getPost('user') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\User::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
149 14
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
0 ignored issues
show
It seems like $this->_request->getPost('description') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
150 14
			'password' => $passwordFilter->sanitize($this->_request->getPost('password')),
0 ignored issues
show
It seems like $this->_request->getPost('password') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Password::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
151 14
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
It seems like $this->_request->getPost('email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
152 14
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
0 ignored issues
show
It seems like $this->_request->getPost('language') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
153 14
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
0 ignored issues
show
It seems like $this->_request->getPost('status') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
154 14
			'groups' => json_encode($this->_request->getPost('groups'))
155
		];
156
	}
157
158
	/**
159
	 * validate the post
160
	 *
161
	 * @since 4.0.0
162
	 *
163
	 * @param array $postArray array of the post
164
	 *
165
	 * @return array
166
	 */
167
168 14
	protected function _validatePost(array $postArray = []) : array
169
	{
170 14
		$nameValidator = new Validator\Name();
171 14
		$userValidator = new Validator\User();
172 14
		$passwordValidator = new Validator\Password();
173 14
		$emailValidator = new Validator\Email();
174 14
		$userModel = new Admin\Model\User();
175 14
		$validateArray = [];
176
177
		/* validate post */
178
179 14
		if (!$postArray['name'])
180
		{
181 9
			$validateArray[] = $this->_language->get('name_empty');
182
		}
183 5
		else if (!$nameValidator->validate($postArray['name']))
184
		{
185 1
			$validateArray[] = $this->_language->get('name_incorrect');
186
		}
187 14
		if (!$postArray['id'])
188
		{
189 9
			if (!$postArray['user'])
190
			{
191 5
				$validateArray[] = $this->_language->get('user_empty');
192
			}
193 4
			else if (!$userValidator->validate($postArray['user']))
194
			{
195 1
				$validateArray[] = $this->_language->get('user_incorrect');
196
			}
197 3
			else if ($userModel->getByUser($postArray['user']))
198
			{
199 1
				$validateArray[] = $this->_language->get('user_exists');
200
			}
201 9
			if (!$postArray['password'])
202
			{
203 5
				$validateArray[] = $this->_language->get('password_empty');
204
			}
205 4
			else if (!$passwordValidator->validate($postArray['password']))
206
			{
207 9
				$validateArray[] = $this->_language->get('password_incorrect');
208
			}
209
		}
210 5
		else if ($postArray['password'] && !$passwordValidator->validate($postArray['password']))
211
		{
212 1
			$validateArray[] = $this->_language->get('password_incorrect');
213
		}
214 14
		if (!$emailValidator->validate($postArray['email']))
215
		{
216 10
			$validateArray[] = $this->_language->get('email_incorrect');
217
		}
218 14
		return $validateArray;
219
	}
220
221
	/**
222
	 * create the user
223
	 *
224
	 * @since 4.0.0
225
	 *
226
	 * @param array $createArray array of the create
227
	 *
228
	 * @return bool
229
	 */
230
231 1
	protected function _create(array $createArray = []) : bool
232
	{
233 1
		$userModel = new Admin\Model\User();
234 1
		return $userModel->createByArray($createArray);
235
	}
236
237
	/**
238
	 * update the user
239
	 *
240
	 * @since 4.0.0
241
	 *
242
	 * @param int $userId identifier of the user
243
	 * @param array $updateArray array of the update
244
	 *
245
	 * @return bool
246
	 */
247
248 2
	protected function _update(int $userId = null, array $updateArray = []) : bool
249
	{
250 2
		$userModel = new Admin\Model\User();
251 2
		return $userModel->updateByIdAndArray($userId, $updateArray);
252
	}
253
254
	/**
255
	 * refresh the auth
256
	 *
257
	 * @since 4.0.0
258
	 *
259
	 * @param array $refreshArray array of the update
260
	 */
261
262 2
	protected function _refresh(array $refreshArray = []) : void
263
	{
264 2
		$auth = new Auth($this->_request);
265 2
		$auth->init();
266 2
		$auth->setUser('name', $refreshArray['name']);
267 2
		$auth->setUser('email', $refreshArray['email']);
268 2
		$auth->setUser('language', $refreshArray['language']);
269 2
		$auth->save();
270 2
	}
271
272
	/**
273
	 * get success route
274
	 *
275
	 * @since 4.0.0
276
	 *
277
	 * @param array $postArray array of the post
278
	 *
279
	 * @return string
280
	 */
281
282 3
	protected function _getSuccessRoute(array $postArray = []) : string
283
	{
284 3
		if ($this->_registry->get('usersEdit') && $postArray['id'])
285
		{
286 1
			return 'admin/view/users#row-' . $postArray['id'];
287
		}
288 2
		if ($this->_registry->get('usersEdit') && $postArray['user'])
289
		{
290 1
			$userModel = new Admin\Model\User();
291 1
			return 'admin/view/users#row-' . $userModel->getByUser($postArray['user'])->id;
292
		}
293 1
		return 'admin';
294
	}
295
296
	/**
297
	 * get error route
298
	 *
299
	 * @since 4.0.0
300
	 *
301
	 * @param array $postArray array of the post
302
	 *
303
	 * @return string
304
	 */
305
306 11
	protected function _getErrorRoute(array $postArray = []) : string
307
	{
308 11
		if ($this->_registry->get('usersEdit') && $postArray['id'])
309
		{
310 1
			return 'admin/edit/users/' . $postArray['id'];
311
		}
312 10
		if ($this->_registry->get('usersNew'))
313
		{
314 7
			return 'admin/new/users';
315
		}
316 3
		return 'admin';
317
	}
318
}
319