*/ public array $memberRoles = []; public function mount(): void { $this->seedMemberRoles(); } #[Computed] public function organization(): Organization { return auth()->user()->currentOrganization()->firstOrFail(); } #[Computed] public function canManage(): bool { return auth()->user()->can('manageMembers', $this->organization); } /** * @return Collection */ #[Computed] public function members(): Collection { return $this->organization->users()->orderBy('name')->get(); } /** * @return Collection */ #[Computed] public function invitations(): Collection { return $this->organization->invitations()->pending()->orderBy('email')->get(); } /** * Invite someone to the active workspace by email. The seat limit is a * hard cap, so no invitation is sent once every seat is taken. */ public function inviteMember(): void { abort_unless($this->canManage, 403); $organization = $this->organization; $validated = $this->validate([ 'inviteEmail' => ['required', 'string', 'email', 'max:255'], 'inviteRole' => ['required', Rule::in([OrganizationRole::Admin->value, OrganizationRole::Member->value])], ]); $email = Str::lower($validated['inviteEmail']); if ($organization->users()->where('users.email', $email)->exists()) { $this->addError('inviteEmail', __('That person is already a member of this workspace.')); return; } if (! $organization->canAddMember()) { $this->addError('inviteEmail', __('This workspace is full, so no more invitations can be sent.')); return; } $invitation = Invitation::updateOrCreate( [ 'inviteable_type' => Organization::class, 'inviteable_id' => $organization->id, 'email' => $email, ], [ 'inviter_id' => auth()->id(), 'role' => $validated['inviteRole'], 'token' => Str::random(64), 'expires_at' => now()->addDays(Invitation::EXPIRY_DAYS), 'accepted_at' => null, 'accepted_by_id' => null, ], ); Mail::to($email)->send(new InvitationMail($invitation)); $this->reset('inviteEmail', 'inviteRole'); unset($this->invitations); Flux::toast(variant: 'success', text: __('Invitation sent to :email.', ['email' => $email])); } public function updateMemberRole(int $userId, string $role): void { abort_unless($this->canManage, 403); $organization = $this->organization; if ($this->memberRole($organization, $userId) === OrganizationRole::Owner) { return; } $validated = validator( ['role' => $role], ['role' => ['required', Rule::in([OrganizationRole::Admin->value, OrganizationRole::Member->value])]], )->validate(); $organization->users()->updateExistingPivot($userId, ['role' => $validated['role']]); unset($this->members); Flux::toast(variant: 'success', text: __('Member role updated.')); } public function updatedMemberRoles(string $value, string $key): void { $this->updateMemberRole((int) $key, $value); } public function removeMember(int $userId): void { abort_unless($this->canManage, 403); if ($userId === auth()->id()) { Flux::toast(variant: 'danger', text: __('You cannot remove yourself from the workspace.')); return; } $organization = $this->organization; if ($this->memberRole($organization, $userId) === OrganizationRole::Owner) { return; } $organization->users()->detach($userId); // Move the removed member back to their personal workspace if they // were currently working in this one. $user = User::find($userId); if ($user !== null && (int) $user->current_organization_id === $organization->id) { $fallback = $user->organizations() ->where('type', WorkspaceType::Personal->value) ->first() ?? $user->organizations()->first(); if ($fallback !== null) { $user->forceFill(['current_organization_id' => $fallback->id])->save(); } } unset($this->members); $this->seedMemberRoles(); Flux::toast(variant: 'success', text: __('Member removed.')); } public function revokeInvitation(int $invitationId): void { abort_unless($this->canManage, 403); $invitation = $this->organization->invitations()->whereKey($invitationId)->firstOrFail(); abort_unless(auth()->user()->can('delete', $invitation), 403); $invitation->delete(); unset($this->invitations); Flux::toast(variant: 'success', text: __('Invitation revoked.')); } private function memberRole(Organization $organization, int $userId): ?OrganizationRole { return OrganizationRole::tryFrom((string) $organization->users() ->where('users.id', $userId) ->withPivot('role') ->first()?->pivot->role); } private function seedMemberRoles(): void { $this->memberRoles = $this->members ->mapWithKeys(fn (User $member): array => [$member->id => (string) $member->pivot->role]) ->all(); } }; ?>
{{ __('Members') }} {{ __('People with access to the ":name" workspace.', ['name' => $this->organization->name]) }}
@php($limit = $this->organization->seatLimit()) @if ($limit === null) {{ __(':count members', ['count' => $this->organization->memberCount()]) }} @else {{ __(':count of :limit seats used', ['count' => $this->organization->memberCount(), 'limit' => $limit]) }} @endif
{{ __('Current members') }} @foreach ($this->members as $member) @php($role = OrganizationRole::tryFrom((string) $member->pivot->role))
{{ $member->name }} {{ $member->email }}
@if ($role === OrganizationRole::Owner || ! $this->canManage || $member->id === auth()->id()) {{ $role?->label() ?? $member->pivot->role }} @else @foreach ([OrganizationRole::Admin, OrganizationRole::Member] as $option) {{ $option->label() }} @endforeach @if ($role !== OrganizationRole::Owner) @endif @endif
@endforeach
@if ($this->canManage) @if ($this->organization->canAddMember())
{{ __('Invite a member') }}
@foreach ([OrganizationRole::Admin, OrganizationRole::Member] as $option) {{ $option->label() }} @endforeach
{{ __('Send invite') }}
@else {{ __('Every seat in this workspace is taken, so no more members can be invited.') }} @endif @if ($this->invitations->isNotEmpty())
{{ __('Pending invitations') }} @foreach ($this->invitations as $invitation)
{{ $invitation->email }} {{ $invitation->roleLabel() }}
{{ __('Revoke') }}
@endforeach
@endif @endif