- Introduced `OidcConfigResolver` to streamline client secret checks based on app type. - Enhanced logging in `OidcService` to provide detailed insights during app creation and error handling. - Updated UI to conditionally display client secret based on app type. - Added placeholder image support for app logos.
107 lines
3.2 KiB
PHP
107 lines
3.2 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): ConnectedAppData
|
|
{
|
|
return DB::transaction(
|
|
function () use ($data): ConnectedAppData {
|
|
$connectedApp = ConnectedApp::query()->create(
|
|
$data->toArray()
|
|
);
|
|
|
|
// Create a permission for this app
|
|
$this->permissionService->add(ConnectedAppData::from($connectedApp), ConnectedAppPermissonEnum::Use);
|
|
|
|
return ConnectedAppData::from($connectedApp);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
}
|