Added roles on UserExporter and UserImporter for consistency and enhanced functionality
This commit is contained in:
@@ -24,11 +24,17 @@ class UserExporter extends Exporter
|
|||||||
return ++$rowNumber;
|
return ++$rowNumber;
|
||||||
}),
|
}),
|
||||||
ExportColumn::make('name')
|
ExportColumn::make('name')
|
||||||
->label('Name'),
|
->label('NAME'),
|
||||||
ExportColumn::make('email')
|
ExportColumn::make('email')
|
||||||
->label('Email'),
|
->label('E-MAIL'),
|
||||||
ExportColumn::make('password')
|
ExportColumn::make('password')
|
||||||
->label('Password'),
|
->label('PASSWORD'),
|
||||||
|
ExportColumn::make('roles')
|
||||||
|
->label('ROLES')
|
||||||
|
->state(function ($record) {
|
||||||
|
// Assuming Spatie\Permission: roles() relationship
|
||||||
|
return $record->roles->pluck('name')->join(', ');
|
||||||
|
}),
|
||||||
ExportColumn::make('created_at')
|
ExportColumn::make('created_at')
|
||||||
->label('CREATED AT'),
|
->label('CREATED AT'),
|
||||||
ExportColumn::make('updated_at')
|
ExportColumn::make('updated_at')
|
||||||
|
|||||||
@@ -7,6 +7,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 Spatie\Permission\Models\Role;
|
||||||
use Str;
|
use Str;
|
||||||
|
|
||||||
class UserImporter extends Importer
|
class UserImporter extends Importer
|
||||||
@@ -19,43 +20,66 @@ class UserImporter extends Importer
|
|||||||
ImportColumn::make('name')
|
ImportColumn::make('name')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Name')
|
->exampleHeader('Name')
|
||||||
->example('Admin')
|
->example('RAW00001')
|
||||||
->label('Name')
|
->label('Name')
|
||||||
->rules(['required']),//, 'max:255'
|
->rules(['required']),//, 'max:255'
|
||||||
ImportColumn::make('email')
|
ImportColumn::make('email')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Email')
|
->exampleHeader('E-mail')
|
||||||
->example('admin@cripumps.com')
|
->example('RAW00001@cripumps.com')
|
||||||
->label('Email')
|
->label('E-mail')
|
||||||
->rules(['required', 'email']),//, 'max:255'
|
->rules(['required', 'email']),//, 'max:255'
|
||||||
ImportColumn::make('password')
|
ImportColumn::make('password')
|
||||||
->requiredMapping()
|
->requiredMapping()
|
||||||
->exampleHeader('Password')
|
->exampleHeader('Password')
|
||||||
->example('admin@pass')
|
->example('RAW00001')
|
||||||
->label('Password')
|
->label('Password')
|
||||||
->rules(['required']),//, 'max:255'
|
->rules(['required']),//, 'max:255'
|
||||||
|
ImportColumn::make('roles')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Roles')
|
||||||
|
->example('Employee')
|
||||||
|
->label('Roles')
|
||||||
|
->rules(['nullable', 'string']), // Optional roles
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolveRecord(): ?User
|
public function resolveRecord(): ?User
|
||||||
{
|
{
|
||||||
$warnMsg = [];
|
$warnMsg = [];
|
||||||
if (Str::length($this->data['name']) < 0) {
|
if (Str::length($this->data['name']) < 1) {
|
||||||
$warnMsg[] = "User name not found";
|
$warnMsg[] = "User name not 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($this->data['email']) < 5) {
|
||||||
$warnMsg[] = "Invalid email found";
|
$warnMsg[] = "Invalid email found!";
|
||||||
}
|
}
|
||||||
if (Str::length($this->data['password']) < 3) {
|
if (Str::length($this->data['password']) < 3) {
|
||||||
$warnMsg[] = "Invalid password found";
|
$warnMsg[] = "Invalid password found!";
|
||||||
|
}
|
||||||
|
// Validate roles if provided
|
||||||
|
$roles = [];
|
||||||
|
if (!empty($this->data['roles'])) {
|
||||||
|
$roles = collect(explode(',', $this->data['roles']))
|
||||||
|
->map(fn($role) => trim($role))
|
||||||
|
->filter()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
foreach ($roles as $roleName) {
|
||||||
|
if (!Role::where('name', $roleName)->exists()) {
|
||||||
|
$warnMsg[] = "Role : '{$roleName}' does not exist!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$warnMsg[] = "User roles not found!";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($warnMsg)) {
|
if (!empty($warnMsg)) {
|
||||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
User::updateOrCreate([
|
$user = User::updateOrCreate([
|
||||||
'email' => $this->data['email'],
|
'email' => $this->data['email'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
@@ -63,6 +87,11 @@ class UserImporter extends Importer
|
|||||||
'password' => $this->data['password'],
|
'password' => $this->data['password'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Assign roles
|
||||||
|
if (!empty($roles)) {
|
||||||
|
$user->syncRoles($roles);
|
||||||
|
}
|
||||||
|
|
||||||
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']`
|
||||||
|
|||||||
Reference in New Issue
Block a user