88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Http\Request;
|
||
use Inertia\Inertia;
|
||
|
||
use App\Models\ApiAdmins;
|
||
use App\Models\ApiKeys;
|
||
|
||
class ApiAdminController extends Controller
|
||
{
|
||
public function index(Request $request) {
|
||
$userId = Auth::id();
|
||
|
||
$isAdmin = ApiAdmins::where('user_id', $userId)->exists();
|
||
|
||
$categories = Apikeys::select('id', 'category')
|
||
->orderBy('category')
|
||
->get();
|
||
|
||
return Inertia::render('Dashboard', [
|
||
'isAdmin' => $isAdmin,
|
||
'categories' => $categories,
|
||
]);
|
||
}
|
||
|
||
public function create() {
|
||
$userId = Auth::id();
|
||
$isAdmin = ApiAdmins::where('user_id', $userId)->exists();
|
||
|
||
if (! $isAdmin) {
|
||
abort(403);
|
||
}
|
||
|
||
$raw = config('api_key_categories', []);
|
||
$categories = collect($raw)
|
||
->map(fn($desc, $key) => ['key' => $key, 'description' => $desc])
|
||
->values()
|
||
->all();
|
||
|
||
return Inertia::render('ApiKeys/Add', [
|
||
'categories' => $categories,
|
||
]);
|
||
}
|
||
|
||
public function store(Request $request) {
|
||
$userId = Auth::id();
|
||
$isAdmin = ApiAdmins::where('user_id', $userId)->exists();
|
||
|
||
if (! $isAdmin) {
|
||
abort(403);
|
||
}
|
||
|
||
$data = $request->validate([
|
||
'category' => 'required|string|unique:apikeys,category',
|
||
'key' => 'required|string',
|
||
]);
|
||
|
||
Apikeys::create([
|
||
'category' => $data['category'],
|
||
'key' => $data['key'],
|
||
]);
|
||
|
||
return redirect()
|
||
->route('dashboard')
|
||
->with('success', 'API‐key category added.');
|
||
}
|
||
|
||
public function destroy(int $id) {
|
||
$userId = Auth::id();
|
||
$isAdmin = ApiAdmins::where('user_id', $userId)->exists();
|
||
|
||
if (! $isAdmin) {
|
||
abort(403);
|
||
}
|
||
|
||
// Attempt to find the record—404 if not found
|
||
$apiKey = Apikeys::findOrFail($id);
|
||
$apiKey->delete();
|
||
|
||
return redirect()
|
||
->route('dashboard')
|
||
->with('success', 'API‐key category removed.');
|
||
}
|
||
}
|