mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 12:07:23 +08:00
user token permission configurable
This commit is contained in:
@@ -14,6 +14,7 @@ use Filament\Facades\Filament;
|
|||||||
use Filament\Resources\Pages\Page;
|
use Filament\Resources\Pages\Page;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Illuminate\Support\HtmlString;
|
use Illuminate\Support\HtmlString;
|
||||||
|
use Nexus\Database\NexusDB;
|
||||||
|
|
||||||
class EditSetting extends Page implements Forms\Contracts\HasForms
|
class EditSetting extends Page implements Forms\Contracts\HasForms
|
||||||
{
|
{
|
||||||
@@ -85,11 +86,22 @@ class EditSetting extends Page implements Forms\Contracts\HasForms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Setting::query()->upsert($data, ['name'], ['value']);
|
Setting::query()->upsert($data, ['name'], ['value']);
|
||||||
|
$this->doAfterUpdate();
|
||||||
do_action("nexus_setting_update");
|
do_action("nexus_setting_update");
|
||||||
clear_setting_cache();
|
clear_setting_cache();
|
||||||
send_admin_success_notification();
|
send_admin_success_notification();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this actions get config must not use cache !!!
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function doAfterUpdate(): void
|
||||||
|
{
|
||||||
|
Setting::updateUserTokenPermissionAllowedCache();
|
||||||
|
}
|
||||||
|
|
||||||
private function getTabs(): array
|
private function getTabs(): array
|
||||||
{
|
{
|
||||||
$tabs = [];
|
$tabs = [];
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Models\PluginStore;
|
use App\Models\PluginStore;
|
||||||
|
use App\Models\Setting;
|
||||||
|
use App\Repositories\TokenRepository;
|
||||||
use App\Repositories\ToolRepository;
|
use App\Repositories\ToolRepository;
|
||||||
use App\Repositories\UploadRepository;
|
use App\Repositories\UploadRepository;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -33,7 +35,8 @@ class ToolController extends Controller
|
|||||||
public function test(Request $request)
|
public function test(Request $request)
|
||||||
{
|
{
|
||||||
$result = ['id' => 1];
|
$result = ['id' => 1];
|
||||||
$result['logFile'] = getLogFile();
|
$result['permissions'] = TokenRepository::listUserTokenPermissionAllowed();
|
||||||
|
// $result['permissions'] = Setting::getPermissionUserTokenAllowed();
|
||||||
$resource = new JsonResource($result);
|
$resource = new JsonResource($result);
|
||||||
return $this->success($resource);
|
return $this->success($resource);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class Setting extends NexusModel
|
|||||||
const ROLE_PERMISSION_CACHE_KEY_PREFIX = 'nexus_role_permissions_';
|
const ROLE_PERMISSION_CACHE_KEY_PREFIX = 'nexus_role_permissions_';
|
||||||
|
|
||||||
const TORRENT_GLOBAL_STATE_CACHE_KEY = 'global_promotion_state';
|
const TORRENT_GLOBAL_STATE_CACHE_KEY = 'global_promotion_state';
|
||||||
|
const USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY = 'user_token_permission_allowed';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get setting autoload = yes with cache
|
* get setting autoload = yes with cache
|
||||||
@@ -100,6 +101,18 @@ class Setting extends NexusModel
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function updateUserTokenPermissionAllowedCache(): void
|
||||||
|
{
|
||||||
|
$redis = NexusDB::redis();
|
||||||
|
$key = self::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY;
|
||||||
|
$redis->del($key);
|
||||||
|
//must not use cache
|
||||||
|
$allowed = self::getFromDb("permission.user_token_allowed");
|
||||||
|
if (!empty($allowed)) {
|
||||||
|
$redis->sAdd($key, ...$allowed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function getDefaultLang(): string
|
public static function getDefaultLang(): string
|
||||||
{
|
{
|
||||||
return self::get("main.defaultlang");
|
return self::get("main.defaultlang");
|
||||||
@@ -223,4 +236,10 @@ class Setting extends NexusModel
|
|||||||
return self::get("smtp.smtptype");
|
return self::get("smtp.smtptype");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getPermissionUserTokenAllowed(): array
|
||||||
|
{
|
||||||
|
return self::get("permission.user_token_allowed");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -615,6 +615,13 @@ class User extends Authenticatable implements FilamentUser, HasName
|
|||||||
return is_null($this->original['notifs']) || str_contains($this->notifs, "[{$name}]");
|
return is_null($this->original['notifs']) || str_contains($this->notifs, "[{$name}]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tokenCan(string $ability)
|
||||||
|
{
|
||||||
|
$redis = NexusDB::redis();
|
||||||
|
return $redis->sismember(Setting::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY, $ability)
|
||||||
|
&& $this->accessToken && $this->accessToken->can($ability);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,21 +2,32 @@
|
|||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
use App\Enums\Permission\RoutePermissionEnum;
|
use App\Enums\Permission\RoutePermissionEnum;
|
||||||
|
use App\Models\Setting;
|
||||||
|
|
||||||
class TokenRepository extends BaseRepository
|
class TokenRepository extends BaseRepository
|
||||||
{
|
{
|
||||||
private static array $userTokenPermissions = [
|
private static array $userTokenPermissions = [
|
||||||
RoutePermissionEnum::TORRENT_LIST,
|
RoutePermissionEnum::TORRENT_LIST->value,
|
||||||
RoutePermissionEnum::TORRENT_VIEW,
|
RoutePermissionEnum::TORRENT_VIEW->value,
|
||||||
RoutePermissionEnum::TORRENT_UPLOAD,
|
RoutePermissionEnum::TORRENT_UPLOAD->value,
|
||||||
RoutePermissionEnum::USER_VIEW,
|
RoutePermissionEnum::USER_VIEW->value,
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function listUserTokenPermissions(): array
|
public static function listUserTokenPermissions(): array
|
||||||
|
{
|
||||||
|
return self::formatPermissions(self::$userTokenPermissions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function listUserTokenPermissionAllowed(): array
|
||||||
|
{
|
||||||
|
return self::formatPermissions(Setting::getPermissionUserTokenAllowed());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function formatPermissions(array $permissions): array
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
foreach (self::$userTokenPermissions as $permission) {
|
foreach ($permissions as $permission) {
|
||||||
$result[$permission->value] = nexus_trans("route-permission.{$permission->value}.text");
|
$result[$permission] = nexus_trans("route-permission.{$permission}.text");
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1128,7 +1128,7 @@ JS;
|
|||||||
//end seed box
|
//end seed box
|
||||||
|
|
||||||
//token start
|
//token start
|
||||||
$permissions = \App\Repositories\TokenRepository::listUserTokenPermissions();
|
$permissions = \App\Repositories\TokenRepository::listUserTokenPermissionAllowed();
|
||||||
$permissionOptions = [];
|
$permissionOptions = [];
|
||||||
foreach ($permissions as $name => $label) {
|
foreach ($permissions as $name => $label) {
|
||||||
$permissionOptions[] = sprintf('<label><input type="checkbox" name="permissions[]" value="%s">%s</label>', $name, $label);
|
$permissionOptions[] = sprintf('<label><input type="checkbox" name="permissions[]" value="%s">%s</label>', $name, $label);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ $userRep = new \App\Repositories\UserRepository();
|
|||||||
if ($user['added'] == "0000-00-00 00:00:00" || $user['added'] == null) {
|
if ($user['added'] == "0000-00-00 00:00:00" || $user['added'] == null) {
|
||||||
$joindate = $lang_userdetails['text_not_available'];
|
$joindate = $lang_userdetails['text_not_available'];
|
||||||
} else {
|
} else {
|
||||||
$weeks = abs($userInfo->added->diffInWeeks()) . nexus_trans('nexus.time_units.week');
|
$weeks = abs(number_format($userInfo->added->diffInWeeks(), 1)) . nexus_trans('nexus.time_units.week');
|
||||||
$joindate = $user['added']." (" . gettime($user["added"], true, false, true).", $weeks)";
|
$joindate = $user['added']." (" . gettime($user["added"], true, false, true).", $weeks)";
|
||||||
}
|
}
|
||||||
$lastseen = $user["last_access"];
|
$lastseen = $user["last_access"];
|
||||||
|
|||||||
Reference in New Issue
Block a user