Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
run: |
composer update --lock
composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
Expand Down
41 changes: 36 additions & 5 deletions app/Http/Controllers/Gui/SquidUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace App\Http\Controllers\Gui;

use App\Http\Controllers\Controller;
use App\Http\Requests\SquidUser\BulkImportRequest;
use App\Http\Requests\SquidUser\CreateRequest;
use App\Http\Requests\SquidUser\DestroyRequest;
use App\Http\Requests\SquidUser\ModifyRequest;
use App\Http\Requests\SquidUser\ReadRequest;
use App\Http\Requests\SquidUser\SearchRequest;
use App\Services\SquidUserService;
use App\UseCases\SquidUser\BulkCreateAction;
use App\UseCases\SquidUser\BulkDeleteAction;
use App\UseCases\SquidUser\BulkUpdateAction;
use App\UseCases\SquidUser\CreateAction;
use App\UseCases\SquidUser\DestroyAction;
use App\UseCases\SquidUser\ModifyAction;
Expand All @@ -19,11 +23,9 @@

class SquidUserController extends Controller
{
private $squidUserService;

public function __construct(SquidUserService $squidUserService)
{
$this->squidUserService = $squidUserService;
public function __construct(
private readonly SquidUserService $squidUserService
) {
}

public function search(SearchRequest $request, SearchAction $action): View
Expand Down Expand Up @@ -72,4 +74,33 @@ public function destroy(DestroyRequest $request, DestroyAction $action): Redirec

return redirect()->route('squiduser.search', $request->user()->id);
}

public function bulkImporter(): View
{
return view('squidusers.bulk_import');
}

public function bulkImport(BulkImportRequest $request): RedirectResponse
{
$rows = $request->parseCsv();
$operation = $request->input('operation');
$userId = $request->user()->id;

$results = match ($operation) {
'create' => (new BulkCreateAction())($rows, $userId),
'update' => (new BulkUpdateAction())($rows, $userId),
'delete' => (new BulkDeleteAction())($rows, $userId),
default => ['success' => 0, 'failed' => 0, 'errors' => ['Invalid operation']],
};

$message = "Success: {$results['success']}, Failed: {$results['failed']}";
if (!empty($results['errors'])) {
$message .= "\nErrors: " . implode("\n", $results['errors']);
}

return redirect()
->route('squiduser.bulk.importer')
->with('message', $message)
->with('results', $results);
}
}
8 changes: 3 additions & 5 deletions app/Http/Controllers/Gui/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

class UserController extends Controller
{
private $user;

public function __construct(UserService $user)
{
$this->user = $user;
public function __construct(
private readonly UserService $user
) {
}

public function create(CreateRequest $request, CreateAction $action): RedirectResponse
Expand Down
78 changes: 78 additions & 0 deletions app/Http/Requests/SquidUser/BulkImportRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Http\Requests\SquidUser;

use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Foundation\Http\FormRequest;

class BulkImportRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(Gate $gate): bool
{
return $gate->allows('create-squid-user', $this->user()->id);
}

/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'csv_file' => 'required|file|mimes:csv,txt|max:10240',
'operation' => 'required|in:create,update,delete',
];
}

