2016-06-28 39 views
0

您好,我正在使用CodeIgniter使用https://github.com/chriskacerguis/codeigniter-restserver/blob/master/application/libraries/REST_Controller.php來構建REST API。CodeIgniter Rest的自定義錯誤消息API

在_detect_api_key()函數中,他只檢查給定的Api Key是否與存儲在表中的API匹配。我修改了一下這個方法,不僅檢查api鍵是否匹配,而且檢查用戶是否被激活。對於這兩種情況我都會收到無效的API密鑰消息。

如何將消息設置爲「您使用的API密鑰不活動」或類似的東西?

文件我使用中的錯誤消息是這樣的:

/* 
* English language 
*/ 

$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key 
$lang['text_rest_invalid_credentials'] = 'Invalid credentials'; 
$lang['text_rest_ip_denied'] = 'IP denied'; 
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized'; 
$lang['text_rest_unauthorized'] = 'Unauthorized'; 
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed'; 
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller'; 
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions'; 
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method'; 
$lang['text_rest_unknown_method'] = 'Unknown method'; 
$lang['text_rest_unsupported'] = 'Unsupported protocol'; 

和_detect_api_key方法沒有提及有關錯誤消息的任何事情剛剛返回false:

if (!($row = $this->rest->db->where($this->config->item('rest_key_column'), $key)->get($this->config->item('rest_keys_table'))->row()) || $isactive[0]->activated === 'no') 
      { 
       return FALSE; 
      } 

我補充這一點:

$isactive[0]->activated === 'no' 

任何想法?

回答

0

試試這個

if (!($row = $this->rest->db 
->where($this->config->item('rest_key_column'), $key) 
->get($this->config->item('rest_keys_table'))->row())) 
{ 
    return array(
     "valid_key" => FALSE, 
     "error" => "text_rest_invalid_api_key" 
    ); 
} else if($isactive[0]->activated === 'no') { 
    return array(
     "valid_key" => FALSE, 
     "error" => "user_not_activated" 
    ); 
}