diff --git a/CHANGELOG.md b/CHANGELOG.md index b1c1864..0df2625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## 1.0.10 - Work In Progress - Fix #37: Fix bower alias in test environment (tekord) - Enh #32: Added Italian Translation (maxxer) -- Fix #30: Prefill username and email in SettingsForm (mattheobjornson) +- Fix #30: Prefill username and email in SettingsForm (mattheobjornson) +- Enh #39: Added `last_login_at` field to user table (pappfer) ## 1.0.9 - July 19, 2017 - Enh #22: Added impersonation feature (@tonydspaniard) diff --git a/src/User/Controller/SecurityController.php b/src/User/Controller/SecurityController.php index a150d43..4e06757 100644 --- a/src/User/Controller/SecurityController.php +++ b/src/User/Controller/SecurityController.php @@ -124,6 +124,8 @@ class SecurityController extends Controller if ($form->load(Yii::$app->request->post())) { $this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event); if ($form->login()) { + Yii::$app->getUser()->identity->updateAttributes(['last_login_at' => time()]); + $this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event); return $this->goBack(); diff --git a/src/User/Migration/m000000_000005_add_last_login_at.php b/src/User/Migration/m000000_000005_add_last_login_at.php new file mode 100644 index 0000000..68807ed --- /dev/null +++ b/src/User/Migration/m000000_000005_add_last_login_at.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace Da\User\Migration; + +use yii\db\Migration; + +class m000000_000005_add_last_login_at extends Migration +{ + public function up() + { + $this->addColumn('{{%user}}', 'last_login_at', $this->integer()); + } + + public function down() + { + $this->dropColumn('{{%user}}', 'last_login_at'); + } +} diff --git a/src/User/Model/User.php b/src/User/Model/User.php index 8605cec..60c019f 100644 --- a/src/User/Model/User.php +++ b/src/User/Model/User.php @@ -43,6 +43,7 @@ use yii\web\IdentityInterface; * @property int $flags * @property int $created_at * @property int $updated_at + * @property int $last_login_at * * Defined relations: * @property SocialNetworkAccount[] $socialNetworkAccounts @@ -134,6 +135,7 @@ class User extends ActiveRecord implements IdentityInterface 'password' => Yii::t('usuario', 'Password'), 'created_at' => Yii::t('usuario', 'Registration time'), 'confirmed_at' => Yii::t('usuario', 'Confirmation time'), + 'last_login_at' => Yii::t('usuario', 'Last login'), ]; } diff --git a/src/User/Search/UserSearch.php b/src/User/Search/UserSearch.php index f04856d..a604c2f 100644 --- a/src/User/Search/UserSearch.php +++ b/src/User/Search/UserSearch.php @@ -30,6 +30,10 @@ class UserSearch extends Model * @var int */ public $created_at; + /** + * @var int + */ + public $last_login_at; /** * @var string */ @@ -57,7 +61,7 @@ class UserSearch extends Model public function rules() { return [ - 'safeFields' => [['username', 'email', 'registration_ip', 'created_at'], 'safe'], + 'safeFields' => [['username', 'email', 'registration_ip', 'created_at', 'last_login_at'], 'safe'], 'createdDefault' => ['created_at', 'default', 'value' => null], ]; } @@ -72,6 +76,7 @@ class UserSearch extends Model 'email' => Yii::t('usuario', 'Email'), 'created_at' => Yii::t('usuario', 'Registration time'), 'registration_ip' => Yii::t('usuario', 'Registration IP'), + 'last_login_at' => Yii::t('usuario', 'Last login'), ]; } @@ -99,6 +104,11 @@ class UserSearch extends Model $query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]); } + if ($this->last_login_at !== null) { + $date = strtotime($this->last_login_at); + $query->andFilterWhere(['between', 'last_login_at', $date, $date + 3600 * 24]); + } + $query ->andFilterWhere(['like', 'username', $this->username]) ->andFilterWhere(['like', 'email', $this->email]) diff --git a/src/User/resources/views/admin/index.php b/src/User/resources/views/admin/index.php index bb4dd17..f5cc5e0 100644 --- a/src/User/resources/views/admin/index.php +++ b/src/User/resources/views/admin/index.php @@ -58,6 +58,18 @@ $module = Yii::$app->getModule('user'); return date('Y-m-d G:i:s', $model->created_at); }, ], + [ + 'attribute' => 'last_login_at', + 'value' => function ($model) { + if (!$model->last_login_at || $model->last_login_at == 0) { + return Yii::t('usuario', 'Never'); + } else if (extension_loaded('intl')) { + return Yii::t('usuario', '{0, date, MMMM dd, YYYY HH:mm}', [$model->last_login_at]); + } else { + return date('Y-m-d G:i:s', $model->last_login_at); + } + }, + ], [ 'header' => Yii::t('usuario', 'Confirmation'), 'value' => function ($model) {