coolify/app/Livewire/Team/Member.php

90 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2023-06-02 10:34:45 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Team;
2023-06-02 10:34:45 +00:00
use App\Enums\Role;
2023-06-02 10:34:45 +00:00
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2023-09-15 09:19:36 +00:00
use Illuminate\Support\Facades\Cache;
2023-06-02 10:34:45 +00:00
use Livewire\Component;
class Member extends Component
{
use AuthorizesRequests;
2023-06-02 10:34:45 +00:00
public User $member;
2023-06-09 13:55:21 +00:00
public function makeAdmin()
{
try {
$this->authorize('manageMembers', currentTeam());
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
throw new \Exception('You are not authorized to perform this action.');
}
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::ADMIN->value]);
$this->dispatch('reloadWindow');
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
2023-06-09 13:55:21 +00:00
}
public function makeOwner()
{
try {
$this->authorize('manageMembers', currentTeam());
if (Role::from(auth()->user()->role())->lt(Role::OWNER)
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
throw new \Exception('You are not authorized to perform this action.');
}
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::OWNER->value]);
$this->dispatch('reloadWindow');
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
}
2024-06-10 20:43:34 +00:00
2023-06-09 13:55:21 +00:00
public function makeReadonly()
{
try {
$this->authorize('manageMembers', currentTeam());
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
throw new \Exception('You are not authorized to perform this action.');
}
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::MEMBER->value]);
$this->dispatch('reloadWindow');
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
2023-06-09 13:55:21 +00:00
}
2023-06-08 09:43:14 +00:00
public function remove()
2023-06-02 10:34:45 +00:00
{
try {
$this->authorize('manageMembers', currentTeam());
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
throw new \Exception('You are not authorized to perform this action.');
}
$teamId = currentTeam()->id;
$this->member->teams()->detach(currentTeam());
// Clear cache for the removed user - both old and new key formats
Cache::forget("team:{$this->member->id}");
Cache::forget("user:{$this->member->id}:team:{$teamId}");
$this->dispatch('reloadWindow');
} catch (\Exception $e) {
$this->dispatch('error', $e->getMessage());
}
2023-06-02 10:34:45 +00:00
}
private function getMemberRole()
{
return $this->member->teams()->where('teams.id', currentTeam()->id)->first()?->pivot?->role;
}
2023-06-02 10:34:45 +00:00
}