From b34769b30bb3a6d0ff85a2515e3e694fd56ef304 Mon Sep 17 00:00:00 2001 From: Mathieu Nayrolles Date: Mon, 8 Feb 2016 13:18:58 -0500 Subject: [PATCH] add limit configuration #109 --- application/config/rest.php | 7 ++++--- application/controllers/api/Users_api.php | 2 +- application/libraries/REST_Controller.php | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/application/config/rest.php b/application/config/rest.php index 821e3f1a..ca5a75bb 100755 --- a/application/config/rest.php +++ b/application/config/rest.php @@ -451,8 +451,9 @@ | `uri` VARCHAR(255) NOT NULL, | `count` INT(10) NOT NULL, | `hour_started` INT(11) NOT NULL, -| `api_key` VARCHAR(40) NOT NULL, -| PRIMARY KEY (`id`) +| `api_key_id` INT(11) NOT NULL, +| PRIMARY KEY (`id`), +| FOREIGN KEY (`api_key_id`) REFERENCES `keys`(`id`) ON DELETE CASCADE | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | | To specify the limits within the controller's __construct() method, add per-method @@ -462,7 +463,7 @@ | | See application/controllers/api/example.php for examples */ -$config['rest_enable_limits'] = FALSE; +$config['rest_enable_limits'] = TRUE; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/api/Users_api.php b/application/controllers/api/Users_api.php index e9a746d7..7d029ac4 100755 --- a/application/controllers/api/Users_api.php +++ b/application/controllers/api/Users_api.php @@ -15,7 +15,7 @@ class Users_api extends REST_Controller { protected $methods = [ 'index_put' => ['key' => false], 'index_post' => ['key' => false], - 'index_delete' => ['key' => true] + 'index_delete' => ['key' => true, 'limit' => 20] ]; /** diff --git a/application/libraries/REST_Controller.php b/application/libraries/REST_Controller.php index 2624a9b0..b9864fc6 100644 --- a/application/libraries/REST_Controller.php +++ b/application/libraries/REST_Controller.php @@ -1157,7 +1157,7 @@ protected function _check_limit($controller_method) // Get data about a keys' usage and limit to one row $result = $this->rest->db ->where('uri', $limited_uri) - ->where('api_key', $this->rest->key) + ->where('api_key_id', $this->rest->key_id) ->get($this->config->item('rest_limits_table')) ->row(); @@ -1167,7 +1167,7 @@ protected function _check_limit($controller_method) // Create a new row for the following key $this->rest->db->insert($this->config->item('rest_limits_table'), [ 'uri' => $limited_uri, - 'api_key' => isset($this->rest->key) ? $this->rest->key : '', + 'api_key_id' => isset($this->rest->key_id) ? $this->rest->key_id : '', 'count' => 1, 'hour_started' => time() ]); @@ -1179,7 +1179,7 @@ protected function _check_limit($controller_method) // Reset the started period and count $this->rest->db ->where('uri', $limited_uri) - ->where('api_key', isset($this->rest->key) ? $this->rest->key : '') + ->where('api_key_id', isset($this->rest->key_id) ? $this->rest->key_id : '') ->set('hour_started', time()) ->set('count', 1) ->update($this->config->item('rest_limits_table')); @@ -1197,7 +1197,7 @@ protected function _check_limit($controller_method) // Increase the count by one $this->rest->db ->where('uri', $limited_uri) - ->where('api_key', $this->rest->key) + ->where('api_key_id', $this->rest->key_id) ->set('count', 'count + 1', FALSE) ->update($this->config->item('rest_limits_table')); }