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\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();