fix: able to select root permission easier

This commit is contained in:
Andras Bacsai
2024-10-03 09:57:37 +02:00
parent e91939a4ea
commit c4e702f096
2 changed files with 28 additions and 12 deletions

View File

@@ -15,6 +15,8 @@ class ApiTokens extends Component
public bool $readOnly = true; public bool $readOnly = true;
public bool $rootAccess = false;
public array $permissions = ['read-only']; public array $permissions = ['read-only'];
public $isApiEnabled; public $isApiEnabled;
@@ -35,12 +37,11 @@ class ApiTokens extends Component
if ($this->viewSensitiveData) { if ($this->viewSensitiveData) {
$this->permissions[] = 'view:sensitive'; $this->permissions[] = 'view:sensitive';
$this->permissions = array_diff($this->permissions, ['*']); $this->permissions = array_diff($this->permissions, ['*']);
$this->rootAccess = false;
} else { } else {
$this->permissions = array_diff($this->permissions, ['view:sensitive']); $this->permissions = array_diff($this->permissions, ['view:sensitive']);
} }
if (count($this->permissions) == 0) { $this->makeSureOneIsSelected();
$this->permissions = ['*'];
}
} }
public function updatedReadOnly() public function updatedReadOnly()
@@ -48,11 +49,30 @@ class ApiTokens extends Component
if ($this->readOnly) { if ($this->readOnly) {
$this->permissions[] = 'read-only'; $this->permissions[] = 'read-only';
$this->permissions = array_diff($this->permissions, ['*']); $this->permissions = array_diff($this->permissions, ['*']);
$this->rootAccess = false;
} else { } else {
$this->permissions = array_diff($this->permissions, ['read-only']); $this->permissions = array_diff($this->permissions, ['read-only']);
} }
if (count($this->permissions) == 0) { $this->makeSureOneIsSelected();
}
public function updatedRootAccess()
{
if ($this->rootAccess) {
$this->permissions = ['*']; $this->permissions = ['*'];
$this->readOnly = false;
$this->viewSensitiveData = false;
} else {
$this->readOnly = true;
$this->permissions = ['read-only'];
}
}
public function makeSureOneIsSelected()
{
if (count($this->permissions) == 0) {
$this->permissions = ['read-only'];
$this->readOnly = true;
} }
} }
@@ -62,12 +82,6 @@ class ApiTokens extends Component
$this->validate([ $this->validate([
'description' => 'required|min:3|max:255', 'description' => 'required|min:3|max:255',
]); ]);
// if ($this->viewSensitiveData) {
// $this->permissions[] = 'view:sensitive';
// }
// if ($this->readOnly) {
// $this->permissions[] = 'read-only';
// }
$token = auth()->user()->createToken($this->description, $this->permissions); $token = auth()->user()->createToken($this->description, $this->permissions);
$this->tokens = auth()->user()->tokens; $this->tokens = auth()->user()->tokens;
session()->flash('token', $token->plainTextToken); session()->flash('token', $token->plainTextToken);

View File

@@ -6,7 +6,8 @@
<div class="pb-4"> <div class="pb-4">
<h2>API Tokens</h2> <h2>API Tokens</h2>
@if (!$isApiEnabled) @if (!$isApiEnabled)
<div>API is disabled. If you want to use the API, please enable it in the <a href="{{ route('settings.index') }}" class="underline dark:text-white">Settings</a> menu.</div> <div>API is disabled. If you want to use the API, please enable it in the <a
href="{{ route('settings.index') }}" class="underline dark:text-white">Settings</a> menu.</div>
@else @else
<div>Tokens are created with the current team as scope. You will only have access to this team's resources. <div>Tokens are created with the current team as scope. You will only have access to this team's resources.
</div> </div>
@@ -25,7 +26,7 @@
@if ($permissions) @if ($permissions)
@foreach ($permissions as $permission) @foreach ($permissions as $permission)
@if ($permission === '*') @if ($permission === '*')
<div>All (root/admin access), be careful!</div> <div>Root access, be careful!</div>
@else @else
<div>{{ $permission }}</div> <div>{{ $permission }}</div>
@endif @endif
@@ -35,6 +36,7 @@
</div> </div>
<h4>Token Permissions</h4> <h4>Token Permissions</h4>
<div class="w-64"> <div class="w-64">
<x-forms.checkbox label="Root Access" wire:model.live="rootAccess"></x-forms.checkbox>
<x-forms.checkbox label="Read-only" wire:model.live="readOnly"></x-forms.checkbox> <x-forms.checkbox label="Read-only" wire:model.live="readOnly"></x-forms.checkbox>
<x-forms.checkbox label="View Sensitive Data" wire:model.live="viewSensitiveData"></x-forms.checkbox> <x-forms.checkbox label="View Sensitive Data" wire:model.live="viewSensitiveData"></x-forms.checkbox>
</div> </div>