forked from techjoomla/plg_api_users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.php
More file actions
394 lines (329 loc) · 8.99 KB
/
user.php
File metadata and controls
394 lines (329 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* @package Com.Api
* @subpackage users
* @copyright Copyright (C) 2009-2017 Techjoomla, Techjoomla Pvt. Ltd. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access.
defined('_JEXEC') or die();
/**
* User Api.
* Creates a new user, updates an existing user and gets data of an user
*
* @package Com.Api
*
* @since 2.0
*/
class UsersApiResourceUser extends ApiResource
{
/**
* Function to create and edit user record.
*
* @return object|void User details on success. raise error on failure.
*
* @since 2.0
*/
public function post()
{
$app = JFactory::getApplication();
$params = JComponentHelper::getParams("com_users");
$formData = $app->input->getArray();
$userIdentifier = $app->input->get('id', 0, 'string');
$xIdentifier = $app->input->server->get('HTTP_X_IDENTIFIER');
$fIdentifier = $app->input->server->get('HTTP_FORCECREATE');
// Get current logged in user.
$me = JFactory::getUser();
// Check if $userIdentifier is not set - POST / CREATE user case
if (empty($userIdentifier))
{
// Validate required fields
if ($formData['username'] == '' || $formData['name'] == '' || $formData['email'] == '' || $formData['password'] == '')
{
ApiError::raiseError(400, JText::_('PLG_API_USERS_REQUIRED_DATA_EMPTY_MESSAGE'));
return;
}
// Set default group if nothing is passed for group.
if (empty($formData['groups']))
{
$formData['groups'] = array($params->get("new_usertype", 2));
}
// Get a blank user object
$user = new JUser;
// Create new user.
$response = $this->storeUser($user, $formData, 1);
$this->plugin->setResponse($response);
return;
}
// PATCH / EDIT user case
else
{
// Get a user object from xIdentifier
$user = $this->retriveUser($xIdentifier, $userIdentifier);
// If user is already present then update it according to access.
if (!empty($user->id))
{
$iAmSuperAdmin = $me->authorise('core.admin');
// Check if regular user is trying to update his/her own profile OR if user is superadmin
if ($me->id == $user->id || $iAmSuperAdmin)
{
// If password present then update password2 or else dont include.
if (!empty($formData['password']))
{
$formData['password2'] = $formData['password'];
}
/*// Add newly added groups and keep the old one as it is.
if (!empty($formData['groups']))
{
$formData['groups'] = array_unique(array_merge($user->groups, $formData['groups']));
}*/
$response = $this->storeUser($user, $formData);
$this->plugin->setResponse($response);
return;
}
else
{
ApiError::raiseError(400, JText::_('JERROR_ALERTNOAUTHOR'));
return;
}
}
else
{
// Forced user creation
if ($fIdentifier)
{
$user = new JUser;
if ($formData['username'] == '' || $formData['name'] == '' || $formData['email'] == '' || $formData['password'] == '')
{
ApiError::raiseError(400, JText::_('PLG_API_USERS_REQUIRED_DATA_EMPTY_MESSAGE'));
return;
}
// Set default group if nothing is passed for group.
if (empty($formData['groups']))
{
$formData['groups'] = array($params->get("new_usertype", 2));
}
// Create new user.
$response = $this->storeUser($user, $formData, 1);
$this->plugin->setResponse($response);
return;
}
// User trying to be updated not found
else
{
ApiError::raiseError(400, JText::_('PLG_API_USERS_USER_NOT_FOUND_MESSAGE'));
return;
}
}
}
}
/**
* Funtion to remove sensitive user info fields like password
*
* @param Object $user The user object.
* @param Array $fields Array of fields to be unset
*
* @return object|void $user
*
* @since 2.0.1
*/
protected function sanitizeUserFields($user, $fields = array('password', 'password_clear', 'otpKey', 'otep'))
{
foreach ($fields as $f)
{
if (isset($user->{$f}))
{
unset($user->{$f});
}
}
return $user;
}
/**
* Function get for user record.
*
* @return object|void User details on success otherwise raise error
*
* @since 2.0
*/
public function get()
{
$input = JFactory::getApplication()->input;
$id = $input->get('id', 0, 'string');
$xIdentifier = $input->server->get('HTTP_X_IDENTIFIER', '', 'string');
/*
* If we have an id try to fetch the user
* @TODO write user field mapping logic here
*/
if ($id)
{
// Get user object
$user = $this->retriveUser($xIdentifier, $id);
if (!$user->id)
{
ApiError::raiseError(400, JText::_('PLG_API_USERS_USER_NOT_FOUND_MESSAGE'));
return;
}
}
else
{
$user = JFactory::getUser();
if ($user->guest)
{
ApiError::raiseError(400, JText::_('JERROR_ALERTNOAUTHOR'));
}
}
$user = $this->sanitizeUserFields($user);
$this->plugin->setResponse($user);
}
/**
* Function to return userid if a user exists depending on email
*
* @param string $email The email to search on.
*
* @return integer The user id or 0 if not found.
*
* @since 2.0
*/
private function getUserId($email)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__users'))
->where($db->quoteName('email') . ' = ' . $db->quote($email));
$db->setQuery($query, 0, 1);
return $db->loadResult();
}
/**
* Funtion for bind and save data and return response.
*
* @param Object $user The user object.
* @param Array $formData Array of user data to be added or updated.
* @param Boolean $isNew Flag to differentiate the update of create action.
*
* @return object|void $response the response object created on after user saving. void and raise error
*
* @since 2.0
*/
private function storeUser($user, $formData, $isNew = 0)
{
$response = new stdClass;
$ignore = array();
// Ignore pasword field if not set to avoid warning on bind()
if (!isset($formData['password']))
{
$ignore[] = 'password';
}
// In case of edit user, set formData->id as $user->id no matter what is passed in x-identifier
// Otherwise - it will try to create new user
if (!$isNew)
{
$formData['id'] = $user->id;
}
if (!$user->bind($formData, $ignore))
{
ApiError::raiseError(400, $user->getError());
return;
}
if (!$user->save())
{
ApiError::raiseError(400, $user->getError());
return;
}
// Set user id to be returned
$response->id = $user->id;
if ($isNew)
{
$response->message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE');
}
else
{
$response->message = JText::_('PLG_API_USERS_ACCOUNT_UPDATED_SUCCESSFULLY_MESSAGE');
}
return $response;
}
/**
* Function delete is used to delete the respective user record.
*
* @return void
*
* @since 2.0
*/
public function delete()
{
$app = JFactory::getApplication();
$userIdentifier = $app->input->get('id', 0, 'string');
$xIdentifier = $app->input->server->get('HTTP_X_IDENTIFIER', '', 'string');
$loggedUser = JFactory::getUser();
// Check if I am a Super Admin
$iAmSuperAdmin = $loggedUser->authorise('core.admin');
$userToDelete = $this->retriveUser($xIdentifier, $userIdentifier);
if (!$userToDelete->id)
{
ApiError::raiseError(400, JText::_('PLG_API_USERS_USER_NOT_FOUND_MESSAGE'));
return;
}
if ($loggedUser->id == $userToDelete->id)
{
ApiError::raiseError(400, JText::_('COM_USERS_USERS_ERROR_CANNOT_DELETE_SELF'));
return;
}
// Access checks.
$allow = $loggedUser->authorise('core.delete', 'com_users');
// Don't allow non-super-admin to delete a super admin
$allow = (!$iAmSuperAdmin && JAccess::check($userToDelete->id, 'core.admin')) ? false : $allow;
if ($allow)
{
if (!$userToDelete->delete())
{
ApiError::raiseError(400, $userToDelete->getError());
return;
}
}
else
{
ApiError::raiseError(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
return;
}
$response = new stdClass;
$response->message = JText::_('PLG_API_USERS_USER_DELETE_MESSAGE');
$this->plugin->setResponse($response);
return;
}
/**
* Function retriveUser for get user details depending upon the identifier.
*
* @param string $xIdentifier Flag to differentiate the column value.
*
* @param string $userIdentifier username
*
* @return object $user Juser object if user exist otherwise std class.
*
* @since 2.0
*/
private function retriveUser($xIdentifier, $userIdentifier)
{
$user = new stdClass;
switch ($xIdentifier)
{
case 'username':
$userId = JUserHelper::getUserId($userIdentifier);
if (!empty($userId))
{
$user = JFactory::getUser($userId);
}
break;
case 'email':
$userId = $this->getUserId($userIdentifier);
if (!empty($userId))
{
$user = JFactory::getUser($userId);
}
break;
default:
$user = JFactory::getUser($userIdentifier);
break;
}
return $user;
}
}