2015-05-02 16:14:51 +00:00
< ? php
2015-05-05 23:25:56 +00:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2016-09-26 00:18:11 +00:00
* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2023-01-18 09:48:18 +00:00
* Copyright ( C ) 2023 Ferran Marcet < fmarcet @ 2 byte . es >
2026-04-15 15:59:06 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2015-05-05 23:25:56 +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 />.
2015-05-05 23:25:56 +00:00
*/
2015-05-02 16:14:51 +00:00
2015-11-22 16:17:06 +00:00
// Create the autoloader for Luracast
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/AutoLoader.php' ;
2024-03-14 20:59:15 +00:00
call_user_func (
/**
* @ return Luracast\Restler\AutoLoader
*/
static function () {
$loader = Luracast\Restler\AutoLoader :: instance ();
spl_autoload_register ( $loader );
return $loader ;
}
);
2015-11-22 16:17:06 +00:00
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/iAuthenticate.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/iUseAuthentication.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/Resources.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/Defaults.php' ;
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/RestException.php' ;
2023-10-26 10:22:54 +00:00
2023-04-30 01:10:20 +00:00
use Luracast\Restler\iAuthenticate ;
use Luracast\Restler\Resources ;
use Luracast\Restler\Defaults ;
use Luracast\Restler\RestException ;
2015-05-02 16:14:51 +00:00
/**
2015-05-05 22:55:42 +00:00
* Dolibarr API access class
2015-05-02 16:14:51 +00:00
*/
class DolibarrApiAccess implements iAuthenticate
{
const REALM = 'Restricted Dolibarr API' ;
2015-05-12 14:42:35 +00:00
2023-08-05 23:26:06 +00:00
/**
* @ var DoliDB Database handler
*/
public $db ;
2015-05-02 16:14:51 +00:00
/**
2025-10-29 17:58:06 +00:00
* @ var string [] Role required by API method user / external / admin
2015-05-02 16:14:51 +00:00
*/
2020-04-10 08:59:32 +00:00
public static $requires = array ( 'user' , 'external' , 'admin' );
2015-05-12 14:42:35 +00:00
2015-05-02 21:54:35 +00:00
/**
2025-10-29 17:58:06 +00:00
* @ var string User role
2015-05-02 21:54:35 +00:00
*/
2020-10-31 13:32:18 +00:00
public static $role = 'user' ;
2015-05-12 14:42:35 +00:00
2015-05-02 23:54:04 +00:00
/**
2025-10-29 17:58:06 +00:00
* @ var User Loggued user
2015-05-02 23:54:04 +00:00
*/
2024-02-09 14:58:49 +00:00
public static $user = null ;
2015-05-12 14:42:35 +00:00
2020-09-19 21:30:29 +00:00
/**
* Constructor
*/
public function __construct ()
{
global $db ;
$this -> db = $db ;
}
2020-10-31 13:32:18 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName
2015-05-02 21:54:35 +00:00
/**
* Check access
2015-05-12 14:42:35 +00:00
*
2015-06-02 13:12:19 +00:00
* @ return bool
2020-04-03 01:04:23 +00:00
*
* @ throws RestException 401 Forbidden
* @ throws RestException 503 Technical error
2015-05-02 21:54:35 +00:00
*/
2015-06-15 11:25:02 +00:00
public function __isAllowed ()
2015-06-02 13:12:19 +00:00
{
2020-10-31 13:32:18 +00:00
// phpcs:enable
2025-11-21 00:19:14 +00:00
global $conf , $langs , $user ;
2015-05-02 23:54:04 +00:00
2016-05-08 10:32:18 +00:00
$login = '' ;
2015-05-02 21:54:35 +00:00
$stored_key = '' ;
2015-05-12 14:42:35 +00:00
2015-05-02 16:14:51 +00:00
$userClass = Defaults :: $userIdentifierClass ;
2015-05-12 14:42:35 +00:00
2017-01-22 19:55:26 +00:00
/* foreach ( $_SERVER as $key => $val )
{
2021-02-23 16:44:43 +00:00
dol_syslog ( $key . ' - ' . $val );
2017-01-22 19:55:26 +00:00
} */
2017-06-25 10:26:16 +00:00
2017-01-22 19:55:26 +00:00
// api key can be provided in url with parameter api_key=xxx or ni header with header DOLAPIKEY:xxx
$api_key = '' ;
2024-03-28 16:10:22 +00:00
if ( isset ( $_GET [ 'api_key' ])) { // For backward compatibility. Keep $_GET here.
2020-10-31 13:32:18 +00:00
// TODO Add option to disable use of api key on url. Return errors if used.
$api_key = $_GET [ 'api_key' ];
2017-01-22 19:55:26 +00:00
}
2021-02-23 16:44:43 +00:00
if ( isset ( $_GET [ 'DOLAPIKEY' ])) {
2020-10-31 13:32:18 +00:00
// TODO Add option to disable use of api key on url. Return errors if used.
$api_key = $_GET [ 'DOLAPIKEY' ]; // With GET method
2017-01-22 19:55:26 +00:00
}
2025-02-17 15:02:22 +00:00
// TODO Can filter on user agent.
//$api_useragent = $_SERVER['HTTP_USER_AGENT'];
if ( isset ( $_SERVER [ 'HTTP_DOLAPIKEY' ])) { // HTTP Header entry "DOLAPIKEY: ..." can be read with $_SERVER["HTTP_DOLAPIKEY"]
$api_key = $_SERVER [ 'HTTP_DOLAPIKEY' ]; // With header method (recommended)
2025-02-17 17:18:57 +00:00
} elseif ( empty ( $api_key )) {
2025-02-17 15:02:22 +00:00
$headers = getallheaders (); // HTTP Header entry "Authorization: Bearer ..." can be read with getallheaders
$api_key = preg_replace ( '/^Bearer\s+/i' , '' , empty ( $headers [ 'Authorization' ]) ? '' : $headers [ 'Authorization' ]);
};
2024-01-31 23:54:03 +00:00
2025-06-30 13:47:49 +00:00
$api_key = dol_string_nounprintableascii ( $api_key , 1 );
2024-01-31 23:54:03 +00:00
2023-03-15 11:54:04 +00:00
if ( preg_match ( '/^dolcrypt:/i' , $api_key )) {
throw new RestException ( 503 , 'Bad value for the API key. An API key should not start with dolcrypt:' );
}
2017-06-25 10:26:16 +00:00
2021-02-23 16:44:43 +00:00
if ( $api_key ) {
2017-10-16 14:23:50 +00:00
$userentity = 0 ;
2025-11-20 22:51:37 +00:00
$token_rowid = 0 ;
2017-10-16 14:23:50 +00:00
2025-11-20 22:51:37 +00:00
if ( ! getDolGlobalString ( 'API_IN_TOKEN_TABLE' )) {
2026-04-14 17:53:29 +00:00
if ( isModEnabled ( 'multicompany' ) && getDolGlobalString ( 'MULTICOMPANY_TRANSVERSE_MODE' ) && defined ( " DOLENTITY " )) {
2026-08-06 15:58:33 +00:00
$sql = " SELECT DISTINCT u.login, u.datec, u.api_key as use_api, u.api_key as api_key, 0 as token_rowid, " ;
2026-04-14 17:53:29 +00:00
$sql .= " u.tms as date_modification, " ;
$sql .= " gu.entity, gu.entity as token_entity " ;
$sql .= " FROM " . $this -> db -> prefix () . " user as u " ;
$sql .= " JOIN " . $this -> db -> prefix () . " usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = " . (( int ) $conf -> entity );
$sql .= " WHERE u.api_key = ' " . $this -> db -> escape ( $api_key ) . " ' OR u.api_key = ' " . $this -> db -> escape ( dolEncrypt ( $api_key , '' , '' , 'dolibarr' )) . " ' " ;
} else {
$sql = " SELECT u.login, u.datec, u.api_key as use_api, u.entity, u.api_key as api_key, u.entity as token_entity, 0 as token_rowid, " ;
$sql .= " u.tms as date_modification " ;
$sql .= " FROM " . $this -> db -> prefix () . " user as u " ;
$sql .= " WHERE u.api_key = ' " . $this -> db -> escape ( $api_key ) . " ' OR u.api_key = ' " . $this -> db -> escape ( dolEncrypt ( $api_key , '' , '' , 'dolibarr' )) . " ' " ;
}
2025-11-20 22:51:37 +00:00
} else {
2026-04-14 17:53:29 +00:00
if ( isModEnabled ( 'multicompany' ) && getDolGlobalString ( 'MULTICOMPANY_TRANSVERSE_MODE' ) && defined ( " DOLENTITY " )) {
2026-08-06 15:58:33 +00:00
$sql = " SELECT DISTINCT u.login, u.datec, u.api_key as use_api, oat.tokenstring as api_key, oat.entity as token_entity, rowid as token_rowid, " ;
2026-04-14 17:53:29 +00:00
$sql .= " oat.tms as date_modification, " ;
$sql .= " gu.entity " ;
$sql .= " FROM " . $this -> db -> prefix () . " oauth_token AS oat " ;
$sql .= " JOIN " . $this -> db -> prefix () . " user AS u ON u.rowid = oat.fk_user " ;
$sql .= " JOIN " . $this -> db -> prefix () . " usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = " . (( int ) $conf -> entity );
$sql .= " WHERE (oat.tokenstring = ' " . $this -> db -> escape ( $api_key ) . " ' " ;
$sql .= " OR oat.tokenstring = ' " . $this -> db -> escape ( dolEncrypt ( $api_key , '' , '' , 'dolibarr' )) . " ') " ;
$sql .= " AND gu.entity = oat.entity " ;
$sql .= " AND oat.service = 'dolibarr_rest_api' " ;
} else {
2026-05-06 20:55:18 +00:00
$sql = " SELECT u.login, u.datec, u.api_key as use_api, u.entity, oat.tokenstring as api_key, oat.entity as token_entity, oat.rowid as token_rowid, " ;
2026-04-14 17:53:29 +00:00
$sql .= " oat.tms as date_modification " ;
$sql .= " FROM " . $this -> db -> prefix () . " oauth_token AS oat " ;
$sql .= " JOIN " . $this -> db -> prefix () . " user AS u ON u.rowid = oat.fk_user " ;
$sql .= " WHERE (oat.tokenstring = ' " . $this -> db -> escape ( $api_key ) . " ' " ;
$sql .= " OR oat.tokenstring = ' " . $this -> db -> escape ( dolEncrypt ( $api_key , '' , '' , 'dolibarr' )) . " ') " ;
$sql .= " AND oat.service = 'dolibarr_rest_api' " ;
}
2025-11-20 22:51:37 +00:00
}
2017-10-19 15:30:08 +00:00
2020-09-19 21:30:29 +00:00
$result = $this -> db -> query ( $sql );
2021-02-23 16:44:43 +00:00
if ( $result ) {
2023-03-13 11:48:25 +00:00
$nbrows = $this -> db -> num_rows ( $result );
if ( $nbrows == 1 ) {
2020-09-19 21:30:29 +00:00
$obj = $this -> db -> fetch_object ( $result );
2025-11-20 22:51:37 +00:00
2015-05-02 21:54:35 +00:00
$login = $obj -> login ;
2023-03-13 11:48:25 +00:00
$stored_key = dolDecrypt ( $obj -> api_key );
2017-10-16 14:23:50 +00:00
$userentity = $obj -> entity ;
2025-11-20 22:51:37 +00:00
$token_entity = $obj -> token_entity ;
$token_rowid = $obj -> token_rowid ;
2017-10-16 14:23:50 +00:00
2021-02-23 16:44:43 +00:00
if ( ! defined ( " DOLENTITY " ) && $conf -> entity != ( $obj -> entity ? $obj -> entity : 1 )) { // If API was not forced with HTTP_DOLENTITY, and user is on another entity, so we reset entity to entity of user
2020-04-10 08:59:32 +00:00
$conf -> entity = ( $obj -> entity ? $obj -> entity : 1 );
2017-10-19 15:30:08 +00:00
// We must also reload global conf to get params from the entity
2024-01-10 18:51:35 +00:00
dol_syslog ( " Entity was not set on http header with HTTP_DOLAPIENTITY (recommended for performance purpose), so we switch now on entity of user ( " . $conf -> entity . " ) and we have to reload configuration. " , LOG_WARNING );
2020-09-19 21:30:29 +00:00
$conf -> setValues ( $this -> db );
2025-08-28 00:50:38 +00:00
// set global mysoc after setting conf entity (the entity can be changed with the user logged)
// see master.inc.php
require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
2025-11-21 00:19:14 +00:00
$fmysoc = new Societe ( $this -> db );
2025-08-28 00:50:38 +00:00
$fmysoc -> setMysoc ( $conf );
// We set some specific default values according to country
if ( $fmysoc -> country_code == 'DE' && ! isset ( $conf -> global -> MAIN_INVERT_SENDER_RECIPIENT )) {
// For DE, we need to invert our address with customer address
$conf -> global -> MAIN_INVERT_SENDER_RECIPIENT = 1 ;
}
if ( $fmysoc -> country_code == 'FR' && ! isset ( $conf -> global -> INVOICE_CATEGORY_OF_OPERATION )) {
// For FR, default value of option to show category of operations is on by default. Decret n°2099-1299 2022-10-07
$conf -> global -> INVOICE_CATEGORY_OF_OPERATION = 1 ;
}
if ( $fmysoc -> country_code == 'FR' && ! isset ( $conf -> global -> INVOICE_DISABLE_REPLACEMENT )) {
// For FR, the replacement invoice type is not allowed.
// From an accounting point of view, this creates holes in the numbering of the invoice.
// This is very problematic during a fiscal control.
$conf -> global -> INVOICE_DISABLE_REPLACEMENT = 1 ;
}
if ( $fmysoc -> country_code == 'GR' && ! isset ( $conf -> global -> INVOICE_DISABLE_REPLACEMENT )) {
// The replacement invoice type is not allowed in Greece.
$conf -> global -> INVOICE_DISABLE_REPLACEMENT = 1 ;
}
if ( $fmysoc -> country_code == 'GR' && ! isset ( $conf -> global -> INVOICE_DISABLE_DEPOSIT )) {
// The deposit invoice type is not allowed in Greece.
$conf -> global -> INVOICE_DISABLE_DEPOSIT = 1 ;
}
if (( $fmysoc -> localtax1_assuj || $fmysoc -> localtax2_assuj ) && ! isset ( $conf -> global -> MAIN_NO_INPUT_PRICE_WITH_TAX )) {
// For countries using the 2nd or 3rd tax, we disable input/edit of lines using the price including tax (because 2nb and 3rd tax not yet taken into account).
// Work In Progress to support all taxes into unit price entry when MAIN_UNIT_PRICE_WITH_TAX_IS_FOR_ALL_TAXES is set.
$conf -> global -> MAIN_NO_INPUT_PRICE_WITH_TAX = 1 ;
}
// Set also the global variable $mysoc
2025-09-07 23:27:38 +00:00
global $mysoc ;
2025-08-28 00:50:38 +00:00
$mysoc = $fmysoc ;
// Reload langs
2025-11-20 22:51:37 +00:00
$langcode = getDolGlobalString ( 'MAIN_LANG_DEFAULT' , 'auto' );
2025-08-28 00:50:38 +00:00
if ( ! empty ( $user -> conf -> MAIN_LANG_DEFAULT )) {
$langcode = $user -> conf -> MAIN_LANG_DEFAULT ;
}
if ( $langs -> getDefaultLang () != $langcode ) {
$langs -> setDefaultLang ( $langcode );
$langs -> tab_translate = array ();
$langs -> loadLangs ( array ( 'main' ));
}
2017-10-16 14:23:50 +00:00
}
2025-11-20 22:51:37 +00:00
if ( $conf -> entity != ( $token_entity ? $token_entity : 1 )) {
throw new RestException ( 401 , " functions_isallowed::check_user_api_key Authentication KO for ' " . $login . " ': Token not valid (may be a typo or a wrong entity) " );
2025-07-09 09:15:07 +00:00
}
2023-03-13 11:48:25 +00:00
} elseif ( $nbrows > 1 ) {
throw new RestException ( 503 , 'Error when fetching user api_key : More than 1 user with this apikey' );
2015-05-02 21:54:35 +00:00
}
2020-05-21 13:05:19 +00:00
} else {
2025-03-02 14:10:25 +00:00
throw new RestException ( 503 , 'Error when fetching user api_key :' . $this -> db -> error );
2015-05-02 23:54:04 +00:00
}
2015-05-02 21:54:35 +00:00
2023-07-19 02:14:49 +00:00
if ( $login && $stored_key != $api_key ) { // This should not happen since we did a search on api_key
2017-01-22 19:55:26 +00:00
$userClass :: setCacheIdentifier ( $api_key );
2015-05-02 16:14:51 +00:00
return false ;
}
2015-05-12 14:42:35 +00:00
2023-07-19 02:14:49 +00:00
$genericmessageerroruser = 'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity=' . $conf -> entity . ')' ;
2021-02-23 16:44:43 +00:00
if ( ! $login ) {
2023-07-19 02:14:49 +00:00
dol_syslog ( " functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key " , LOG_NOTICE );
2024-01-10 18:51:35 +00:00
sleep ( 1 ); // Anti brute force protection. Must be same delay when user and password are not valid.
2023-07-19 02:14:49 +00:00
throw new RestException ( 401 , $genericmessageerroruser );
2016-05-08 10:32:18 +00:00
}
2023-01-18 18:54:38 +00:00
2020-09-19 21:30:29 +00:00
$fuser = new User ( $this -> db );
2024-07-31 16:54:52 +00:00
$result = $fuser -> fetch ( 0 , $login , '' , 0 , ( empty ( $userentity ) ? - 1 : $conf -> entity )); // If user is not entity 0, we search in working entity $conf->entity (that may have been forced to a different value than user entity)
2017-10-16 14:23:50 +00:00
if ( $result <= 0 ) {
2023-07-19 02:14:49 +00:00
dol_syslog ( " functions_isallowed::check_user_api_key Authentication KO for ' " . $login . " ': Failed to fetch on entity " , LOG_NOTICE );
2024-01-10 18:51:35 +00:00
sleep ( 1 ); // Anti brute force protection. Must be same delay when user and password are not valid.
2023-07-19 02:14:49 +00:00
throw new RestException ( 401 , $genericmessageerroruser );
2015-05-02 23:54:04 +00:00
}
2023-01-27 13:27:51 +00:00
// Check if user status is enabled
2025-07-28 10:05:25 +00:00
if ( $fuser -> status != $fuser :: STATUS_ENABLED ) {
2023-01-27 13:27:51 +00:00
// Status is disabled
2023-07-19 02:14:49 +00:00
dol_syslog ( " functions_isallowed::check_user_api_key Authentication KO for ' " . $login . " ': The user has been disabled " , LOG_NOTICE );
2024-01-10 18:51:35 +00:00
sleep ( 1 ); // Anti brute force protection. Must be same delay when user and password are not valid.
2023-07-19 02:14:49 +00:00
throw new RestException ( 401 , $genericmessageerroruser );
2023-01-18 09:48:18 +00:00
}
2021-09-02 14:06:19 +00:00
2023-01-27 13:06:31 +00:00
// Check if session was unvalidated by a password change
if (( $fuser -> flagdelsessionsbefore && ! empty ( $_SESSION [ " dol_logindate " ]) && $fuser -> flagdelsessionsbefore > $_SESSION [ " dol_logindate " ])) {
// Session is no more valid
2023-07-19 02:14:49 +00:00
dol_syslog ( " functions_isallowed::check_user_api_key Authentication KO for ' " . $login . " ': The user has a date for session invalidation = " . $fuser -> flagdelsessionsbefore . " and a session date = " . $_SESSION [ " dol_logindate " ] . " . We must invalidate its sessions. " );
2024-01-10 18:51:35 +00:00
sleep ( 1 ); // Anti brute force protection. Must be same delay when user and password are not valid.
2023-07-19 02:14:49 +00:00
throw new RestException ( 401 , $genericmessageerroruser );
2023-01-18 18:54:38 +00:00
}
2023-01-27 13:06:31 +00:00
// Check date validity
if ( $fuser -> isNotIntoValidityDateRange ()) {
// User validity dates are no more valid
2024-01-10 18:51:35 +00:00
dol_syslog ( " functions_isallowed::check_user_api_key Authentication KO for ' " . $login . " ': The user login has a validity between [ " . $fuser -> datestartvalidity . " and " . $fuser -> dateendvalidity . " ], current date is " . dol_now ());
sleep ( 1 ); // Anti brute force protection. Must be same delay when user and password are not valid.
2023-07-19 02:14:49 +00:00
throw new RestException ( 401 , $genericmessageerroruser );
2023-01-18 18:54:38 +00:00
}
2025-07-28 10:05:25 +00:00
// Increase counter of API access
if ( getDolGlobalString ( 'API_COUNTER_ENABLED' )) {
2025-11-20 22:51:37 +00:00
if ( ! getDolGlobalString ( 'API_IN_TOKEN_TABLE' )) {
// Update the counter into table llx_const
include DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
dolibarr_set_const ( $this -> db , 'API_COUNTER_COUNT' , getDolGlobalInt ( 'API_COUNTER_COUNT' ) + 1 );
//var_dump('eeee');exit;
} else {
// Update the counter into table llx_oauth_token
$tmpnow = dol_getdate ( dol_now ( 'gmt' ), true , 'gmt' );
2025-11-21 00:19:14 +00:00
$sqlforcounter = " UPDATE " . $this -> db -> prefix () . " oauth_token SET " ;
2025-11-20 22:51:37 +00:00
$sqlforcounter .= " apicount_total = apicount_total + 1, " ;
$sqlforcounter .= " apicount_month = apicount_month + 1, " ;
// if last access was done during previous month, we save pageview_month into pageviews_previous_month
2025-11-21 00:19:14 +00:00
$sqlforcounter .= " pageviews_previous_month = " . $this -> db -> ifsql ( " lastaccess < ' " . $this -> db -> idate ( dol_mktime ( 0 , 0 , 0 , $tmpnow [ 'mon' ], 1 , $tmpnow [ 'year' ], 'gmt' , 0 ), 'gmt' ) . " ' " , 'apicount_month' , 'apicount_previous_month' ) . " , " ;
$sqlforcounter .= " lastaccess = ' " . $this -> db -> idate ( dol_now ( 'gmt' ), 'gmt' ) . " ' " ;
2025-11-20 22:51:37 +00:00
$sqlforcounter .= " WHERE rowid = " . (( int ) $token_rowid );
$this -> db -> query ( $sqlforcounter );
}
2025-07-28 10:05:25 +00:00
}
2023-01-18 18:54:38 +00:00
// User seems valid
2025-11-20 22:51:37 +00:00
$fuser -> loadRights ();
2021-09-02 14:06:19 +00:00
// Set the property $user to the $user of API
2015-05-03 12:44:37 +00:00
static :: $user = $fuser ;
2015-05-12 14:42:35 +00:00
2021-09-02 14:06:19 +00:00
// Set also the global variable $user to the $user of API
2021-08-29 19:13:41 +00:00
$user = $fuser ;
2015-05-12 14:42:35 +00:00
2019-11-04 19:20:38 +00:00
if ( $fuser -> socid ) {
2015-05-02 21:54:35 +00:00
static :: $role = 'external' ;
2020-10-31 13:32:18 +00:00
}
2015-05-12 14:42:35 +00:00
2019-11-04 19:20:38 +00:00
if ( $fuser -> admin ) {
2015-05-02 21:54:35 +00:00
static :: $role = 'admin' ;
2020-10-31 13:32:18 +00:00
}
} else {
2025-02-17 15:02:22 +00:00
throw new RestException ( 401 , " Failed to login to API. Neither parameter 'HTTP_DOLAPIKEY' nor 'Authentication: Bearer' found on HTTP header (and no parameter DOLAPIKEY in URL). " );
2015-05-02 16:14:51 +00:00
}
2015-05-02 23:54:04 +00:00
2020-10-31 13:32:18 +00:00
$userClass :: setCacheIdentifier ( static :: $role );
2026-08-18 10:31:40 +00:00
2020-10-31 13:32:18 +00:00
Resources :: $accessControlFunction = 'DolibarrApiAccess::verifyAccess' ;
$requirefortest = static :: $requires ;
2021-02-23 16:44:43 +00:00
if ( ! is_array ( $requirefortest )) {
$requirefortest = explode ( ',' , $requirefortest );
}
2020-10-31 13:32:18 +00:00
return in_array ( static :: $role , ( array ) $requirefortest ) || static :: $role == 'admin' ;
2015-05-02 16:14:51 +00:00
}
2015-05-12 14:42:35 +00:00
2020-10-31 13:32:18 +00:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName
2015-06-02 13:12:19 +00:00
/**
* @ return string string to be used with WWW - Authenticate header
*/
2020-10-31 13:32:18 +00:00
public function __getWWWAuthenticateString ()
{
// phpcs:enable
return '' ;
}
/**
2026-08-18 10:31:40 +00:00
* Check that the role of user is among a the given list defined into static :: $requires
2020-10-31 13:32:18 +00:00
*
2025-03-02 14:10:25 +00:00
* @ param array { class : array { DolibarrApiAccess : array { properties : array { requires ? : bool }}}} $m Properties of method
2020-10-31 13:32:18 +00:00
*
* @ access private
* @ return bool
*/
public static function verifyAccess ( array $m )
{
$requires = isset ( $m [ 'class' ][ 'DolibarrApiAccess' ][ 'properties' ][ 'requires' ])
? $m [ 'class' ][ 'DolibarrApiAccess' ][ 'properties' ][ 'requires' ]
: false ;
return $requires
? static :: $role == 'admin' || in_array ( static :: $role , ( array ) $requires )
: true ;
}
2015-05-02 16:14:51 +00:00
}