setPrivilege($user, $action, $privilege); } /** * @throws AuditionAdminException */ public function setPrivilege(User|int $user, string $action, string $privilege): void { if (is_int($user)) { $user = User::findOrFail($user); } if (! User::where('id', $user->id)->exists()) { throw new AuditionAdminException('User does not exist'); } if (! in_array($action, ['grant', 'revoke'])) { throw new AuditionAdminException('Invalid Action'); } $field = match ($privilege) { 'admin' => 'is_admin', 'tab' => 'is_tab', default => throw new AuditionAdminException('Invalid Privilege'), }; if ($user->$field == 1 && $action == 'revoke') { $user->$field = 0; $user->save(); } if ($user->$field == 0 && $action == 'grant') { $user->$field = 1; $user->save(); } } }