Updated alignment and validation logic on import
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s

This commit is contained in:
dhanabalan
2026-01-13 16:44:42 +05:30
parent 2521c04f03
commit bf6b973fd1

View File

@@ -29,19 +29,19 @@ class UserImporter extends Importer
->exampleHeader('Name') ->exampleHeader('Name')
->example('RAW00001') ->example('RAW00001')
->label('Name') ->label('Name')
->rules(['required']),//, 'max:255' ->rules(['required']), // , 'max:255'
ImportColumn::make('email') ImportColumn::make('email')
->requiredMapping() ->requiredMapping()
->exampleHeader('E-Mail') ->exampleHeader('E-Mail')
->example('RAW00001@cripumps.com') ->example('RAW00001@cripumps.com')
->label('E-Mail') ->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('RAW00001') ->example('RAW00001')
->label('Password') ->label('Password')
->rules(['required']),//, 'max:255' ->rules(['required']), // , 'max:255'
ImportColumn::make('roles') ImportColumn::make('roles')
->requiredMapping() ->requiredMapping()
->exampleHeader('Roles') ->exampleHeader('Roles')
@@ -54,51 +54,47 @@ class UserImporter extends Importer
public function resolveRecord(): ?User public function resolveRecord(): ?User
{ {
$warnMsg = []; $warnMsg = [];
$plantCod = $this->data['plant'];
$plant = null; $plant = null;
if (Str::length($this->data['plant']) > 0) { if (Str::length($plantCod) < 4 || ! is_numeric($plantCod) || ! preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
if (Str::length($this->data['plant']) < 4 || !is_numeric($this->data['plant']) || !preg_match('/^[1-9]\d{3,}$/', $this->data['plant'])) { $warnMsg[] = 'Invalid plant code found!';
$warnMsg[] = "Invalid plant code found!"; } else {
} $plant = Plant::where('code', $plantCod)->first();
else { if (! $plant) {
$plant = Plant::where('code', $this->data['plant'])->first(); $warnMsg[] = 'Plant not found';
if (!$plant) { } else {
$warnMsg[] = "Plant not found";
}
else {
$plant = $plant->id ?? null; $plant = $plant->id ?? null;
} }
} }
}
if (Str::length($this->data['name']) < 1) { if (Str::length($this->data['name']) < 3) {
$warnMsg[] = "User name not 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($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 // Validate roles if provided
$roles = []; $roles = [];
if (!empty($this->data['roles'])) { if (! empty($this->data['roles'])) {
$roles = collect(explode(',', $this->data['roles'])) $roles = collect(explode(',', $this->data['roles']))
->map(fn($role) => trim($role)) ->map(fn ($role) => trim($role))
->filter() ->filter()
->toArray(); ->toArray();
foreach ($roles as $roleName) { foreach ($roles as $roleName) {
if (!Role::where('name', $roleName)->exists()) { if (! Role::where('name', $roleName)->exists()) {
$warnMsg[] = "Role : '{$roleName}' does not exist!"; $warnMsg[] = "Role : '{$roleName}' does not exist!";
} }
} }
} } else {
else { $warnMsg[] = 'User roles not found!';
$warnMsg[] = "User roles not found!";
} }
if (!empty($warnMsg)) { if (! empty($warnMsg)) {
throw new RowImportFailedException(implode(', ', $warnMsg)); throw new RowImportFailedException(implode(', ', $warnMsg));
} }
@@ -112,7 +108,7 @@ class UserImporter extends Importer
]); ]);
// Assign roles // Assign roles
if (!empty($roles)) { if (! empty($roles)) {
$user->syncRoles($roles); $user->syncRoles($roles);
} }
@@ -122,15 +118,15 @@ class UserImporter extends Importer
// 'email' => $this->data['email'], // 'email' => $this->data['email'],
// ]); // ]);
//return new User(); // return new User();
} }
public static function getCompletedNotificationBody(Import $import): string public static function getCompletedNotificationBody(Import $import): string
{ {
$body = 'Your user import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.'; $body = 'Your user import has completed and '.number_format($import->successful_rows).' '.str('row')->plural($import->successful_rows).' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) { if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.'; $body .= ' '.number_format($failedRowsCount).' '.str('row')->plural($failedRowsCount).' failed to import.';
} }
return $body; return $body;