Enregistrement de la dernière connexion de l'utilisateur et affichage de celle-ci dans le backend au niveau de la liste des utilisateurs.prodstable
} | } | ||||
$model = new LoginForm(); | $model = new LoginForm(); | ||||
if ($model->load(Yii::$app->request->post()) && $model->login()) { | |||||
if ($model->load(Yii::$app->request->post()) && $model->login()) | |||||
{ | |||||
return $this->goBack(); | return $this->goBack(); | ||||
} else { | |||||
} | |||||
else { | |||||
return $this->render('login', [ | return $this->render('login', [ | ||||
'model' => $model, | 'model' => $model, | ||||
]); | ]); |
['class' => 'form-control'] | ['class' => 'form-control'] | ||||
) | ) | ||||
], | ], | ||||
[ | |||||
'attribute' => 'created_at', | |||||
'label' => 'Date d\'inscription', | |||||
'value' => function($model) { | |||||
if(isset($model['created_at'])) | |||||
return date('m/d/Y à H:i', $model['created_at']); | |||||
} | |||||
], | |||||
[ | |||||
'attribute' => 'date_derniere_connexion', | |||||
'label' => 'Dernière connexion', | |||||
'value' => function($model) { | |||||
if(isset($model['date_derniere_connexion'])) | |||||
return date('m/d/Y à H:i', strtotime($model['date_derniere_connexion'])); | |||||
else | |||||
return '' ; | |||||
} | |||||
], | |||||
[ | [ | ||||
'class' => 'yii\grid\ActionColumn', | 'class' => 'yii\grid\ActionColumn', | ||||
'template' => '{commandes}', | 'template' => '{commandes}', |
{ | { | ||||
return [ | return [ | ||||
['email', 'filter', 'filter' => 'trim'], | |||||
['email', 'required','message'=>'Champs obligatoire'], | |||||
['email', 'filter', 'filter' => 'trim'], | |||||
['email', 'required','message'=>'Champs obligatoire'], | |||||
['email', 'email'], | ['email', 'email'], | ||||
// username and password are both required | // username and password are both required | ||||
public function login() | public function login() | ||||
{ | { | ||||
if ($this->validate()) { | if ($this->validate()) { | ||||
$this->updateDerniereConnexion() ; | |||||
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | ||||
} else { | } else { | ||||
return false; | return false; | ||||
return $this->_user; | return $this->_user; | ||||
} | } | ||||
public function attributeLabels() | |||||
{ | |||||
return [ | |||||
'id' => 'ID', | |||||
'username' => 'Identifiant', | |||||
'password' => 'Mot de passe', | |||||
public function attributeLabels() | |||||
{ | |||||
return [ | |||||
'id' => 'ID', | |||||
'username' => 'Identifiant', | |||||
'password' => 'Mot de passe', | |||||
'rememberMe' => 'Se souvenir de moi', | 'rememberMe' => 'Se souvenir de moi', | ||||
'email' => 'Email', | 'email' => 'Email', | ||||
]; | |||||
]; | |||||
} | |||||
public function updateDerniereConnexion() | |||||
{ | |||||
$user = $this->getUser() ; | |||||
$user->date_derniere_connexion = date('Y-m-d H:i:s') ; | |||||
$user->save() ; | |||||
} | } | ||||
} | } |
['email','verifyEmail'], | ['email','verifyEmail'], | ||||
['status', 'default', 'value' => self::STATUS_ACTIVE], | ['status', 'default', 'value' => self::STATUS_ACTIVE], | ||||
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN,self::STATUS_BOULANGER ]], | ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN,self::STATUS_BOULANGER ]], | ||||
[['date_derniere_connexion'],'safe'], | |||||
]; | ]; | ||||
} | } | ||||
<?php | |||||
use yii\db\Migration; | |||||
use yii\db\Schema; | |||||
class m161227_130138_champs_date_derniere_connexion extends Migration | |||||
{ | |||||
public function up() | |||||
{ | |||||
$this->addColumn('user', 'date_derniere_connexion', Schema::TYPE_DATETIME) ; | |||||
} | |||||
public function down() | |||||
{ | |||||
$this->dropColumn('user', 'date_derniere_connexion') ; | |||||
} | |||||
} |