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

Commit

Permalink
refactor(Entity): Separate Entity User, Torrent 's const to App\Repos…
Browse files Browse the repository at this point in the history
…itory
  • Loading branch information
Rhilip committed Jan 11, 2020
1 parent 407bb66 commit 50ecdbf
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 136 deletions.
8 changes: 5 additions & 3 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
'buff.random_percent_free',
);

/**
/** Config - gravatar
* If you set config('user.avatar_provider') == 'gravatar' , then you should edit this section
* Otherwise this section will not work
*
Expand All @@ -110,7 +110,10 @@
* - https://secure.gravatar.com/avatar/
* - https://cn.gravatar.com/avatar/
* - https://cdn.v2ex.com/gravatar/
* - https://gravatar.loli.net/
* - https://grv.luotianyi.vc/avatar/
* - https://gravatar.cat.net/avatar/
* - https://v2ex.assets.uxengine.net/gravatar/
*
* @var string
* @example 'https://www.gravatar.com/avatar/'
Expand All @@ -123,7 +126,7 @@
* across correctly since we not urlencode this value
* Or you can use offical built in options ,
* like:
* - https%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg (URL-encoded)
* - https://example.com/images/avatar.jpg
* - 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response
* - mp: (mystery-person) a simple, cartoon-style silhouetted outline of a person (does not vary by email hash)
* - identicon: a geometric pattern based on an email hash
Expand All @@ -135,7 +138,6 @@
*
* @var string
* @example 'identicon'
*
*/
'gravatar.default_fallback',

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- **layout:** Add anti-robots html meta tag (9c21e73)

### Fix
- **Config:** Fix JSON type config return False (1129008)
- **Cron:** Fix components lost in CronTabProcess (1ced4bf)
- **Redis:** Fix wrong type of Redis call function hMset() to hMSet() (a163150)
- **Tracker:** Fix typo in TrackerController (323c8ec)
Expand Down
3 changes: 2 additions & 1 deletion application/Components/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use App\Entity;
use App\Libraries\Constant;
use App\Repository\User\UserStatus;

use Rid\Base\Component;
use Rid\Helpers\JWTHelper;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected function loadCurUser($grant = 'cookies')
if ($user_id !== false && is_int($user_id) && $user_id > 0) {
$user_id = intval($user_id);
$curuser = app()->site->getUser($user_id);
if ($curuser->getStatus() !== Entity\User::STATUS_DISABLED) // user status shouldn't be disabled
if ($curuser->getStatus() !== UserStatus::DISABLED) // user status shouldn't be disabled
return $curuser;
}

Expand Down
7 changes: 5 additions & 2 deletions application/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

namespace App\Controllers;

use App\Entity\User;
use App\Models\Form\Auth;
use App\Repository\User\UserStatus;

use Rid\Http\Controller;


