mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 20:17:24 +08:00
[admin] add grant props
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Filament\Resources\User\UserResource\Pages;
|
||||
use App\Filament\Resources\User\UserResource;
|
||||
use App\Models\Medal;
|
||||
use App\Models\User;
|
||||
use App\Models\UserMeta;
|
||||
use App\Repositories\ExamRepository;
|
||||
use App\Repositories\MedalRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
@@ -14,12 +15,15 @@ use Filament\Resources\Pages\Page;
|
||||
use Filament\Pages\Actions;
|
||||
use Filament\Forms;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class UserProfile extends Page
|
||||
{
|
||||
use InteractsWithRecord;
|
||||
use HasRelationManagers;
|
||||
|
||||
private static $rep;
|
||||
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected static string $view = 'filament.resources.user.user-resource.pages.user-profile';
|
||||
@@ -30,6 +34,14 @@ class UserProfile extends Page
|
||||
self::EVENT_RECORD_UPDATED => 'updateRecord'
|
||||
];
|
||||
|
||||
private function getRep(): UserRepository
|
||||
{
|
||||
if (!self::$rep) {
|
||||
self::$rep = new UserRepository();
|
||||
}
|
||||
return self::$rep;
|
||||
}
|
||||
|
||||
public function updateRecord($id)
|
||||
{
|
||||
$this->record = $this->resolveRecord($id);
|
||||
@@ -49,8 +61,9 @@ class UserProfile extends Page
|
||||
{
|
||||
$actions = [];
|
||||
if (Auth::user()->class > $this->record->class) {
|
||||
$actions[] = $this->buildAssignExamAction();
|
||||
$actions[] = $this->buildGrantPropsAction();
|
||||
$actions[] = $this->buildGrantMedalAction();
|
||||
$actions[] = $this->buildAssignExamAction();
|
||||
$actions[] = $this->buildChangeBonusEtcAction();
|
||||
if ($this->record->two_step_secret) {
|
||||
$actions[] = $this->buildDisableTwoStepAuthenticationAction();
|
||||
@@ -75,7 +88,7 @@ class UserProfile extends Page
|
||||
Forms\Components\Hidden::make('uid')->default($this->record->id),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
$userRep = $this->getRep();
|
||||
try {
|
||||
if ($data['action'] == 'enable') {
|
||||
$userRep->enableUser(Auth::user(), $data['uid'], $data['reason']);
|
||||
@@ -96,7 +109,7 @@ class UserProfile extends Page
|
||||
->modalHeading(__('admin.resources.user.actions.disable_two_step_authentication'))
|
||||
->requiresConfirmation()
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
$userRep = $this->getRep();
|
||||
try {
|
||||
$userRep->removeTwoStepAuthentication(Auth::user(), $this->record->id);
|
||||
$this->notify('success', 'Success!');
|
||||
@@ -129,7 +142,7 @@ class UserProfile extends Page
|
||||
Forms\Components\Textarea::make('reason')->label(__('admin.resources.user.actions.change_bonus_etc_reason_label')),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
$userRep = $this->getRep();
|
||||
try {
|
||||
$userRep->incrementDecrement(Auth::user(), $this->record->id, $data['action'], $data['field'], $data['value'], $data['reason']);
|
||||
$this->notify('success', 'Success!');
|
||||
@@ -152,7 +165,7 @@ class UserProfile extends Page
|
||||
->required(),
|
||||
])
|
||||
->action(function ($data) {
|
||||
$userRep = new UserRepository();
|
||||
$userRep = $this->getRep();
|
||||
try {
|
||||
$userRep->resetPassword($this->record->id, $data['password'], $data['password_confirmation']);
|
||||
$this->notify('success', 'Success!');
|
||||
@@ -241,7 +254,7 @@ class UserProfile extends Page
|
||||
// ->modalHeading($this->record->enabled == 'yes' ? __('admin.resources.user.actions.disable_modal_title') : __('admin.resources.user.actions.enable_modal_title'))
|
||||
->requiresConfirmation()
|
||||
->action(function () {
|
||||
$userRep = new UserRepository();
|
||||
$userRep = $this->getRep();
|
||||
try {
|
||||
$userRep->updateDownloadPrivileges(Auth::user(), $this->record->id, $this->record->downloadpos == 'yes' ? 'no' : 'yes');
|
||||
$this->notify('success', 'Success!');
|
||||
@@ -251,4 +264,57 @@ class UserProfile extends Page
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function buildGrantPropsAction()
|
||||
{
|
||||
return Actions\Action::make(__('admin.resources.user.actions.grant_prop_btn'))
|
||||
->modalHeading(__('admin.resources.user.actions.grant_prop_btn'))
|
||||
->form([
|
||||
Forms\Components\Select::make('meta_key')
|
||||
->options(UserMeta::listProps())
|
||||
->label(__('admin.resources.user.actions.grant_prop_form_prop'))->required(),
|
||||
Forms\Components\TextInput::make('duration')->label(__('admin.resources.user.actions.grant_prop_form_duration'))
|
||||
->helperText(__('admin.resources.user.actions.grant_prop_form_duration_help')),
|
||||
|
||||
])
|
||||
->action(function ($data) {
|
||||
$rep = $this->getRep();
|
||||
try {
|
||||
if (!empty($data['duration'])) {
|
||||
$data['deadline'] = now()->addDays($data['duration']);
|
||||
}
|
||||
$rep->addMeta($this->record, $data);
|
||||
$this->notify('success', 'Success!');
|
||||
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
|
||||
} catch (\Exception $exception) {
|
||||
$this->notify('danger', $exception->getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function getViewData(): array
|
||||
{
|
||||
return [
|
||||
'props' => $this->listUserProps(),
|
||||
];
|
||||
}
|
||||
|
||||
private function listUserProps(): array
|
||||
{
|
||||
$metaKeys = [
|
||||
UserMeta::META_KEY_PERSONALIZED_USERNAME,
|
||||
UserMeta::META_KEY_CHANGE_USERNAME,
|
||||
];
|
||||
$metaList = $this->getRep()->listMetas($this->record->id, $metaKeys);
|
||||
$props = [];
|
||||
foreach ($metaList as $metaKey => $metas) {
|
||||
$meta = $metas->first();
|
||||
$text = sprintf('[%s]', $meta->metaKeyText, );
|
||||
if ($meta->meta_key == UserMeta::META_KEY_PERSONALIZED_USERNAME) {
|
||||
$text .= sprintf('(%s)', $meta->getDeadlineText());
|
||||
}
|
||||
$props[] = "<div>{$text}</div>";
|
||||
}
|
||||
return $props;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +42,13 @@ class NexusModel extends Model
|
||||
return $d && $d->format($format) === $date;
|
||||
}
|
||||
|
||||
public function getDeadlineText($field = 'deadline')
|
||||
{
|
||||
$raw = $this->getRawOriginal($field);
|
||||
if (in_array($raw, [null, '0000-00-00 00:00:00', ''], true)) {
|
||||
return nexus_trans("label.permanent");
|
||||
}
|
||||
return sprintf('%s: %s', nexus_trans('label.deadline'), $raw);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -503,5 +503,4 @@ class User extends Authenticatable implements FilamentUser, HasName
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
+13
-1
@@ -10,7 +10,6 @@ class UserMeta extends NexusModel
|
||||
|
||||
const STATUS_NORMAL = 0;
|
||||
|
||||
|
||||
const META_KEY_PERSONALIZED_USERNAME = 'PERSONALIZED_USERNAME';
|
||||
|
||||
const META_KEY_CHANGE_USERNAME = 'CHANGE_USERNAME';
|
||||
@@ -21,6 +20,19 @@ class UserMeta extends NexusModel
|
||||
'deadline' => 'datetime',
|
||||
];
|
||||
|
||||
public static array $metaKeys = [
|
||||
self::META_KEY_PERSONALIZED_USERNAME => ['text' => 'PERSONALIZED_USERNAME', 'multiple' => false],
|
||||
self::META_KEY_CHANGE_USERNAME => ['text' => 'CHANGE_USERNAME', 'multiple' => false],
|
||||
];
|
||||
|
||||
public static function listProps()
|
||||
{
|
||||
return [
|
||||
self::META_KEY_PERSONALIZED_USERNAME => nexus_trans('label.user_meta.meta_keys.' . self::META_KEY_PERSONALIZED_USERNAME),
|
||||
self::META_KEY_CHANGE_USERNAME => nexus_trans('label.user_meta.meta_keys.' . self::META_KEY_CHANGE_USERNAME),
|
||||
];
|
||||
}
|
||||
|
||||
public function getMetaKeyTextAttribute()
|
||||
{
|
||||
return nexus_trans('label.user_meta.meta_keys.' . $this->meta_key) ?? '';
|
||||
|
||||
@@ -440,5 +440,29 @@ class UserRepository extends BaseRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addMeta($user, array $metaData, array $keyExistsUpdates = [])
|
||||
{
|
||||
$user = $this->getUser($user);
|
||||
$metaKey = $metaData['meta_key'];
|
||||
$allowMultiple = UserMeta::$metaKeys[$metaKey]['multiple'];
|
||||
if ($allowMultiple) {
|
||||
//Allow multiple, just insert
|
||||
$result = $user->metas()->create($metaData);
|
||||
} else {
|
||||
$metaExists = $user->metas()->where('meta_key', $metaKey)->first();
|
||||
if (!$metaExists) {
|
||||
$result = $user->metas()->create($metaData);
|
||||
} else {
|
||||
if (empty($keyExistsUpdates)) {
|
||||
$keyExistsUpdates = ['updated_at' => now()];
|
||||
}
|
||||
$result = $metaExists->update($keyExistsUpdates);
|
||||
}
|
||||
}
|
||||
if ($result) {
|
||||
clear_user_cache($user->id, $user->passkey);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user