Files
nexusphp/app/Repositories/BaseRepository.php

73 lines
2.1 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;
2025-04-17 01:39:40 +07:00
use Illuminate\Http\Request;
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
2025-04-17 01:39:40 +07:00
protected function getPerPageFromRequest(Request $request)
{
return $request->get('per_page');
}
2023-05-01 13:13:53 +08:00
protected function handleAnonymous($username, $user, User $authenticator, Torrent $torrent = null)
2022-06-12 15:15:09 +08:00
{
2023-05-01 13:13:53 +08:00
if (!$user) {
return "";
}
2022-06-12 15:15:09 +08:00
if($user->privacy == "strong" || ($torrent && $torrent->anonymous == 'yes' && $user->id == $torrent->owner)) {
//用户强私密,或者种子作者匿名而当前项作者刚好为种子作者
2022-09-19 16:27:04 +08:00
$anonymousText = nexus_trans('label.anonymous');
2022-09-20 00:01:45 +08:00
if(user_can('viewanonymous', false, $authenticator->id) || $user->id == $authenticator->id) {
2022-06-12 15:15:09 +08:00
//但当前用户权限可以查看匿名者,或当前用户查看自己的数据,显示个匿名,后边加真实用户名
2022-09-19 16:27:04 +08:00
return sprintf('%s(%s)', $anonymousText, $username);
2022-06-12 15:15:09 +08:00
} else {
2022-09-19 16:27:04 +08:00
return $anonymousText;
2022-06-12 15:15:09 +08:00
}
} else {
return $username;
}
}
2022-06-15 15:43:33 +08:00
/**
* @param $user
* @param null $fields
2022-07-30 15:06:51 +08:00
* @return User|null
2022-06-15 15:43:33 +08:00
*/
2022-07-30 15:06:51 +08:00
protected function getUser($user, $fields = null): User|null
2022-06-15 15:43:33 +08:00
{
2022-07-30 15:06:51 +08:00
if ($user === null) {
return null;
}
2022-06-15 15:43:33 +08:00
if ($user instanceof User) {
return $user;
}
if ($fields === null) {
$fields = User::$commonFields;
}
return User::query()->findOrFail(intval($user), $fields);
}
2022-09-17 14:59:46 +08:00
protected function executeCommand($command, $format = 'string'): string|array
{
2023-01-07 16:27:27 +08:00
return executeCommand($command, $format);
2022-09-17 14:59:46 +08:00
}
2021-04-17 19:01:33 +08:00
}