beate
(Beate)
March 13, 2016, 9:28pm
1
Hi,
I’d like to expand this post with Events, after login a user should have another parameter.
in my config file:
'user' => [
'identityClass' => 'common\models\Kundenstamm',
'enableAutoLogin' => false,
'on '.\yii\web\User::EVENT_AFTER_LOGIN => ['frontend\events\KundenstammEvents', 'handleAfterLogin'],
],
in my model
class Kundenstamm extends ActiveRecord implements IdentityInterface
{
public $zeitraum;
....
}
in my KundenstammEvents
namespace frontend\events;
class KundenstammEvents {
public static function handleAfterLogin($event)
{
$event->identity->zeitraum = 2;
}
}
if I var_dump($event->identity) there directly, zeitraum is 2, but in my layout \Yii::$app->user->identity->zeitraum is empty
Does anyone see what is wrong there?
Thanks!
bobonov
(Bobonov)
March 14, 2016, 12:26pm
2
That because, I guess in $event, you don’t have the logged user… Just try to var_dump it to see what in it.
If you want to store the value in zeitraum which is a property of the User model/identity just do:
class KundenstammEvents {
public static function handleAfterLogin($event)
{
Yii::$app->user->zeitraum = 2;
}
It should work.
beate
(Beate)
March 14, 2016, 2:00pm
3
I tested this already:
object(yii\web\UserEvent)#162 (<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> {
["identity"]=>
object(common\models\Kundenstamm)#107 (10) {
["authKey"]=>
NULL
["zeitraum"]=>
NULL
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(48) {
.....
unfortunatly not.
Setting unknown property: yii\web\User::zeitraum
And if I change it to
Yii::$app->user->identity->zeitraum = 2;
there is no error any more, but the value is empty in my template.
bobonov
(Bobonov)
March 14, 2016, 2:18pm
4
Did you implement the get / set method for zeitraum in the user model?
Just declaring the var should not work you need to setup 2 method in the model:
function setZeitraum($val) {
$this->zeitraum=$val;
}
function getZeitraum($val) {
return $this->zeitraum;
}
now you should be able to Yii::$app->user->identity->zeitraum = 2;
bobonov
(Bobonov)
March 14, 2016, 4:07pm
5
Done some investigation and I think is not possible to do what you want.
According to this issue is not possible to change the user identity data once initialized.
You should use session.
beate
(Beate)
March 16, 2016, 1:17pm
6
That is what I use now, and it works. And it is also better, because it doesn’t belong to the user.
Thanks!
beate
(Beate)
July 20, 2016, 11:16am
7
To come back to this issue: Session is not really a solution for another case, because session expires after 1440 seconds (if session.gc_maxlifetime wasn’t changed), and if user logs in with marked to remember, session data is gone after some time… so I don’t know how tho handle this problem
yiim
July 20, 2016, 6:29pm
8
Where do you store your users?
Use a table/model and add a zeitraum field there.
softark
(Softark)
July 20, 2016, 10:47pm
9
beate:
class Kundenstamm extends ActiveRecord implements IdentityInterface
{
public $zeitraum;
....
}
…
class KundenstammEvents {
public static function handleAfterLogin($event)
{
$event->identity->zeitraum = 2;
}
}
if I var_dump($event->identity) there directly, zeitraum is 2, but in my layout \Yii::$app->user->identity->zeitraum is empty
Looks like that this attribute (zeitraum) doesn’t have a corresponding column in “kundenstamm” table.
If it doesn’t, it’s volatile and will be reset to null every time you load the model from db.
The value set after login will soon be lost and you’ll get null in the next round of the requests.
beate
(Beate)
July 22, 2016, 1:20pm
10
Thanks for your replys. Actually, the zeitraum-example was another project which is ready
Now I have a dropdown in my menu, where the user can select a company, which he is allowed to view. In yii1, I saved the allowed companys (id and name) after login in the user-cookie, so that I can easily create the dropdown in my layout with the cookie.
So of course there is no column in my user table like company_ids or something like that.