2025-06-23 14:32:34 +00:00
< ? php
/* Copyright ( C ) 2005 - 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2010 - 2015 Regis Houssin < regis . houssin @ inodbox . com >
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro . com >
* Copyright ( C ) 2018 Ferran Marcet < fmarcet @ 2 byte . es >
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
*
* 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
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
/**
2025-11-20 22:51:37 +00:00
* \file htdocs / user / api_token / list . php
2025-07-10 08:34:04 +00:00
* \brief Page to show user list of token
2025-06-23 14:32:34 +00:00
*/
// Load Dolibarr environment
require '../../main.inc.php' ;
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
2025-11-20 22:51:37 +00:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/usergroups.lib.php' ;
2025-06-23 14:32:34 +00:00
// Load translation files required by page
2025-06-24 09:40:42 +00:00
$langs -> loadLangs ( array ( 'admin' , 'users' ));
2025-07-01 07:33:12 +00:00
$error = 0 ;
2025-06-23 14:32:34 +00:00
2025-06-24 09:40:42 +00:00
// Security check
2025-06-23 14:32:34 +00:00
$id = GETPOSTINT ( 'id' );
if ( ! isset ( $id ) || empty ( $id )) {
accessforbidden ();
}
2025-07-10 08:34:04 +00:00
$socid = 0 ;
if ( $user -> socid > 0 ) {
$socid = $user -> socid ;
}
$feature2 = (( $socid && $user -> hasRight ( " user " , " self " , " write " )) ? '' : 'user' );
$result = restrictedArea ( $user , 'user' , $id , 'user&user' , $feature2 );
// $user is current user, $id is id of edited user
$canreaduser = ( $user -> admin || ( $user -> id == $id ));
$canedittoken = ( $user -> admin || (( $user -> id == $id ) && $user -> hasRight ( " user " , " self " , " write " )));
if ( ! $canreaduser ) {
accessforbidden ();
}
2025-06-24 09:40:42 +00:00
// Retrieve needed GETPOSTS for this file
2025-06-26 15:04:42 +00:00
// Action / Massaction
$action = GETPOST ( 'action' , 'aZ09' );
$massaction = GETPOST ( 'massaction' , 'alpha' );
2025-06-27 08:21:08 +00:00
$confirm = GETPOST ( 'confirm' , 'alpha' );
2025-06-24 09:18:06 +00:00
$toselect = GETPOST ( 'toselect' , 'array' );
2025-06-26 15:04:42 +00:00
// List filters
$search_entity = GETPOST ( 'search_entity' , 'alpha' );
$search_datec_startday = GETPOSTINT ( 'search_datec_startday' );
$search_datec_startmonth = GETPOSTINT ( 'search_datec_startmonth' );
$search_datec_startyear = GETPOSTINT ( 'search_datec_startyear' );
$search_datec_endday = GETPOSTINT ( 'search_datec_endday' );
$search_datec_endmonth = GETPOSTINT ( 'search_datec_endmonth' );
$search_datec_endyear = GETPOSTINT ( 'search_datec_endyear' );
$search_datec_start = dol_mktime ( 0 , 0 , 0 , $search_datec_startmonth , $search_datec_startday , $search_datec_startyear );
$search_datec_end = dol_mktime ( 23 , 59 , 59 , $search_datec_endmonth , $search_datec_endday , $search_datec_endyear );
$search_tms_startday = GETPOSTINT ( 'search_tms_startday' );
$search_tms_startmonth = GETPOSTINT ( 'search_tms_startmonth' );
$search_tms_startyear = GETPOSTINT ( 'search_tms_startyear' );
$search_tms_endday = GETPOSTINT ( 'search_tms_endday' );
$search_tms_endmonth = GETPOSTINT ( 'search_tms_endmonth' );
$search_tms_endyear = GETPOSTINT ( 'search_tms_endyear' );
$search_tms_start = dol_mktime ( 0 , 0 , 0 , $search_tms_startmonth , $search_tms_startday , $search_tms_startyear );
$search_tms_end = dol_mktime ( 23 , 59 , 59 , $search_tms_endmonth , $search_tms_endday , $search_tms_endyear );
// Pagination
$limit = GETPOSTINT ( 'limit' ) ? GETPOSTINT ( 'limit' ) : $conf -> liste_limit ;
$sortfield = GETPOST ( 'sortfield' , 'aZ09comma' );
$sortorder = GETPOST ( 'sortorder' , 'aZ09comma' );
$page = GETPOSTISSET ( 'pageplusone' ) ? ( GETPOSTINT ( 'pageplusone' ) - 1 ) : GETPOSTINT ( " page " );
if ( empty ( $page ) || $page < 0 || GETPOST ( 'button_search' , 'alpha' ) || GETPOST ( 'button_removefilter' , 'alpha' )) {
$page = 0 ;
}
$offset = $limit * $page ;
$pageprev = $page - 1 ;
$pagenext = $page + 1 ;
if ( ! $sortfield ) {
2025-07-07 09:39:15 +00:00
$sortfield = 'oat.tms' ;
2025-06-26 15:04:42 +00:00
}
if ( ! $sortorder ) {
$sortorder = 'DESC' ;
}
$arrayfields = array (
2025-07-01 08:06:53 +00:00
'oat.datec' => array ( 'label' => " DateCreation " , 'checked' => '1' ),
'oat.tms' => array ( 'label' => " DateModification " , 'checked' => '1' ),
2025-06-26 15:04:42 +00:00
);
2025-06-23 14:32:34 +00:00
$object = new User ( $db );
$object -> fetch ( $id , '' , '' , 1 );
$object -> loadRights ();
2025-07-09 07:25:57 +00:00
// Deny access if user not using api
if ( empty ( $object -> api_key )) {
accessforbidden ();
}
2025-06-23 14:32:34 +00:00
$form = new Form ( $db );
2025-11-20 22:51:37 +00:00
2025-06-23 14:32:34 +00:00
/*
* Actions
*/
$parameters = array ( 'id' => $socid );
$reshook = $hookmanager -> executeHooks ( 'doActions' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
}
if ( empty ( $reshook )) {
2025-06-26 15:04:42 +00:00
if ( GETPOST ( 'button_removefilter_x' , 'alpha' ) || GETPOST ( 'button_removefilter.x' , 'alpha' ) || GETPOST ( 'button_removefilter' , 'alpha' )) { // All tests are required to be compatible with all browsers
$search_entity = '' ;
$search_datec_start = '' ;
$search_datec_end = '' ;
$search_tms_start = '' ;
$search_tms_end = '' ;
$toselect = array ();
}
2025-06-27 08:21:08 +00:00
if ( GETPOST ( 'button_removefilter_x' , 'alpha' ) || GETPOST ( 'button_removefilter.x' , 'alpha' ) || GETPOST ( 'button_removefilter' , 'alpha' )
|| GETPOST ( 'button_search_x' , 'alpha' ) || GETPOST ( 'button_search.x' , 'alpha' ) || GETPOST ( 'button_search' , 'alpha' )) {
$massaction = '' ; // Protection to avoid mass action if we force a new search during a mass action confirmation
}
2025-06-26 15:04:42 +00:00
2025-07-09 07:25:57 +00:00
if (( $action == 'delete' && $confirm == 'yes' ) && $canedittoken ) {
2025-07-01 07:33:12 +00:00
$db -> begin ();
$nbok = 0 ;
2025-11-21 13:40:33 +00:00
// $toselect could contain duplicate entries, cf https://github.com/Dolibarr/dolibarr/issues/26244
2025-07-01 07:33:12 +00:00
$unique_arr = array_unique ( $toselect );
foreach ( $unique_arr as $toselectid ) {
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " oauth_token " ;
2025-11-21 13:40:33 +00:00
$sql .= " WHERE rowid = " . (( int ) $toselectid );
2025-07-01 07:33:12 +00:00
$sql .= " AND service = 'dolibarr_rest_api' " ;
2025-11-21 13:40:33 +00:00
$sql .= " AND entity = " . (( int ) $conf -> entity );
2025-07-01 07:33:12 +00:00
$result = $db -> query ( $sql );
if ( $result > 0 ) {
$nbok ++ ;
} else {
setEventMessages ( $db -> error (), null , 'errors' );
$error ++ ;
break ;
}
}
if ( empty ( $error )) {
// Message for elements well deleted
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsDeleted " , $nbok ), null , 'mesgs' );
} elseif ( $nbok > 0 ) {
setEventMessages ( $langs -> trans ( " RecordDeleted " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " NoRecordDeleted " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
2025-06-23 14:32:34 +00:00
}
/*
* View
*/
$person_name = ! empty ( $object -> firstname ) ? $object -> lastname . " , " . $object -> firstname : $object -> lastname ;
2025-07-07 14:12:24 +00:00
$title = $person_name . " - " . $langs -> trans ( 'ApiTokens' );
2025-06-23 14:32:34 +00:00
$help_url = '' ;
2025-06-26 15:04:42 +00:00
$nbtotalofrecords = '' ;
if ( ! getDolGlobalInt ( 'MAIN_DISABLE_FULL_SCANLIST' )) {
/* The fast and low memory method to get and count full list converts the sql into a sql count */
$sqlforcount = 'SELECT COUNT(*) as nbtotalofrecords' ;
2025-07-01 08:06:53 +00:00
$sqlforcount .= " FROM " . MAIN_DB_PREFIX . " oauth_token as oat " ;
2025-11-21 13:41:31 +00:00
$sqlforcount .= " WHERE entity = " . (( int ) $conf -> entity );
2025-11-20 22:51:37 +00:00
$sqlforcount .= " AND fk_user = " . (( int ) $id );
2025-07-01 10:11:37 +00:00
$sqlforcount .= " AND service = 'dolibarr_rest_api' " ;
2025-06-26 15:04:42 +00:00
$resql = $db -> query ( $sqlforcount );
if ( $resql ) {
$objforcount = $db -> fetch_object ( $resql );
$nbtotalofrecords = $objforcount -> nbtotalofrecords ;
} else {
dol_print_error ( $db );
}
if (( $page * $limit ) > $nbtotalofrecords ) { // if total resultset is smaller then paging size (filtering), goto and load page 0
$page = 0 ;
$offset = 0 ;
}
$db -> free ( $resql );
}
2025-07-07 09:39:15 +00:00
$sql = " SELECT oat.rowid, oat.token, oat.entity, oat.state as rights, oat.datec as date_creation, oat.tms as date_modification " ;
2025-07-01 14:09:12 +00:00
if ( isModEnabled ( 'multicompany' )) {
$sql .= " , e.label as entity_name " ;
}
2025-07-01 08:06:53 +00:00
$sql .= " FROM " . MAIN_DB_PREFIX . " oauth_token as oat " ;
2025-07-01 14:09:12 +00:00
if ( isModEnabled ( 'multicompany' )) {
$sql .= " JOIN " . $db -> prefix () . " entity as e ON oat.entity = e.rowid " ;
}
2025-07-01 10:11:37 +00:00
$sql .= " WHERE oat.fk_user = " . (( int ) $object -> id );
$sql .= " AND service = 'dolibarr_rest_api' " ;
2025-06-26 15:04:42 +00:00
if ( $search_entity ) {
2025-07-01 08:06:53 +00:00
$sql .= natural_search ( 'oat.entity' , $search_entity );
2025-06-26 15:04:42 +00:00
}
if ( $search_datec_start ) {
2025-07-01 08:06:53 +00:00
$sql .= " AND oat.datec >= ' " . $db -> idate ( $search_datec_start ) . " ' " ;
2025-06-26 15:04:42 +00:00
}
if ( $search_datec_end ) {
2025-07-01 08:06:53 +00:00
$sql .= " AND oat.datec <= ' " . $db -> idate ( $search_datec_end ) . " ' " ;
2025-06-26 15:04:42 +00:00
}
if ( $search_tms_start ) {
2025-07-01 08:06:53 +00:00
$sql .= " AND oat.tms >= ' " . $db -> idate ( $search_tms_start ) . " ' " ;
2025-06-26 15:04:42 +00:00
}
if ( $search_tms_end ) {
2025-07-01 08:06:53 +00:00
$sql .= " AND oat.tms <= ' " . $db -> idate ( $search_tms_end ) . " ' " ;
2025-06-26 15:04:42 +00:00
}
$sql .= $db -> order ( $sortfield , $sortorder );
if ( $limit ) {
$sql .= $db -> plimit ( $limit + 1 , $offset );
}
$resql = $db -> query ( $sql );
$num = $db -> num_rows ( $resql );
$param = '&id=' . $id ; // We always need the id of the user
if ( $limit > 0 && $limit != $conf -> liste_limit ) {
$param .= '&limit=' . (( int ) $limit );
}
if ( $search_datec_startday ) {
$param .= '&search_date_startday=' . urlencode (( string ) ( $search_datec_startday ));
}
if ( $search_datec_startmonth ) {
$param .= '&search_date_startmonth=' . urlencode (( string ) ( $search_datec_startmonth ));
}
if ( $search_datec_startyear ) {
$param .= '&search_date_startyear=' . urlencode (( string ) ( $search_datec_startyear ));
}
if ( $search_datec_endday ) {
$param .= '&search_date_endday=' . urlencode (( string ) ( $search_datec_endday ));
}
if ( $search_datec_endmonth ) {
$param .= '&search_date_endmonth=' . urlencode (( string ) ( $search_datec_endmonth ));
}
if ( $search_datec_endyear ) {
$param .= '&search_date_endyear=' . urlencode (( string ) ( $search_datec_endyear ));
}
if ( $search_tms_startday ) {
$param .= '&search_date_startday=' . urlencode (( string ) ( $search_tms_startday ));
}
if ( $search_tms_startmonth ) {
$param .= '&search_date_startmonth=' . urlencode (( string ) ( $search_tms_startmonth ));
}
if ( $search_tms_startyear ) {
$param .= '&search_date_startyear=' . urlencode (( string ) ( $search_tms_startyear ));
}
if ( $search_tms_endday ) {
$param .= '&search_date_endday=' . urlencode (( string ) ( $search_tms_endday ));
}
if ( $search_tms_endmonth ) {
$param .= '&search_date_endmonth=' . urlencode (( string ) ( $search_tms_endmonth ));
}
if ( $search_tms_endyear ) {
$param .= '&search_date_endyear=' . urlencode (( string ) ( $search_tms_endyear ));
}
2025-06-24 09:18:06 +00:00
$arrayofselected = is_array ( $toselect ) ? $toselect : array ();
2025-06-23 14:32:34 +00:00
$head = user_prepare_head ( $object );
$linkback = '<a href="' . DOL_URL_ROOT . '/user/list.php?restore_lastsearch_values=1">' . $langs -> trans ( " BackToList " ) . '</a>' ;
$morehtmlref = '<a href="' . DOL_URL_ROOT . '/user/vcard.php?id=' . $object -> id . '&output=file&file=' . urlencode ( dol_sanitizeFileName ( $object -> getFullName ( $langs ) . '.vcf' )) . '" class="refid" rel="noopener">' ;
$morehtmlref .= img_picto ( $langs -> trans ( " Download " ) . ' ' . $langs -> trans ( " VCard " ), 'vcard.png' , 'class="valignmiddle marginleftonly paddingrightonly"' );
$morehtmlref .= '</a>' ;
$urltovirtualcard = '/user/virtualcard.php?id=' . (( int ) $object -> id );
$morehtmlref .= dolButtonToOpenUrlInDialogPopup ( 'publicvirtualcard' , $langs -> transnoentitiesnoconv ( " PublicVirtualCardUrl " ) . ' - ' . $object -> getFullName ( $langs ), img_picto ( $langs -> trans ( " PublicVirtualCardUrl " ), 'card' , 'class="valignmiddle marginleftonly paddingrightonly"' ), $urltovirtualcard , '' , 'nohover' );
2025-07-07 13:54:52 +00:00
llxHeader ( '' , $title , $help_url , '' , 0 , 0 , '' , '' , '' , 'mod-user page-card_param_ihm' );
2025-06-23 14:32:34 +00:00
print '<!-- Token section -->' . " \n " ;
2025-07-04 07:54:34 +00:00
$arrayofmassactions = array (
'predelete' => img_picto ( '' , 'delete' , 'class="pictofixedwidth"' ) . $langs -> trans ( " Delete " )
);
2025-06-27 08:21:08 +00:00
if ( GETPOSTINT ( 'nomassaction' ) || in_array ( $massaction , array ( 'presend' , 'predelete' ))) {
$arrayofmassactions = array ();
}
2025-06-24 09:40:42 +00:00
$massactionbutton = $form -> selectMassAction ( '' , $arrayofmassactions );
2025-06-26 08:57:52 +00:00
$tmpurlforbutton = DOL_URL_ROOT . '/user/api_token/card.php?id=' . $id . '&action=create' ;
2025-07-07 13:54:52 +00:00
$morehtmlright = dolGetButtonTitle ( $langs -> trans ( 'New' ), '' , 'fa fa-plus-circle' , $tmpurlforbutton );
2025-06-23 14:32:34 +00:00
2025-06-26 15:04:42 +00:00
print '<form method="POST" action="' . $_SERVER [ " PHP_SELF " ] . '?id=' . $id . '">' . " \n " ;
2025-06-24 09:18:06 +00:00
print '<input type="hidden" name="token" value="' . newToken () . '">' ;
2025-06-26 15:04:42 +00:00
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">' ;
print '<input type="hidden" name="action" value="list">' ;
print '<input type="hidden" name="sortfield" value="' . $sortfield . '">' ;
print '<input type="hidden" name="sortorder" value="' . $sortorder . '">' ;
2025-07-09 14:44:21 +00:00
// @phan-suppress-next-line PhanPluginSuspiciousParamOrder
2025-07-07 15:04:39 +00:00
print_barre_liste ( $langs -> trans ( " ListOfTokensForUser " ), $page , $_SERVER [ " PHP_SELF " ], $param , $sortfield , $sortorder , $massactionbutton , $num , $nbtotalofrecords , 'fa-at' , 0 , $morehtmlright , '' , $limit , 0 , 0 , 1 );
2025-06-23 14:32:34 +00:00
2025-07-01 07:33:12 +00:00
include DOL_DOCUMENT_ROOT . '/core/tpl/massactions_pre.tpl.php' ;
2025-07-07 13:54:52 +00:00
$colspan = 5 ; // Base colspan for empty list
2025-06-26 15:04:42 +00:00
2025-07-07 13:54:52 +00:00
include DOL_DOCUMENT_ROOT . '/core/tpl/apitoken_list.tpl.php' ;
2025-06-26 15:04:42 +00:00
2025-07-07 13:54:52 +00:00
print '</form>' ;
2025-06-23 14:32:34 +00:00
// End of page
llxFooter ();
$db -> close ();