Implement Gitea user connection foundation
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Settings;
|
||||
|
||||
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 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
|
||||
{
|
||||
try {
|
||||
$gitProvider = $this->gitProviderManager->client($request->provider());
|
||||
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
||||
$request->baseUrl(),
|
||||
$request->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' => $request->apiToken(),
|
||||
'base_url' => $request->baseUrl(),
|
||||
'provider' => $request->provider(),
|
||||
'provider_user_id' => $authenticatedUser->providerUserId,
|
||||
'user_id' => $request->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);
|
||||
|
||||
try {
|
||||
$gitProvider = $this->gitProviderManager->client($request->provider());
|
||||
$authenticatedUser = $gitProvider->getAuthenticatedUser(
|
||||
$request->baseUrl(),
|
||||
$request->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' => $request->apiToken(),
|
||||
'base_url' => $request->baseUrl(),
|
||||
'provider' => $request->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
|
||||
{
|
||||
abort_unless($gitConnection->user_id === $request->user()->id, 403);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreUserGitConnectionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'api_token' => ['required', 'string', 'max:2048'],
|
||||
'base_url' => [
|
||||
'required',
|
||||
'url:http,https',
|
||||
'max:255',
|
||||
Rule::unique('user_git_connections')
|
||||
->where(fn ($query) => $query
|
||||
->where('user_id', $this->user()->id)
|
||||
->where('provider', $this->input('provider'))),
|
||||
],
|
||||
'provider' => ['required', Rule::in(array_map(
|
||||
static fn (GitProviderKey $providerKey): string => $providerKey->value,
|
||||
GitProviderKey::cases(),
|
||||
))],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'base_url' => $this->normalizedBaseUrl(),
|
||||
'provider' => Str::lower(trim((string) $this->input('provider'))),
|
||||
]);
|
||||
}
|
||||
|
||||
public function apiToken(): string
|
||||
{
|
||||
return (string) $this->validated('api_token');
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return (string) $this->validated('base_url');
|
||||
}
|
||||
|
||||
public function provider(): GitProviderKey
|
||||
{
|
||||
return GitProviderKey::from((string) $this->validated('provider'));
|
||||
}
|
||||
|
||||
private function normalizedBaseUrl(): string
|
||||
{
|
||||
$baseUrl = Str::of((string) $this->input('base_url'))
|
||||
->trim()
|
||||
->rtrim('/')
|
||||
->value();
|
||||
|
||||
return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Settings;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateUserGitConnectionRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var UserGitConnection $gitConnection */
|
||||
$gitConnection = $this->route('gitConnection');
|
||||
|
||||
return [
|
||||
'api_token' => ['required', 'string', 'max:2048'],
|
||||
'base_url' => [
|
||||
'required',
|
||||
'url:http,https',
|
||||
'max:255',
|
||||
Rule::unique('user_git_connections')
|
||||
->ignore($gitConnection->id)
|
||||
->where(fn ($query) => $query
|
||||
->where('user_id', $this->user()->id)
|
||||
->where('provider', $this->input('provider'))),
|
||||
],
|
||||
'provider' => ['required', Rule::in(array_map(
|
||||
static fn (GitProviderKey $providerKey): string => $providerKey->value,
|
||||
GitProviderKey::cases(),
|
||||
))],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'base_url' => $this->normalizedBaseUrl(),
|
||||
'provider' => Str::lower(trim((string) $this->input('provider'))),
|
||||
]);
|
||||
}
|
||||
|
||||
public function apiToken(): string
|
||||
{
|
||||
return (string) $this->validated('api_token');
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return (string) $this->validated('base_url');
|
||||
}
|
||||
|
||||
public function provider(): GitProviderKey
|
||||
{
|
||||
return GitProviderKey::from((string) $this->validated('provider'));
|
||||
}
|
||||
|
||||
private function normalizedBaseUrl(): string
|
||||
{
|
||||
$baseUrl = Str::of((string) $this->input('base_url'))
|
||||
->trim()
|
||||
->rtrim('/')
|
||||
->value();
|
||||
|
||||
return preg_replace('#/api/v1$#i', '', $baseUrl) ?? $baseUrl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user