From 6929a061eb18dc0fe213aaee0a76d943724fc8c3 Mon Sep 17 00:00:00 2001 From: phl0 Date: Tue, 21 Mar 2023 12:26:15 +0100 Subject: [PATCH] Add a column to API keys to show last usage timestamp Squashed commit of the following: commit 83f12cc945fa717945062a841854498368d8cb10 Author: phl0 Date: Tue Mar 21 12:24:53 2023 +0100 Change default value to current date/time commit e1cb72f3fc80f581ee30d927772efe0ffe155059 Author: phl0 Date: Tue Mar 21 12:05:46 2023 +0100 Add functions to update timestamps commit d303f629a0b2d200e49da36766cf72ed37c3fe92 Merge: 01a9606a 7dd76923 Author: phl0 Date: Tue Mar 21 11:13:21 2023 +0100 Merge remote-tracking branch 'origin/dev' into apiKeyLastUsed commit 01a9606afde6aad6be0f07f34f8584e540c50e8c Author: phl0 Date: Tue Mar 21 11:11:51 2023 +0100 Reorder DB migration due to previous commits commit ec5cd743b96dc1ed0f2740f7b2051f88549f4248 Author: phl0 Date: Fri Mar 17 16:23:34 2023 +0100 Basics for API key last used --- application/config/migration.php | 2 +- application/controllers/Api.php | 7 +++++++ .../migrations/116_add_timestamp_to_api.php | 21 +++++++++++++++++++ application/models/Api_model.php | 7 +++++++ application/views/api/help.php | 2 ++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 application/migrations/116_add_timestamp_to_api.php diff --git a/application/config/migration.php b/application/config/migration.php index 9a64222f..595bcece 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = TRUE; | be upgraded / downgraded to. | */ -$config['migration_version'] = 115; +$config['migration_version'] = 116; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 0d6c3e51..92cb8c48 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -221,6 +221,8 @@ class API extends CI_Controller { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); } + $this->api_model->update_last_used($obj['key']); + // Retrieve the arguments from the query string $data['data']['format'] = $arguments['format']; @@ -295,6 +297,8 @@ class API extends CI_Controller { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); } + $this->api_model->update_last_used($obj['key']); + // Retrieve the arguments from the query string $data['data']['format'] = $arguments['format']; @@ -432,6 +436,7 @@ class API extends CI_Controller { die(); } + $this->api_model->update_last_used($obj['key']); if($obj['type'] == "adif" && $obj['string'] != "") { // Load the logbook model for adding QSO records @@ -500,6 +505,8 @@ class API extends CI_Controller { die(); } + $this->api_model->update_last_used($obj['key']); + $user_id = $this->api_model->key_userid($obj['key']); // Store Result to Database diff --git a/application/migrations/116_add_timestamp_to_api.php b/application/migrations/116_add_timestamp_to_api.php new file mode 100644 index 00000000..c13a2106 --- /dev/null +++ b/application/migrations/116_add_timestamp_to_api.php @@ -0,0 +1,21 @@ +db->field_exists('last_used', 'api')) { + $this->dbforge->add_column('api', $fields); + } + } + + public function down() + { + $this->dbforge->drop_column('api', 'last_used'); + } +} diff --git a/application/models/Api_model.php b/application/models/Api_model.php index 76ede10a..87c3d9b3 100644 --- a/application/models/Api_model.php +++ b/application/models/Api_model.php @@ -175,6 +175,13 @@ class API_Model extends CI_Model { } } + function update_last_used($key) { + $this->db->set('last_used', 'NOW()', FALSE); + $this->db->where('key', xss_clean($key)); + $this->db->update('api'); + //$query = $this->db->query('UPDATE `api` SET `last_used` = NOW() WHERE `key` = "'.xss_clean($key).'";'); + } + // FUNCTION: string name(string $column) // Converts a MySQL column name to a more friendly name function name($col) diff --git a/application/views/api/help.php b/application/views/api/help.php index c1caf1c8..de10141b 100644 --- a/application/views/api/help.php +++ b/application/views/api/help.php @@ -26,6 +26,7 @@ API Key Description + Last Used Rights Status @@ -35,6 +36,7 @@ key; ?> description; ?> + last_used; ?>