Completed
Push — master ( 1b8bd1...a662d7 )
by Henry
06:59
created

User::_refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Auth;
6
use Redaxscript\Hash;
7
use Redaxscript\Filter;
8
use Redaxscript\Validator;
9
10
/**
11
 * children class to process the admin user request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class User extends ControllerAbstract
21
{
22
	/**
23
	 * process the class
24
	 *
25
	 * @since 4.0.0
26
	 *
27
	 * @param string $action action to process
28
	 *
29
	 * @return string
30
	 */
31
32
	public function process(string $action = null) : string
33
	{
34
		$postArray = $this->_normalizePost($this->_sanitizePost());
35
		$validateArray = $this->_validatePost($postArray);
36
		$passwordHash = new Hash();
37
		$myId = $this->_registry->get('myId');
38
39
		/* validate post */
40
41
		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...
42
		{
43
			return $this->_error(
44
			[
45
				'route' => $this->_getErrorRoute($postArray),
46
				'message' => $validateArray
47
			]);
48
		}
49
50
		/* handle create */
51
52
		if ($action === 'create')
53
		{
54
			$passwordHash->init($postArray['password']);
55
			$createArray =
56
			[
57
				'name' => $postArray['name'],
58
				'user' => $postArray['user'],
59
				'description' => $postArray['description'],
60
				'password' => $passwordHash->getHash(),
61
				'email' => $postArray['email'],
62
				'language' => $postArray['language'],
63
				'status' => $postArray['status'],
64
				'groups' => $postArray['groups']
65
			];
66
			if ($this->_create($createArray))
67
			{
68
				return $this->_success(
69
				[
70
					'route' => $this->_getSuccessRoute($postArray),
71
					'timeout' => 2
72
				]);
73
			}
74
		}
75
76
		/* handle update */
77
78
		if ($action === 'update')
79
		{
80
			$updateFullArray =
81
			[
82
				'name' => $postArray['name'],
83
				'description' => $postArray['description'],
84
				'email' => $postArray['email'],
85
				'language' => $postArray['language'],
86
				'status' => $postArray['status'],
87
				'groups' => $postArray['groups']
88
			];
89
			$updateLiteArray =
90
			[
91
				'name' => $postArray['name'],
92
				'description' => $postArray['description'],
93
				'email' => $postArray['email'],
94
				'language' => $postArray['language']
95
			];
96
			if ($postArray['password'])
97
			{
98
				$passwordHash->init($postArray['password']);
99
				$updateFullArray['password'] = $updateLiteArray['password'] = $passwordHash->getHash();
100
			}
101
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
102
			{
103
				if ($postArray['id'] === $myId)
104
				{
105
					$this->_refresh($postArray);
106
				}
107
				return $this->_success(
108
				[
109
					'route' => $this->_getSuccessRoute($postArray),
110
					'timeout' => 2
111
				]);
112
			}
113
		}
114
115
		/* handle error */
116
117
		return $this->_error(
118
		[
119
			'route' => $this->_getErrorRoute($postArray)
120
		]);
121
	}
122
123
	/**
124
	 * sanitize the post
125
	 *
126
	 * @since 4.0.0
127
	 *
128
	 * @return array
129
	 */
130
131
	protected function _sanitizePost() : array
132
	{
133
		$specialFilter = new Filter\Special();
134
		$emailFilter = new Filter\Email();
135
136
		/* sanitize post */
137
138
		return
139
		[
140
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
141
			'name' => $this->_request->getPost('name'),
142
			'user' => $this->_request->getPost('user'),
143
			'description' => $this->_request->getPost('description'),
144
			'password' => $this->_request->getPost('password'),
145
			'password_confirm' => $this->_request->getPost('password_confirm'),
146
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
147
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
148
			'status' => $specialFilter->sanitize($this->_request->getPost('status')),
149
			'groups' => json_encode($this->_request->getPost('groups'))
150
		];
151
	}
152
153
	/**
154
	 * validate the post
155
	 *
156
	 * @since 4.0.0
157
	 *
158
	 * @param array $postArray array of the post
159
	 *
160
	 * @return array
161
	 */
162
163
	protected function _validatePost(array $postArray = []) : array
164
	{
165
		$loginValidator = new Validator\Login();
166
		$emailValidator = new Validator\Email();
167
		$userModel = new Admin\Model\User();
168
		$validateArray = [];
169
170
		/* validate post */
171
172
		if (!$postArray['name'])
173
		{
174
			$validateArray[] = $this->_language->get('name_empty');
175
		}
176
		if (!$postArray['id'])
177
        {
178
			if (!$postArray['user'])
179
			{
180
				$validateArray[] = $this->_language->get('user_empty');
181
			}
182
			else if (!$loginValidator->validate($postArray['user']))
183
			{
184
				$validateArray[] = $this->_language->get('user_incorrect');
185
			}
186
			else if ($userModel->getByUser($postArray['user'])->id !== $userModel->getById($postArray['id'])->id)
187
			{
188
				$validateArray[] = $this->_language->get('user_exists');
189
			}
190
			if (!$postArray['password'])
191
			{
192
				$validateArray[] = $this->_language->get('password_empty');
193
			}
194
			else if (!$loginValidator->validate($postArray['password']))
195
			{
196
				$validateArray[] = $this->_language->get('password_incorrect');
197
			}
198
			else if ($postArray['password'] !== $postArray['password_confirm'])
199
			{
200
				$validateArray[] = $this->_language->get('password_mismatch');
201
			}
202
		}
203
		else if ($postArray['password'])
204
		{
205
			if (!$loginValidator->validate($postArray['password']))
206
			{
207
				$validateArray[] = $this->_language->get('password_incorrect');
208
			}
209
			else if ($postArray['password'] !== $postArray['password_confirm'])
210
			{
211
				$validateArray[] = $this->_language->get('password_mismatch');
212
			}
213
		}
214
		if (!$emailValidator->validate($postArray['email']))
215
		{
216
			$validateArray[] = $this->_language->get('email_incorrect');
217
		}
218
		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
	protected function _create(array $createArray = []) : bool
232
	{
233
		$userModel = new Admin\Model\User();
234
		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
	public function _update(int $userId = null, array $updateArray = []) : bool
249
	{
250
		$userModel = new Admin\Model\User();
251
		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
	public function _refresh(array $refreshArray = [])
263
	{
264
		$auth = new Auth($this->_request);
265
		$auth->init();
266
		$auth->setUser('name', $refreshArray['name']);
267
		$auth->setUser('email', $refreshArray['email']);
268
		$auth->setUser('language', $refreshArray['language']);
269
		$auth->save();
270
	}
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
	protected function _getSuccessRoute(array $postArray = []) : string
283
	{
284
		if ($this->_registry->get('usersEdit') && $postArray['id'])
285
		{
286
			return 'admin/view/users#row-' . $postArray['id'];
287
		}
288
		if ($this->_registry->get('usersEdit') && $postArray['user'])
289
		{
290
			$userModel = new Admin\Model\User();
291
			return 'admin/view/users#row-' . $userModel->getByUser($postArray['user'])->id;
292
		}
293
		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
	protected function _getErrorRoute(array $postArray = []) : string
307
	{
308
		if ($this->_registry->get('usersEdit') && $postArray['id'])
309
		{
310
			return 'admin/edit/users/' . $postArray['id'];
311
		}
312
		if ($this->_registry->get('usersNew'))
313
		{
314
			return 'admin/new/users';
315
		}
316
		return 'admin';
317
	}
318
}
319