Added crud admin rights to particular users and edit email permission to particular user
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled

This commit is contained in:
dhanabalan
2026-05-30 11:56:57 +05:30
parent 531c627e72
commit 028d985e9f
2 changed files with 27 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ use Filament\Actions\Imports\Exceptions\RowImportFailedException;
use Filament\Actions\Imports\ImportColumn; use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer; use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import; use Filament\Actions\Imports\Models\Import;
use Filament\Facades\Filament;
use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Role;
use Str; use Str;
@@ -56,6 +57,12 @@ class UserImporter extends Importer
$warnMsg = []; $warnMsg = [];
$plantCod = $this->data['plant_id']; $plantCod = $this->data['plant_id'];
$plant = null; $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))) { if (Str::length($plantCod) > 0 && (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod))) {
$warnMsg[] = 'Invalid plant code found!'; $warnMsg[] = 'Invalid plant code found!';
} elseif (Str::length($plantCod) <= 0) { } 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!'; $warnMsg[] = 'Invalid user name found!';
} }
// || !is_numeric($this->data['code']) || !preg_match('/^[1-9]\d{3,}$/', $this->data['code']) // || !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!'; $warnMsg[] = 'Invalid email found!';
} }
if (Str::length($this->data['password']) < 3) { if (Str::length($pass) < 3) {
$warnMsg[] = 'Invalid password found!'; $warnMsg[] = 'Invalid password found!';
} }
// Validate roles if provided // Validate roles if provided
@@ -97,16 +104,20 @@ class UserImporter extends Importer
$warnMsg[] = 'User roles not found!'; $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)) { if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
$user = User::updateOrCreate([ $user = User::updateOrCreate([
'email' => $this->data['email'], 'email' => $mail,
], ],
[ [
'name' => $this->data['name'], 'name' => $user,
'password' => $this->data['password'], 'password' => $pass,
'plant_id' => $plant, 'plant_id' => $plant,
]); ]);
@@ -118,7 +129,7 @@ class UserImporter extends Importer
return null; return null;
// return User::firstOrNew([ // return User::firstOrNew([
// // Update existing records, matching them by `$this->data['column_name']` // // Update existing records, matching them by `$this->data['column_name']`
// 'email' => $this->data['email'], // 'email' => $mail,
// ]); // ]);
// return new User(); // return new User();

View File

@@ -10,6 +10,7 @@ use App\Models\User;
use Filament\Facades\Filament; use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ExportAction;
@@ -53,16 +54,19 @@ class UserResource extends Resource
->minLength(3) ->minLength(3)
// ->reactive() // ->reactive()
->live(debounce: 600) ->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) { ->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), ->maxLength(255),
Forms\Components\TextInput::make('email') Forms\Components\TextInput::make('email')
->label('Email') ->label('Email')
->required()
// ->email() // ->email()
->unique(ignoreRecord: true) ->unique(ignoreRecord: true)
->required() ->readOnly(fn () => Filament::auth()->user()?->name != 'Dhanabalan S')
->readOnly()
// ->rule(function (callable $get) { // ->rule(function (callable $get) {
// return Rule::unique('users', 'email') // return Rule::unique('users', 'email')
// ->ignore($get('id')); // Ignore current record during updates // ->ignore($get('id')); // Ignore current record during updates
@@ -75,10 +79,11 @@ class UserResource extends Resource
->label('Email Verified At'), ->label('Email Verified At'),
Forms\Components\TextInput::make('password') Forms\Components\TextInput::make('password')
->label('Password') ->label('Password')
->required()
->password() ->password()
->minLength(3) ->minLength(3)
->revealable() ->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)) // ->dehydrateStateUsing(fn (string $state): string => Hash::make($state))
->maxLength(255), ->maxLength(255),
// Forms\Components\Select::make('roles') // Forms\Components\Select::make('roles')