From c5566441bf25ad45d706c50267bfd61c4b273989 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 5 Jun 2019 12:35:18 +0800 Subject: [PATCH] feat(Invite): Add base invite table 1. Add Table `user_invitations` to store user temp invite, ALTER table `users` with new column `invite` to store user unexpired invites count. 2. Table name changed from `users_{confirm,session_log}` to `user_{confirm,session_log}` 3. Fix user privilege check may throw exception, if config key is not exist, The privilege check will compare with `class=1`. 4. Improve with the info get and cache of true_{uploaded,downloaded} in User info. 5. Add user invites (include temp invites) information in UserTrait. 6. Placeholder of new Route `/collections` in nav BREAKING CHANGE: Some Table added or name changed --- apps/components/User/User.php | 4 +- apps/components/User/UserTrait.php | 61 +++++++++++++++++------ apps/controllers/UserController.php | 4 +- apps/lang/en.php | 1 + apps/models/form/UserConfirmForm.php | 8 +-- apps/models/form/UserLoginForm.php | 4 +- apps/models/form/UserRecoverForm.php | 2 +- apps/models/form/UserRegisterForm.php | 2 +- apps/task/CronTabTimer.php | 2 +- apps/views/layout/base.php | 25 ++++++---- migration/ridpt.sql | 70 +++++++++++++++++++-------- 11 files changed, 126 insertions(+), 57 deletions(-) diff --git a/apps/components/User/User.php b/apps/components/User/User.php index 79bcd28..903f830 100644 --- a/apps/components/User/User.php +++ b/apps/components/User/User.php @@ -77,7 +77,7 @@ public function loadUserFromPasskey() public function deleteUserThisSession() { $success = app()->redis->zRem($this->sessionSaveKey, $this->_userSessionId); - app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([ + app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([ 'sid' => $this->_userSessionId ])->execute(); app()->cookie->delete(Constant::cookie_name); @@ -115,7 +115,7 @@ public function inBookmarkList($tid = null) public function isPrivilege($require_class) { if (is_string($require_class)) { - $require_class = app()->config->get('authority.' . $require_class); + $require_class = app()->config->get('authority.' . $require_class, false) ?: 1; } return $this->class >= $require_class; diff --git a/apps/components/User/UserTrait.php b/apps/components/User/UserTrait.php index 6df5058..eda5b34 100644 --- a/apps/components/User/UserTrait.php +++ b/apps/components/User/UserTrait.php @@ -40,10 +40,15 @@ trait UserTrait private $last_tracker_ip; private $uploaded; + private $true_uploaded; private $downloaded; + private $true_downloaded; private $seedtime; private $leechtime; + private $invites; + private $temp_invites; + protected $peer_status; protected $infoCacheKey; @@ -214,14 +219,15 @@ public function getLastTrackerIp($readable = true) public function getUploaded($real = false) { if ($real) { - $upload = app()->redis->hGet($this->infoCacheKey, 'true_uploaded'); - if (false === $upload) { - $upload = app()->pdo->createCommand('SELECT SUM(`true_uploaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ - "uid" => $this->id - ])->queryScalar() ?? 0; - app()->redis->hSet($this->infoCacheKey, 'true_uploaded', $upload); + if (is_null($this->true_uploaded)) { + $this->true_uploaded = app()->redis->hGet($this->infoCacheKey, 'true_uploaded'); + if (false === $this->true_uploaded) { + $this->true_uploaded = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ + "uid" => $this->id + ])->queryScalar() ?? 0; + app()->redis->hSet($this->infoCacheKey, 'true_uploaded', $this->true_uploaded); + } } - return $upload; } return $this->uploaded; } @@ -233,14 +239,16 @@ public function getUploaded($real = false) public function getDownloaded($real = false) { if ($real) { - $download = app()->redis->hGet($this->infoCacheKey, 'true_downloaded'); - if (false === $download) { - $download = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ - "uid" => $this->id - ])->queryScalar() ?? 0; - app()->redis->hSet($this->infoCacheKey, 'true_downloaded', $download); + if (is_null($this->true_downloaded)) { + $this->true_downloaded = app()->redis->hGet($this->infoCacheKey, 'true_downloaded'); + if (false === $this->true_downloaded) { + $this->true_downloaded = app()->pdo->createCommand('SELECT SUM(`true_downloaded`) FROM `snatched` WHERE `user_id` = :uid')->bindParams([ + "uid" => $this->id + ])->queryScalar() ?? 0; + app()->redis->hSet($this->infoCacheKey, 'true_downloaded', $this->true_downloaded); + } } - return $download; + return $this->true_downloaded; } return $this->downloaded; } @@ -306,4 +314,29 @@ public function getPasskey() { return $this->passkey; } + + /** + * @return mixed + */ + public function getInvites() + { + return $this->invites; + } + + /** + * @return mixed + */ + public function getTempInvites() + { + if (is_null($this->temp_invites)) { + $this->temp_invites = app()->redis->hGet($this->infoCacheKey, 'temp_invite'); + if (false === $this->temp_invites) { + $this->temp_invites = app()->pdo->createCommand('SELECT SUM(`qty`) FROM `user_invitations` WHERE `user_id` = :uid AND `qty` > 0 AND `expire_at` < NOW()')->bindParams([ + "uid" => $this->id + ])->queryScalar() ?? 0; + app()->redis->hSet($this->infoCacheKey, 'temp_invite', $this->temp_invites); + } + } + return $this->temp_invites; + } } diff --git a/apps/controllers/UserController.php b/apps/controllers/UserController.php index fa1dc21..3e77839 100644 --- a/apps/controllers/UserController.php +++ b/apps/controllers/UserController.php @@ -43,7 +43,7 @@ public function actionSessions() $to_del_session = app()->request->post('session'); // expired it from Database first - app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE uid = :uid AND sid = :sid')->bindParams([ + app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE uid = :uid AND sid = :sid')->bindParams([ 'uid' => app()->user->getId(), 'sid' => $to_del_session ])->execute(); $success = app()->pdo->getRowCount(); @@ -56,7 +56,7 @@ public function actionSessions() } } - $sessions = app()->pdo->createCommand('SELECT sid,login_at,login_ip,user_agent,last_access_at FROM users_session_log WHERE uid = :uid and expired = 0')->bindParams([ + $sessions = app()->pdo->createCommand('SELECT sid,login_at,login_ip,user_agent,last_access_at FROM user_session_log WHERE uid = :uid and expired = 0')->bindParams([ 'uid' => app()->user->getId() ])->queryAll(); return $this->render('user/sessions', ['sessions' => $sessions]); diff --git a/apps/lang/en.php b/apps/lang/en.php index 92538e7..fa34c5d 100644 --- a/apps/lang/en.php +++ b/apps/lang/en.php @@ -16,6 +16,7 @@ class en const nav_index = 'Index'; const nav_forums = 'Forums'; const nav_torrents = 'Torrents'; + const nav_collections = 'Collections'; const nav_requests = 'Requests'; const nav_upload = 'Upload'; const nav_subtitles = 'Subtitles'; diff --git a/apps/models/form/UserConfirmForm.php b/apps/models/form/UserConfirmForm.php index 33f8a52..df28c51 100644 --- a/apps/models/form/UserConfirmForm.php +++ b/apps/models/form/UserConfirmForm.php @@ -42,13 +42,13 @@ public static function callbackRules() } /** - * Verity The confirm secret and action exist in table `users_confirm` or not + * Verity The confirm secret and action exist in table `user_confirm` or not */ protected function validConfirmSecret() { $record = app()->pdo->createCommand( - 'SELECT `users_confirm`.`id`,`users_confirm`.`uid`,`users`.`status` FROM `users_confirm` - LEFT JOIN `users` ON `users`.`id` = `users_confirm`.`uid` + 'SELECT `user_confirm`.`id`,`user_confirm`.`uid`,`users`.`status` FROM `user_confirm` + LEFT JOIN `users` ON `users`.`id` = `user_confirm`.`uid` WHERE `serect` = :serect AND `action` = :action AND used = 0 LIMIT 1;')->bindParams([ 'serect' => $this->secret , 'action' => $this->action ])->queryOne(); @@ -64,7 +64,7 @@ protected function validConfirmSecret() } protected function update_confirm_status() { - app()->pdo->createCommand('UPDATE `users_confirm` SET `used` = 1 WHERE id = :id')->bindParams([ + app()->pdo->createCommand('UPDATE `user_confirm` SET `used` = 1 WHERE id = :id')->bindParams([ 'id' => $this->id ])->execute(); } diff --git a/apps/models/form/UserLoginForm.php b/apps/models/form/UserLoginForm.php index 5c69c6a..cc0f67e 100644 --- a/apps/models/form/UserLoginForm.php +++ b/apps/models/form/UserLoginForm.php @@ -135,13 +135,13 @@ public function createUserSession() $userSessionId = StringHelper::getRandomString($this->sessionLength - 1); $userSessionId = ($this->securelogin === 'yes' ? '1' : '0') . $userSessionId; - $count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `users_session_log` WHERE sid = :sid')->bindParams([ + $count = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `user_session_log` WHERE sid = :sid')->bindParams([ 'sid' => $userSessionId ])->queryScalar(); } while ($count != 0); // store user login information , ( for example `login ip`,`user_agent`,`last activity at` ) - app()->pdo->createCommand('INSERT INTO `users_session_log`(`uid`, `sid`, `login_ip`, `user_agent` , `last_access_at`) ' . + app()->pdo->createCommand('INSERT INTO `user_session_log`(`uid`, `sid`, `login_ip`, `user_agent` , `last_access_at`) ' . 'VALUES (:uid,:sid,INET6_ATON(:login_ip),:ua, NOW())')->bindParams([ 'uid' => $userId, 'sid' => $userSessionId, 'login_ip' => app()->request->getClientIp(), diff --git a/apps/models/form/UserRecoverForm.php b/apps/models/form/UserRecoverForm.php index e2f3db1..67aa950 100644 --- a/apps/models/form/UserRecoverForm.php +++ b/apps/models/form/UserRecoverForm.php @@ -55,7 +55,7 @@ public function flush() { // Send user email to get comfirm link $confirm_key = StringHelper::getRandomString(32); - app()->pdo->createCommand('INSERT INTO `users_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([ + app()->pdo->createCommand('INSERT INTO `user_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([ 'uid' => $user_info['id'], 'serect' => $confirm_key, 'action' => $this->_action ])->execute(); $confirm_url = app()->request->root() . '/auth/confirm?' . http_build_query([ diff --git a/apps/models/form/UserRegisterForm.php b/apps/models/form/UserRegisterForm.php index 953c974..7cbb9ba 100644 --- a/apps/models/form/UserRegisterForm.php +++ b/apps/models/form/UserRegisterForm.php @@ -273,7 +273,7 @@ public function flush() if ($this->confirm_way == 'email') { $confirm_key = StringHelper::getRandomString(32); - app()->pdo->createCommand('INSERT INTO `users_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([ + app()->pdo->createCommand('INSERT INTO `user_confirm` (`uid`,`serect`,`action`) VALUES (:uid,:serect,:action)')->bindParams([ 'uid' => $this->id, 'serect' => $confirm_key, 'action' => $this->_action ])->execute(); $confirm_url = app()->request->root() . '/auth/confirm?' . http_build_query([ diff --git a/apps/task/CronTabTimer.php b/apps/task/CronTabTimer.php index b3c2105..f20f5a5 100644 --- a/apps/task/CronTabTimer.php +++ b/apps/task/CronTabTimer.php @@ -79,7 +79,7 @@ protected function clean_expired_session() { $expired_sessions = app()->redis->zRangeByScore('Site:Sessions:to_expire', 0, $timenow); foreach ($expired_sessions as $session) { - app()->pdo->createCommand('UPDATE `users_session_log` SET `expired` = 1 WHERE sid = :sid')->createCommand([ + app()->pdo->createCommand('UPDATE `user_session_log` SET `expired` = 1 WHERE sid = :sid')->bindParams([ 'sid' => $session ])->execute(); } diff --git a/apps/views/layout/base.php b/apps/views/layout/base.php index ca7fa90..d635c72 100644 --- a/apps/views/layout/base.php +++ b/apps/views/layout/base.php @@ -51,20 +51,21 @@