2016-04-10 11:57:53 +00:00
< ? php
2025-09-23 21:22:38 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
* Copyright ( C ) 2020 - 2025 Thibault FOUCART < support @ ptibogxiv . net >
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2025-11-20 00:47:46 +00:00
* Copyright ( C ) 2024 - 2025 Frédéric France < frederic . france @ free . fr >
2025-09-23 21:22:38 +00:00
* Copyright ( C ) 2025 William Mead < william @ m34d . com >
* Copyright ( C ) 2025 Jean François Baillette < jean - francois @ swiiptel . net >
2026-01-15 02:19:59 +00:00
* Copyright ( C ) 2026 Charlene Benke < charlene @ patas - monkey . com >
2016-04-10 11:57:53 +00:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2016-04-10 11:57:53 +00:00
*/
use Luracast\Restler\RestException ;
2019-01-27 12:52:08 +00:00
require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/user/class/usergroup.class.php' ;
2025-05-14 14:54:33 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/class/notify.class.php' ;
2016-04-10 11:57:53 +00:00
2021-05-17 17:50:10 +00:00
2016-04-10 11:57:53 +00:00
/**
2016-06-13 21:24:54 +00:00
* API class for users
2016-04-10 11:57:53 +00:00
*
2025-06-13 22:04:14 +00:00
* @ since 5.0 . 0 Initial implementation
*
2017-10-22 13:42:19 +00:00
* @ access protected
2016-04-10 11:57:53 +00:00
* @ class DolibarrApiAccess { @ requires user , external }
*/
2016-06-13 21:24:54 +00:00
class Users extends DolibarrApi
2016-04-10 11:57:53 +00:00
{
/**
2025-02-21 20:41:23 +00:00
* @ var string [] Mandatory fields , checked when create and update object
2016-04-10 11:57:53 +00:00
*/
2023-12-04 12:53:48 +00:00
public static $FIELDS = array (
2018-08-15 12:28:34 +00:00
'login' ,
2016-04-10 11:57:53 +00:00
);
/**
2025-02-21 20:41:23 +00:00
* @ var User { @ type User }
2016-04-10 11:57:53 +00:00
*/
2016-04-11 21:27:43 +00:00
public $useraccount ;
2016-04-10 11:57:53 +00:00
/**
* Constructor
*/
2020-10-28 23:34:41 +00:00
public function __construct ()
{
2024-02-01 12:34:55 +00:00
global $db ;
2021-05-18 00:06:35 +00:00
2016-04-10 11:57:53 +00:00
$this -> db = $db ;
2016-04-11 21:27:43 +00:00
$this -> useraccount = new User ( $this -> db );
2016-04-10 11:57:53 +00:00
}
2017-10-22 13:42:19 +00:00
2016-09-19 00:13:16 +00:00
/**
2025-06-13 22:04:14 +00:00
* List users
2016-09-19 00:13:16 +00:00
*
* Get a list of Users
*
2025-06-13 22:04:14 +00:00
* @ since 5.0 . 0 Initial implementation
*
2016-09-19 00:13:16 +00:00
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
2023-09-26 16:43:25 +00:00
* @ param string $user_ids User ids filter field . Example : '1' or '1,2,3' { @ pattern /^ [ 0 - 9 ,] * $ / i }
2021-05-17 14:17:03 +00:00
* @ param int $category Use this param to filter list by category
2026-05-29 10:16:42 +00:00
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:>:'20160101') "
2024-01-12 16:18:52 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2016-10-25 16:33:45 +00:00
* @ return array Array of User objects
2024-10-06 11:52:05 +00:00
* @ phan - return Object []
* @ phpstan - return Object []
2025-06-13 22:04:14 +00:00
*
* @ throws RestException
2016-09-19 00:13:16 +00:00
*/
2024-01-10 16:01:40 +00:00
public function index ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $user_ids = '0' , $category = 0 , $sqlfilters = '' , $properties = '' )
2020-10-28 23:34:41 +00:00
{
2024-02-01 12:34:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
throw new RestException ( 403 , " You are not allowed to read list of users " );
2021-05-18 00:06:35 +00:00
}
2020-10-28 23:34:41 +00:00
2021-04-07 22:33:25 +00:00
$obj_ret = array ();
2020-10-28 23:34:41 +00:00
// case of external user, $societe param is ignored and replaced by user's socid
2025-07-01 18:34:09 +00:00
//$socid = DolibarrApiAccess::$user->socid ?: $societe;
2020-10-28 23:34:41 +00:00
$sql = " SELECT t.rowid " ;
2023-04-28 07:31:33 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " user AS t LEFT JOIN " . MAIN_DB_PREFIX . " user_extrafields AS ef ON (ef.fk_object = t.rowid) " ; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
2020-10-28 23:34:41 +00:00
if ( $category > 0 ) {
2022-01-27 09:00:26 +00:00
$sql .= " , " . $this -> db -> prefix () . " categorie_user as c " ;
2020-10-28 23:34:41 +00:00
}
$sql .= ' WHERE t.entity IN (' . getEntity ( 'user' ) . ')' ;
if ( $user_ids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.rowid IN ( " . $this -> db -> sanitize ( $user_ids ) . " ) " ;
2020-10-28 23:34:41 +00:00
}
// Select products of given category
if ( $category > 0 ) {
2021-06-09 13:36:47 +00:00
$sql .= " AND c.fk_categorie = " . (( int ) $category );
2021-03-22 12:31:06 +00:00
$sql .= " AND c.fk_user = t.rowid " ;
2020-10-28 23:34:41 +00:00
}
// Add sql filters
if ( $sqlfilters ) {
2026-08-09 01:11:50 +00:00
// List of properties we can't filter on, whatever are permissions (to avoid guess by search binary attack)
$forbiddenfilterfields = array (
'pass' ,
'pass_crypted' ,
'pass_temp' ,
'api_key' ,
'openid'
);
$canreadsalary = (( isModEnabled ( 'salaries' ) && DolibarrApiAccess :: $user -> hasRight ( 'salaries' , 'read' )) || ! isModEnabled ( 'salaries' ));
if ( ! $canreadsalary ) {
$forbiddenfilterfields [] = 'salary' ;
$forbiddenfilterfields [] = 'salaryextra' ;
$forbiddenfilterfields [] = 'thm' ;
$forbiddenfilterfields [] = 'tjm' ;
}
2021-12-20 19:49:32 +00:00
$errormessage = '' ;
2026-08-09 01:11:50 +00:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage , 0 , 0 , 0 , $forbiddenfilterfields );
2023-02-25 18:48:33 +00:00
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-28 23:34:41 +00:00
}
}
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
if ( $result ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min ) {
$obj = $this -> db -> fetch_object ( $result );
$user_static = new User ( $this -> db );
if ( $user_static -> fetch ( $obj -> rowid )) {
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $user_static ), $properties );
2020-10-28 23:34:41 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve User list : ' . $this -> db -> lasterror ());
}
2023-12-31 13:11:05 +00:00
2020-10-28 23:34:41 +00:00
return $obj_ret ;
2016-09-19 00:13:16 +00:00
}
2017-10-22 13:42:19 +00:00
2016-04-10 11:57:53 +00:00
/**
2025-06-13 22:04:14 +00:00
* Get a user
*
* @ since 5.0 . 0 Initial implementation
2016-04-10 11:57:53 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param int $id ID of user
2020-12-24 01:33:11 +00:00
* @ param int $includepermissions Set this to 1 to have the array of permissions loaded ( not done by default for performance purpose )
2023-09-26 16:43:25 +00:00
* @ return array | mixed data without useless information
2024-10-06 11:52:05 +00:00
* @ phan - return Object
* @ phpstan - return Object
2017-10-22 13:42:19 +00:00
*
2025-10-23 11:56:22 +00:00
* @ throws RestException 400 Bad request
2020-12-24 01:33:11 +00:00
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
2016-04-10 11:57:53 +00:00
*/
2020-10-28 23:34:41 +00:00
public function get ( $id , $includepermissions = 0 )
{
2025-10-23 11:56:22 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No user with id=0 can exist' );
}
2026-08-08 23:18:22 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin ) && DolibarrApiAccess :: $user -> id != $id ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Not allowed' );
2021-04-07 22:33:25 +00:00
}
2020-10-28 23:34:41 +00:00
if ( $id == 0 ) {
$result = $this -> useraccount -> initAsSpecimen ();
} else {
$result = $this -> useraccount -> fetch ( $id );
}
if ( ! $result ) {
2016-04-10 11:57:53 +00:00
throw new RestException ( 404 , 'User not found' );
}
2020-10-28 23:34:41 +00:00
if ( $id > 0 && ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2016-04-10 11:57:53 +00:00
}
2020-08-31 15:09:14 +00:00
if ( $includepermissions ) {
2024-08-04 16:47:22 +00:00
$this -> useraccount -> loadRights ();
2020-08-31 15:09:14 +00:00
}
return $this -> _cleanObjectDatas ( $this -> useraccount );
}
2020-08-31 15:11:05 +00:00
2020-08-31 15:09:14 +00:00
/**
2025-06-13 22:04:14 +00:00
* Get a user by login
*
* @ since 13.0 . 0 Initial implementation
2020-08-31 15:09:14 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param string $login Login of user
2020-12-24 01:33:11 +00:00
* @ param int $includepermissions Set this to 1 to have the array of permissions loaded ( not done by default for performance purpose )
2023-09-26 16:43:25 +00:00
* @ return array | mixed Data without useless information
2024-10-06 11:52:05 +00:00
* @ phan - return Object
* @ phpstan - return Object
2020-08-31 15:09:14 +00:00
*
* @ url GET login / { login }
2020-08-31 15:11:05 +00:00
*
2021-09-10 11:29:19 +00:00
* @ throws RestException 400 Bad request
2023-09-26 16:43:25 +00:00
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
2020-08-31 15:09:14 +00:00
*/
2020-10-28 23:34:41 +00:00
public function getByLogin ( $login , $includepermissions = 0 )
{
2021-09-10 11:29:19 +00:00
if ( empty ( $login )) {
throw new RestException ( 400 , 'Bad parameters' );
}
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin ) && DolibarrApiAccess :: $user -> login != $login ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Not allowed' );
2021-04-07 22:33:25 +00:00
}
2020-08-31 15:09:14 +00:00
2024-10-06 11:52:05 +00:00
$result = $this -> useraccount -> fetch ( 0 , $login );
2020-10-28 23:34:41 +00:00
if ( ! $result ) {
2020-08-31 15:09:14 +00:00
throw new RestException ( 404 , 'User not found' );
}
2020-10-28 23:34:41 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-08-31 15:09:14 +00:00
}
if ( $includepermissions ) {
2024-08-04 16:47:22 +00:00
$this -> useraccount -> loadRights ();
2020-08-31 15:09:14 +00:00
}
return $this -> _cleanObjectDatas ( $this -> useraccount );
}
2020-08-31 15:11:05 +00:00
2020-08-31 15:09:14 +00:00
/**
2025-06-13 22:04:14 +00:00
* Get a user by email
*
* @ since 13.0 . 0 Initial implementation
2020-08-31 15:09:14 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param string $email Email of user
2020-12-24 01:33:11 +00:00
* @ param int $includepermissions Set this to 1 to have the array of permissions loaded ( not done by default for performance purpose )
2023-09-26 16:43:25 +00:00
* @ return array | mixed Data without useless information
2024-10-06 11:52:05 +00:00
* @ phan - return Object
2025-06-13 22:04:14 +00:00
* @ phpstan - return Object
2020-08-31 15:09:14 +00:00
*
* @ url GET email / { email }
2020-08-31 15:11:05 +00:00
*
2021-09-10 11:29:19 +00:00
* @ throws RestException 400 Bad request
2020-08-31 15:09:14 +00:00
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
*/
2020-10-28 23:34:41 +00:00
public function getByEmail ( $email , $includepermissions = 0 )
{
2021-09-10 11:29:19 +00:00
if ( empty ( $email )) {
throw new RestException ( 400 , 'Bad parameters' );
}
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin ) && DolibarrApiAccess :: $user -> email != $email ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Not allowed' );
2021-04-07 22:33:25 +00:00
}
2020-08-31 15:09:14 +00:00
2024-10-06 11:52:05 +00:00
$result = $this -> useraccount -> fetch ( 0 , '' , '' , 0 , - 1 , $email );
2020-10-28 23:34:41 +00:00
if ( ! $result ) {
2020-08-31 15:09:14 +00:00
throw new RestException ( 404 , 'User not found' );
}
2020-10-28 23:34:41 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2020-08-31 15:09:14 +00:00
}
2020-02-03 02:47:45 +00:00
if ( $includepermissions ) {
2024-08-04 16:47:22 +00:00
$this -> useraccount -> loadRights ();
2020-02-03 02:47:45 +00:00
}
2016-04-11 21:27:43 +00:00
return $this -> _cleanObjectDatas ( $this -> useraccount );
2016-04-10 11:57:53 +00:00
}
2017-10-22 13:42:19 +00:00
2020-10-28 23:34:41 +00:00
/**
2025-08-04 03:38:31 +00:00
* Get more properties of the current user ( so user of API token ) .
*
* This route could also ave been named " /users/me " .
2020-10-28 23:34:41 +00:00
*
2025-06-13 22:04:14 +00:00
* @ since 11.0 . 0 Initial implementation
*
2020-10-28 23:34:41 +00:00
* @ url GET / info
*
2021-06-04 23:17:16 +00:00
* @ param int $includepermissions Set this to 1 to have the array of permissions loaded ( not done by default for performance purpose )
2023-09-26 16:43:25 +00:00
* @ return array | mixed Data without useless information
2020-10-28 23:34:41 +00:00
*
* @ throws RestException 401 Insufficient rights
* @ throws RestException 404 User or group not found
*/
2020-11-28 09:28:23 +00:00
public function getInfo ( $includepermissions = 0 )
2020-10-28 23:34:41 +00:00
{
2024-03-19 21:02:42 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'self' , 'creer' ) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Not allowed' );
2021-04-07 22:33:25 +00:00
}
2020-10-28 23:34:41 +00:00
$apiUser = DolibarrApiAccess :: $user ;
$result = $this -> useraccount -> fetch ( $apiUser -> id );
if ( ! $result ) {
throw new RestException ( 404 , 'User not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed to current logged user' );
2020-10-28 23:34:41 +00:00
}
2020-11-28 09:32:02 +00:00
2020-11-28 09:28:23 +00:00
if ( $includepermissions ) {
2024-08-04 16:47:22 +00:00
$this -> useraccount -> loadRights ();
2020-11-28 09:28:23 +00:00
}
2020-10-28 23:34:41 +00:00
$usergroup = new UserGroup ( $this -> db );
$userGroupList = $usergroup -> listGroupsForUser ( $apiUser -> id , false );
if ( ! is_array ( $userGroupList )) {
throw new RestException ( 404 , 'User group not found' );
}
$this -> useraccount -> user_group_list = $this -> _cleanUserGroupListDatas ( $userGroupList );
return $this -> _cleanObjectDatas ( $this -> useraccount );
}
2017-10-22 13:42:19 +00:00
2016-05-13 18:20:48 +00:00
/**
2025-06-13 22:04:14 +00:00
* Create a user
*
* @ since 5.0 . 0 Initial implementation
2016-05-13 18:20:48 +00:00
*
* @ param array $request_data New user data
2024-10-06 11:52:05 +00:00
* @ phan - param ? array < string , mixed > $request_data
* @ phpstan - param ? array < string , mixed > $request_data
2016-05-13 18:20:48 +00:00
* @ return int
2021-04-07 22:33:25 +00:00
*
2026-03-16 09:19:43 +00:00
* @ throws RestException 403 Not allowed
2016-05-13 18:20:48 +00:00
*/
2020-10-28 23:34:41 +00:00
public function post ( $request_data = null )
{
2021-04-07 22:33:25 +00:00
// Check user authorization
2025-11-23 00:59:02 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , " User creation not allowed for login " . DolibarrApiAccess :: $user -> login );
2021-04-07 22:33:25 +00:00
}
2020-10-28 23:34:41 +00:00
// check mandatory fields
2025-11-23 00:59:02 +00:00
if ( ! isset ( $request_data [ " login " ]))
throw new RestException ( 500 , " login field missing " );
/* if ( ! isset ( $request_data [ " password " ]))
2020-10-28 23:34:41 +00:00
throw new RestException ( 400 , " password field missing " );
if ( ! isset ( $request_data [ " lastname " ]))
throw new RestException ( 400 , " lastname field missing " ); */
2021-04-07 22:33:25 +00:00
2020-10-28 23:34:41 +00:00
//assign field values
foreach ( $request_data as $field => $value ) {
2026-08-18 10:45:00 +00:00
if ( in_array ( $field , array ( 'pass_crypted' , 'pass_indatabase' , 'pass_indatabase_crypted' , 'pass_temp' , 'api_key' , 'openid' ))) {
2021-04-07 22:33:25 +00:00
// This properties can't be set/modified with API
2024-04-07 12:59:25 +00:00
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs " );
2021-04-07 22:33:25 +00:00
}
/* if ( $field == 'pass' ) {
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'password' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'You are not allowed to modify/set password of other users' );
2021-04-07 22:33:25 +00:00
continue ;
}
}
*/
2026-08-18 10:45:00 +00:00
$canreadsalary = (( isModEnabled ( 'salaries' ) && DolibarrApiAccess :: $user -> hasRight ( 'salaries' , 'read' )) || ! isModEnabled ( 'salaries' ));
if ( ! $canreadsalary ) {
if ( in_array ( $field , array ( 'salary' , 'salaryextra' , 'thm' , 'tjm' ))) {
// This properties can't be set/modified with API
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs with permission on salaries " );
}
}
2026-03-25 14:55:50 +00:00
if ( $field === 'caller' ) {
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
$this -> useraccount -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
continue ;
}
if ( DolibarrApiAccess :: $user -> admin ) { // If user for API is admin
if ( $field == 'admin' && $value != $this -> useraccount -> admin && empty ( $value )) {
throw new RestException ( 403 , 'Reseting the admin status of a user is not possible using the API' );
}
} else {
if ( $field == 'admin' && $value != $this -> useraccount -> admin ) {
throw new RestException ( 403 , 'Only an admin user can modify the admin status of another user' );
}
}
2021-04-07 22:33:25 +00:00
2024-04-02 10:28:55 +00:00
$this -> useraccount -> $field = $this -> _checkValForAPI ( $field , $value , $this -> useraccount );
2020-10-28 23:34:41 +00:00
}
if ( $this -> useraccount -> create ( DolibarrApiAccess :: $user ) < 0 ) {
2020-10-31 20:04:38 +00:00
throw new RestException ( 500 , 'Error creating' , array_merge ( array ( $this -> useraccount -> error ), $this -> useraccount -> errors ));
2020-10-28 23:34:41 +00:00
}
return $this -> useraccount -> id ;
}
2017-10-22 13:42:19 +00:00
2016-04-10 11:57:53 +00:00
/**
2025-06-13 22:04:14 +00:00
* Update a user
*
* @ since 5.0 . 0 Initial implementation
2016-04-10 11:57:53 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param int $id Id of account to update
* @ param array $request_data Datas
2024-10-06 11:52:05 +00:00
* @ phan - param ? array < string , mixed > $request_data
* @ phpstan - param ? array < string , mixed > $request_data
2024-02-22 00:32:55 +00:00
* @ return Object Updated object
2020-10-28 23:34:41 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Not allowed
2020-12-24 01:09:11 +00:00
* @ throws RestException 404 Not found
* @ throws RestException 500 System error
2016-04-10 11:57:53 +00:00
*/
2020-10-28 23:34:41 +00:00
public function put ( $id , $request_data = null )
{
2026-04-03 01:19:07 +00:00
$isSelfUpdate = (( int ) $id === ( int ) DolibarrApiAccess :: $user -> id );
2021-04-07 22:33:25 +00:00
// Check user authorization
2026-04-03 01:19:07 +00:00
if (
! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )
&& ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'write' )
&& ! (
$isSelfUpdate
&& (
DolibarrApiAccess :: $user -> hasRight ( 'user' , 'self' , 'creer' )
|| DolibarrApiAccess :: $user -> hasRight ( 'user' , 'self' , 'write' )
)
)
&& empty ( DolibarrApiAccess :: $user -> admin )
) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , " User update not allowed " );
2021-04-07 22:33:25 +00:00
}
2016-04-10 11:57:53 +00:00
2016-04-11 21:27:43 +00:00
$result = $this -> useraccount -> fetch ( $id );
2020-10-28 23:34:41 +00:00
if ( ! $result ) {
2016-04-10 11:57:53 +00:00
throw new RestException ( 404 , 'Account not found' );
}
2020-10-28 23:34:41 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2016-04-10 11:57:53 +00:00
}
2020-10-28 23:34:41 +00:00
foreach ( $request_data as $field => $value ) {
2026-08-18 10:45:00 +00:00
if ( in_array ( $field , array ( 'pass_crypted' , 'pass_indatabase' , 'pass_indatabase_crypted' , 'pass_temp' , 'api_key' , 'openid' ))) {
2021-04-07 22:33:25 +00:00
// This properties can't be set/modified with API
2024-04-07 12:59:25 +00:00
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs " );
2021-04-07 22:33:25 +00:00
}
2026-08-18 10:45:00 +00:00
$canreadsalary = (( isModEnabled ( 'salaries' ) && DolibarrApiAccess :: $user -> hasRight ( 'salaries' , 'read' )) || ! isModEnabled ( 'salaries' ));
if ( ! $canreadsalary ) {
if ( in_array ( $field , array ( 'salary' , 'salaryextra' , 'thm' , 'tjm' ))) {
// This properties can't be set/modified with API
throw new RestException ( 405 , 'The property ' . $field . " can't be set/modified using the APIs with permission on salaries " );
}
}
2023-12-15 11:15:33 +00:00
if ( $field == 'id' ) {
continue ;
}
2021-04-07 22:33:25 +00:00
if ( $field == 'pass' ) {
2024-02-09 14:58:49 +00:00
if ( $this -> useraccount -> id != DolibarrApiAccess :: $user -> id && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'password' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'You are not allowed to modify password of other users' );
2021-04-07 22:33:25 +00:00
}
2024-02-09 14:58:49 +00:00
if ( $this -> useraccount -> id == DolibarrApiAccess :: $user -> id && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'self' , 'password' )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'You are not allowed to modify your own password' );
2021-04-07 22:33:25 +00:00
}
}
2023-12-15 11:15:33 +00:00
if ( $field === 'caller' ) {
2024-01-12 16:18:52 +00:00
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
2024-04-02 10:28:55 +00:00
$this -> useraccount -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
2023-12-15 11:15:33 +00:00
continue ;
}
2025-01-20 13:57:45 +00:00
if ( $field == 'array_options' && is_array ( $value )) {
foreach ( $value as $index => $val ) {
2026-05-08 23:16:35 +00:00
$this -> useraccount -> array_options [ $index ] = $this -> _checkValExtrafieldsForAPI ( $index , $val , $this -> useraccount );
2025-01-20 13:57:45 +00:00
}
2023-12-15 11:15:33 +00:00
continue ;
}
2021-04-07 22:33:25 +00:00
if ( DolibarrApiAccess :: $user -> admin ) { // If user for API is admin
if ( $field == 'admin' && $value != $this -> useraccount -> admin && empty ( $value )) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Reseting the admin status of a user is not possible using the API' );
2021-04-07 22:33:25 +00:00
}
} else {
if ( $field == 'admin' && $value != $this -> useraccount -> admin ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Only an admin user can modify the admin status of another user' );
2021-04-07 22:33:25 +00:00
}
}
if ( $field == 'entity' && $value != $this -> useraccount -> entity ) {
2024-04-07 12:59:25 +00:00
throw new RestException ( 403 , 'Changing entity of a user using the APIs is not possible' );
2021-04-07 22:33:25 +00:00
}
2023-12-15 11:15:33 +00:00
2019-01-27 12:20:33 +00:00
// The status must be updated using setstatus() because it
// is not handled by the update() method.
2023-12-15 11:15:33 +00:00
if ( $field == 'statut' || $field == 'status' ) {
2019-01-27 12:20:33 +00:00
$result = $this -> useraccount -> setstatus ( $value );
if ( $result < 0 ) {
2020-10-28 23:34:41 +00:00
throw new RestException ( 500 , 'Error when updating status of user: ' . $this -> useraccount -> error );
2019-01-27 12:20:33 +00:00
}
} else {
2024-04-02 10:28:55 +00:00
$this -> useraccount -> $field = $this -> _checkValForAPI ( $field , $value , $this -> useraccount );
2019-01-27 12:20:33 +00:00
}
2016-04-10 11:57:53 +00:00
}
2019-01-27 12:20:33 +00:00
// If there is no error, update() returns the number of affected
// rows so if the update is a no op, the return value is zezo.
2020-10-28 23:34:41 +00:00
if ( $this -> useraccount -> update ( DolibarrApiAccess :: $user ) >= 0 ) {
2016-04-10 11:57:53 +00:00
return $this -> get ( $id );
2020-05-21 13:05:19 +00:00
} else {
2017-12-21 15:50:18 +00:00
throw new RestException ( 500 , $this -> useraccount -> error );
2017-12-01 14:39:18 +00:00
}
2020-10-28 23:34:41 +00:00
}
2016-04-27 06:59:12 +00:00
2024-08-17 14:19:55 +00:00
/**
* Update a user password
*
2025-06-13 22:04:14 +00:00
* @ since 21.0 . 0 Initial implementation
*
2024-08-17 14:41:30 +00:00
* @ param int $id User ID
2024-08-17 14:19:55 +00:00
* @ param bool $send_password Only if set to true , the new password will send to the user
2024-08-17 14:41:30 +00:00
* @ return int 1 if password changed , 2 if password changed and sent
2024-08-17 14:19:55 +00:00
*
* @ throws RestException 403 Not allowed
* @ throws RestException 404 User not found
* @ throws RestException 500 System error
*
* @ url GET { id } / setPassword
*/
public function setPassword ( $id , $send_password = false )
{
2026-04-20 19:18:30 +00:00
if ( ! getDolGlobalInt ( 'API_ENABLE_LOGIN_API' )) {
2024-08-17 14:41:30 +00:00
throw new RestException ( 403 , " Error: login and password reset APIs are disabled. You can get access token from the backoffice to get access permission but permission and password manipulation from APIs are forbidden. " );
}
if ( ! getDolGlobalString ( 'API_ALLOW_PASSWORD_RESET' )) {
throw new RestException ( 403 , " Error: password reset APIs are disabled by default. To allow this, the option API_ALLOW_PASSWORD_RESET must be set. " );
}
2024-08-17 14:19:55 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
throw new RestException ( 403 , " setPassword on user not allowed for login " . DolibarrApiAccess :: $user -> login );
}
$result = $this -> useraccount -> fetch ( $id );
if ( ! $result ) {
throw new RestException ( 404 , 'User not found, no password changed' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
}
$newpassword = $this -> useraccount -> setPassword ( $this -> useraccount , '' ); // This will generate a new password
if ( is_int ( $newpassword ) && $newpassword < 0 ) {
throw new RestException ( 500 , 'ErrorFailedToSetNewPassword' . $this -> useraccount -> error );
} else {
// Success
if ( $send_password ) {
if ( $this -> useraccount -> send_password ( $this -> useraccount , $newpassword ) > 0 ) {
return 2 ;
} else {
throw new RestException ( 500 , 'ErrorFailedSendingNewPassword - ' . $this -> useraccount -> error );
}
} else {
return 1 ;
}
}
}
2019-01-27 12:52:08 +00:00
/**
* List the groups of a user
*
2025-06-13 22:04:14 +00:00
* @ since 10.0 . 0 Initial implementation
*
2019-01-27 12:52:08 +00:00
* @ param int $id Id of user
* @ return array Array of group objects
2024-10-06 11:52:05 +00:00
* @ phan - return Object []
* @ phpstan - return Object []
2019-01-27 12:52:08 +00:00
*
2025-10-23 11:56:22 +00:00
* @ throws RestException 400 Bad request
2020-03-17 12:50:20 +00:00
* @ throws RestException 403 Not allowed
2020-10-28 23:34:41 +00:00
* @ throws RestException 404 Not found
2019-01-27 12:52:08 +00:00
*
* @ url GET { id } / groups
*/
2019-02-25 19:35:59 +00:00
public function getGroups ( $id )
2019-01-27 12:52:08 +00:00
{
2025-10-23 11:56:22 +00:00
if ( $id == 0 ) {
throw new RestException ( 400 , 'No user with id=0 can exist' );
}
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2020-03-17 12:50:20 +00:00
throw new RestException ( 403 );
2019-01-27 12:52:08 +00:00
}
$user = new User ( $this -> db );
$result = $user -> fetch ( $id );
2019-11-16 22:24:29 +00:00
if ( ! $result ) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 404 , 'User not found' );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $user -> id , 'user' )) {
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2019-01-27 12:52:08 +00:00
}
$usergroup = new UserGroup ( $this -> db );
$groups = $usergroup -> listGroupsForUser ( $id , false );
$obj_ret = array ();
foreach ( $groups as $group ) {
$obj_ret [] = $this -> _cleanObjectDatas ( $group );
}
return $obj_ret ;
}
2020-10-28 23:34:41 +00:00
/**
2025-06-13 22:04:14 +00:00
* Add a user to a group
*
* @ since 5.0 . 0 Initial implementation
2016-04-27 06:59:12 +00:00
*
2017-05-24 13:24:35 +00:00
* @ param int $id User ID
* @ param int $group Group ID
2018-07-25 15:00:49 +00:00
* @ param int $entity Entity ID ( valid only for superadmin in multicompany transverse mode )
2017-05-24 13:24:35 +00:00
* @ return int 1 if success
2020-10-28 23:34:41 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Not allowed
2021-04-07 22:33:25 +00:00
* @ throws RestException 404 User not found
2022-03-18 15:14:20 +00:00
* @ throws RestException 500 System error
2021-04-07 22:33:25 +00:00
*
2016-06-13 21:24:54 +00:00
* @ url GET { id } / setGroup / { group }
2016-04-27 06:59:12 +00:00
*/
2020-10-28 23:34:41 +00:00
public function setGroup ( $id , $group , $entity = 1 )
{
2018-07-24 15:16:21 +00:00
global $conf ;
2023-06-19 22:59:44 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-08-17 14:20:06 +00:00
throw new RestException ( 403 , 'setGroup on users not allowed for login ' . DolibarrApiAccess :: $user -> login );
2021-04-07 22:33:25 +00:00
}
2018-07-25 15:00:49 +00:00
$result = $this -> useraccount -> fetch ( $id );
2020-10-28 23:34:41 +00:00
if ( ! $result ) {
2018-07-25 15:00:49 +00:00
throw new RestException ( 404 , 'User not found' );
}
2017-10-22 13:42:19 +00:00
2020-10-28 23:34:41 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2018-07-25 15:00:49 +00:00
}
2017-10-22 13:42:19 +00:00
2023-11-27 11:08:48 +00:00
if ( isModEnabled ( 'multicompany' ) && getDolGlobalString ( 'MULTICOMPANY_TRANSVERSE_MODE' ) && ! empty ( DolibarrApiAccess :: $user -> admin ) && empty ( DolibarrApiAccess :: $user -> entity )) {
2025-09-07 23:48:52 +00:00
$entity = ( ! empty ( $entity ) ? ( int ) $entity : $conf -> entity );
2020-05-21 13:05:19 +00:00
} else {
2018-07-25 15:00:49 +00:00
// 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
// hack the security by giving himself permissions on another entity.
2025-09-07 23:48:52 +00:00
$entity = ((( int ) DolibarrApiAccess :: $user -> entity ) > 0 ? ( int ) DolibarrApiAccess :: $user -> entity : $conf -> entity );
2018-07-25 15:00:49 +00:00
}
2017-10-22 13:42:19 +00:00
2018-07-25 15:00:49 +00:00
$result = $this -> useraccount -> SetInGroup ( $group , $entity );
2020-10-28 23:34:41 +00:00
if ( ! ( $result > 0 )) {
2018-07-25 15:00:49 +00:00
throw new RestException ( 500 , $this -> useraccount -> error );
}
return 1 ;
}
2019-11-26 20:36:30 +00:00
2025-10-23 11:56:22 +00:00
/**
* Create user group
*
* @ param array $request_data New user group data
* @ phan - param ? array < string , mixed > $request_data
* @ phpstan - param ? array < string , mixed > $request_data
* @ return int
*
* @ url POST / groups
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Not allowed
* @ throws RestException 500 Server Error
*/
public function postGroups ( $request_data = null )
{
// Check user authorization
2025-11-23 00:59:02 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'group_advance' , 'write' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2025-10-23 11:56:22 +00:00
throw new RestException ( 403 , " Usergroup creation not allowed for login " . DolibarrApiAccess :: $user -> login );
}
$usergroup = new UserGroup ( $this -> db );
foreach ( $request_data as $field => $value ) {
if ( $field === 'caller' ) {
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
$usergroup -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
continue ;
}
if ( $field == 'id' ) {
throw new RestException ( 400 , 'Creating with id field is forbidden' );
}
$usergroup -> $field = $this -> _checkValForAPI ( $field , $value , $usergroup );
}
if ( $usergroup -> create ( 1 ) < 0 ) {
throw new RestException ( 500 , 'Error creating' , array_merge ( array ( $usergroup -> error ), $usergroup -> errors ));
}
return $usergroup -> id ;
}
/**
* Update user group
*
* @ since 23.0 . 0 Initial implementation
*
* @ param int $group Id of usergroup to update
* @ param array $request_data Datas
* @ phan - param ? array < string , mixed > $request_data
* @ phpstan - param ? array < string , mixed > $request_data
* @ return Object Updated object
*
* @ url PUT / groups / { group }
*
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Not allowed
* @ throws RestException 500 Server Error
*/
public function putGroups ( $group , $request_data = null )
{
// Check user authorization
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'group_advance' , 'write' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
throw new RestException ( 403 , " Usergroup update not allowed " );
}
$usergroup = new UserGroup ( $this -> db );
$result = $usergroup -> fetch ( $group );
if ( $result < 1 ) {
throw new RestException ( 404 , 'Usergroup not found' );
}
foreach ( $request_data as $field => $value ) {
if ( $field == 'id' ) {
throw new RestException ( 400 , 'Updating with id field is forbidden' );
}
if ( $field === 'caller' ) {
// Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
$usergroup -> context [ 'caller' ] = sanitizeVal ( $request_data [ 'caller' ], 'aZ09' );
continue ;
}
if ( $field == 'entity' && $value != $usergroup -> entity ) {
throw new RestException ( 403 , 'Changing entity of a user using the APIs is not possible' );
}
$usergroup -> $field = $this -> _checkValForAPI ( $field , $value , $usergroup );
}
// If there is no error, update() returns the number of affected
// rows so if the update is a no op, the return value is zezo.
if ( $usergroup -> update () >= 0 ) {
return $this -> infoGroups ( $group );
} else {
throw new RestException ( 500 , $usergroup -> error );
}
}
2025-09-23 21:22:38 +00:00
/**
* Remove user from group ( only admin )
*
* @ since 23.0 . 0 Initial implementation
*
* @ url POST { id } / remove - group / { group }
*
* @ param int $id User ID
* @ param int $group Group ID
* @ return array { success : boolean , message : string }
*
* @ throws RestException 403 Not allowed - only admin
* @ throws RestException 503 Error
*
*/
public function removeUserFromGroup ( $id , $group )
{
if ( ! DolibarrApiAccess :: $user -> admin ) {
throw new RestException ( 403 , 'Only admin can remove users from groups' );
}
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " usergroup_user " ;
$sql .= " WHERE fk_user = " . (( int ) $id );
$sql .= " AND fk_usergroup = " . (( int ) $group );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
throw new RestException ( 503 , 'DB error: ' . $this -> db -> lasterror ());
}
return [
'success' => true ,
'message' => " User $id removed from group $group "
];
}
2019-11-26 20:33:42 +00:00
/**
2025-08-04 03:38:31 +00:00
* List groups of the current user ( so user of API token )
2019-11-26 20:33:42 +00:00
*
* Return an array with a list of Groups
2019-11-26 20:36:30 +00:00
*
2025-06-13 22:04:14 +00:00
* @ since 11.0 . 0 Initial implementation
*
2019-11-26 20:33:42 +00:00
* @ url GET / groups
*
* @ param string $sortfield Sort field
* @ param string $sortorder Sort order
* @ param int $limit Limit for list
* @ param int $page Page number
2023-09-26 16:43:25 +00:00
* @ param string $group_ids Groups ids filter field . Example : '1' or '1,2,3' { @ pattern /^ [ 0 - 9 ,] * $ / i }
2026-05-29 10:16:42 +00:00
* @ param string $sqlfilters Other criteria to filter answers separated by a comma . Syntax example " (t.ref:like:'SO-%') and (t.date_creation:>:'20160101') "
2024-01-12 16:18:52 +00:00
* @ param string $properties Restrict the data returned to these properties . Ignored if empty . Comma separated list of properties names
2019-11-26 20:33:42 +00:00
* @ return array Array of User objects
2024-10-06 11:52:05 +00:00
* @ phan - return Object []
* @ phpstan - return Object []
2021-04-07 22:33:25 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Not allowed
2021-04-07 22:33:25 +00:00
* @ throws RestException 404 User not found
* @ throws RestException 503 Error
2019-11-26 20:33:42 +00:00
*/
2024-01-10 16:01:40 +00:00
public function listGroups ( $sortfield = " t.rowid " , $sortorder = 'ASC' , $limit = 100 , $page = 0 , $group_ids = '0' , $sqlfilters = '' , $properties = '' )
2020-10-28 23:34:41 +00:00
{
$obj_ret = array ();
2019-11-26 20:33:42 +00:00
2024-02-09 14:58:49 +00:00
if (( ! getDolGlobalString ( 'MAIN_USE_ADVANCED_PERMS' ) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) ||
getDolGlobalString ( 'MAIN_USE_ADVANCED_PERMS' ) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'group_advance' , 'read' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , " You are not allowed to read groups " );
2020-10-28 23:34:41 +00:00
}
// case of external user, $societe param is ignored and replaced by user's socid
2025-07-01 18:34:09 +00:00
//$socid = DolibarrApiAccess::$user->socid ?: $societe;
2020-10-28 23:34:41 +00:00
$sql = " SELECT t.rowid " ;
2023-04-28 07:31:33 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " usergroup AS t LEFT JOIN " . MAIN_DB_PREFIX . " usergroup_extrafields AS ef ON (ef.fk_object = t.rowid) " ; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
2020-10-28 23:34:41 +00:00
$sql .= ' WHERE t.entity IN (' . getEntity ( 'user' ) . ')' ;
if ( $group_ids ) {
2021-03-22 10:30:18 +00:00
$sql .= " AND t.rowid IN ( " . $this -> db -> sanitize ( $group_ids ) . " ) " ;
2020-10-28 23:34:41 +00:00
}
// Add sql filters
if ( $sqlfilters ) {
2021-12-20 19:49:32 +00:00
$errormessage = '' ;
2023-02-25 18:48:33 +00:00
$sql .= forgeSQLFromUniversalSearchCriteria ( $sqlfilters , $errormessage );
if ( $errormessage ) {
throw new RestException ( 400 , 'Error when validating parameter sqlfilters -> ' . $errormessage );
2020-10-28 23:34:41 +00:00
}
}
$sql .= $this -> db -> order ( $sortfield , $sortorder );
if ( $limit ) {
if ( $page < 0 ) {
$page = 0 ;
}
$offset = $limit * $page ;
$sql .= $this -> db -> plimit ( $limit + 1 , $offset );
}
$result = $this -> db -> query ( $sql );
if ( $result ) {
$i = 0 ;
$num = $this -> db -> num_rows ( $result );
$min = min ( $num , ( $limit <= 0 ? $num : $limit ));
while ( $i < $min ) {
$obj = $this -> db -> fetch_object ( $result );
$group_static = new UserGroup ( $this -> db );
if ( $group_static -> fetch ( $obj -> rowid )) {
2023-09-26 16:04:48 +00:00
$obj_ret [] = $this -> _filterObjectProperties ( $this -> _cleanObjectDatas ( $group_static ), $properties );
2020-10-28 23:34:41 +00:00
}
$i ++ ;
}
} else {
throw new RestException ( 503 , 'Error when retrieve Group list : ' . $this -> db -> lasterror ());
}
2023-12-31 13:11:05 +00:00
2020-10-28 23:34:41 +00:00
return $obj_ret ;
2019-11-26 20:33:42 +00:00
}
2019-11-26 20:36:30 +00:00
2019-11-26 20:33:42 +00:00
/**
2025-06-13 22:04:14 +00:00
* Get properties of a user group
2019-11-26 20:33:42 +00:00
*
2024-01-12 16:18:52 +00:00
* Return an array with group information
2019-11-26 20:36:30 +00:00
*
2025-06-13 22:04:14 +00:00
* @ since 11.0 . 0 Initial implementation
*
2019-11-26 20:34:45 +00:00
* @ url GET / groups / { group }
2019-11-26 20:33:42 +00:00
*
2025-02-21 20:41:23 +00:00
* @ param int $group ID of group
* @ param int $load_members Load members list or not { @ min 0 } { @ max 1 }
2026-01-15 02:19:59 +00:00
* @ param int $includepermissions Set this to 1 to have the array of permissions loaded ( not done by default for performance purpose )
2025-02-21 20:41:23 +00:00
* @ return Object object of User objects
2021-04-07 22:33:25 +00:00
*
2025-10-23 11:56:22 +00:00
* @ throws RestException 400 Bad Request
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Not allowed
2021-04-07 22:33:25 +00:00
* @ throws RestException 404 User not found
2019-11-26 20:33:42 +00:00
*/
2026-01-15 02:19:59 +00:00
public function infoGroups ( $group , $load_members = 0 , $includepermissions = 0 )
2020-10-28 23:34:41 +00:00
{
2025-10-23 11:56:22 +00:00
if ( $group == 0 ) {
throw new RestException ( 400 , 'No usergroup with id=0 can exist' );
}
2024-02-09 14:58:49 +00:00
if (( ! getDolGlobalString ( 'MAIN_USE_ADVANCED_PERMS' ) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) ||
getDolGlobalString ( 'MAIN_USE_ADVANCED_PERMS' ) && ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'group_advance' , 'read' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , " You are not allowed to read groups " );
2020-10-28 23:34:41 +00:00
}
2019-11-26 20:33:42 +00:00
2020-10-28 23:34:41 +00:00
$group_static = new UserGroup ( $this -> db );
2025-02-21 20:41:23 +00:00
$result = $group_static -> fetch ( $group , '' , ( bool ) $load_members );
2019-11-26 20:33:42 +00:00
2025-10-23 11:56:22 +00:00
if ( $result < 1 ) {
throw new RestException ( 404 , 'Usergroup not found' );
2019-11-26 20:33:42 +00:00
}
2019-11-26 20:36:30 +00:00
2026-01-15 02:19:59 +00:00
if ( $includepermissions ) {
$group_static -> loadRights ();
}
2026-06-08 23:06:30 +00:00
if ( $load_members > 0 && is_array ( $group_static -> members ) && count ( $group_static -> members ) > 0 ) {
foreach ( $group_static -> members as & $member ) {
$member = $this -> _cleanObjectDatas ( $member );
}
}
2025-10-20 22:36:44 +00:00
return $this -> _cleanUserGroup ( $group_static );
2019-11-26 20:33:42 +00:00
}
2016-04-27 06:59:12 +00:00
2016-04-10 11:57:53 +00:00
/**
2025-06-13 22:04:14 +00:00
* Delete a user
*
* @ since 5.0 . 0 Initial implementation
2016-04-10 11:57:53 +00:00
*
* @ param int $id Account ID
* @ return array
2024-10-06 11:52:05 +00:00
* @ phan - return array { success : array { code : int , message : string }}
* @ phpstan - return array { success : array { code : int , message : string }}
2021-04-07 22:33:25 +00:00
*
2024-02-01 12:34:55 +00:00
* @ throws RestException 403 Not allowed
2021-04-07 22:33:25 +00:00
* @ throws RestException 404 User not found
2016-04-10 11:57:53 +00:00
*/
2020-10-28 23:34:41 +00:00
public function delete ( $id )
{
2024-02-09 14:58:49 +00:00
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'supprimer' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Not allowed' );
2021-04-07 22:33:25 +00:00
}
2016-04-11 21:27:43 +00:00
$result = $this -> useraccount -> fetch ( $id );
2020-10-28 23:34:41 +00:00
if ( ! $result ) {
2016-04-10 11:57:53 +00:00
throw new RestException ( 404 , 'User not found' );
}
2020-10-28 23:34:41 +00:00
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $this -> useraccount -> id , 'user' )) {
2024-02-01 12:34:55 +00:00
throw new RestException ( 403 , 'Access not allowed for login ' . DolibarrApiAccess :: $user -> login );
2016-04-10 11:57:53 +00:00
}
2025-02-21 20:41:23 +00:00
$this -> useraccount -> oldcopy = clone $this -> useraccount ; // @phan-suppress-current-line PhanTypeMismatchProperty
2022-11-25 23:50:45 +00:00
if ( ! $this -> useraccount -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 );
}
return array (
'success' => array (
'code' => 200 ,
2025-10-23 11:56:22 +00:00
'message' => 'User deleted'
)
);
}
/**
* Delete a usergroup
*
* @ since 23.0 . 0 Initial implementation
*
* @ param int $group usergroup ID
* @ return array
* @ phan - return array { success : array { code : int , message : string }}
* @ phpstan - return array { success : array { code : int , message : string }}
*
* @ url DELETE / groups / { group }
*
* @ throws RestException 403 Not allowed
* @ throws RestException 404 User not found
*/
public function deleteGroups ( $group )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'group_advance' , 'delete' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
throw new RestException ( 403 , 'Not allowed' );
}
$usergroup = new UserGroup ( $this -> db );
$result = $usergroup -> fetch ( $group );
if ( $result < 0 ) {
throw new RestException ( 404 , 'Usergroup not found' );
}
if ( ! $usergroup -> delete ( DolibarrApiAccess :: $user )) {
throw new RestException ( 500 );
}
return array (
'success' => array (
'code' => 200 ,
'message' => 'Usergroup deleted'
2022-11-25 23:50:45 +00:00
)
);
2016-04-10 11:57:53 +00:00
}
2025-05-14 14:54:33 +00:00
/**
2025-06-13 22:04:14 +00:00
* Get notifications for a user
2025-05-14 14:54:33 +00:00
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id ID of the user
*
* @ return array
* @ phan - return array < array { id : int , socid : int , event : string , contact_id : int , datec : int , tms : string , type : string } >
* @ phpstan - return array < array { id : int , socid : int , event : string , contact_id : int , datec : int , tms : string , type : string } >
*
* @ url GET { id } / notifications
*
2025-10-23 11:56:22 +00:00
* @ throws RestException 400 Bad Request
* @ throws RestException 403 Access denied
* @ throws RestException 404 Not found
2025-05-14 14:54:33 +00:00
*/
public function getUserNotification ( $id )
{
if ( empty ( $id )) {
2025-10-23 11:56:22 +00:00
throw new RestException ( 400 , 'No user with id=0 can exist' );
2025-05-14 14:54:33 +00:00
}
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'lire' ) && empty ( DolibarrApiAccess :: $user -> admin )) {
throw new RestException ( 403 );
}
if ( ! DolibarrApi :: _checkAccessToResource ( 'user' , $id )) {
2026-08-08 23:18:22 +00:00
throw new RestException ( 403 , 'Access on this object not allowed for login ' . DolibarrApiAccess :: $user -> login );
2025-05-14 14:54:33 +00:00
}
/**
* We select all the records that match the socid
*/
$sql = " SELECT rowid as id, fk_action as event, fk_user, type, datec, tms " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " notify_def " ;
$sql .= " WHERE fk_user = " . (( int ) $id );
$result = $this -> db -> query ( $sql );
if ( $this -> db -> num_rows ( $result ) == 0 ) {
throw new RestException ( 404 , 'Notification not found' );
}
$i = 0 ;
$notifications = array ();
if ( $result ) {
$num = $this -> db -> num_rows ( $result );
2025-09-28 20:20:44 +00:00
//$min = min($num, ($limit <= 0 ? $num : $limit));
$min = $num ;
while ( $i < $min ) {
2025-05-14 14:54:33 +00:00
$obj = $this -> db -> fetch_object ( $result );
$notifications [] = $obj ;
$i ++ ;
}
} else {
throw new RestException ( 404 , 'No notifications found' );
}
$fields = array ( 'id' , 'fk_user' , 'event' , 'datec' , 'tms' , 'type' );
$returnNotifications = array ();
foreach ( $notifications as $notification ) {
$object = array ();
foreach ( $notification as $key => $value ) {
if ( in_array ( $key , $fields )) {
$object [ $key ] = $value ;
}
}
$returnNotifications [] = $object ;
}
// Too complex for phan ?: @phan-suppress-next-line PhanTypeMismatchReturn
return $returnNotifications ;
}
/**
2025-06-13 22:04:14 +00:00
* Create a notification for a user
2025-05-14 14:54:33 +00:00
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id ID of the user
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
*
* @ return array | mixed Notification of the user
*
* @ url POST { id } / notifications
*
* @ throws RestException
*/
public function createUserNotification ( $id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
throw new RestException ( 403 , " User has no right to update users " );
}
if ( $this -> useraccount -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Error creating User Notification, User doesn\'t exists' );
}
$notification = new Notify ( $this -> db );
$notification -> fk_user = $id ;
foreach ( $request_data as $field => $value ) {
2026-05-08 23:16:35 +00:00
$notification -> $field = $this -> _checkValForAPI ( $field , $value , $notification );
2025-05-14 14:54:33 +00:00
}
$event = $notification -> event ;
if ( ! $event ) {
throw new RestException ( 500 , 'Error creating User Notification, request_data missing event' );
}
$fk_user = $notification -> fk_user ;
$exists_sql = " SELECT rowid, fk_action as event, fk_user, type, datec, tms as datem " ;
$exists_sql .= " FROM " . MAIN_DB_PREFIX . " notify_def " ;
$exists_sql .= " WHERE fk_action = ' " . $this -> db -> escape (( string ) $event ) . " ' " ;
$exists_sql .= " AND fk_user = ' " . $this -> db -> escape (( string ) $fk_user ) . " ' " ;
$exists_result = $this -> db -> query ( $exists_sql );
if ( $this -> db -> num_rows ( $exists_result ) > 0 ) {
throw new RestException ( 403 , 'Notification already exists' );
}
if ( $notification -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error creating User Notification' );
}
if ( $notification -> update ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error updating values' );
}
return $this -> _cleanObjectDatas ( $notification );
}
/**
2025-06-13 22:04:14 +00:00
* Create a notification for a user using action trigger code
2025-05-14 14:54:33 +00:00
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id ID of the user
* @ param string $code Action Trigger code
* @ param array $request_data Request data
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
*
* @ return array | mixed Notification for the user
* @ phan - return Notify
*
* @ url POST { id } / notificationsbycode / { code }
*
* @ throws RestException
*/
public function createUserNotificationByCode ( $id , $code , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
throw new RestException ( 403 , " User has no right to update users " );
}
if ( $this -> useraccount -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Error creating User Notification, User doesn\'t exists' );
}
$notification = new Notify ( $this -> db );
$notification -> fk_user = $id ;
$sql = " SELECT t.rowid as id FROM " . MAIN_DB_PREFIX . " c_action_trigger as t " ;
$sql .= " WHERE t.code = ' " . $this -> db -> escape ( $code ) . " ' " ;
$result = $this -> db -> query ( $sql );
if ( $this -> db -> num_rows ( $result ) == 0 ) {
throw new RestException ( 404 , 'Action Trigger code not found' );
}
$notification -> event = $this -> db -> fetch_row ( $result )[ 0 ];
foreach ( $request_data as $field => $value ) {
if ( $field === 'event' ) {
throw new RestException ( 500 , 'Error creating User Notification, request_data contains event key' );
}
if ( $field === 'fk_action' ) {
throw new RestException ( 500 , 'Error creating User Notification, request_data contains fk_action key' );
}
2026-05-08 23:16:35 +00:00
$notification -> $field = $this -> _checkValForAPI ( $field , $value , $notification );
2025-05-14 14:54:33 +00:00
}
$event = $notification -> event ;
$fk_user = $notification -> fk_user ;
$exists_sql = " SELECT rowid, fk_action as event, fk_user, type, datec, tms as datem " ;
$exists_sql .= " FROM " . MAIN_DB_PREFIX . " notify_def " ;
$exists_sql .= " WHERE fk_action = ' " . $this -> db -> escape (( string ) $event ) . " ' " ;
$exists_sql .= " AND fk_user = ' " . $this -> db -> escape (( string ) $fk_user ) . " ' " ;
$exists_result = $this -> db -> query ( $exists_sql );
if ( $this -> db -> num_rows ( $exists_result ) > 0 ) {
throw new RestException ( 403 , 'Notification already exists' );
}
if ( $notification -> create ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error creating User Notification, are request_data well formed?' );
}
if ( $notification -> update ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error updating values' );
}
return $this -> _cleanObjectDatas ( $notification );
}
/**
2025-06-13 22:04:14 +00:00
* Delete a notification attached to a user
2025-05-14 14:54:33 +00:00
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id ID of the user
* @ param int $notification_id ID of UserNotification
*
* @ return int - 1 if error , 1 if correct deletion
*
* @ url DELETE { id } / notifications / { notification_id }
*
* @ throws RestException
*/
public function deleteUserNotification ( $id , $notification_id )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
throw new RestException ( 403 , " User has no right to update users " );
}
$notification = new Notify ( $this -> db );
$notification -> fetch ( $notification_id );
$fk_user = ( int ) $notification -> fk_user ;
if ( $fk_user == $id ) {
return $notification -> delete ( DolibarrApiAccess :: $user );
} else {
throw new RestException ( 403 , " Not allowed due to bad consistency of input data " );
}
}
/**
2025-06-13 22:04:14 +00:00
* Update a notification for a user
2025-05-14 14:54:33 +00:00
*
* @ since 22.0 . 0 Initial implementation
*
* @ param int $id ID of the User
* @ param int $notification_id ID of UserNotification
* @ param array $request_data Request data
* @ return array | mixed Notification for the user
*
* @ phan - param ? array < string , string > $request_data
* @ phpstan - param ? array < string , string > $request_data
*
* @ url PUT { id } / notifications / { notification_id }
*
* @ throws RestException
*/
public function updateUserNotification ( $id , $notification_id , $request_data = null )
{
if ( ! DolibarrApiAccess :: $user -> hasRight ( 'user' , 'user' , 'creer' )) {
throw new RestException ( 403 , " User has no right to update users " );
}
if ( $this -> useraccount -> fetch ( $id ) <= 0 ) {
throw new RestException ( 404 , 'Error creating Notification, User doesn\'t exists' );
}
$notification = new Notify ( $this -> db );
// @phan-suppress-next-line PhanPluginSuspiciousParamPosition
$notification -> fetch ( $notification_id , $id );
if ( $notification -> fk_user != $id ) {
throw new RestException ( 403 , " Not allowed due to bad consistency of input data " );
}
foreach ( $request_data as $field => $value ) {
2026-05-08 23:16:35 +00:00
$notification -> $field = $this -> _checkValForAPI ( $field , $value , $notification );
2025-05-14 14:54:33 +00:00
}
if ( $notification -> update ( DolibarrApiAccess :: $user ) < 0 ) {
throw new RestException ( 500 , 'Error updating values' );
}
return $this -> _cleanObjectDatas ( $notification );
}
2020-10-31 14:59:33 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
2017-05-16 16:12:10 +00:00
/**
* Clean sensible object datas
2025-11-20 00:47:46 +00:00
* @ phpstan - template T
2017-05-16 16:12:10 +00:00
*
2023-09-26 16:43:25 +00:00
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
2025-11-20 00:47:46 +00:00
* @ phpstan - param T $object
* @ phpstan - return T
2017-05-16 16:12:10 +00:00
*/
2019-03-04 18:57:46 +00:00
protected function _cleanObjectDatas ( $object )
2017-11-25 12:17:38 +00:00
{
2020-10-31 14:59:33 +00:00
// phpcs:enable
2020-10-28 23:34:41 +00:00
$object = parent :: _cleanObjectDatas ( $object );
unset ( $object -> default_values );
unset ( $object -> lastsearch_values );
unset ( $object -> lastsearch_values_tmp );
unset ( $object -> total_ht );
unset ( $object -> total_tva );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
unset ( $object -> label_incoterms );
unset ( $object -> location_incoterms );
unset ( $object -> fk_delivery_address );
unset ( $object -> fk_incoterms );
unset ( $object -> all_permissions_are_loaded );
unset ( $object -> shipping_method_id );
unset ( $object -> nb_rights );
unset ( $object -> search_sid );
unset ( $object -> ldap_sid );
unset ( $object -> clicktodial_loaded );
2026-08-09 01:11:50 +00:00
unset ( $object -> lines );
unset ( $object -> model_pdf );
2020-10-28 23:34:41 +00:00
// List of properties never returned by API, whatever are permissions
unset ( $object -> pass );
unset ( $object -> pass_indatabase );
unset ( $object -> pass_indatabase_crypted );
unset ( $object -> pass_temp );
unset ( $object -> api_key );
unset ( $object -> clicktodial_password );
unset ( $object -> openid );
2024-02-09 14:58:49 +00:00
$canreadsalary = (( isModEnabled ( 'salaries' ) && DolibarrApiAccess :: $user -> hasRight ( 'salaries' , 'read' )) || ! isModEnabled ( 'salaries' ));
2020-10-28 23:34:41 +00:00
if ( ! $canreadsalary ) {
2017-11-25 00:24:24 +00:00
unset ( $object -> salary );
unset ( $object -> salaryextra );
unset ( $object -> thm );
unset ( $object -> tjm );
}
2017-11-25 00:16:12 +00:00
2020-10-28 23:34:41 +00:00
return $object ;
2017-10-22 13:42:19 +00:00
}
2025-10-20 22:36:44 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
/**
* Clean sensible usergroup object datas
*
* @ param Object $object Object to clean
* @ return Object Object with cleaned properties
*/
private function _cleanUserGroup ( $object )
{
// phpcs:enable
$object = parent :: _cleanObjectDatas ( $object );
unset ( $object -> actiontypecode );
unset ( $object -> all_permissions_are_loaded );
unset ( $object -> barcode_type_coder );
unset ( $object -> barcode_type );
unset ( $object -> canvas );
unset ( $object -> civility_code );
unset ( $object -> civility_id );
unset ( $object -> clicktodial_loaded );
unset ( $object -> cond_reglement_id );
unset ( $object -> cond_reglement_supplier_id );
unset ( $object -> contact_id );
unset ( $object -> contacts_ids_internal );
unset ( $object -> contacts_ids );
unset ( $object -> country_code );
unset ( $object -> country_id );
unset ( $object -> date_cloture );
unset ( $object -> date_creation );
unset ( $object -> date_modification );
unset ( $object -> date_validation );
unset ( $object -> default_values );
unset ( $object -> demand_reason_id );
unset ( $object -> deposit_percent );
unset ( $object -> extraparams );
unset ( $object -> firstname );
unset ( $object -> fk_account );
unset ( $object -> fk_delivery_address );
unset ( $object -> fk_incoterms );
unset ( $object -> fk_multicurrency );
unset ( $object -> fk_project );
unset ( $object -> fk_user_creat );
unset ( $object -> fk_user_modif );
unset ( $object -> globalgroup );
unset ( $object -> import_key );
unset ( $object -> last_main_doc );
unset ( $object -> lastname );
unset ( $object -> lastsearch_values_tmp );
unset ( $object -> lastsearch_values );
unset ( $object -> ldap_sid );
unset ( $object -> libelle_incoterms );
unset ( $object -> lines );
unset ( $object -> linkedObjectsIds );
unset ( $object -> location_incoterms );
unset ( $object -> members );
unset ( $object -> mode_reglement_id );
unset ( $object -> module );
unset ( $object -> multicurrency_code );
unset ( $object -> multicurrency_total_ht );
unset ( $object -> multicurrency_total_localtax1 );
unset ( $object -> multicurrency_total_localtax2 );
unset ( $object -> multicurrency_total_ttc );
unset ( $object -> multicurrency_total_tva );
unset ( $object -> multicurrency_tx );
unset ( $object -> nb_rights );
unset ( $object -> nb_users );
2025-10-23 11:56:22 +00:00
unset ( $object -> note_public );
2025-10-20 22:36:44 +00:00
unset ( $object -> origin_id );
unset ( $object -> origin_type );
unset ( $object -> product );
unset ( $object -> ref_ext );
unset ( $object -> ref );
unset ( $object -> region_id );
unset ( $object -> retained_warranty_fk_cond_reglement );
unset ( $object -> rights );
unset ( $object -> search_sid );
unset ( $object -> shipping_method_id );
unset ( $object -> shipping_method );
unset ( $object -> specimen );
unset ( $object -> state_id );
unset ( $object -> status );
unset ( $object -> statut );
unset ( $object -> total_ht );
unset ( $object -> total_localtax1 );
unset ( $object -> total_localtax2 );
unset ( $object -> total_ttc );
unset ( $object -> total_tva );
unset ( $object -> totalpaid_multicurrency );
unset ( $object -> totalpaid );
unset ( $object -> transport_mode_id );
unset ( $object -> TRIGGER_PREFIX );
unset ( $object -> user_closing_id );
unset ( $object -> user_creation_id );
unset ( $object -> user_modification_id );
unset ( $object -> user_validation_id );
unset ( $object -> user );
unset ( $object -> usergroup_entity );
unset ( $object -> warehouse_id );
return $object ;
}
2020-10-28 23:34:41 +00:00
/**
* Clean sensible user group list datas
*
2024-10-06 11:52:05 +00:00
* @ param array < UserGroup > $objectList Array of object to clean
* @ return array < UserGroup > Array of cleaned object properties
2020-10-28 23:34:41 +00:00
*/
private function _cleanUserGroupListDatas ( $objectList )
{
$cleanObjectList = array ();
foreach ( $objectList as $object ) {
$cleanObject = parent :: _cleanObjectDatas ( $object );
unset ( $cleanObject -> default_values );
unset ( $cleanObject -> lastsearch_values );
unset ( $cleanObject -> lastsearch_values_tmp );
unset ( $cleanObject -> total_ht );
unset ( $cleanObject -> total_tva );
unset ( $cleanObject -> total_localtax1 );
unset ( $cleanObject -> total_localtax2 );
unset ( $cleanObject -> total_ttc );
unset ( $cleanObject -> libelle_incoterms );
unset ( $cleanObject -> location_incoterms );
unset ( $cleanObject -> fk_delivery_address );
unset ( $cleanObject -> fk_incoterms );
unset ( $cleanObject -> all_permissions_are_loaded );
unset ( $cleanObject -> shipping_method_id );
unset ( $cleanObject -> nb_rights );
unset ( $cleanObject -> search_sid );
unset ( $cleanObject -> ldap_sid );
unset ( $cleanObject -> clicktodial_loaded );
unset ( $cleanObject -> datec );
2023-04-03 10:22:05 +00:00
unset ( $cleanObject -> tms );
2020-10-28 23:34:41 +00:00
unset ( $cleanObject -> members );
unset ( $cleanObject -> note );
unset ( $cleanObject -> note_private );
$cleanObjectList [] = $cleanObject ;
}
return $cleanObjectList ;
}
2019-11-14 16:57:29 +00:00
2016-04-10 11:57:53 +00:00
/**
* Validate fields before create or update object
2020-10-28 23:34:41 +00:00
*
2024-10-06 11:52:05 +00:00
* @ param ? array < string , mixed > $data Data to validate
* @ return array < string , mixed >
2016-04-10 11:57:53 +00:00
* @ throws RestException
2020-10-28 23:34:41 +00:00
*/
2024-06-09 11:26:45 +00:00
private function _validate ( $data ) // @phpstan-ignore-line
2020-10-28 23:34:41 +00:00
{
$account = array ();
foreach ( Users :: $FIELDS as $field ) {
if ( ! isset ( $data [ $field ])) {
throw new RestException ( 400 , " $field field missing " );
}
$account [ $field ] = $data [ $field ];
}
return $account ;
}
2016-04-10 11:57:53 +00:00
}