Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(User): Add user load by passkey
Browse files Browse the repository at this point in the history
1. Add user load content by passkey.
2. Fix user cache key.
3.
  • Loading branch information
Rhilip committed Mar 10, 2019
1 parent ec415ac commit b93d94c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
21 changes: 21 additions & 0 deletions apps/controllers/RssController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/10
* Time: 9:14
*/

namespace apps\controllers;


class RssController
{
public function actionIndex() {
app()->user->loadUserFromPasskey();



return 'TODO';
}
}
1 change: 1 addition & 0 deletions apps/middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function handle($callable, \Closure $next)
list($controller, $action) = $callable;
$controllerName = get_class($controller);

app()->user->loadUserFromCookies();
$isAnonymousUser = app()->user->isAnonymous();

if ($controllerName === \apps\controllers\AuthController::class) {
Expand Down
2 changes: 1 addition & 1 deletion apps/models/form/UserConfirmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function flush()
app()->pdo->createCommand('DELETE FROM `users_confirm` WHERE id = :id')->bindParams([
'id' => $this->id
])->execute();
app()->redis->del('USER:content_' . $this->uid);
app()->redis->del('User:content_' . $this->uid);
}
}
16 changes: 8 additions & 8 deletions apps/views/torrents/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<table class="layui-table">
<thead>
<tr>
<th class="text-center" style="width: 70px">Category</th>
<th class="text-center" style="width: 20px">Category</th>
<th>Torrent</th>
<th class="text-center" style="width: 70px">Link</th>
<th class="text-center" style="width: 100px">Size</th>
<th class="text-center" style="width: 140px">Date</th>
<th class="text-center" style="width: 30px"><i class="fas fa-arrow-up" title="Seeders"></th>
<th class="text-center" style="width: 30px"><i class="fas fa-arrow-down" title="Leechers"></i></th>
<th class="text-center" style="width: 30px"><i class="fas fa-check" title="Completed"></th>
<th class="text-center" style="width: 70px"><i class="fas fa-user" title="Owner"></i></th>
<th class="text-center" style="width: 5px">Link</th>
<th class="text-center" style="width: 50px">Size</th>
<th class="text-center" style="width: 100px">Date</th>
<th class="text-center" style="width: 15px"><i class="fas fa-arrow-up" title="Seeders"></th>
<th class="text-center" style="width: 15px"><i class="fas fa-arrow-down" title="Leechers"></i></th>
<th class="text-center" style="width: 15px"><i class="fas fa-check" title="Completed"></th>
<th class="text-center" style="width: 50px"><i class="fas fa-user" title="Owner"></i></th>
</tr>
</thead>
<tbody>
Expand Down
33 changes: 22 additions & 11 deletions framework/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,57 @@ class User extends Component implements UserInterface
use UserTrait;

// Cookie
public $sessionSaveKey = 'SESSION:user_set';
public $sessionSaveKey = 'User_Map:session_to_id';
public $cookieName = 'rid';

// Key of User Session
protected $_userSessionId;

// Passkey
public $passkeyCacheKey = 'User_Map:passkey_to_id';

private $anonymous = false;

public function onRequestBefore()
{
parent::onRequestBefore();
$this->loadUser();
$this->anonymous = true;
}

public function onRequestAfter()
{
parent::onRequestAfter();
app()->redis->disconnect();
}

public function isAnonymous()
{
return $this->anonymous;
}

public function loadUser() {
// TODO Load User From Passkey in some route , for example '/rss'
return $this->loadUserFromCookies();
}

public function loadUserFromCookies()
{
$this->_userSessionId = \Rid::app()->request->cookie($this->cookieName);
$this->_userSessionId = app()->request->cookie($this->cookieName);
$userId = app()->redis->zScore($this->sessionSaveKey, $this->_userSessionId);

if ($userId) {
$this->loadUserContentById($userId);
$this->anonymous = false;
} else {
$this->anonymous = true;
}
}

public function loadUserFromPasskey()
{
$passkey = app()->request->get('passkey');
$userId = app()->redis->zScore($this->passkeyCacheKey, $passkey);
if ($userId == false) {
$userId = app()->pdo->createCommand('SELECT `id` FROM `users` WHERE `passkey` = :passkey LIMIT 1')->bindParams([
'passkey' => $passkey
])->queryScalar() ?: 0;
app()->redis->zAdd($this->passkeyCacheKey, $userId, $passkey);
}
if ($userId !== 0) {
$this->loadUserContentById($userId);
$this->anonymous = false;
}
}

Expand Down
6 changes: 3 additions & 3 deletions framework/User/UserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait UserTrait

public function loadUserContentById($id)
{
$this->infoCacheKey = 'USER:id_' . $id . '_content';
$this->infoCacheKey = 'User:id_' . $id . '_content';
$self = app()->redis->hGetAll($this->infoCacheKey);
if (empty($self)) {
$self = app()->pdo->createCommand("SELECT * FROM `users` WHERE id = :id;")->bindParams([
Expand All @@ -61,12 +61,12 @@ public function loadUserContentById($id)

public function loadUserContentByName($name)
{
$uid = app()->redis->hGet('USER:map_name_to_id', $name);
$uid = app()->redis->hGet('User_Map:name_to_id', $name);
if (false === $uid) {
$uid = app()->pdo->createCommand('SELECT id FROM `users` WHERE LOWER(`username`) = LOWER(:uname) LIMIT 1;')->bindParams([
'uname' => $name
])->queryScalar();
app()->redis->hSet('USER:map_name_to_id', $name, $uid);
app()->redis->hSet('User_Map:name_to_id', $name, $uid);
}
if ($uid) {
$this->loadTorrentContentById($uid);
Expand Down

0 comments on commit b93d94c

Please sign in to comment.