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
|
|
|
|
2024-10-28 14:40:23 +00:00
|
|
|
use App\Enums\Role;
|
2023-06-02 10:34:45 +00:00
|
|
|
use App\Models\User;
|
2025-08-26 08:27:31 +00:00
|
|
|
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
|
|
|
|
|
{
|
2025-08-26 08:27:31 +00:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2023-06-02 10:34:45 +00:00
|
|
|
public User $member;
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-09 13:55:21 +00:00
|
|
|
public function makeAdmin()
|
|
|
|
|
{
|
2024-10-28 09:47:29 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('manageMembers', currentTeam());
|
|
|
|
|
|
2024-10-28 14:40:23 +00:00
|
|
|
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|
|
|
|
|
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
|
2024-10-28 09:47:29 +00:00
|
|
|
throw new \Exception('You are not authorized to perform this action.');
|
|
|
|
|
}
|
2024-10-28 14:40:23 +00:00
|
|
|
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::ADMIN->value]);
|
2024-10-28 09:47:29 +00:00
|
|
|
$this->dispatch('reloadWindow');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2023-06-09 13:55:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-02-23 11:34:36 +00:00
|
|
|
public function makeOwner()
|
|
|
|
|
{
|
2024-10-28 09:47:29 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('manageMembers', currentTeam());
|
|
|
|
|
|
2024-10-28 14:40:23 +00:00
|
|
|
if (Role::from(auth()->user()->role())->lt(Role::OWNER)
|
|
|
|
|
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
|
2024-10-28 09:47:29 +00:00
|
|
|
throw new \Exception('You are not authorized to perform this action.');
|
|
|
|
|
}
|
2024-10-28 14:40:23 +00:00
|
|
|
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::OWNER->value]);
|
2024-10-28 09:47:29 +00:00
|
|
|
$this->dispatch('reloadWindow');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2024-02-23 11:34:36 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-06-09 13:55:21 +00:00
|
|
|
public function makeReadonly()
|
|
|
|
|
{
|
2024-10-28 09:47:29 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('manageMembers', currentTeam());
|
|
|
|
|
|
2024-10-28 14:40:23 +00:00
|
|
|
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|
|
|
|
|
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
|
2024-10-28 09:47:29 +00:00
|
|
|
throw new \Exception('You are not authorized to perform this action.');
|
|
|
|
|
}
|
2024-10-28 14:40:23 +00:00
|
|
|
$this->member->teams()->updateExistingPivot(currentTeam()->id, ['role' => Role::MEMBER->value]);
|
2024-10-28 09:47:29 +00:00
|
|
|
$this->dispatch('reloadWindow');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2023-06-09 13:55:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-06-08 09:43:14 +00:00
|
|
|
public function remove()
|
2023-06-02 10:34:45 +00:00
|
|
|
{
|
2024-10-28 09:47:29 +00:00
|
|
|
try {
|
2025-08-26 08:27:31 +00:00
|
|
|
$this->authorize('manageMembers', currentTeam());
|
|
|
|
|
|
2024-10-28 14:40:23 +00:00
|
|
|
if (Role::from(auth()->user()->role())->lt(Role::ADMIN)
|
|
|
|
|
|| Role::from($this->getMemberRole())->gt(auth()->user()->role())) {
|
2024-10-28 09:47:29 +00:00
|
|
|
throw new \Exception('You are not authorized to perform this action.');
|
|
|
|
|
}
|
2025-12-28 13:50:59 +00:00
|
|
|
$teamId = currentTeam()->id;
|
2024-10-28 09:47:29 +00:00
|
|
|
$this->member->teams()->detach(currentTeam());
|
2025-12-28 13:50:59 +00:00
|
|
|
// Clear cache for the removed user - both old and new key formats
|
2024-10-28 09:47:29 +00:00
|
|
|
Cache::forget("team:{$this->member->id}");
|
2025-12-28 13:50:59 +00:00
|
|
|
Cache::forget("user:{$this->member->id}:team:{$teamId}");
|
2024-10-28 09:47:29 +00:00
|
|
|
$this->dispatch('reloadWindow');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->dispatch('error', $e->getMessage());
|
|
|
|
|
}
|
2023-06-02 10:34:45 +00:00
|
|
|
}
|
2024-10-28 14:40:23 +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
|
|
|
}
|