Skip to content

Commit

Permalink
Add keys in your RestApi
Browse files Browse the repository at this point in the history
  • Loading branch information
DionMontolalu committed Jul 3, 2021
1 parent 001e1f4 commit fc2558c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion application/config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
*/
$config['rest_enable_keys'] = false;
$config['rest_enable_keys'] = true;

/*
|--------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions application/controllers/Siswa.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function index_get()
$this->response([
'status' => true,
'data' => $siswa,
], 200);
], RestController::HTTP_OK);
} else {
$this->response([
'status' => false,
'message' => 'id not found'
], 404);
], RestController::HTTP_NOT_FOUND);
}
}

Expand All @@ -42,7 +42,7 @@ public function index_delete()
'status' => true,
'id' => $id,
'messages' => 'Deleted'
], 200);
], RestController::HTTP_OK);
} else {
$this->response([
'status' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public function __construct($config = 'rest')
$this->response([
$this->config->item('rest_status_field_name') => false,
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ajax_only'),
], HTTP_NOT_ACCEPTABLE);
], self::HTTP_NOT_ACCEPTABLE);
}

// When there is no specific override for the current class/method, use the default auth value set in the config
Expand Down Expand Up @@ -486,7 +486,7 @@ public function _remap($object_called, $arguments = [])
$this->response([
$this->config->item('rest_status_field_name') => false,
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_unsupported'),
], HTTP_FORBIDDEN);
], self::HTTP_FORBIDDEN);
}

// Remove the supported format from the function name e.g. index.json => index
Expand Down Expand Up @@ -519,7 +519,7 @@ public function _remap($object_called, $arguments = [])
$this->response([
$this->config->item('rest_status_field_name') => false,
$this->config->item('rest_message_field_name') => sprintf($this->lang->line('text_rest_invalid_api_key'), $this->rest->key),
], HTTP_FORBIDDEN);
], self::HTTP_FORBIDDEN);
}

// Check to see if this key has access to the requested controller
Expand All @@ -531,7 +531,7 @@ public function _remap($object_called, $arguments = [])
$this->response([
$this->config->item('rest_status_field_name') => false,
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_unauthorized'),
], HTTP_UNAUTHORIZED);
], self::HTTP_UNAUTHORIZED);
}

// Sure it exists, but can they do anything with it?
Expand All @@ -547,7 +547,7 @@ public function _remap($object_called, $arguments = [])
// Check the limit
if ($this->config->item('rest_enable_limits') && $this->_check_limit($controller_method) === false) {
$response = [$this->config->item('rest_status_field_name') => false, $this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_time_limit')];
$this->response($response, HTTP_UNAUTHORIZED);
$this->response($response, self::HTTP_UNAUTHORIZED);
}

// If no level is set use 0, they probably aren't using permissions
Expand All @@ -562,14 +562,14 @@ public function _remap($object_called, $arguments = [])
if ($authorized === false) {
// They don't have good enough perms
$response = [$this->config->item('rest_status_field_name') => false, $this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_permissions')];
$this->response($response, HTTP_UNAUTHORIZED);
$this->response($response, self::HTTP_UNAUTHORIZED);
}
}

//check request limit by ip without login
elseif ($this->config->item('rest_limits_method') == 'IP_ADDRESS' && $this->config->item('rest_enable_limits') && $this->_check_limit($controller_method) === false) {
$response = [$this->config->item('rest_status_field_name') => false, $this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ip_address_time_limit')];
$this->response($response, HTTP_UNAUTHORIZED);
$this->response($response, self::HTTP_UNAUTHORIZED);
}

// No key stuff, but record that stuff is happening
Expand Down Expand Up @@ -618,7 +618,7 @@ public function response($data = null, $http_code = null, $continue = false)

// If data is NULL and no HTTP status code provided, then display, error and exit
if ($data === null && $http_code === null) {
$http_code = HTTP_NOT_FOUND;
$http_code = self::HTTP_NOT_FOUND;
}

// If data is not NULL and a HTTP status code provided, then continue
Expand Down Expand Up @@ -656,7 +656,7 @@ public function response($data = null, $http_code = null, $continue = false)
// If not greater than zero, then set the HTTP status code as 200 by default
// Though perhaps 500 should be set instead, for the developer not passing a
// correct HTTP status code
$http_code > 0 || $http_code = HTTP_OK;
$http_code > 0 || $http_code = self::HTTP_OK;

$this->output->set_status_header($http_code);

Expand Down

0 comments on commit fc2558c

Please sign in to comment.