Files
nexusphp/app/Repositories/BaseRepository.php

56 lines
1.7 KiB
PHP
Raw Normal View History

2021-04-17 19:01:33 +08:00
<?php
namespace App\Repositories;
2022-06-12 15:15:09 +08:00
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\User;
2021-06-01 23:33:28 +08:00
use Illuminate\Encryption\Encrypter;
2021-04-17 19:01:33 +08:00
use Illuminate\Support\Str;
class BaseRepository
{
2021-06-01 23:33:28 +08:00
protected function getSortFieldAndType(array $params): array
2021-04-17 19:01:33 +08:00
{
2022-03-04 16:16:56 +08:00
$field = !empty($params['sort_field']) ? $params['sort_field'] : 'id';
2021-04-17 19:01:33 +08:00
$type = 'desc';
if (!empty($params['sort_type']) && Str::startsWith($params['sort_type'], 'asc')) {
$type = 'asc';
}
return [$field, $type];
}
2021-06-01 23:33:28 +08:00
2022-06-12 15:15:09 +08:00
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;
}
}
2022-06-15 15:43:33 +08:00
/**
* @param $user
* @param null $fields
* @return User
*/
protected function getUser($user, $fields = null): User
{
if ($user instanceof User) {
return $user;
}
if ($fields === null) {
$fields = User::$commonFields;
}
return User::query()->findOrFail(intval($user), $fields);
}
2021-04-17 19:01:33 +08:00
}