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