snipe-it/app/Http/Livewire/OauthClients.php

84 lines
2 KiB
PHP
Raw Normal View History

2023-12-05 12:52:14 -08:00
<?php
namespace App\Http\Livewire;
use Laravel\Passport\Client;
2023-12-05 12:52:14 -08:00
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Passport;
2023-12-05 12:52:14 -08:00
use Livewire\Component;
class OauthClients extends Component
{
public $name;
public $redirect;
public $editClientId;
public $editName;
public $editRedirect;
protected $clientRepository;
public function __construct()
{
$this->clientRepository = app(ClientRepository::class);
parent::__construct();
}
2023-12-05 12:52:14 -08:00
public function render()
{
return view('livewire.oauth-clients', [
'clients' => $this->clientRepository->activeForUser(auth()->user()->id),
2023-12-05 12:52:14 -08:00
]);
}
public function createClient(): void
2023-12-05 12:52:14 -08:00
{
$this->validate([
2023-12-05 12:52:14 -08:00
'name' => 'required|string|max:255',
'redirect' => 'required|url|max:255',
]);
$newClient = $this->clientRepository->create(
auth()->user()->id,
$this->name,
$this->redirect,
);
$this->dispatchBrowserEvent('clientCreated');
2023-12-05 12:52:14 -08:00
}
public function deleteClient(Client $clientId): void
2023-12-05 12:52:14 -08:00
{
// test for safety
// ->delete must be of type Client - thus the model binding
$this->clientRepository->delete($clientId);
}
2023-12-05 12:52:14 -08:00
public function editClient(Client $editClientId): void
{
$this->editName = $editClientId->name;
$this->editRedirect = $editClientId->redirect;
2023-12-05 12:52:14 -08:00
$this->dispatchBrowserEvent('editClient');
2023-12-05 12:52:14 -08:00
}
public function updateClient(Client $editClientId): void
2023-12-05 12:52:14 -08:00
{
$this->validate([
'editName' => 'required|string|max:255',
'editRedirect' => 'required|url|max:255',
]);
$client = $this->clientRepository->find($editClientId->id);
if ($client->user_id == auth()->user()->id) {
$client->name = $this->editName;
$client->redirect = $this->editRedirect;
$client->save();
} else {
// throw error
}
$this->dispatchBrowserEvent('clientUpdated');
2023-12-05 12:52:14 -08:00
}
}