From 9ecfb979df6cc4472948af86bd30512207e32c4f Mon Sep 17 00:00:00 2001 From: Rhilip Date: Mon, 12 Aug 2019 20:32:04 +0800 Subject: [PATCH] feat(Sessions/List): Use SessionsListForm to show user sessions --- CHANGELOG.md | 3 + apps/components/Auth.php | 13 +++- apps/controllers/UserController.php | 6 +- apps/models/form/User/SessionsListForm.php | 42 +++++++++-- apps/models/form/User/UserDetailsForm.php | 15 ++++ apps/views/user/sessions.php | 86 ++++++++++++---------- 6 files changed, 115 insertions(+), 50 deletions(-) create mode 100644 apps/models/form/User/UserDetailsForm.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bec666..4a6dce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## [Unreleased] +### Build +- **Validator:** Upgrade siriusphp/validation to 2.3 (eb039eb) + ### Docs - **template:** Add git commit hash in `CHANGELOG.md` (76bc527) diff --git a/apps/components/Auth.php b/apps/components/Auth.php index 12a758f..3eb737f 100644 --- a/apps/components/Auth.php +++ b/apps/components/Auth.php @@ -19,6 +19,8 @@ class Auth extends Component protected $cur_user; protected $cur_user_session_id; + protected $grant; + public function onRequestBefore() { parent::onRequestBefore(); // TODO: Change the autogenerated stub @@ -35,19 +37,22 @@ public function onRequestBefore() public function getCurUser($grant = 'cookies', $flush = false) { if (is_null($this->cur_user) || $flush) { + $this->grant = $grant; $this->cur_user = $this->loadCurUser($grant); } return $this->cur_user; } - /** - * @return mixed - */ - public function getCurUserSessionId() + public function getCurUserSessionId(): string { return $this->cur_user_session_id ?? ''; } + public function getGrant(): string + { + return $this->grant ?? ''; + } + /** * @param string $grant * @return models\User|boolean diff --git a/apps/controllers/UserController.php b/apps/controllers/UserController.php index b00b0a1..06948c3 100644 --- a/apps/controllers/UserController.php +++ b/apps/controllers/UserController.php @@ -80,8 +80,8 @@ public function actionPanel() public function actionSessions() { if (app()->request->isPost()) { - $action = app()->request->post('action'); - if ($action == 'delsession') { + $action = app()->request->post('action'); // FIXME + if ($action == 'revoke') { $to_del_session = app()->request->post('session'); // expired it from Database first @@ -100,7 +100,7 @@ public function actionSessions() $session_list = new User\SessionsListForm(); if (false === $session_list->validate()) { - return $this->render('action/action_fail',['msg' => $session_list->getError()]); + return $this->render('action/action_fail', ['msg' => $session_list->getError()]); } return $this->render('user/sessions', ['session_list' => $session_list]); diff --git a/apps/models/form/User/SessionsListForm.php b/apps/models/form/User/SessionsListForm.php index 4bbd3b3..6b78095 100644 --- a/apps/models/form/User/SessionsListForm.php +++ b/apps/models/form/User/SessionsListForm.php @@ -13,22 +13,54 @@ class SessionsListForm extends Pager { + public $uid; + public $expired = [-1, 0]; // Default not show expired session + + public static $DEFAULT_LIMIT = 10; + public static $MAX_LIMIT = 50; + protected $_autoload_data = true; protected $_autoload_data_from = ['get']; - protected function getRemoteTotal(): int + public static function defaultData() { - return app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE uid = :uid')->bindParams([ + return [ + 'page' => static::getDefaultPage(), 'limit' => static::getDefaultLimit(), 'uid' => app()->auth->getCurUser()->getId() + ]; + } + + public static function inputRules() + { + $rules = [ + 'expired[*]' => [ + ['Integer'], + ['Inlist', ['list' => [-1 /* Never Expired */, 0 /* Temporary */, 1 /* Expired */]]] + ] + ]; + + // TODO allow admin to see other people session log + $rules['uid'] = ['Integer', ['Equal', ['value' => app()->auth->getCurUser()->getId()]]]; + + return $rules; + } + + protected function getRemoteTotal(): int + { + var_dump($this->getData('expired')); + return app()->pdo->createCommand([ + ['SELECT COUNT(`id`) FROM `user_session_log` WHERE uid = :uid ', 'params' => ['uid' => $this->getData('uid')]], + ['AND `expired` IN (:expired)', 'params' => ['expired' => $this->getData('expired')]], ])->queryScalar(); } protected function getRemoteData(): array { return app()->pdo->createCommand([ - ['SELECT `id`, `sid`, `login_at`, `login_ip`, `user_agent`, `last_access_at` FROM `user_session_log` WHERE 1=1 '], - ['AND uid = :uid ' , 'params' => ['uid' => app()->auth->getCurUser()->getId()]], - ['ORDER BY `expired` DESC, `id` DESC'], + ['SELECT `id`, `sid`, `login_at`, `login_ip`, `user_agent`, `last_access_at`, `expired` FROM `user_session_log` WHERE 1=1 '], + ['AND `uid` = :uid ', 'params' => ['uid' => app()->auth->getCurUser()->getId()]], + ['AND `expired` IN (:expired)', 'params' => ['expired' => $this->expired]], + ['ORDER BY `expired`, `id` DESC'], ['LIMIT :o, :l', 'params' => ['o' => $this->offset, 'l' => $this->limit]] ])->queryAll(); } diff --git a/apps/models/form/User/UserDetailsForm.php b/apps/models/form/User/UserDetailsForm.php new file mode 100644 index 0000000..baffa4b --- /dev/null +++ b/apps/models/form/User/UserDetailsForm.php @@ -0,0 +1,15 @@ +layout('user/setting_layout') ?> start('panel') ?> -

Sessions

-This is a list of devices that have logged into your account. Revoke any sessions that you do not recognize. -
- - - - - - - - - - - - getPagerData() as $s): ?> - - - - - - - - - -
Login AtLogin IPUser AgentLast access atRevoke
- auth->getCurUserSessionId()): ?> - Current - -
- - - -
- -
+
+
+

Sessions

+ This is a list of devices that have logged into your account. Revoke any sessions that you do not recognize. +
+
+ + + + + + + + + + + + getPagerData() as $s): ?> + class="warning" data-toggle="tooltip" data-placement="bottom" title="This session will expired automatically."> + + + + + + + + +
Login AtLogin IPUser AgentLast access atRevoke
+ auth->getCurUserSessionId()): ?> + Current + +
+ + + +
+ +
+ +
+
    +
    +
    +
    + stop() ?>