diff --git a/admin/api_settings.php b/admin/api_settings.php index f6a16102..02c7a5ea 100644 --- a/admin/api_settings.php +++ b/admin/api_settings.php @@ -188,12 +188,17 @@ require_once(HESK_PATH . 'inc/show_admin_nav.inc.php'); - - + + Generated Token:

NOTE: Please record this token, as this is the only time you will be able to view it!

+ + +

All tokens for this user have been removed!

+ + diff --git a/internal-api/admin/api-authentication/index.php b/internal-api/admin/api-authentication/index.php index cb6189f7..eb922362 100644 --- a/internal-api/admin/api-authentication/index.php +++ b/internal-api/admin/api-authentication/index.php @@ -34,7 +34,8 @@ if ($request_method == 'POST') { output($token); return http_response_code(200); } elseif ($action == 'reset') { - //TODO + reset_tokens($user_id, $hesk_settings); + return http_response_code(204); } else { return http_response_code(400); } diff --git a/internal-api/dao/api_authentication_dao.php b/internal-api/dao/api_authentication_dao.php index 109e52e4..a9f62316 100644 --- a/internal-api/dao/api_authentication_dao.php +++ b/internal-api/dao/api_authentication_dao.php @@ -2,6 +2,11 @@ function store_token($user_id, $token_hash, $hesk_settings) { $sql = "INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "user_api_tokens` (`user_id`, `token`) - VALUES ('" . hesk_dbEscape($user_id) . "', '" . hesk_dbEscape($token_hash) . "')"; + VALUES (" . intval($user_id) . ", '" . hesk_dbEscape($token_hash) . "')"; + hesk_dbQuery($sql); +} + +function reset_tokens($user_id, $hesk_settings) { + $sql = "DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "user_api_tokens` WHERE `user_id` = ".intval($user_id); hesk_dbQuery($sql); } \ No newline at end of file diff --git a/internal-api/js/api-settings.js b/internal-api/js/api-settings.js index 67931e27..f224a954 100644 --- a/internal-api/js/api-settings.js +++ b/internal-api/js/api-settings.js @@ -69,6 +69,8 @@ function generateToken(userId) { var endpoint = getHelpdeskUrl(); endpoint += '/internal-api/admin/api-authentication/'; markSaving('token-' + userId); + $('#token-' + userId + '-reset').addClass('hide'); + $('#token-' + userId + '-created').addClass('hide'); var data = { userId: userId, action: 'generate' @@ -78,7 +80,7 @@ function generateToken(userId) { data: data, method: 'POST', success: function (data) { - $('#token-' + userId + '-created > .token').text(data); + $('#token-' + userId + '-created > td > .token').text(data); $('#token-' + userId + '-created').removeClass('hide'); markSuccess('token-' + userId); var oldNumberOfTokens = parseInt($('#token-' + userId + '-count').text()); @@ -92,5 +94,27 @@ function generateToken(userId) { } function clearTokens(userId) { - alert(userId); + var endpoint = getHelpdeskUrl(); + endpoint += '/internal-api/admin/api-authentication/'; + markSaving('token-' + userId); + $('#token-' + userId + '-reset').addClass('hide'); + $('#token-' + userId + '-created').addClass('hide'); + var data = { + userId: userId, + action: 'reset' + }; + $.ajax({ + url: endpoint, + data: data, + method: 'POST', + success: function() { + $('#token-' + userId + '-reset').removeClass('hide'); + $('#token-' + userId + '-count').text('0'); + markSuccess('token-' + userId); + }, + error: function(data) { + console.error(data); + markFailure('token-' + userId); + } + }); } \ No newline at end of file