2022-08-31 00:33:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Initialize\Actions;
|
|
|
|
|
|
|
|
use App\Initialize\InitializeJob;
|
|
|
|
use App\Setting\NamiSettings;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2022-09-01 23:56:34 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2022-08-31 00:33:36 +02:00
|
|
|
use Lorisleiva\Actions\ActionRequest;
|
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
2022-09-01 23:56:34 +02:00
|
|
|
use Zoomyboy\LaravelNami\Nami;
|
2022-08-31 00:33:36 +02:00
|
|
|
|
|
|
|
class InitializeAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
2022-09-01 23:56:34 +02:00
|
|
|
public function handle(int $mglnr, string $password, int $groupId): void
|
2022-08-31 00:33:36 +02:00
|
|
|
{
|
|
|
|
InitializeJob::dispatch();
|
|
|
|
}
|
|
|
|
|
2022-09-01 23:56:34 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
2022-08-31 00:33:36 +02:00
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'mglnr' => 'required|numeric',
|
|
|
|
'password' => 'required|string',
|
|
|
|
'group_id' => 'required|numeric',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-01 23:56:34 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
|
|
|
public function getValidationAttributes(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'group_id' => 'Gruppierungsnr',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:33:36 +02:00
|
|
|
public function asController(ActionRequest $request, NamiSettings $settings): RedirectResponse
|
|
|
|
{
|
2022-09-01 23:56:34 +02:00
|
|
|
$api = Nami::login($request->input('mglnr'), $request->input('password'));
|
|
|
|
|
|
|
|
if (!$api->hasGroup($request->input('group_id'))) {
|
|
|
|
throw ValidationException::withMessages(['nami' => 'Gruppierung nicht gefunden.']);
|
|
|
|
}
|
|
|
|
|
2022-08-31 00:33:36 +02:00
|
|
|
$settings->mglnr = $request->input('mglnr');
|
|
|
|
$settings->password = $request->input('password');
|
|
|
|
$settings->default_group_id = $request->input('group_id');
|
|
|
|
$settings->save();
|
|
|
|
|
|
|
|
$this->handle(
|
|
|
|
(int) $request->input('mglnr', 0),
|
|
|
|
(string) $request->input('password', ''),
|
|
|
|
(int) $request->input('group_id', 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
return redirect()->route('home');
|
|
|
|
}
|
|
|
|
}
|