From 028d985e9f76c3583d3963f0e14386ef5d4a7441 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 30 May 2026 11:56:57 +0530 Subject: [PATCH] Added crud admin rights to particular users and edit email permission to particular user --- app/Filament/Imports/UserImporter.php | 25 ++++++++++++++++++------- app/Filament/Resources/UserResource.php | 13 +++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/Filament/Imports/UserImporter.php b/app/Filament/Imports/UserImporter.php index da3758d..abb1b56 100644 --- a/app/Filament/Imports/UserImporter.php +++ b/app/Filament/Imports/UserImporter.php @@ -8,6 +8,7 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException; use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Models\Import; +use Filament\Facades\Filament; use Spatie\Permission\Models\Role; use Str; @@ -56,6 +57,12 @@ class UserImporter extends Importer $warnMsg = []; $plantCod = $this->data['plant_id']; $plant = null; + $user = $this->data['name']; + $mail = $this->data['email']; + $pass = $this->data['password']; + $restrictedUsers = ['Admin', 'Dhanabalan S', 'Ranjith B']; + $adminUser = Filament::auth()->user()?->name; + if (Str::length($plantCod) > 0 && (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod))) { $warnMsg[] = 'Invalid plant code found!'; } elseif (Str::length($plantCod) <= 0) { @@ -70,14 +77,14 @@ class UserImporter extends Importer } } - if (Str::length($this->data['name']) < 3) { + if (Str::length($user) < 3) { $warnMsg[] = 'Invalid user name found!'; } // || !is_numeric($this->data['code']) || !preg_match('/^[1-9]\d{3,}$/', $this->data['code']) - if (Str::length($this->data['email']) < 5) { + if (Str::length($mail) < 5) { $warnMsg[] = 'Invalid email found!'; } - if (Str::length($this->data['password']) < 3) { + if (Str::length($pass) < 3) { $warnMsg[] = 'Invalid password found!'; } // Validate roles if provided @@ -97,16 +104,20 @@ class UserImporter extends Importer $warnMsg[] = 'User roles not found!'; } + if (! in_array($adminUser, $restrictedUsers, true) && in_array($user, $restrictedUsers, true)) { + throw new RowImportFailedException("You don't have permission to import user with name '{$user}'!"); + } + if (! empty($warnMsg)) { throw new RowImportFailedException(implode(', ', $warnMsg)); } $user = User::updateOrCreate([ - 'email' => $this->data['email'], + 'email' => $mail, ], [ - 'name' => $this->data['name'], - 'password' => $this->data['password'], + 'name' => $user, + 'password' => $pass, 'plant_id' => $plant, ]); @@ -118,7 +129,7 @@ class UserImporter extends Importer return null; // return User::firstOrNew([ // // Update existing records, matching them by `$this->data['column_name']` - // 'email' => $this->data['email'], + // 'email' => $mail, // ]); // return new User(); diff --git a/app/Filament/Resources/UserResource.php b/app/Filament/Resources/UserResource.php index 308a290..dc7aabf 100644 --- a/app/Filament/Resources/UserResource.php +++ b/app/Filament/Resources/UserResource.php @@ -10,6 +10,7 @@ use App\Models\User; use Filament\Facades\Filament; use Filament\Forms; use Filament\Forms\Form; +use Filament\Forms\Get; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Actions\ExportAction; @@ -53,16 +54,19 @@ class UserResource extends Resource ->minLength(3) // ->reactive() ->live(debounce: 600) + ->disabled(fn (Get $get) => ! empty($get('id')) && Filament::auth()->user()?->name != 'Admin' && Filament::auth()->user()?->name != 'Dhanabalan S' && Filament::auth()->user()?->name != 'Ranjith B' && ($get('name') == 'Admin' || $get('name') == 'Dhanabalan S' || $get('name') == 'Ranjith B')) ->afterStateUpdated(function ($state, callable $set, callable $get) { - $set('email', $state.'@cripumps.com'); + if (Filament::auth()->user()?->name != 'Dhanabalan S') { + $set('email', $state.'@cripumps.com'); + } }) ->maxLength(255), Forms\Components\TextInput::make('email') ->label('Email') + ->required() // ->email() ->unique(ignoreRecord: true) - ->required() - ->readOnly() + ->readOnly(fn () => Filament::auth()->user()?->name != 'Dhanabalan S') // ->rule(function (callable $get) { // return Rule::unique('users', 'email') // ->ignore($get('id')); // Ignore current record during updates @@ -75,10 +79,11 @@ class UserResource extends Resource ->label('Email Verified At'), Forms\Components\TextInput::make('password') ->label('Password') + ->required() ->password() ->minLength(3) ->revealable() - ->required() + ->disabled(fn (Get $get) => ! empty($get('id')) && Filament::auth()->user()?->name != 'Admin' && Filament::auth()->user()?->name != 'Dhanabalan S' && Filament::auth()->user()?->name != 'Ranjith B' && ($get('name') == 'Admin' || $get('name') == 'Dhanabalan S' || $get('name') == 'Ranjith B')) // ->dehydrateStateUsing(fn (string $state): string => Hash::make($state)) ->maxLength(255), // Forms\Components\Select::make('roles')