Webservice: add get_user_last_connexion webservice -refs BT#21212

pull/5015/head
NicoDucou 2 years ago
parent 71096d13ae
commit c59ef36968
  1. 27
      main/inc/lib/webservices/Rest.php
  2. 14
      main/webservices/api/v2.php

@ -101,6 +101,7 @@ class Rest extends WebService
public const DELETE_USER = 'delete_user';
public const GET_USERS_API_KEYS = 'get_users_api_keys';
public const GET_USER_API_KEY = 'get_user_api_key';
public const GET_USER_LAST_CONNEXION = 'get_user_last_connexion';
public const GET_USER_SUB_GROUP = 'get_user_sub_group';
public const GET_COURSES = 'get_courses';
@ -3957,6 +3958,32 @@ class Rest extends WebService
];
}
/**
* @throws Exception
*/
public function getUserLastConnexion(string $username): array
{
if (false === api_get_configuration_value('webservice_enable_adminonly_api')
|| !UserManager::is_admin($this->user->getId())
) {
self::throwNotAllowedException();
}
$userInfo = api_get_user_info_from_username($username);
if (empty($userInfo)) {
throw new Exception(get_lang('UserNotFound'));
}
$lastConnexionDate = Tracking::get_last_connection_date($userInfo['id']);
return [
'id' => $userInfo['id'],
'username' => $userInfo['username'],
'last_connexion_date' => $lastConnexionDate,
];
}
public static function isAllowedByRequest(bool $inpersonate = false): bool
{
$username = $_GET['username'] ?? null;

@ -627,6 +627,20 @@ try {
)
);
break;
case Rest::GET_USER_LAST_CONNEXION:
$username = (string) $httpRequest->query->get('user');
if (empty($username)) {
throw new Exception(get_lang('NoData'));
}
Event::addEvent(LOG_WS.$action, 'username', $username);
$restResponse->setData(
$restApi->getUserLastConnexion(
$username,
)
);
break;
case Rest::GET_USER_SUB_GROUP:
$userId = isset($_POST['user_id']) ? (int) $_POST['user_id'] : 0;
if (empty($userId)) {

Loading…
Cancel
Save