137 lines
4.8 KiB
PHP
137 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Settings;
|
|
|
|
use App\Actions\Git\GitProviderKey;
|
|
use App\Actions\Git\GitProviderManager;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Settings\StoreUserGitConnectionRequest;
|
|
use App\Http\Requests\Settings\UpdateUserGitConnectionRequest;
|
|
use App\Models\UserGitConnection;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
use Throwable;
|
|
|
|
class GitConnectionController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly GitProviderManager $gitProviderManager,
|
|
) {}
|
|
|
|
public function edit(Request $request): Response
|
|
{
|
|
|
|
return Inertia::render('settings/GitConnections', [
|
|
'gitConnections' => $request->user()->gitConnections
|
|
->map(fn (UserGitConnection $gitConnection): array => [
|
|
'account_login' => $gitConnection->account_login,
|
|
'account_name' => $gitConnection->account_name,
|
|
'base_url' => $gitConnection->base_url,
|
|
'created_at' => $gitConnection->created_at?->toISOString(),
|
|
'id' => $gitConnection->id,
|
|
'provider' => $gitConnection->provider->value,
|
|
'provider_label' => $gitConnection->provider->label(),
|
|
'provider_user_id' => $gitConnection->provider_user_id,
|
|
'updated_at' => $gitConnection->updated_at?->toISOString(),
|
|
'user_id' => $gitConnection->user_id,
|
|
])
|
|
->values(),
|
|
'status' => $request->session()->get('status'),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreUserGitConnectionRequest $request): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
|
|
$apiToken = Arr::get($validated, 'api_token');
|
|
$baseUrl = Arr::get($validated, 'base_url');
|
|
$provider = GitProviderKey::from(Arr::get($validated, 'provider'));
|
|
$user = $request->user();
|
|
|
|
try {
|
|
$gitProvider = $this->gitProviderManager->client($provider);
|
|
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
|
$baseUrl,
|
|
$apiToken,
|
|
);
|
|
} catch (Throwable) {
|
|
return back()
|
|
->withErrors([
|
|
'api_token' => 'KanbanGit could not authenticate against Gitea with this base URL and token.',
|
|
]);
|
|
}
|
|
|
|
UserGitConnection::query()->create([
|
|
'account_login' => $authenticatedUser->login,
|
|
'account_name' => $authenticatedUser->name,
|
|
'api_token' => $apiToken,
|
|
'base_url' => $baseUrl,
|
|
'provider' => $provider,
|
|
'provider_user_id' => $authenticatedUser->providerUserId,
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
return to_route('git-connections.edit')
|
|
->with('status', 'Git connection was added.');
|
|
}
|
|
|
|
public function update(
|
|
UpdateUserGitConnectionRequest $request,
|
|
UserGitConnection $gitConnection,
|
|
): RedirectResponse {
|
|
$this->abortUnlessOwner($request, $gitConnection);
|
|
|
|
$validated = $request->validated();
|
|
$apiToken = Arr::get($validated, 'api_token');
|
|
$baseUrl = Arr::get($validated, 'base_url');
|
|
$provider = GitProviderKey::from(Arr::get($validated, 'provider'));
|
|
|
|
try {
|
|
$gitProvider = $this->gitProviderManager->client($provider);
|
|
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
|
$baseUrl,
|
|
$apiToken,
|
|
);
|
|
} catch (Throwable) {
|
|
return back()
|
|
->withErrors([
|
|
'api_token' => 'KanbanGit could not authenticate against Gitea with this base URL and token.',
|
|
]);
|
|
}
|
|
|
|
$gitConnection->update([
|
|
'account_login' => $authenticatedUser->login,
|
|
'account_name' => $authenticatedUser->name,
|
|
'api_token' => $apiToken,
|
|
'base_url' => $baseUrl,
|
|
'provider' => $provider,
|
|
'provider_user_id' => $authenticatedUser->providerUserId,
|
|
]);
|
|
|
|
return to_route('git-connections.edit')
|
|
->with('status', 'Git connection was updated.');
|
|
}
|
|
|
|
public function destroy(Request $request, UserGitConnection $gitConnection): RedirectResponse
|
|
{
|
|
$this->abortUnlessOwner($request, $gitConnection);
|
|
|
|
$label = "{$gitConnection->account_login} @ {$gitConnection->base_url}";
|
|
|
|
$gitConnection->delete();
|
|
|
|
return to_route('git-connections.edit')
|
|
->with('status', "{$label} was removed.");
|
|
}
|
|
|
|
private function abortUnlessOwner(Request $request, UserGitConnection $gitConnection): void
|
|
{
|
|
$user = $request->user();
|
|
abort_unless($gitConnection->user_id === $user->id, 403);
|
|
}
|
|
}
|