简体   繁体   English

Yii2 - 更新用户身份信息

[英]Yii2 - Update user identity info

I have implemented a login screen. 我已经实现了登录屏幕。 I understand how identity works. 我理解身份如何运作。 I can read user info from identity like this: 我可以从身份中读取用户信息,如下所示:

Yii::$app->user->identity->firstname

Has Yii a way to update identity info, in case user update his/her name on profile? 如果用户在个人资料上更新他/她的名字,Yii有办法更新身份信息吗?

I have been checking the code, and I see Yii triggers an event to set the web/user identity: 我一直在检查代码,我看到Yii触发了一个事件来设置web /用户身份:

$event = new UserEvent([
            'identity' => $identity,
        ]);
        $this->trigger(self::EVENT_BEFORE_LOGOUT, $event);

So since Identity is an instance of User maybe I could do: 所以,由于Identity是User的一个实例,我可以做:

$event = new UserEvent([
                'identity' => $identity,
            ]);
            $this->trigger('afterSave', $event);

Any security issues could bring this? 任何安全问题都可能带来这个? any other ways to do it? 还有其他方法吗?

UPDATE I have found a method called "setIdentity": 更新我发现了一个名为“setIdentity”的方法:

http://www.yiiframework.com/doc-2.0/yii-web-user.html#setIdentity%28%29-detail http://www.yiiframework.com/doc-2.0/yii-web-user.html#setIdentity%28%29-detail

I will try it and see if it is what I am looking for. 我会尝试一下,看看它是不是我要找的东西。

At the end the answer was easier than I thought. 最后答案比我想象的要容易。 It updates automatically!!! 它会自动更新! when you update the user. 当您更新用户时。 In any case it was useful to learn something new. 无论如何,学习新东西是有用的。 In Yii 1 worked different that was the cause of my confusion. 在Yii 1工作不同,这是我的困惑的原因。 Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM