2026-03-29 22:24:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
use App\Models\AccessToken;
|
2026-03-29 22:24:28 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
|
|
|
|
|
class ApiKeyController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request): Response|RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('app.workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:40:18 +00:00
|
|
|
$this->authorize('manageTeam', $workspace);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$tokens = AccessToken::where('user_id', $request->user()->id)
|
|
|
|
|
->where('workspace_id', $workspace->id)
|
|
|
|
|
->where('revoked', false)
|
|
|
|
|
->latest()
|
|
|
|
|
->get()
|
|
|
|
|
->map(fn (AccessToken $token) => [
|
|
|
|
|
'id' => $token->id,
|
|
|
|
|
'name' => $token->name,
|
|
|
|
|
'last_used_at' => $token->last_used_at,
|
|
|
|
|
'expires_at' => $token->expires_at,
|
|
|
|
|
'created_at' => $token->created_at,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 16:44:13 +00:00
|
|
|
return Inertia::render('settings/workspace/ApiKeys', [
|
2026-03-29 22:24:28 +00:00
|
|
|
'workspace' => $workspace,
|
2026-05-03 21:38:17 +00:00
|
|
|
'apiTokens' => $tokens,
|
2026-03-29 22:24:28 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store(Request $request): RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('app.workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:40:18 +00:00
|
|
|
$this->authorize('manageTeam', $workspace);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
|
|
|
|
$validated = $request->validate([
|
|
|
|
|
'name' => ['required', 'string', 'max:255'],
|
|
|
|
|
'expires_at' => ['nullable', 'date', 'after:today'],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$result = $request->user()->createToken($validated['name']);
|
|
|
|
|
$accessToken = AccessToken::find($result->token->id);
|
|
|
|
|
$accessToken->forceFill([
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'expires_at' => $validated['expires_at'] ?? null,
|
|
|
|
|
])->saveQuietly();
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
return back()
|
|
|
|
|
->with('flash.success', __('settings.api_keys.flash.created'))
|
|
|
|
|
->with('flash.plainToken', $result->accessToken);
|
2026-03-29 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
public function destroy(Request $request, string $tokenId): RedirectResponse
|
2026-03-29 22:24:28 +00:00
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('app.workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 03:40:18 +00:00
|
|
|
$this->authorize('manageTeam', $workspace);
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$token = AccessToken::where('id', $tokenId)
|
|
|
|
|
->where('user_id', $request->user()->id)
|
|
|
|
|
->where('workspace_id', $workspace->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if (! $token) {
|
2026-03-29 22:24:28 +00:00
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
$token->forceFill(['revoked' => true])->saveQuietly();
|
2026-03-29 22:24:28 +00:00
|
|
|
|
2026-05-03 21:38:17 +00:00
|
|
|
return back()->with('flash.success', __('settings.api_keys.flash.deleted'));
|
2026-03-29 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
}
|