- Applied `Confirmation` trait to multiple Livewire components for unified confirmation handling. - Replaced inline confirmation modals with `requireConfirmation` method for streamlined interaction. - Updated button styles and added reusable confirmation prompts for delete/detach actions. - Enhanced `AppRoleService` and `ConnectionProviderService` to support deletion with confirmation.
104 lines
3.0 KiB
PHP
104 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Data\ConnectedApp\{ConnectAppRequest, ConnectedAppData};
|
|
use App\Data\Ui\ConnectedApps\ConnectedAppData as UiConnectedAppData;
|
|
use App\Enums\ConnectedAppPermissonEnum;
|
|
use App\Models\{AppRole, ConnectedApp};
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Facades\DB;
|
|
use InvalidArgumentException;
|
|
use Spatie\LaravelData\PaginatedDataCollection;
|
|
use Throwable;
|
|
|
|
final class ConnectedAppService
|
|
{
|
|
public function __construct(
|
|
private readonly AppsPermissionService $permissionService,
|
|
) {}
|
|
|
|
/**
|
|
* Create a new connected app.
|
|
* By Default creates a use permission for the app.
|
|
*
|
|
* @throws Throwable when an error occurs during the creation.
|
|
*/
|
|
public function create(ConnectAppRequest $data): void
|
|
{
|
|
DB::transaction(
|
|
function () use ($data): void {
|
|
$connectedApp = ConnectedApp::query()->create(
|
|
$data->toArray()
|
|
);
|
|
|
|
$this->permissionService->add(ConnectedAppData::from($connectedApp), ConnectedAppPermissonEnum::Use);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidArgumentException when id is invalid
|
|
* @throws Throwable when an error occurs during the update.
|
|
*/
|
|
public function update(int $id, ConnectAppRequest $data): void
|
|
{
|
|
if ($id <= 0) {
|
|
throw new InvalidArgumentException('Invalid id');
|
|
}
|
|
DB::transaction(
|
|
function () use ($id, $data): void {
|
|
$app = ConnectedApp::query()->findOrFail($id);
|
|
$app->update($data->toArray());
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @throws Throwable when an error occurs during the deletion.
|
|
* @throws InvalidArgumentException when id is invalid
|
|
*/
|
|
public function delete(int $id): void
|
|
{
|
|
if ($id <= 0) {
|
|
throw new InvalidArgumentException('Invalid id');
|
|
}
|
|
|
|
DB::transaction(
|
|
function () use ($id): void {
|
|
$app = ConnectedApp::query()->findOrFail($id);
|
|
AppRole::query()->where('app_id', $app->id)->delete();
|
|
$app->delete();
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return PaginatedDataCollection<int, UiConnectedAppData>
|
|
*/
|
|
public function getAll(): PaginatedDataCollection
|
|
{
|
|
$data = ConnectedApp::with('provider:name,id', 'protocol:id,name', 'status:id,name')
|
|
->paginate();
|
|
|
|
return UiConnectedAppData::collect($data, PaginatedDataCollection::class);
|
|
}
|
|
|
|
/**
|
|
* @throws ModelNotFoundException<ConnectedApp> when the connected app is not found.
|
|
* @throws InvalidArgumentException when id is invalid.
|
|
*/
|
|
public function getConnectedApp(int $id): ConnectedAppData
|
|
{
|
|
if ($id <= 0) {
|
|
throw new InvalidArgumentException('Invalid id');
|
|
}
|
|
$data = ConnectedApp::with('provider:id', 'protocol:id', 'status:id')
|
|
->findOrFail($id);
|
|
|
|
return ConnectedAppData::from($data);
|
|
}
|
|
}
|