rbrecruiter/app/Http/Controllers/ApiKeyController.php

96 lines
2.3 KiB
PHP
Raw Normal View History

2021-03-30 00:27:49 +00:00
<?php
namespace App\Http\Controllers;
use App\ApiKey;
use App\Http\Requests\CreateApiKeyRequest;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
class ApiKeyController extends Controller
{
2021-03-31 02:55:09 +00:00
2021-03-30 00:27:49 +00:00
public function index()
{
2021-03-31 02:55:09 +00:00
$this->authorize('viewAny', ApiKey::class);
2021-03-30 00:27:49 +00:00
2021-03-31 02:55:09 +00:00
return view('dashboard.administration.keys')
->with('keys', ApiKey::all());
2021-03-30 17:16:01 +00:00
}
2021-03-30 00:27:49 +00:00
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*/
public function store(CreateApiKeyRequest $request)
{
2021-03-31 02:55:09 +00:00
$this->authorize('create', ApiKey::class);
$discriminator = "#" . bin2hex(random_bytes(7));
$secret = bin2hex(random_bytes(32));
2021-03-30 00:27:49 +00:00
$key = ApiKey::create([
'name' => $request->keyName,
'discriminator' => $discriminator,
'secret' => Hash::make($secret),
'status' => 'active',
'owner_user_id' => Auth::user()->id
]);
if ($key)
{
$request->session()->flash('success', __('Key successfully registered!'));
2021-03-30 00:27:49 +00:00
$request->session()->flash('finalKey', $discriminator . '.' . $secret);
return redirect()
->back();
}
return redirect()
->back()
->with('error', __('An error occurred whilst trying to create an API key.'));
2021-03-30 00:27:49 +00:00
}
public function revokeKey(Request $request, ApiKey $key)
{
2021-03-31 02:55:09 +00:00
$this->authorize('update', $key);
2021-03-30 00:27:49 +00:00
2021-03-31 02:55:09 +00:00
if ($key->status == 'active')
{
$key->status = 'disabled';
$key->save();
}
else
{
2021-03-30 00:27:49 +00:00
return redirect()
->back()
->with('error', __('Key already revoked.'));
2021-03-30 00:27:49 +00:00
}
return redirect()
->back()
->with('success', __('Key revoked. Apps using this key will stop working.'));
2021-03-31 02:55:09 +00:00
2021-03-30 00:27:49 +00:00
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$key = ApiKey::findOrFail($id);
2021-03-31 02:55:09 +00:00
$this->authorize('delete', $key);
2021-03-30 00:27:49 +00:00
2021-03-31 02:55:09 +00:00
$key->delete();
2021-03-30 00:27:49 +00:00
return redirect()
->back()
->with('success', __('Key deleted successfully. Apps using this key will stop working.'));
2021-03-31 02:55:09 +00:00
2021-03-30 00:27:49 +00:00
}
}