Files
nexusphp/app/Repositories/BaseRepository.php

76 lines
2.5 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)) {
//用户强私密,或者种子作者匿名而当前项作者刚好为种子作者
2022-09-19 16:27:04 +08:00
$anonymousText = nexus_trans('label.anonymous');
2022-06-12 15:15:09 +08:00
if($authenticator->class >= $canViewAnonymousClass || $user->id == $authenticator->id) {
//但当前用户权限可以查看匿名者,或当前用户查看自己的数据,显示个匿名,后边加真实用户名
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
{
$append = " 2>&1";
if (!str_ends_with($command, $append)) {
$command .= $append;
}
do_log("command: $command");
$result = exec($command, $output, $result_code);
$outputString = implode("\n", $output);
do_log(sprintf('result_code: %s, result: %s, output: %s', $result_code, $result, $outputString));
if ($result_code != 0) {
throw new \RuntimeException($outputString);
}
return $format == 'string' ? $outputString : $output;
}
2021-04-17 19:01:33 +08:00
}