class AuthController extends Controller
{

/** @noinspection PhpUnused */
public function actionRegister()
{
if (app()->request->isPost()) {
Expand All @@ -31,7 +32,7 @@ public function actionRegister()
} else {
$register_form->flush(); // Save this user in our database and do clean work~

if ($register_form->getStatus() == User::STATUS_CONFIRMED) {
if ($register_form->getStatus() == UserStatus::CONFIRMED) {
return app()->response->redirect('/index');
} else {
return $this->render('auth/register_pending', [
Expand All @@ -45,6 +46,7 @@ public function actionRegister()
}
}

/** @noinspection PhpUnused */
public function actionConfirm()
{
$confirm = new Auth\UserConfirmForm();
Expand All @@ -64,6 +66,7 @@ public function actionConfirm()
}
}

/** @noinspection PhpUnused */
public function actionRecover()
{
if (app()->request->isPost()) {
Expand Down
8 changes: 4 additions & 4 deletions application/Controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace App\Controllers;

use App\Libraries\Constant;
use App\Entity\User;
use Rid\Utils\IpUtils;
use App\Repository\User\UserRole;
use App\Libraries\Bencode\Bencode;

use App\Exceptions\TrackerException;

use Rid\Utils\IpUtils;

/** @noinspection PhpUnused */
class TrackerController
{
Expand Down Expand Up @@ -721,7 +721,7 @@ private function checkSession($queries, $seeder, $userInfo, $torrentInfo)
throw new TrackerException(161, [':count' => config('tracker.user_max_leech')]);
}

if ($userInfo['class'] < User::ROLE_VIP) {
if ($userInfo['class'] < UserRole::VIP) {
$ratio = (($userInfo['downloaded'] > 0) ? ($userInfo['uploaded'] / $userInfo['downloaded']) : 1);
$gigs = $userInfo['downloaded'] / (1024 * 1024 * 1024);

Expand Down
14 changes: 0 additions & 14 deletions application/Entity/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ class Torrent

protected $comment_perpage = 10; // FIXME

const TORRENT_TYPE_SINGLE = 'single';
const TORRENT_TYPE_MULTI = 'multi';

const TORRENT_STATUSES = [
self::TORRENT_STATUS_CONFIRMED,
self::TORRENT_STATUS_PENDING,
self::TORRENT_STATUS_BANNED,
];

const TORRENT_STATUS_DELETED = 'deleted';
const TORRENT_STATUS_BANNED = 'banned';
const TORRENT_STATUS_PENDING = 'pending';
const TORRENT_STATUS_CONFIRMED = 'confirmed';

public function __construct($id = null)
{
$this->loadTorrentContentById($id);
Expand Down
63 changes: 1 addition & 62 deletions application/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,9 @@

class User
{

use AttributesImportUtils;
use ClassValueCacheUtils;

// User class
public const ROLE_PEASANT = 0;
public const ROLE_USER = 1;
public const ROLE_POWER_USER = 2;
public const ROLE_ELITE_USER = 3;
public const ROLE_CRAZY_USER = 4;
public const ROLE_INSANE_USER = 5;
public const ROLE_VETERAN_USER = 6;
public const ROLE_EXTREME_USER = 7;
public const ROLE_ULTIMATE_USER = 8;
public const ROLE_MASTER_USER = 9; # The max level that user can reached if they reached the level setting
public const ROLE_TEMP_VIP = 10; # The max level that user can reached via bonus exchange

// Contributor class
public const ROLE_VIP = 20;
public const ROLE_RETIREE = 30;

// Uploader class
public const ROLE_UPLOADER = 40;
public const ROLE_HELPER = 50;

// Administrator class
public const ROLE_FORUM_MODERATOR = 60;
public const ROLE_MODERATOR = 70;
public const ROLE_ADMINISTRATOR = 80;
public const ROLE_SYSOP = 90;
public const ROLE_STAFFLEADER = 100;

public const ROLE = [
self::ROLE_PEASANT => 'PEASANT',
self::ROLE_USER => 'USER',
self::ROLE_POWER_USER => 'POWER_USER',
self::ROLE_ELITE_USER => 'ELITE_USER',
self::ROLE_CRAZY_USER => 'CRAZY_USER',
self::ROLE_INSANE_USER => 'INSANE_USER',
self::ROLE_VETERAN_USER => 'VETERAN_USER',
self::ROLE_EXTREME_USER => 'EXTREME_USER',
self::ROLE_ULTIMATE_USER => 'ULTIMATE_USER',
self::ROLE_MASTER_USER => 'MASTER_USER',
self::ROLE_TEMP_VIP => 'TEMP_VIP',

self::ROLE_VIP => 'VIP',
self::ROLE_RETIREE => 'RETIREE',

self::ROLE_UPLOADER => 'UPLOADER',
self::ROLE_HELPER => 'HELPER',

self::ROLE_FORUM_MODERATOR => 'FORUM_MODERATOR',
self::ROLE_MODERATOR => 'MODERATOR',
self::ROLE_ADMINISTRATOR => 'ADMINISTRATOR',
self::ROLE_SYSOP => 'SYSOP',
self::ROLE_STAFFLEADER => 'STAFFLEADER'
];

// User Status
public const STATUS_DISABLED = 'disabled';
public const STATUS_PENDING = 'pending';
public const STATUS_PARKED = 'parked';
public const STATUS_CONFIRMED = 'confirmed';

private $id;
private $username;
private $email;
Expand Down Expand Up @@ -183,7 +122,7 @@ public function getAvatar(array $opts = []): string
's' => $opts['s'] ?? 80,
'd' => $opts['d'] ?? config('gravatar.default_fallback') ?? 'identicon',
'r' => $opts['r'] ?? config('gravatar.maximum_rating') ?? 'g'
]);
], null, '&', PHP_QUERY_RFC3986);
return $url;
}/* elseif (config('user.avatar_provider') === 'remote') {
// For example : another Image Hosting
Expand Down
11 changes: 6 additions & 5 deletions application/Models/Form/Auth/UserConfirmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace App\Models\Form\Auth;

use App\Entity\User;
use App\Repository\User\UserStatus;

use Rid\Helpers\StringHelper;
use Rid\Validators\Validator;

Expand Down Expand Up @@ -51,7 +52,7 @@ public static function callbackRules(): array
protected function validConfirmSecret()
{
$record = app()->pdo->createCommand(
'SELECT `user_confirm`.`id`,`user_confirm`.`uid`,`users`.`status`,`users`.`username`,`users`.`email` FROM `user_confirm`
'SELECT `user_confirm`.`id`,`user_confirm`.`uid`,`users`.`status`,`users`.`username`,`users`.`email` FROM `user_confirm`
LEFT JOIN `users` ON `users`.`id` = `user_confirm`.`uid`
WHERE `secret` = :secret AND `action` = :action AND used = 0 LIMIT 1;')->bindParams([
'secret' => $this->getInput('secret'), 'action' => $this->getInput('action')
Expand All @@ -78,12 +79,12 @@ private function update_confirm_status()

private function flush_register()
{
if ($this->user_status !== User::STATUS_PENDING) {
if ($this->user_status !== UserStatus::PENDING) {
return 'user status is not pending , they may already confirmed or banned'; // FIXME msg
}

app()->pdo->createCommand('UPDATE `users` SET `status` = :s WHERE `id` = :uid')->bindParams([
's' => User::STATUS_CONFIRMED, 'uid' => $this->uid
's' => UserStatus::CONFIRMED, 'uid' => $this->uid
])->execute();
$this->update_confirm_status();
app()->redis->del('User:content_' . $this->uid);
Expand All @@ -92,7 +93,7 @@ private function flush_register()

private function flush_recover()
{
if ($this->user_status !== User::STATUS_CONFIRMED) {
if ($this->user_status !== UserStatus::CONFIRMED) {
return 'user status is not confirmed , they may in pending or banned'; // FIXME msg
}

Expand Down
9 changes: 3 additions & 6 deletions application/Models/Form/Auth/UserLoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
namespace App\Models\Form\Auth;

use App\Libraries\Constant;
use App\Entity\User;
use App\Repository\User\UserStatus;

use Rid\Helpers\StringHelper;
use Rid\Helpers\JWTHelper;
use Rid\Validators\CaptchaTrait;
use Rid\Validators\Validator;

use RobThree\Auth\TwoFactorAuth;
use RobThree\Auth\TwoFactorAuthException;


class UserLoginForm extends Validator
{
Expand Down Expand Up @@ -87,7 +84,7 @@ protected function loadUserFromPdo()
return;
}

// User enable 2FA but it's code is wrong
// FIXME User enable 2FA but it's code is wrong
/*
if (!is_null($this->self['opt'])) {
try {
Expand All @@ -104,7 +101,7 @@ protected function loadUserFromPdo()
*/

// User 's status is banned or pending~
if (in_array($this->self['status'], [User::STATUS_DISABLED, User::STATUS_PENDING])) {
if (in_array($this->self['status'], [UserStatus::DISABLED, UserStatus::PENDING])) {
$this->buildCallbackFailMsg('Account', 'User account is disabled or may not confirmed.');
return;
}
Expand Down
5 changes: 3 additions & 2 deletions application/Models/Form/Auth/UserRecoverForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace App\Models\Form\Auth;

use App\Entity\User;
use App\Repository\User\UserStatus;

use Rid\Helpers\StringHelper;
use Rid\Validators\CaptchaTrait;
use Rid\Validators\Validator;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function flush() {
'email' => $this->email
])->queryOne();
if ($user_info !== false) {
if ($user_info['status'] !== User::STATUS_CONFIRMED) {
if ($user_info['status'] !== UserStatus::CONFIRMED) {
return 'std_user_account_unconfirmed';
}

Expand Down
16 changes: 9 additions & 7 deletions application/Models/Form/Auth/UserRegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace App\Models\Form\Auth;

use App\Libraries\Constant;
use App\Entity\User;
use App\Libraries\Constant;
use App\Repository\User\UserRole;
use App\Repository\User\UserStatus;

use Rid\Helpers\StringHelper;
use Rid\Validators\Validator;
Expand Down Expand Up @@ -92,8 +94,8 @@ public static function callbackRules(): array

public function buildDefaultPropAfterValid()
{
$this->status = config('register.user_default_status') ?? User::STATUS_PENDING;
$this->class = config('register.user_default_class') ?? User::ROLE_USER;
$this->status = config('register.user_default_status') ?? UserStatus::PENDING;
$this->class = config('register.user_default_class') ?? UserRole::USER;
$this->uploadpos = config('register.user_default_uploadpos') ?? 1;
$this->downloadpos = config('register.user_default_downloadpos') ?? 1;
$this->uploaded = config('register.user_default_uploaded') ?? 1;
Expand Down Expand Up @@ -266,14 +268,14 @@ public function flush()
* and can access the (super)admin panel to change site config .
*/
if (app()->site::fetchUserCount() == 0) {
$this->status = User::STATUS_CONFIRMED;
$this->class = User::ROLE_STAFFLEADER;
$this->status = UserStatus::CONFIRMED;
$this->class = UserRole::STAFFLEADER;
$this->confirm_way = 'auto';
}

// User status should be confirmed if site confirm_way is auto
if ($this->confirm_way == 'auto' and $this->status != User::STATUS_CONFIRMED) {
$this->status = User::STATUS_CONFIRMED;
if ($this->confirm_way == 'auto' and $this->status != UserStatus::CONFIRMED) {
$this->status = UserStatus::CONFIRMED;
}

// Insert into `users` table and get insert id
Expand Down
9 changes: 4 additions & 5 deletions application/Models/Form/Torrent/DownloadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ public function getSendFileContent()
$dict = $this->getTorrentFileContentDict();

$scheme = 'http://';
if (filter_var($this->https, FILTER_VALIDATE_BOOLEAN))
$scheme = 'https://';
else if (filter_var($this->https, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))
$scheme = 'http://';
else if (app()->request->isSecure())
if (isset($this->https)) {
$scheme = filter_var($this->https, FILTER_VALIDATE_BOOLEAN) ? 'https://' : 'http://';
} elseif (app()->request->isSecure()) {
$scheme = 'https://';
}

$announce_suffix = '/announce?passkey=' . app()->auth->getCurUser()->getPasskey();
$dict['announce'] = $scheme . config('base.site_tracker_url') . $announce_suffix;
Expand Down
Loading

0 comments on commit 50ecdbf

Please sign in to comment.