get_username() support in laravel

This commit is contained in:
xiaomlove
2022-06-12 15:15:09 +08:00
parent 4fc91f52cc
commit 43156f7fc5
8 changed files with 108 additions and 24 deletions

View File

@@ -77,9 +77,10 @@ class Test extends Command
*/
public function handle()
{
$torrent = \App\Models\Torrent::query()->find(3);
$promotionInfo = apply_filter('torrent_promotion', $torrent->toArray());
dd($promotionInfo);
$end = Carbon::parse('2022-06-06 14:10');
$begin = Carbon::parse('2022-06-06 03:10');
$r = $end->diffInHours($begin);
dd($r);
}

View File

@@ -34,7 +34,7 @@ class Torrent extends NexusModel
public static $commentFields = [
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'pos_state', 'hr', 'picktype', 'picktime',
'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover'
'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous',
];
public static $basicRelations = [

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\HasApiTokens;
use Nexus\Database\NexusDB;
@@ -173,7 +174,7 @@ class User extends Authenticatable
'id', 'username', 'email', 'class', 'status', 'added', 'avatar',
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
'invited_by', 'enabled', 'seed_points', 'last_access', 'invites',
'lang', 'attendance_card',
'lang', 'attendance_card', 'privacy', 'noad',
];
public static function getDefaultUserAttributes(): array
@@ -196,12 +197,28 @@ class User extends Authenticatable
];
}
public static function defaultUser()
public static function defaultUser(): static
{
return new static(self::getDefaultUserAttributes());
}
public function checkIsNormal(array $fields = ['status', 'enabled'])
public static function getClassName($class, $compact = false, $b_colored = false, $I18N = false)
{
$class_name = self::$classes[$class]['text'];
if ($class >= self::CLASS_VIP && $I18N) {
$class_name = nexus_trans("user.class_names.$class");
}
$class_name_color = self::$classes[$class]['text'];
if ($compact) {
$class_name = str_replace(" ", "",$class_name);
}
if ($b_colored) {
return "<b class='" . str_replace(" ", "",$class_name_color) . "_Name'>" . $class_name . "</b>";
}
return $class_name;
}
public function checkIsNormal(array $fields = ['status', 'enabled']): bool
{
if (in_array('status', $fields) && $this->getAttribute('status') != self::STATUS_CONFIRMED) {
throw new \InvalidArgumentException(sprintf('User: %s is not confirmed.', $this->id));
@@ -416,7 +433,7 @@ class User extends Authenticatable
return $this->update($update);
}
public function canAccessAdmin()
public function canAccessAdmin(): bool
{
$targetClass = self::CLASS_SYSOP;
if (!$this->class || $this->class < $targetClass) {
@@ -426,7 +443,7 @@ class User extends Authenticatable
return true;
}
public function isDonating()
public function isDonating(): bool
{
$rawDonorUntil = $this->getRawOriginal('donoruntil');
if (
@@ -438,4 +455,6 @@ class User extends Authenticatable
return false;
}
}

View File

@@ -2,6 +2,9 @@
namespace App\Repositories;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Str;
@@ -17,4 +20,20 @@ class BaseRepository
return [$field, $type];
}
protected function handleAnonymous($username, User $user, User $authenticator, Torrent $torrent = null)
{
$canViewAnonymousClass = Setting::get('authority.viewanonymous');
if($user->privacy == "strong" || ($torrent && $torrent->anonymous == 'yes' && $user->id == $torrent->owner)) {
//用户强私密,或者种子作者匿名而当前项作者刚好为种子作者
if($authenticator->class >= $canViewAnonymousClass || $user->id == $authenticator->id) {
//但当前用户权限可以查看匿名者,或当前用户查看自己的数据,显示个匿名,后边加真实用户名
return sprintf('匿名(%s)', $username);
} else {
return '匿名';
}
} else {
return $username;
}
}
}