Browse Source

Utilisateur : dernière connexion

Enregistrement de la dernière connexion de l'utilisateur et affichage de celle-ci dans le backend au niveau de la liste des utilisateurs.
master
keun 8 years ago
parent
commit
53cf002841
5 changed files with 57 additions and 11 deletions
  1. +4
    -2
      backend/controllers/SiteController.php
  2. +18
    -0
      backend/views/user/index.php
  3. +17
    -9
      common/models/LoginForm.php
  4. +1
    -0
      common/models/User.php
  5. +17
    -0
      console/migrations/m161227_130138_champs_date_derniere_connexion.php

+ 4
- 2
backend/controllers/SiteController.php View File

} }


$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,
]); ]);

+ 18
- 0
backend/views/user/index.php View File

['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}',

+ 17
- 9
common/models/LoginForm.php View File

{ {
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() ;
} }
} }

+ 1
- 0
common/models/User.php View File

['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'],
]; ];
} }



+ 17
- 0
console/migrations/m161227_130138_champs_date_derniere_connexion.php View File

<?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') ;
}
}

Loading…
Cancel
Save