dongmoyu0336 2017-02-07 06:01
浏览 109

YII2 - 授权

I hope you can help me before I'm going crazy. I'm trying to login a user. This is the actionLogin

public function actionLogin() {
    $model = new User();

    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        $arrPost = Yii::$app->request->post();

        $identity = User::findOne(['email'=>$arrPost['User']['email'],'password'=>$arrPost['User']['password']]);

        $identity->id = $identity->getAttribute('id');
        $identity->last_name = $identity->getAttribute('last_name');
        $identity->first_name = $identity->getAttribute('first_name');
        $identity->admin = $identity->getAttribute('admin');
        $identity->address = $identity->getAttribute('address');
        $identity->ministry = $identity->getAttribute('ministry');

        \yii::$app->user->login($identity,86400);                        

        return $this->redirect(Url::to(['app/index']));
    } else {
        // either the page is initially displayed or there is some validation error
        return $this->render("@app/forms/user/login", ['model' => $model]);
    }
}

If I'm doing a var_dump after

\yii::$app->user->login

I got the following dump

object(app\models\User)#88 (17) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> NULL ["admin"]=> bool(true) ["password"]=> NULL ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "" ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Köster" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Lieber Christian" ["ministry"]=> string(6) "Diakon" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

You see, the the fields for id, first_name, last_name etc. are filled. After an redirect to the entry page and doing a var_dump on

\yii::$app->user->identity

I got the following content:

object(app\models\User)#93 (17) { ["id"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["email"]=> NULL ["admin"]=> NULL ["password"]=> NULL ["address"]=> NULL ["ministry"]=> NULL ["auth_key"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(8) { ["id"]=> int(1) ["first_name"]=> string(12) "Christianfds" ["last_name"]=> string(7) "Koester" ["email"]=> string(25) "kirche@familie-koester.eu" ["admin"]=> bool(true) ["password"]=> string(5) "chris" ["address"]=> string(16) "Dear Christian" ["ministry"]=> string(6) "" } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(0) { } ["_behaviors":"yii\base\Component":private]=> array(0) { } }

You see that the contents of id, first_name, last_name etc. are gone. It is possible for to ask if the user is a guest, which is returned as wrong (the user is logged in). But it is not possible for me to get the values from fields like id, first_name, last_name etc, because they are NULL:

Config file for users:

'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
        'enableSession' => true,
    ],

Can anybody help me before I'm going mad!

Thanks Chris

  • 写回答

2条回答 默认 最新

  • dta25920 2017-02-07 10:36
    关注

    There are lots of unnecessary code...it can be simply done by following if all the other things are as planned.

    public function actionLogin() {
        $model = new User();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            \yii::$app->user->login($model); //does your user model have login function??                       
            return $this->redirect(Url::to(['app/index']));
        } else {
            return $this->render("@app/forms/user/login", ['model' => $model]);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题