Authenticate Against Database with Phil Sturgeon’s Rest-Server
I'm trying to modify Phil Sturgeon's rest-server to authenticate against ion-auth. I changed _check_login to:
private function _check_login($username = '', $password = NULL)
{
if (empty($username))
{
return FALSE;
}
$this->rest->db = $this->load->database('default', TRUE);
$sql = 'SELECT api_key FROM users WHERE email = ?';
$query = $this->rest->db->query($sql, urldecode($username));
if ($q开发者_运维技巧uery->num_rows() > 0) {
$user = $query->row_array();
} else {
return FALSE;
}
// If actually NULL (not empty string) then do not check it
if ($password !== NULL AND $user['api_key'] != $password)
{
return FALSE;
}
//return TRUE;
return $user['api_key'];
}
My first question is do I need to do:
$this->rest->db = $this->load->database('default', TRUE);
It seems to me like $this->rest->db should already be accessible since it's set in the constructor however I get an error saying I'm trying to query on a non-object if I don't add the load->database() line in _check_login like above. NOTE: I removed the if statement in the constructor that only initializes the db connection if keys or logging are enabled so this shouldn't be the issue.
My second issue I think is a result of question 1. Anyhow, after making the changes to REST_Controller my models in my REST controllers no longer work. The error I'm getting is:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$db
Filename: core/Model.php
Line Number: 50
which appears to be occurring when I make a call to a method in one of my model's.
Hopefully that's enough info to help pinpoint the problem. Any ideas on what I might be doing wrong? Thanks!
I believe the db class is at $this->db. Have you tried that?
EDIT: ah it seems that $this->rest->db is also an alias for $this->db.
精彩评论