public function parseCsv(): array
{
$file = $this->file('csv_file');
$handle = fopen($file->getRealPath(), 'r');
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file handle from fopen is not checked for failure. If fopen fails, subsequent operations on a false handle will cause errors. Add error handling after opening the file.

Suggested change
$handle = fopen($file->getRealPath(), 'r');
$handle = fopen($file->getRealPath(), 'r');
if ($handle === false) {
throw new \RuntimeException('Unable to open CSV file for reading.');
}

Copilot uses AI. Check for mistakes.

if ($handle === false) {
throw new \RuntimeException('Failed to open CSV file');
}

$rows = [];
$header = null;
$lineNumber = 0;

try {
while (($data = fgetcsv($handle, 0, ',')) !== false) {
$lineNumber++;

if ($header === null) {
$header = array_map('trim', $data);
continue;
}

// Skip empty lines
if (empty(array_filter($data))) {
continue;
}

// Ensure column count matches header
if (count($data) !== count($header)) {
throw new \RuntimeException(
"Line {$lineNumber}: Column count mismatch. Expected " . count($header) . " columns, got " . count($data)
);
}

$row = array_combine($header, array_map('trim', $data));
if ($row !== false) {
$rows[] = $row;
}
}
} finally {
fclose($handle);
}

if (empty($rows)) {
throw new \RuntimeException('CSV file is empty or contains no valid data rows');
}

return $rows;
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

public function setPasswordAttribute($value)
public function setPasswordAttribute(string $value): void
{
$this->attributes['password'] = Hash::needsRehash($value) ? Hash::make($value) : $value;
}
Expand Down
6 changes: 2 additions & 4 deletions app/Services/SquidAllowedIpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class SquidAllowedIpService
{
public function getById($id) : SquidAllowedIp
public function getById(int|string $id): SquidAllowedIp
{
$ip = SquidAllowedIp::query()->where('id', '=', $id)->first();

return $ip ?? new SquidAllowedIp();
return SquidAllowedIp::query()->findOr($id, fn() => new SquidAllowedIp());
}
}
6 changes: 2 additions & 4 deletions app/Services/SquidUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class SquidUserService
{
public function getById($id) : SquidUser
public function getById(int|string $id): SquidUser
{
$su = SquidUser::query()->where('id', '=', $id)->first();

return $su ?? new SquidUser();
return SquidUser::query()->findOr($id, fn() => new SquidUser());
}
}
6 changes: 2 additions & 4 deletions app/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class UserService
{
public function getById($id) : User
public function getById(int|string $id): User
{
$user = User::query()->where('id', '=', $id)->first();

return $user ?? new User();
return User::query()->findOr($id, fn() => new User());
}
}
45 changes: 45 additions & 0 deletions app/UseCases/SquidUser/BulkCreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\UseCases\SquidUser;

use App\Models\SquidUser;
use Illuminate\Support\Facades\DB;

class BulkCreateAction
{
public function __invoke(array $rows, int $userId): array
{
$results = [
'success' => 0,
'failed' => 0,
'errors' => [],
];

DB::beginTransaction();

try {
foreach ($rows as $index => $row) {
$squidUser = new SquidUser([
'user' => $row['user'] ?? '',
'password' => $row['password'] ?? '',
'enabled' => $row['enabled'] ?? 1,
'fullname' => $row['fullname'] ?? '',
'comment' => $row['comment'] ?? '',
]);
$squidUser->user_id = $userId;
$squidUser->save();

$results['success']++;
}

DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$results['failed'] = count($rows);
$results['success'] = 0;
$results['errors'][] = $e->getMessage();
}

return $results;
}
}
45 changes: 45 additions & 0 deletions app/UseCases/SquidUser/BulkDeleteAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\UseCases\SquidUser;

use App\Models\SquidUser;
use Illuminate\Support\Facades\DB;

class BulkDeleteAction
{
public function __invoke(array $rows, int $userId): array
{
$results = [
'success' => 0,
'failed' => 0,
'errors' => [],
];

DB::beginTransaction();

try {
foreach ($rows as $index => $row) {
$squidUser = SquidUser::query()
->where('user', $row['user'] ?? '')
->where('user_id', $userId)
->first();

if (!$squidUser) {
throw new \Exception("Row " . ($index + 2) . ": User '{$row['user']}' not found");
}

$squidUser->delete();
$results['success']++;
}

DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$results['failed'] = count($rows);
$results['success'] = 0;
$results['errors'][] = $e->getMessage();
}

return $results;
}
}
58 changes: 58 additions & 0 deletions app/UseCases/SquidUser/BulkUpdateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\UseCases\SquidUser;

use App\Models\SquidUser;
use Illuminate\Support\Facades\DB;

class BulkUpdateAction
{
public function __invoke(array $rows, int $userId): array
{
$results = [
'success' => 0,
'failed' => 0,
'errors' => [],
];

DB::beginTransaction();

try {
foreach ($rows as $index => $row) {
$squidUser = SquidUser::query()
->where('user', $row['user'] ?? '')
->where('user_id', $userId)
->first();

if (!$squidUser) {
throw new \Exception("Row " . ($index + 2) . ": User '{$row['user']}' not found");
}

if (isset($row['password']) && !empty($row['password'])) {
$squidUser->password = $row['password'];
}
if (isset($row['enabled'])) {
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'enabled' field should not be updated when it's an empty string. Add a check for !empty($row['enabled']) similar to the password field to prevent setting enabled to an empty string value.

Suggested change
if (isset($row['enabled'])) {
if (isset($row['enabled']) && !empty($row['enabled'])) {

Copilot uses AI. Check for mistakes.
$squidUser->enabled = $row['enabled'];
}
if (isset($row['fullname'])) {
$squidUser->fullname = $row['fullname'];
}
if (isset($row['comment'])) {
Comment on lines +37 to +40
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty strings in 'fullname' and 'comment' fields will overwrite existing values. According to the view's documentation stating 'Empty fields will not be updated', add !empty() checks to these conditions to prevent unintentional data loss.

Suggested change
if (isset($row['fullname'])) {
$squidUser->fullname = $row['fullname'];
}
if (isset($row['comment'])) {
if (isset($row['fullname']) && !empty($row['fullname'])) {
$squidUser->fullname = $row['fullname'];
}
if (isset($row['comment']) && !empty($row['comment'])) {

Copilot uses AI. Check for mistakes.
$squidUser->comment = $row['comment'];
}

$squidUser->save();
$results['success']++;
}

DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$results['failed'] = count($rows);
$results['success'] = 0;
$results['errors'][] = $e->getMessage();
}

return $results;
}
}
Loading
Loading