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