| Total Complexity | 84 |
| Total Lines | 683 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Users often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Users, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Users extends DolibarrApi |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * |
||
| 34 | * @var array $FIELDS Mandatory fields, checked when create and update object |
||
| 35 | */ |
||
| 36 | static $FIELDS = array( |
||
| 37 | 'login', |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var User $user {@type User} |
||
| 42 | */ |
||
| 43 | public $useraccount; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructor |
||
| 47 | */ |
||
| 48 | public function __construct() |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * List Users |
||
| 58 | * |
||
| 59 | * Get a list of Users |
||
| 60 | * |
||
| 61 | * @param string $sortfield Sort field |
||
| 62 | * @param string $sortorder Sort order |
||
| 63 | * @param int $limit Limit for list |
||
| 64 | * @param int $page Page number |
||
| 65 | * @param string $user_ids User ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} |
||
| 66 | * @param int $category Use this param to filter list by category |
||
| 67 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 68 | * @return array Array of User objects |
||
| 69 | */ |
||
| 70 | public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $category = 0, $sqlfilters = '') |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get properties of an user object |
||
| 146 | * |
||
| 147 | * @param int $id ID of user |
||
| 148 | * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) |
||
| 149 | * @return array|mixed data without useless information |
||
| 150 | * |
||
| 151 | * @throws RestException 401 Insufficient rights |
||
| 152 | * @throws RestException 404 User or group not found |
||
| 153 | */ |
||
| 154 | public function get($id, $includepermissions = 0) |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get properties of an user object by login |
||
| 180 | * |
||
| 181 | * @param string $login Login of user |
||
| 182 | * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) |
||
| 183 | * @return array|mixed data without useless information |
||
| 184 | * |
||
| 185 | * @url GET login/{login} |
||
| 186 | * |
||
| 187 | * @throws RestException 401 Insufficient rights |
||
| 188 | * @throws RestException 404 User or group not found |
||
| 189 | */ |
||
| 190 | public function getByLogin($login, $includepermissions = 0) |
||
| 191 | { |
||
| 192 | //if (!DolibarrApiAccess::$user->rights->user->user->lire) { |
||
| 193 | //throw new RestException(401); |
||
| 194 | //} |
||
| 195 | |||
| 196 | $result = $this->useraccount->fetch('', $login); |
||
| 197 | if (!$result) |
||
| 198 | { |
||
| 199 | throw new RestException(404, 'User not found'); |
||
| 200 | } |
||
| 201 | |||
| 202 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) |
||
| 203 | { |
||
| 204 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($includepermissions) { |
||
| 208 | $this->useraccount->getRights(); |
||
| 209 | } |
||
| 210 | |||
| 211 | return $this->_cleanObjectDatas($this->useraccount); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get properties of an user object by Email |
||
| 216 | * |
||
| 217 | * @param string $email Email of user |
||
| 218 | * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) |
||
| 219 | * @return array|mixed data without useless information |
||
| 220 | * |
||
| 221 | * @url GET email/{email} |
||
| 222 | * |
||
| 223 | * @throws RestException 401 Insufficient rights |
||
| 224 | * @throws RestException 404 User or group not found |
||
| 225 | */ |
||
| 226 | public function getByEmail($email, $includepermissions = 0) |
||
| 227 | { |
||
| 228 | //if (!DolibarrApiAccess::$user->rights->user->user->lire) { |
||
| 229 | //throw new RestException(401); |
||
| 230 | //} |
||
| 231 | |||
| 232 | $result = $this->useraccount->fetch('', '', '', 0, -1, $email); |
||
| 233 | if (!$result) |
||
| 234 | { |
||
| 235 | throw new RestException(404, 'User not found'); |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) |
||
| 239 | { |
||
| 240 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 241 | } |
||
| 242 | |||
| 243 | if ($includepermissions) { |
||
| 244 | $this->useraccount->getRights(); |
||
| 245 | } |
||
| 246 | |||
| 247 | return $this->_cleanObjectDatas($this->useraccount); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get properties of user connected |
||
| 252 | * |
||
| 253 | * @url GET /info |
||
| 254 | * |
||
| 255 | * @return array|mixed Data without useless information |
||
| 256 | * |
||
| 257 | * @throws RestException 401 Insufficient rights |
||
| 258 | * @throws RestException 404 User or group not found |
||
| 259 | */ |
||
| 260 | public function getInfo() |
||
| 261 | { |
||
| 262 | $apiUser = DolibarrApiAccess::$user; |
||
| 263 | |||
| 264 | $result = $this->useraccount->fetch($apiUser->id); |
||
| 265 | if (!$result) { |
||
| 266 | throw new RestException(404, 'User not found'); |
||
| 267 | } |
||
| 268 | |||
| 269 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) { |
||
| 270 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 271 | } |
||
| 272 | |||
| 273 | $usergroup = new UserGroup($this->db); |
||
| 274 | $userGroupList = $usergroup->listGroupsForUser($apiUser->id, false); |
||
| 275 | if (!is_array($userGroupList)) { |
||
| 276 | throw new RestException(404, 'User group not found'); |
||
| 277 | } |
||
| 278 | |||
| 279 | $this->useraccount->user_group_list = $this->_cleanUserGroupListDatas($userGroupList); |
||
|
|
|||
| 280 | |||
| 281 | return $this->_cleanObjectDatas($this->useraccount); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Create user account |
||
| 286 | * |
||
| 287 | * @param array $request_data New user data |
||
| 288 | * @return int |
||
| 289 | */ |
||
| 290 | public function post($request_data = null) |
||
| 291 | { |
||
| 292 | // check user authorization |
||
| 293 | //if(! DolibarrApiAccess::$user->rights->user->creer) { |
||
| 294 | // throw new RestException(401, "User creation not allowed"); |
||
| 295 | //} |
||
| 296 | // check mandatory fields |
||
| 297 | /*if (!isset($request_data["login"])) |
||
| 298 | throw new RestException(400, "login field missing"); |
||
| 299 | if (!isset($request_data["password"])) |
||
| 300 | throw new RestException(400, "password field missing"); |
||
| 301 | if (!isset($request_data["lastname"])) |
||
| 302 | throw new RestException(400, "lastname field missing");*/ |
||
| 303 | //assign field values |
||
| 304 | foreach ($request_data as $field => $value) |
||
| 305 | { |
||
| 306 | $this->useraccount->$field = $value; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) { |
||
| 310 | throw new RestException(500, 'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors)); |
||
| 311 | } |
||
| 312 | return $this->useraccount->id; |
||
| 313 | } |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Update account |
||
| 318 | * |
||
| 319 | * @param int $id Id of account to update |
||
| 320 | * @param array $request_data Datas |
||
| 321 | * @return array |
||
| 322 | * |
||
| 323 | * @throws RestException |
||
| 324 | */ |
||
| 325 | public function put($id, $request_data = null) |
||
| 326 | { |
||
| 327 | //if (!DolibarrApiAccess::$user->rights->user->user->creer) { |
||
| 328 | //throw new RestException(401); |
||
| 329 | //} |
||
| 330 | |||
| 331 | $result = $this->useraccount->fetch($id); |
||
| 332 | if (!$result) |
||
| 333 | { |
||
| 334 | throw new RestException(404, 'Account not found'); |
||
| 335 | } |
||
| 336 | |||
| 337 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) |
||
| 338 | { |
||
| 339 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 340 | } |
||
| 341 | |||
| 342 | foreach ($request_data as $field => $value) |
||
| 343 | { |
||
| 344 | if ($field == 'id') continue; |
||
| 345 | // The status must be updated using setstatus() because it |
||
| 346 | // is not handled by the update() method. |
||
| 347 | if ($field == 'statut') { |
||
| 348 | $result = $this->useraccount->setstatus($value); |
||
| 349 | if ($result < 0) { |
||
| 350 | throw new RestException(500, 'Error when updating status of user: '.$this->useraccount->error); |
||
| 351 | } |
||
| 352 | } else { |
||
| 353 | $this->useraccount->$field = $value; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | // If there is no error, update() returns the number of affected |
||
| 358 | // rows so if the update is a no op, the return value is zezo. |
||
| 359 | if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) |
||
| 360 | { |
||
| 361 | return $this->get($id); |
||
| 362 | } else { |
||
| 363 | throw new RestException(500, $this->useraccount->error); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * List the groups of a user |
||
| 370 | * |
||
| 371 | * @param int $id Id of user |
||
| 372 | * @return array Array of group objects |
||
| 373 | * |
||
| 374 | * @throws RestException 403 Not allowed |
||
| 375 | * @throws RestException 404 Not found |
||
| 376 | * |
||
| 377 | * @url GET {id}/groups |
||
| 378 | */ |
||
| 379 | public function getGroups($id) |
||
| 380 | { |
||
| 381 | $obj_ret = array(); |
||
| 382 | |||
| 383 | if (!DolibarrApiAccess::$user->rights->user->user->lire) { |
||
| 384 | throw new RestException(403); |
||
| 385 | } |
||
| 386 | |||
| 387 | $user = new User($this->db); |
||
| 388 | $result = $user->fetch($id); |
||
| 389 | if (!$result) { |
||
| 390 | throw new RestException(404, 'user not found'); |
||
| 391 | } |
||
| 392 | |||
| 393 | $usergroup = new UserGroup($this->db); |
||
| 394 | $groups = $usergroup->listGroupsForUser($id, false); |
||
| 395 | $obj_ret = array(); |
||
| 396 | foreach ($groups as $group) { |
||
| 397 | $obj_ret[] = $this->_cleanObjectDatas($group); |
||
| 398 | } |
||
| 399 | return $obj_ret; |
||
| 400 | } |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Add a user into a group |
||
| 405 | * |
||
| 406 | * @param int $id User ID |
||
| 407 | * @param int $group Group ID |
||
| 408 | * @param int $entity Entity ID (valid only for superadmin in multicompany transverse mode) |
||
| 409 | * @return int 1 if success |
||
| 410 | * |
||
| 411 | * @url GET {id}/setGroup/{group} |
||
| 412 | */ |
||
| 413 | public function setGroup($id, $group, $entity = 1) |
||
| 414 | { |
||
| 415 | |||
| 416 | global $conf; |
||
| 417 | |||
| 418 | //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) { |
||
| 419 | //throw new RestException(401); |
||
| 420 | //} |
||
| 421 | $result = $this->useraccount->fetch($id); |
||
| 422 | if (!$result) |
||
| 423 | { |
||
| 424 | throw new RestException(404, 'User not found'); |
||
| 425 | } |
||
| 426 | |||
| 427 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) |
||
| 428 | { |
||
| 429 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 430 | } |
||
| 431 | |||
| 432 | if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) |
||
| 433 | { |
||
| 434 | $entity = (!empty($entity) ? $entity : $conf->entity); |
||
| 435 | } else { |
||
| 436 | // When using API, action is done on entity of logged user because a user of entity X with permission to create user should not be able to |
||
| 437 | // hack the security by giving himself permissions on another entity. |
||
| 438 | $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity); |
||
| 439 | } |
||
| 440 | |||
| 441 | $result = $this->useraccount->SetInGroup($group, $entity); |
||
| 442 | if (!($result > 0)) |
||
| 443 | { |
||
| 444 | throw new RestException(500, $this->useraccount->error); |
||
| 445 | } |
||
| 446 | |||
| 447 | return 1; |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * List Groups |
||
| 452 | * |
||
| 453 | * Return an array with a list of Groups |
||
| 454 | * |
||
| 455 | * @url GET /groups |
||
| 456 | * |
||
| 457 | * @param string $sortfield Sort field |
||
| 458 | * @param string $sortorder Sort order |
||
| 459 | * @param int $limit Limit for list |
||
| 460 | * @param int $page Page number |
||
| 461 | * @param string $group_ids Groups ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} |
||
| 462 | * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" |
||
| 463 | * @return array Array of User objects |
||
| 464 | */ |
||
| 465 | public function listGroups($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters = '') |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get properties of an group object |
||
| 531 | * |
||
| 532 | * Return an array with group informations |
||
| 533 | * |
||
| 534 | * @url GET /groups/{group} |
||
| 535 | * |
||
| 536 | * @param int $group ID of group |
||
| 537 | * @param int $load_members Load members list or not {@min 0} {@max 1} |
||
| 538 | * @return array Array of User objects |
||
| 539 | */ |
||
| 540 | public function infoGroups($group, $load_members = 0) |
||
| 541 | { |
||
| 542 | global $db, $conf; |
||
| 543 | |||
| 544 | if (!DolibarrApiAccess::$user->rights->user->group_advance->read) { |
||
| 545 | throw new RestException(401, "You are not allowed to read groups"); |
||
| 546 | } |
||
| 547 | |||
| 548 | $group_static = new UserGroup($this->db); |
||
| 549 | $result = $group_static->fetch($group, '', $load_members); |
||
| 550 | |||
| 551 | if (!$result) |
||
| 552 | { |
||
| 553 | throw new RestException(404, 'Group not found'); |
||
| 554 | } |
||
| 555 | |||
| 556 | return $this->_cleanObjectDatas($group_static); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Delete account |
||
| 561 | * |
||
| 562 | * @param int $id Account ID |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | public function delete($id) |
||
| 566 | { |
||
| 567 | //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) { |
||
| 568 | //throw new RestException(401); |
||
| 569 | //} |
||
| 570 | $result = $this->useraccount->fetch($id); |
||
| 571 | if (!$result) |
||
| 572 | { |
||
| 573 | throw new RestException(404, 'User not found'); |
||
| 574 | } |
||
| 575 | |||
| 576 | if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) |
||
| 577 | { |
||
| 578 | throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); |
||
| 579 | } |
||
| 580 | $this->useraccount->oldcopy = clone $this->useraccount; |
||
| 581 | return $this->useraccount->delete(DolibarrApiAccess::$user); |
||
| 582 | } |
||
| 583 | |||
| 584 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 585 | /** |
||
| 586 | * Clean sensible object datas |
||
| 587 | * |
||
| 588 | * @param object $object Object to clean |
||
| 589 | * @return array Array of cleaned object properties |
||
| 590 | */ |
||
| 591 | protected function _cleanObjectDatas($object) |
||
| 592 | { |
||
| 593 | // phpcs:enable |
||
| 594 | global $conf; |
||
| 595 | |||
| 596 | $object = parent::_cleanObjectDatas($object); |
||
| 597 | |||
| 598 | unset($object->default_values); |
||
| 599 | unset($object->lastsearch_values); |
||
| 600 | unset($object->lastsearch_values_tmp); |
||
| 601 | |||
| 602 | unset($object->total_ht); |
||
| 603 | unset($object->total_tva); |
||
| 604 | unset($object->total_localtax1); |
||
| 605 | unset($object->total_localtax2); |
||
| 606 | unset($object->total_ttc); |
||
| 607 | |||
| 608 | unset($object->label_incoterms); |
||
| 609 | unset($object->location_incoterms); |
||
| 610 | |||
| 611 | unset($object->fk_delivery_address); |
||
| 612 | unset($object->fk_incoterms); |
||
| 613 | unset($object->all_permissions_are_loaded); |
||
| 614 | unset($object->shipping_method_id); |
||
| 615 | unset($object->nb_rights); |
||
| 616 | unset($object->search_sid); |
||
| 617 | unset($object->ldap_sid); |
||
| 618 | unset($object->clicktodial_loaded); |
||
| 619 | |||
| 620 | // List of properties never returned by API, whatever are permissions |
||
| 621 | unset($object->pass); |
||
| 622 | unset($object->pass_indatabase); |
||
| 623 | unset($object->pass_indatabase_crypted); |
||
| 624 | unset($object->pass_temp); |
||
| 625 | unset($object->api_key); |
||
| 626 | unset($object->clicktodial_password); |
||
| 627 | unset($object->openid); |
||
| 628 | |||
| 629 | unset($object->lines); |
||
| 630 | unset($object->modelpdf); |
||
| 631 | unset($object->skype); |
||
| 632 | unset($object->twitter); |
||
| 633 | unset($object->facebook); |
||
| 634 | unset($object->linkedin); |
||
| 635 | |||
| 636 | $canreadsalary = ((!empty($conf->salaries->enabled) && !empty(DolibarrApiAccess::$user->rights->salaries->read)) |
||
| 637 | || (!empty($conf->hrm->enabled) && !empty(DolibarrApiAccess::$user->rights->hrm->employee->read))); |
||
| 638 | |||
| 639 | if (!$canreadsalary) |
||
| 640 | { |
||
| 641 | unset($object->salary); |
||
| 642 | unset($object->salaryextra); |
||
| 643 | unset($object->thm); |
||
| 644 | unset($object->tjm); |
||
| 645 | } |
||
| 646 | |||
| 647 | return $object; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Clean sensible user group list datas |
||
| 652 | * |
||
| 653 | * @param array $objectList Array of object to clean |
||
| 654 | * @return array Array of cleaned object properties |
||
| 655 | */ |
||
| 656 | private function _cleanUserGroupListDatas($objectList) |
||
| 657 | { |
||
| 658 | $cleanObjectList = array(); |
||
| 659 | |||
| 660 | foreach ($objectList as $object) { |
||
| 661 | $cleanObject = parent::_cleanObjectDatas($object); |
||
| 662 | |||
| 663 | unset($cleanObject->default_values); |
||
| 664 | unset($cleanObject->lastsearch_values); |
||
| 665 | unset($cleanObject->lastsearch_values_tmp); |
||
| 666 | |||
| 667 | unset($cleanObject->total_ht); |
||
| 668 | unset($cleanObject->total_tva); |
||
| 669 | unset($cleanObject->total_localtax1); |
||
| 670 | unset($cleanObject->total_localtax2); |
||
| 671 | unset($cleanObject->total_ttc); |
||
| 672 | |||
| 673 | unset($cleanObject->libelle_incoterms); |
||
| 674 | unset($cleanObject->location_incoterms); |
||
| 675 | |||
| 676 | unset($cleanObject->fk_delivery_address); |
||
| 677 | unset($cleanObject->fk_incoterms); |
||
| 678 | unset($cleanObject->all_permissions_are_loaded); |
||
| 679 | unset($cleanObject->shipping_method_id); |
||
| 680 | unset($cleanObject->nb_rights); |
||
| 681 | unset($cleanObject->search_sid); |
||
| 682 | unset($cleanObject->ldap_sid); |
||
| 683 | unset($cleanObject->clicktodial_loaded); |
||
| 684 | |||
| 685 | unset($cleanObject->datec); |
||
| 686 | unset($cleanObject->datem); |
||
| 687 | unset($cleanObject->members); |
||
| 688 | unset($cleanObject->note); |
||
| 689 | unset($cleanObject->note_private); |
||
| 690 | |||
| 691 | $cleanObjectList[] = $cleanObject; |
||
| 692 | } |
||
| 693 | |||
| 694 | return $cleanObjectList; |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Validate fields before create or update object |
||
| 699 | * |
||
| 700 | * @param array|null $data Data to validate |
||
| 701 | * @return array |
||
| 702 | * @throws RestException |
||
| 703 | */ |
||
| 704 | private function _validate($data) |
||
| 713 | } |
||
| 714 | } |
||
| 715 |