php - 无法使用 user_id 以编程方式登录

标签 php cakephp cakephp-2.3

我的用户模型中有 HybridRegister 回调。一旦用户使用社交登录(例如 Facebook)注册,我就会尝试检查他的电子邮件。如果数据库中存在电子邮件,我将停止注册并使用与此电子邮件相关的用户数据进行登录。

public function hybridRegister($provider, Hybrid_User_Profile $profile) {

    $profile = (array)$profile;


    $data = array(
        'provider' => $provider,
        'provider_uid' => $profile['identifier'],
        //Extra fields here as needed like group_id, status etc.
    );




    if (strtolower($provider) === "vkontakte"){

        $data['firstName'] = $profile['firstName'];
        $data['lastName']  = $profile['lastName'];
        //TODO make fetching picture to our server
        $data['photo']     = $profile['photoURL']; 

        $data['email']     = $profile['emailVerified'];     
    }

    if (strtolower($provider) === "facebook"){

        $data['firstName'] = $profile['firstName'];
        $data['lastName']  = $profile['lastName'];

           //TODO выкачивать картинку на сервер
        $data['photo']     = $profile['photoURL']; 
        $data['email']     = $profile['emailVerified'];   
    }

    //Check if an email exists in our db. if yes, then login user else
    // continue register...

    $user = $this->existEmail($data);
    if ($user){

        **//AuthComponent::login throw error somewhere inside it**
        AuthComponent::login($user);

        return true;
    }
    else {
        // если нет, то продолжаем регистрацию

        $data['password'] = $this->generatePassword();
        $data['username'] = $data['provider'] . $profile['identifier']; 


       $this->sendRegistrationEmail($data['email'], $data['username'], $data['password']);




        $this->create();
        return $this->save($data, false);
    }
}

错误下方:

Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an    error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_setDefaults' at line 1
SQL Query: _setDefaults
Notice: If you want to customize this error message, create  app/View/Errors/pdo_error.ctp
Stack Trace
CORE/Cake/Model/Datasource/DboSource.php line 460 → PDOStatement->execute(array)
CORE/Cake/Model/Datasource/DboSource.php line 426 → DboSource->_execute(string, array)
CORE/Cake/Model/Datasource/DboSource.php line 668 → DboSource->execute(string, array, array)
CORE/Cake/Model/Datasource/DboSource.php line 611 → DboSource->fetchAll(string, array, array)
CORE/Cake/Model/Model.php line 827 → DboSource->query(string, array, User)
CORE/Cake/Controller/Component/AuthComponent.php line 604 → Model->__call(string, array)
CORE/Cake/Controller/Component/AuthComponent.php line 604 → User->_setDefaults()
APP/Model/User.php line 250 → AuthComponent->login(array)
[internal function] → User->hybridRegister(string, Hybrid_User_Profile)

我研究了 authComponent 代码(登录功能)。有说明

/**
* Log a user in.
*
* **If a $user is provided that data will be stored as the logged in user.** If   `$user` is empty or not
* specified, the request will be used to identify a user.

我不明白为什么我的代码不起作用。请告诉我。

回答

hybridRegister 函数的代码应该稍微改变一下。一旦现有的用户电子邮件在数据库中得到批准,函数应该只返回用户。 HybridAuth 自行进行身份验证。代码如下:

//Check if an email exists in our db. if yes, then login user else
// continue register...

    $user = $this->existEmail($data);
    if ($user){
        return $user;
    }

我的错误是认为 HybridRegister 必须保存用户或返回 false。但它只能从数据库获取任何现有的用户记录并返回它。

感谢@AD7six 和@burzum 的想法。 @AD7six 的回答对我来说是最有帮助的。

最佳答案

I have hybridRegister callback in my User model.

**//AuthComponent::login throw error somewhere inside it**
AuthComponent::login($user);

这行不通

登录功能不是静态的

不起作用的原因是 in that function它调用其他实例方法:

public function login($user = null) {
    $this->_setDefaults(); # <-

该方法存在于 auth 组件上,但静态调用时不存在于 $this (您的用户模型)上。

正确的解决方案

用户登录是“ Controller ”逻辑;它属于 Controller 或组件,但不属于模型。有一个简单的例子 how to create a register function在书中,应用于问题中的代码将是:

public function register() {
    if ($this->User->hybridRegister($this->request->data)) {
        $id = $this->User->id;
        $this->request->data['User'] = array_merge(
            $this->request->data['User'],
            array('id' => $id)
        );
        unset($this->request->data['User']['password']);
        $this->Auth->login($this->request->data['User']);
        return $this->redirect('/users/home');
    }
}

其中 hybridRegiser 修改为 only 负责创建(或不创建)用户记录,根据需要发送注册电子邮件,并根据需要返回 true/false .

关于php - 无法使用 user_id 以编程方式登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729042/

相关文章:

php - 计算行和列中相同的数字 php mysql

php - 如何在 php 或 mysql 中查找递归日期?

php - 按 CakePHP 中的特定字段值排序

cakephp - Cake php如何更新AROS

templates - 使用 CakePHP 为自定义模板链接创建分页

php - 检查行是否在 while 循环中分组

php - CakePHP 模型关系在同一张表上有 2 个外键

php - 如何从 Controller 将 Assets (JS) 包含到布局中 - CakePHP

mysql - CakePHP 和引用完整性

php - 从 Controller 通过其 Controller 调用另一个模型中的函数时出现数据库错误