added roles and organizations
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-15 20:59:32 -07:00
parent 2ca3860557
commit 177f122c10
57 changed files with 6378 additions and 247 deletions
@@ -2,19 +2,24 @@
use App\Models\User;
use Flux\Flux;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;
use Livewire\WithPagination;
new #[Title('Notes limits')] class extends Component
{
use WithPagination;
private const PER_PAGE = 10;
public function mount(): void
{
if (! auth()->user()->isAdmin()) {
abort(403);
}
Gate::authorize('accessAdmin');
}
#[Locked]
@@ -24,12 +29,33 @@ new #[Title('Notes limits')] class extends Component
public string $notesLimitInput = '';
public string $search = '';
public function updatedSearch(): void
{
$this->resetPage();
}
#[Computed]
public function users(): Collection
public function users(): LengthAwarePaginator
{
return $this->filteredUserQuery()
->orderBy('name')
->paginate(self::PER_PAGE);
}
/**
* @return Builder<User>
*/
private function filteredUserQuery(): Builder
{
return User::query()
->orderBy('name')
->get();
->when(
$this->search !== '',
fn (Builder $query) => $query->where(fn (Builder $query) => $query
->where('name', 'like', '%'.$this->search.'%')
->orWhere('email', 'like', '%'.$this->search.'%'))
);
}
public function startEditLimit(int $userId): void
@@ -44,6 +70,8 @@ new #[Title('Notes limits')] class extends Component
public function saveNotesLimit(): void
{
Gate::authorize('accessAdmin');
$user = $this->userById($this->editingUserId);
$validated = $this->validate([
@@ -69,24 +97,49 @@ new #[Title('Notes limits')] class extends Component
<flux:text class="mt-1">{{ __('Set how many characters each user may write in todo notes.') }}</flux:text>
</div>
<div class="flex flex-wrap items-center gap-2">
<flux:input
size="sm"
type="search"
icon="magnifying-glass"
wire:model.live.debounce.300ms="search"
placeholder="{{ __('Search users…') }}"
class="w-full sm:w-64"
data-test="search-input"
/>
@if ($search !== '')
<flux:button
size="sm"
variant="danger"
icon="x-mark"
wire:click="$set('search', '')"
class="shrink-0"
data-test="clear-search"
>
{{ __('Clear') }}
</flux:button>
@endif
</div>
<flux:card>
<flux:table>
<flux:table :paginate="$this->users">
<flux:table.columns>
<flux:table.column>{{ __('Name') }}</flux:table.column>
<flux:table.column>{{ __('Email') }}</flux:table.column>
<flux:table.column>{{ __('Role') }}</flux:table.column>
<flux:table.column>{{ __('Site role') }}</flux:table.column>
<flux:table.column>{{ __('Notes limit') }}</flux:table.column>
<flux:table.column align="end" width="5rem">{{ __('Actions') }}</flux:table.column>
</flux:table.columns>
<flux:table.rows>
@foreach ($this->users as $user)
@forelse ($this->users as $user)
<flux:table.row :key="$user->id" data-test="user-row-{{ $user->id }}">
<flux:table.cell>{{ $user->name }}</flux:table.cell>
<flux:table.cell class="text-zinc-500">{{ $user->email }}</flux:table.cell>
<flux:table.cell>
@if ($user->isAdmin())
<flux:badge size="sm" color="blue">{{ __('Admin') }}</flux:badge>
@if ($user->isSiteAdmin())
<flux:badge size="sm" color="blue">{{ __('Site admin') }}</flux:badge>
@else
<flux:badge size="sm" color="zinc">{{ __('Member') }}</flux:badge>
@endif
@@ -107,7 +160,13 @@ new #[Title('Notes limits')] class extends Component
</div>
</flux:table.cell>
</flux:table.row>
@endforeach
@empty
<flux:table.row>
<flux:table.cell colspan="5" align="center" class="py-10">
<flux:text>{{ __('No users match your search.') }}</flux:text>
</flux:table.cell>
</flux:table.row>
@endforelse
</flux:table.rows>
</flux:table>
</flux:card>
@@ -4,6 +4,7 @@ use App\Models\Organization;
use App\Models\Setting;
use Flux\Flux;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Title;
use Livewire\Component;
@@ -12,9 +13,7 @@ new #[Title('Plan pricing')] class extends Component
{
public function mount(): void
{
if (! auth()->user()->isAdmin()) {
abort(403);
}
Gate::authorize('accessAdmin');
$this->amounts = $this->plans()
->mapWithKeys(fn (array $plan): array => [
@@ -61,6 +60,8 @@ new #[Title('Plan pricing')] class extends Component
public function saveAmounts(): void
{
Gate::authorize('accessAdmin');
$validated = $this->validate([
'amounts' => ['required', 'array'],
'amounts.*' => ['required', 'numeric', 'min:0', 'decimal:0,2'],
@@ -0,0 +1,82 @@
<?php
use App\Models\Invitation;
use App\Models\Organization;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Invitation')] class extends Component
{
public Invitation $invitation;
public function mount(Invitation $invitation): void
{
$this->invitation = $invitation;
}
/**
* Redeem the invitation for the signed-in user, then send them into the
* app. Workspace invitations are refused once the workspace is full.
*/
public function accept(): void
{
if (! $this->invitation->isValid()) {
return;
}
$organization = $this->invitation->inviteable;
if ($organization instanceof Organization && ! $organization->canAddMember()) {
$this->addError('invitation', __('This workspace is full, so this invitation can no longer be accepted.'));
return;
}
$this->invitation->accept(auth()->user());
$this->redirect(route('dashboard'), navigate: true);
}
}; ?>
<div class="mx-auto flex w-full max-w-xl flex-col gap-6">
<flux:heading size="xl">{{ __('Invitation') }}</flux:heading>
@if ($invitation->isAccepted())
<flux:callout variant="success" icon="check-circle" data-test="invitation-accepted">
{{ __('This invitation has already been accepted.') }}
</flux:callout>
@elseif ($invitation->isExpired())
<flux:callout variant="warning" icon="clock" data-test="invitation-expired">
{{ __('This invitation has expired. Ask the sender to invite you again.') }}
</flux:callout>
@else
<flux:callout icon="envelope" data-test="invitation-summary">
@if ($invitation->isForFolder())
{{ __(':inviter invited you to collaborate on the folder ":name" as :role.', [
'inviter' => $invitation->inviter?->name ?? __('Someone'),
'name' => $invitation->inviteable?->name ?? __('a folder'),
'role' => $invitation->roleLabel(),
]) }}
@else
{{ __(':inviter invited you to join the ":name" workspace as :role.', [
'inviter' => $invitation->inviter?->name ?? __('Someone'),
'name' => $invitation->inviteable?->name ?? __('a workspace'),
'role' => $invitation->roleLabel(),
]) }}
@endif
</flux:callout>
@error('invitation')
<flux:callout variant="danger" data-test="invitation-error">{{ $message }}</flux:callout>
@enderror
<div class="flex justify-end gap-3">
<flux:button variant="ghost" :href="route('dashboard')" wire:navigate>
{{ __('Not now') }}
</flux:button>
<flux:button variant="primary" wire:click="accept" data-test="accept-invitation-button">
{{ __('Accept invitation') }}
</flux:button>
</div>
@endif
</div>
@@ -0,0 +1,334 @@
<?php
use App\Mail\InvitationMail;
use App\Models\Invitation;
use App\Models\Organization;
use App\Models\User;
use App\OrganizationRole;
use App\WorkspaceType;
use Flux\Flux;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Members')] class extends Component
{
public string $inviteEmail = '';
public string $inviteRole = 'member';
/** @var array<int, string> */
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<int, User>
*/
#[Computed]
public function members(): Collection
{
return $this->organization->users()->orderBy('name')->get();
}
/**
* @return Collection<int, Invitation>
*/
#[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();
}
}; ?>
<div class="flex h-full w-full flex-1 flex-col gap-6">
<div>
<flux:heading size="xl">{{ __('Members') }}</flux:heading>
<flux:text class="mt-1">
{{ __('People with access to the ":name" workspace.', ['name' => $this->organization->name]) }}
</flux:text>
</div>
@php($limit = $this->organization->seatLimit())
<flux:text size="sm" class="text-zinc-500 dark:text-zinc-400" data-test="seat-usage">
@if ($limit === null)
{{ __(':count members', ['count' => $this->organization->memberCount()]) }}
@else
{{ __(':count of :limit seats used', ['count' => $this->organization->memberCount(), 'limit' => $limit]) }}
@endif
</flux:text>
<flux:card>
<div class="space-y-4">
<flux:heading size="sm">{{ __('Current members') }}</flux:heading>
@foreach ($this->members as $member)
@php($role = OrganizationRole::tryFrom((string) $member->pivot->role))
<div class="flex flex-wrap items-center justify-between gap-3" data-test="member-row-{{ $member->id }}">
<div class="flex min-w-0 items-center gap-2">
<flux:text class="truncate">{{ $member->name }}</flux:text>
<flux:text size="sm" class="truncate text-zinc-500 dark:text-zinc-400">{{ $member->email }}</flux:text>
</div>
<div class="flex items-center gap-2">
@if ($role === OrganizationRole::Owner || ! $this->canManage || $member->id === auth()->id())
<flux:badge size="sm" color="{{ $role === OrganizationRole::Owner ? 'blue' : 'zinc' }}" data-test="member-role-{{ $member->id }}">
{{ $role?->label() ?? $member->pivot->role }}
</flux:badge>
@else
<flux:select
size="sm"
wire:model.live="memberRoles.{{ $member->id }}"
data-test="member-role-select-{{ $member->id }}"
>
@foreach ([OrganizationRole::Admin, OrganizationRole::Member] as $option)
<flux:select.option value="{{ $option->value }}">{{ $option->label() }}</flux:select.option>
@endforeach
</flux:select>
@if ($role !== OrganizationRole::Owner)
<flux:button
size="sm"
variant="ghost"
icon="trash"
wire:click="removeMember({{ $member->id }})"
data-test="remove-member-{{ $member->id }}"
/>
@endif
@endif
</div>
</div>
@endforeach
</div>
</flux:card>
@if ($this->canManage)
@if ($this->organization->canAddMember())
<flux:card>
<form wire:submit="inviteMember" class="space-y-4">
<flux:heading size="sm">{{ __('Invite a member') }}</flux:heading>
<div class="flex flex-wrap items-start gap-3">
<div class="min-w-56 flex-1">
<flux:input
wire:model="inviteEmail"
type="email"
placeholder="name@example.com"
data-test="member-invite-email-input"
/>
<flux:error name="inviteEmail" />
</div>
<div>
<flux:select wire:model="inviteRole" data-test="member-invite-role-select">
@foreach ([OrganizationRole::Admin, OrganizationRole::Member] as $option)
<flux:select.option value="{{ $option->value }}">{{ $option->label() }}</flux:select.option>
@endforeach
</flux:select>
<flux:error name="inviteRole" />
</div>
<flux:button type="submit" variant="primary" icon="paper-airplane" data-test="invite-member-button">
{{ __('Send invite') }}
</flux:button>
</div>
</form>
</flux:card>
@else
<flux:callout variant="warning" icon="users" data-test="workspace-full-notice">
{{ __('Every seat in this workspace is taken, so no more members can be invited.') }}
</flux:callout>
@endif
@if ($this->invitations->isNotEmpty())
<flux:card>
<div class="space-y-4">
<flux:heading size="sm">{{ __('Pending invitations') }}</flux:heading>
@foreach ($this->invitations as $invitation)
<div class="flex flex-wrap items-center justify-between gap-3" data-test="member-invitation-row-{{ $invitation->id }}">
<div class="flex min-w-0 items-center gap-2">
<flux:text class="truncate">{{ $invitation->email }}</flux:text>
<flux:badge size="sm">{{ $invitation->roleLabel() }}</flux:badge>
</div>
<flux:button
size="sm"
variant="ghost"
icon="x-mark"
wire:click="revokeInvitation({{ $invitation->id }})"
data-test="revoke-member-invitation-{{ $invitation->id }}"
>
{{ __('Revoke') }}
</flux:button>
</div>
@endforeach
</div>
</flux:card>
@endif
@endif
</div>
+86 -59
View File
@@ -55,7 +55,7 @@ new #[Title('Dashboard')] class extends Component
$folderId = (int) $folderId;
if (Folder::query()->forUser(auth()->id())->whereKey($folderId)->exists()) {
if (Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($folderId)->exists()) {
$this->folderId = $folderId;
}
}
@@ -93,7 +93,7 @@ new #[Title('Dashboard')] class extends Component
public function openEditForm(int $todoId): void
{
$todo = $this->todoForUser($todoId);
$todo = $this->todoForAction($todoId, 'update');
$this->editingTodoId = $todo->id;
$this->description = $todo->description;
@@ -113,11 +113,20 @@ new #[Title('Dashboard')] class extends Component
'todoStatus' => ['required', Rule::enum(TodoStatus::class)],
'dueDate' => ['nullable', 'date'],
'notes' => ['nullable', 'string', 'max:'.auth()->user()->notesLimit()],
'formFolderId' => ['required', 'integer', Rule::exists('folders', 'id')->where('user_id', auth()->id())],
'formFolderId' => ['required', 'integer', Rule::exists('folders', 'id')->whereIn(
'id',
Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->select('folders.id')
)],
]);
$folder = Folder::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->findOrFail($validated['formFolderId']);
abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403);
$data = [
'folder_id' => $validated['formFolderId'],
'folder_id' => $folder->id,
'description' => $validated['description'],
'status' => TodoStatus::from($validated['todoStatus']),
'due_date' => filled($validated['dueDate']) ? Carbon::parse($validated['dueDate']) : null,
@@ -125,11 +134,11 @@ new #[Title('Dashboard')] class extends Component
];
if ($this->editingTodoId !== null) {
$this->todoForUser($this->editingTodoId)->update($data);
$this->todoForAction($this->editingTodoId, 'update')->update($data);
} else {
Todo::create($data + [
'user_id' => auth()->id(),
'position' => Todo::nextPositionFor(auth()->user()),
'position' => Todo::nextPositionFor($folder),
]);
}
@@ -138,7 +147,7 @@ new #[Title('Dashboard')] class extends Component
public function toggleComplete(int $todoId): void
{
$todo = $this->todoForUser($todoId);
$todo = $this->todoForAction($todoId, 'markComplete');
$data = ['completed_at' => $todo->completed_at === null ? now() : null];
@@ -153,7 +162,7 @@ new #[Title('Dashboard')] class extends Component
public function confirmTodoDelete(int $todoId): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'delete');
$this->confirmingTodoDelete = $todoId;
$this->modal('confirm-todo-delete')->show();
@@ -161,7 +170,7 @@ new #[Title('Dashboard')] class extends Component
public function deleteTodo(): void
{
$this->todoForUser($this->confirmingTodoDelete)->delete();
$this->todoForAction($this->confirmingTodoDelete ?? 0, 'delete')->delete();
$this->confirmingTodoDelete = null;
$this->modal('confirm-todo-delete')->close();
@@ -176,7 +185,7 @@ new #[Title('Dashboard')] class extends Component
#[Computed]
public function todoCounts(): array
{
$query = Todo::query()->forUser(auth()->id());
$query = Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId());
return [
'open' => (clone $query)->active()->count(),
@@ -189,7 +198,7 @@ new #[Title('Dashboard')] class extends Component
#[Computed]
public function folderTree(): Collection
{
return Folder::treeForUser(auth()->id());
return Folder::treeForVisibleTo(auth()->id(), $this->activeWorkspaceId());
}
#[Computed]
@@ -208,12 +217,12 @@ new #[Title('Dashboard')] class extends Component
$tree = $this->folderTree;
$openByFolder = Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->active()
->pluck('folder_id')
->countBy();
$overdueByFolder = Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->overdue()
->pluck('folder_id')
->countBy();
@@ -264,7 +273,7 @@ new #[Title('Dashboard')] class extends Component
public function moveTodo(int $todoId, string $direction): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'update');
$ids = $this->orderedTodoIds;
$currentIndex = array_search($todoId, $ids, true);
@@ -284,7 +293,7 @@ new #[Title('Dashboard')] class extends Component
public function handleSort(int $todoId, int $position): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'update');
$offset = ($this->todos->currentPage() - 1) * self::PER_PAGE;
@@ -317,7 +326,7 @@ new #[Title('Dashboard')] class extends Component
unset($visibleOrder[$currentIndex]);
array_splice($visibleOrder, $newIndex, 0, $todoId);
$ids = Todo::query()->forUser(auth()->id())->priority()->pluck('id')->all();
$ids = Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->priority()->pluck('id')->all();
$slots = array_keys(array_intersect($ids, $visibleIds));
@@ -326,7 +335,7 @@ new #[Title('Dashboard')] class extends Component
}
foreach ($ids as $position => $id) {
Todo::query()->forUser(auth()->id())->whereKey($id)->update(['position' => $position]);
Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($id)->update(['position' => $position]);
}
unset($this->todos, $this->orderedTodoIds);
@@ -338,7 +347,7 @@ new #[Title('Dashboard')] class extends Component
private function filteredTodoQuery(): Builder
{
return Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->when(
$this->search !== '',
fn (Builder $query) => $query->where('description', 'like', '%'.$this->search.'%')
@@ -369,9 +378,21 @@ new #[Title('Dashboard')] class extends Component
return $ids;
}
private function todoForUser(int $todoId): Todo
private function todoForAction(int $todoId, string $ability): Todo
{
return Todo::query()->forUser(auth()->id())->findOrFail($todoId);
$todo = Todo::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->with('folder')
->findOrFail($todoId);
abort_unless(auth()->user()->can($ability, $todo), 403);
return $todo;
}
private function activeWorkspaceId(): int
{
return (int) auth()->user()->current_organization_id;
}
}; ?>
@@ -510,18 +531,22 @@ new #[Title('Dashboard')] class extends Component
@forelse ($this->todos as $todo)
<flux:table.row :key="$todo->id" wire:sort:item="{{ $todo->id }}" data-test="todo-row">
<flux:table.cell class="py-0 text-zinc-300 dark:text-zinc-600">
<div wire:sort:handle class="inline-flex cursor-grab" title="{{ __('Reorder') }}">
<flux:icon.chevrons-up-down variant="micro" />
</div>
@can('update', $todo)
<div wire:sort:handle class="inline-flex cursor-grab" title="{{ __('Reorder') }}">
<flux:icon.chevrons-up-down variant="micro" />
</div>
@endcan
</flux:table.cell>
<flux:table.cell class="py-0">
<flux:checkbox
:checked="$todo->completed_at !== null"
:title="$todo->completed_at ? __('Mark as not done') : __('Mark as done')"
wire:click="toggleComplete({{ $todo->id }})"
data-test="toggle-complete-{{ $todo->id }}"
/>
@can('markComplete', $todo)
<flux:checkbox
:checked="$todo->completed_at !== null"
:title="$todo->completed_at ? __('Mark as not done') : __('Mark as done')"
wire:click="toggleComplete({{ $todo->id }})"
data-test="toggle-complete-{{ $todo->id }}"
/>
@endcan
</flux:table.cell>
<flux:table.cell :variant="$todo->completed_at ? 'strong' : 'default'" data-test="todo-description-{{ $todo->id }}">
@@ -581,44 +606,46 @@ new #[Title('Dashboard')] class extends Component
<flux:table.cell class="py-0">
<div class="flex justify-end gap-1">
@if ($todo->id !== ($this->orderedTodoIds[0] ?? null))
@can('update', $todo)
@if ($todo->id !== ($this->orderedTodoIds[0] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-up"
:title="__('Move up')"
wire:click="moveTodo({{ $todo->id }}, 'up')"
data-test="move-todo-up-{{ $todo->id }}"
/>
@endif
@if ($todo->id !== ($this->orderedTodoIds[array_key_last($this->orderedTodoIds)] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-down"
:title="__('Move down')"
wire:click="moveTodo({{ $todo->id }}, 'down')"
data-test="move-todo-down-{{ $todo->id }}"
/>
@endif
<flux:button
size="sm"
variant="ghost"
icon="chevron-up"
:title="__('Move up')"
wire:click="moveTodo({{ $todo->id }}, 'up')"
data-test="move-todo-up-{{ $todo->id }}"
icon="pencil-square"
:title="__('Edit todo')"
wire:click="openEditForm({{ $todo->id }})"
data-test="edit-todo-{{ $todo->id }}"
/>
@endif
@if ($todo->id !== ($this->orderedTodoIds[array_key_last($this->orderedTodoIds)] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-down"
:title="__('Move down')"
wire:click="moveTodo({{ $todo->id }}, 'down')"
data-test="move-todo-down-{{ $todo->id }}"
icon="trash"
:title="__('Delete todo')"
wire:click="confirmTodoDelete({{ $todo->id }})"
data-test="delete-todo-{{ $todo->id }}"
/>
@endif
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
:title="__('Edit todo')"
wire:click="openEditForm({{ $todo->id }})"
data-test="edit-todo-{{ $todo->id }}"
/>
<flux:button
size="sm"
variant="ghost"
icon="trash"
:title="__('Delete todo')"
wire:click="confirmTodoDelete({{ $todo->id }})"
data-test="delete-todo-{{ $todo->id }}"
/>
@endcan
</div>
</flux:table.cell>
</flux:table.row>
+491 -87
View File
@@ -1,10 +1,19 @@
<?php
use App\FolderRole;
use App\FolderVisibility;
use App\Mail\InvitationMail;
use App\Models\Folder;
use App\Models\Invitation;
use App\Models\Organization;
use App\Models\Todo;
use App\Models\User;
use App\TodoStatus;
use Flux\Flux;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
@@ -40,6 +49,18 @@ new #[Title('Folders')] class extends Component
public string $colorDraft = '';
#[Locked]
public ?int $sharingFolderId = null;
public string $inviteEmail = '';
public string $inviteRole = 'viewer';
public string $shareVisibility = 'private';
/** @var array<int, string> */
public array $collaboratorRoles = [];
public bool $showTodoForm = false;
#[Locked]
@@ -55,8 +76,15 @@ new #[Title('Folders')] class extends Component
public function createTopLevelFolder(): void
{
$organization = auth()->user()->currentOrganization()->first();
abort_unless(
$organization !== null && auth()->user()->can('create', [Folder::class, $organization]),
403
);
$validated = $this->validate([
'newFolderName' => $this->folderNameRules(parentId: null),
'newFolderName' => $this->folderNameRules($organization->id, parentId: null),
'newFolderColor' => $this->folderColorRules(),
]);
@@ -72,7 +100,7 @@ new #[Title('Folders')] class extends Component
public function startChildCreate(int $folderId): void
{
$this->folderForUser($folderId);
$this->folderForAction($folderId, 'createChild');
$this->creatingChildOf = $folderId;
$this->reset('newChildName', 'newChildColor');
@@ -86,18 +114,19 @@ new #[Title('Folders')] class extends Component
public function createChildFolder(): void
{
$this->folderForUser($this->creatingChildOf);
$parent = $this->folderForAction($this->creatingChildOf ?? 0, 'createChild');
$validated = $this->validate([
'newChildName' => $this->folderNameRules(parentId: $this->creatingChildOf),
'newChildName' => $this->folderNameRules($parent->organization_id, parentId: $parent->id),
'newChildColor' => $this->folderColorRules(),
]);
Folder::create([
'user_id' => auth()->id(),
'organization_id' => $parent->organization_id,
'name' => $validated['newChildName'],
'color' => filled($validated['newChildColor']) ? $validated['newChildColor'] : null,
'parent_id' => $this->creatingChildOf,
'parent_id' => $parent->id,
]);
$this->reset('creatingChildOf', 'newChildName', 'newChildColor');
@@ -105,7 +134,7 @@ new #[Title('Folders')] class extends Component
public function startRename(int $folderId): void
{
$folder = $this->folderForUser($folderId);
$folder = $this->folderForAction($folderId, 'update');
$this->renamingFolderId = $folder->id;
$this->renameName = $folder->name;
@@ -119,10 +148,10 @@ new #[Title('Folders')] class extends Component
public function saveRename(): void
{
$folder = $this->folderForUser($this->renamingFolderId);
$folder = $this->folderForAction($this->renamingFolderId ?? 0, 'update');
$validated = $this->validate([
'renameName' => $this->folderNameRules(parentId: $folder->parent_id, ignoreId: $folder->id),
'renameName' => $this->folderNameRules($folder->organization_id, parentId: $folder->parent_id, ignoreId: $folder->id),
]);
$folder->update(['name' => $validated['renameName']]);
@@ -132,7 +161,7 @@ new #[Title('Folders')] class extends Component
public function confirmFolderDelete(int $folderId): void
{
$this->folderForUser($folderId);
$this->folderForAction($folderId, 'delete');
$this->confirmingFolderDelete = $folderId;
$this->modal('confirm-folder-delete')->show();
@@ -140,7 +169,7 @@ new #[Title('Folders')] class extends Component
public function deleteFolder(): void
{
$folder = $this->folderForUser($this->confirmingFolderDelete);
$folder = $this->folderForAction($this->confirmingFolderDelete ?? 0, 'delete');
if ($this->expandedFolderId === $folder->id) {
$this->expandedFolderId = null;
@@ -154,14 +183,14 @@ new #[Title('Folders')] class extends Component
public function toggleExpandFolder(int $folderId): void
{
$this->folderForUser($folderId);
$this->visibleFolder($folderId);
$this->expandedFolderId = $this->expandedFolderId === $folderId ? null : $folderId;
}
public function startColorEdit(int $folderId): void
{
$folder = $this->folderForUser($folderId);
$folder = $this->folderForAction($folderId, 'update');
$this->colorEditingFolderId = $folder->id;
$this->colorDraft = $folder->color ?? '';
@@ -171,7 +200,7 @@ new #[Title('Folders')] class extends Component
public function saveFolderColor(): void
{
$folder = $this->folderForUser($this->colorEditingFolderId ?? 0);
$folder = $this->folderForAction($this->colorEditingFolderId ?? 0, 'update');
$validated = $this->validate([
'colorDraft' => $this->folderColorRules(),
@@ -185,21 +214,179 @@ new #[Title('Folders')] class extends Component
$this->modal('folder-color')->close();
}
public function startShare(int $folderId): void
{
$folder = $this->folderForAction($folderId, 'manageShares');
$this->sharingFolderId = $folder->id;
$this->shareVisibility = $folder->visibility->value;
$this->collaboratorRoles = $folder->shares()
->get()
->mapWithKeys(fn (User $user): array => [$user->id => (string) $user->pivot->role])
->all();
$this->reset('inviteEmail', 'inviteRole');
$this->resetErrorBag('inviteEmail', 'inviteRole');
$this->modal('folder-share')->show();
}
public function closeShare(): void
{
$this->reset('sharingFolderId', 'inviteEmail', 'inviteRole', 'collaboratorRoles');
$this->modal('folder-share')->close();
}
public function updatedCollaboratorRoles(string $value, string $key): void
{
$this->updateCollaboratorRole((int) $key, $value);
}
public function updatedShareVisibility(string $value): void
{
$this->updateVisibility($value);
}
public function updateVisibility(string $visibility): void
{
$folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares');
$validated = validator(
['shareVisibility' => $visibility],
['shareVisibility' => ['required', Rule::enum(FolderVisibility::class)]],
)->validate();
// Personal workspaces hold a single member, so there is nothing to
// share a folder with. Only ever move such a folder back to private.
if ($validated['shareVisibility'] === FolderVisibility::Workspace->value
&& $folder->organization?->isPersonal()) {
return;
}
$folder->update(['visibility' => $validated['shareVisibility']]);
unset($this->sharingFolder);
Flux::toast(variant: 'success', text: __('Folder sharing updated.'));
}
public function inviteCollaborator(): void
{
$folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares');
$validated = $this->validate([
'inviteEmail' => ['required', 'string', 'email', 'max:255'],
'inviteRole' => ['required', Rule::enum(FolderRole::class)],
]);
$email = Str::lower($validated['inviteEmail']);
if ($folder->shares()->where('users.email', $email)->exists()) {
$this->addError('inviteEmail', __('That person already has access to this folder.'));
return;
}
if (Str::lower((string) $folder->user?->email) === $email) {
$this->addError('inviteEmail', __('That person already owns this folder.'));
return;
}
$invitation = Invitation::updateOrCreate(
[
'inviteable_type' => Folder::class,
'inviteable_id' => $folder->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->sharingInvitations);
Flux::toast(variant: 'success', text: __('Invitation sent to :email.', ['email' => $email]));
}
public function updateCollaboratorRole(int $userId, string $role): void
{
$folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares');
if ($userId === $folder->user_id) {
return;
}
$validated = validator(
['role' => $role],
['role' => ['required', Rule::enum(FolderRole::class)]],
)->validate();
$folder->shares()->updateExistingPivot($userId, ['role' => $validated['role']]);
unset($this->sharingCollaborators);
Flux::toast(variant: 'success', text: __('Collaborator role updated.'));
}
public function removeCollaborator(int $userId): void
{
$folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares');
if ($userId === $folder->user_id) {
return;
}
$folder->shares()->detach($userId);
unset($this->sharingCollaborators);
Flux::toast(variant: 'success', text: __('Collaborator removed.'));
}
public function revokeFolderInvitation(int $invitationId): void
{
$folder = $this->folderForAction($this->sharingFolderId ?? 0, 'manageShares');
$invitation = $folder->invitations()->whereKey($invitationId)->firstOrFail();
abort_unless(auth()->user()->can('delete', $invitation), 403);
$invitation->delete();
unset($this->sharingInvitations);
Flux::toast(variant: 'success', text: __('Invitation revoked.'));
}
public function moveTodoToFolder(int $todoId, int $folderId): void
{
$todo = Todo::query()->forUser(auth()->id())->findOrFail($todoId);
$todo = Todo::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->with('folder')
->findOrFail($todoId);
$folder = Folder::query()->forUser(auth()->id())->findOrFail($folderId);
abort_unless(auth()->user()->can('update', $todo), 403);
$folder = $this->visibleFolder($folderId);
abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403);
$todo->update(['folder_id' => $folder->id]);
}
public function moveFolderToTopLevel(int $folderId): void
{
$folder = $this->folderForUser($folderId);
$folder = $this->folderForAction($folderId, 'update');
$hasSiblingConflict = Folder::query()
->forUser(auth()->id())
->where('organization_id', $folder->organization_id)
->whereNull('parent_id')
->whereKeyNot($folder->id)
->where('name', $folder->name)
@@ -214,8 +401,8 @@ new #[Title('Folders')] class extends Component
public function moveFolderToFolder(int $folderId, int $targetFolderId): void
{
$folder = $this->folderForUser($folderId);
$target = $this->folderForUser($targetFolderId);
$folder = $this->folderForAction($folderId, 'update');
$target = $this->folderForAction($targetFolderId, 'createChild');
if ($folder->id === $target->id || $this->isDescendantOf($target, $folder)) {
return;
@@ -251,7 +438,7 @@ new #[Title('Folders')] class extends Component
public function todosForFolder(int $folderId): Collection
{
return Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->where('folder_id', $folderId)
->active()
->priority()
@@ -260,7 +447,7 @@ new #[Title('Folders')] class extends Component
public function startTodoCreate(int $folderId): void
{
$this->folderForUser($folderId);
$this->folderForTodoCreate($folderId);
$this->todoFolderId = $folderId;
$this->reset('todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes');
@@ -270,7 +457,7 @@ new #[Title('Folders')] class extends Component
public function saveFolderTodo(): void
{
$folder = $this->folderForUser($this->todoFolderId ?? 0);
$folder = $this->folderForTodoCreate($this->todoFolderId ?? 0);
$validated = $this->validate([
'todoDescription' => ['required', 'string', 'max:255'],
@@ -286,7 +473,7 @@ new #[Title('Folders')] class extends Component
'status' => TodoStatus::from($validated['todoStatus']),
'due_date' => filled($validated['todoDueDate']) ? Carbon::parse($validated['todoDueDate']) : null,
'notes' => filled($validated['todoNotes']) ? $validated['todoNotes'] : null,
'position' => Todo::nextPositionFor(auth()->user()),
'position' => Todo::nextPositionFor($folder),
]);
$this->reset('showTodoForm', 'todoFolderId', 'todoDescription', 'todoStatus', 'todoDueDate', 'todoNotes');
@@ -295,7 +482,7 @@ new #[Title('Folders')] class extends Component
#[Computed]
public function folderTree(): Collection
{
return Folder::treeForUser(auth()->id());
return Folder::treeForVisibleTo(auth()->id(), $this->activeWorkspaceId());
}
#[Computed]
@@ -305,7 +492,10 @@ new #[Title('Folders')] class extends Component
return null;
}
return Folder::query()->with('parent')->forUser(auth()->id())->find($this->todoFolderId);
return Folder::query()
->with('parent')
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->find($this->todoFolderId);
}
#[Computed]
@@ -324,12 +514,43 @@ new #[Title('Folders')] class extends Component
public function openCounts(): Collection
{
return Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->active()
->pluck('folder_id')
->countBy();
}
#[Computed]
public function sharingFolder(): ?Folder
{
if ($this->sharingFolderId === null) {
return null;
}
return Folder::query()
->with('organization')
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->find($this->sharingFolderId);
}
/**
* @return Collection<int, User>
*/
#[Computed]
public function sharingCollaborators(): Collection
{
return $this->sharingFolder?->shares()->orderBy('name')->get() ?? collect();
}
/**
* @return Collection<int, Invitation>
*/
#[Computed]
public function sharingInvitations(): Collection
{
return $this->sharingFolder?->invitations()->pending()->orderBy('email')->get() ?? collect();
}
/**
* @return array<int, mixed>
*/
@@ -341,7 +562,7 @@ new #[Title('Folders')] class extends Component
/**
* @return array<int, mixed>
*/
private function folderNameRules(?int $parentId = null, ?int $ignoreId = null): array
private function folderNameRules(int $organizationId, ?int $parentId = null, ?int $ignoreId = null): array
{
return [
'required',
@@ -350,14 +571,39 @@ new #[Title('Folders')] class extends Component
Rule::unique('folders', 'name')
->ignore($ignoreId)
->where(fn ($query) => $query
->where('user_id', auth()->id())
->where('organization_id', $organizationId)
->where('parent_id', $parentId)),
];
}
private function folderForUser(int $folderId): Folder
private function activeWorkspaceId(): int
{
return Folder::query()->forUser(auth()->id())->findOrFail($folderId);
return (int) auth()->user()->current_organization_id;
}
private function visibleFolder(int $folderId): Folder
{
return Folder::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->findOrFail($folderId);
}
private function folderForAction(int $folderId, string $ability): Folder
{
$folder = $this->visibleFolder($folderId);
abort_unless(auth()->user()->can($ability, $folder), 403);
return $folder;
}
private function folderForTodoCreate(int $folderId): Folder
{
$folder = $this->visibleFolder($folderId);
abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403);
return $folder;
}
}; ?>
@@ -465,22 +711,36 @@ new #[Title('Folders')] class extends Component
</form>
@else
<div class="flex min-w-0 items-center gap-2">
<div
x-data="{ dragging: false }"
draggable="true"
class="inline-flex shrink-0 cursor-grab text-zinc-400 dark:text-zinc-500"
:class="dragging && 'opacity-40'"
@dragstart="dragging = true; window.__mtFolderDrag = { folderId: {{ $node['folder']->id }} }; $dispatch('folder-drag-start')"
@dragend="dragging = false; window.__mtFolderDrag = null; $dispatch('folder-drag-end')"
title="{{ __('Drag to move this folder') }}"
data-test="drag-folder-{{ $node['folder']->id }}"
>
<flux:icon.chevrons-up-down variant="micro" />
</div>
@can('update', $node['folder'])
<div
x-data="{ dragging: false }"
draggable="true"
class="inline-flex shrink-0 cursor-grab text-zinc-400 dark:text-zinc-500"
:class="dragging && 'opacity-40'"
@dragstart="dragging = true; window.__mtFolderDrag = { folderId: {{ $node['folder']->id }} }; $dispatch('folder-drag-start')"
@dragend="dragging = false; window.__mtFolderDrag = null; $dispatch('folder-drag-end')"
title="{{ __('Drag to move this folder') }}"
data-test="drag-folder-{{ $node['folder']->id }}"
>
<flux:icon.chevrons-up-down variant="micro" />
</div>
@endcan
<x-folder-dot :color="$node['folder']->color" data-test="folder-dot-{{ $node['folder']->id }}" />
<flux:heading size="sm" class="truncate" data-test="folder-name-{{ $node['folder']->id }}">
{{ $node['folder']->name }}
</flux:heading>
<span
class="truncate text-xs text-zinc-400 dark:text-zinc-500"
title="{{ __('Workspace') }}"
data-test="folder-workspace-{{ $node['folder']->id }}"
>
{{ $node['folder']->organization?->name }}
</span>
@if ($node['folder']->visibility === \App\FolderVisibility::Workspace || $node['folder']->shares_count > 0)
<flux:badge size="sm" color="zinc" icon="users" data-test="folder-shared-{{ $node['folder']->id }}">
{{ __('Shared') }}
</flux:badge>
@endif
</div>
<flux:badge
@@ -500,15 +760,28 @@ new #[Title('Folders')] class extends Component
<flux:spacer />
<div class="flex items-center gap-1">
<flux:tooltip :content="__('Choose color')">
<flux:button
size="sm"
variant="ghost"
icon="swatch"
wire:click="startColorEdit({{ $node['folder']->id }})"
data-test="color-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@can('manageShares', $node['folder'])
<flux:tooltip :content="__('Share')">
<flux:button
size="sm"
variant="ghost"
icon="user-plus"
wire:click="startShare({{ $node['folder']->id }})"
data-test="share-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@endcan
@can('update', $node['folder'])
<flux:tooltip :content="__('Choose color')">
<flux:button
size="sm"
variant="ghost"
icon="swatch"
wire:click="startColorEdit({{ $node['folder']->id }})"
data-test="color-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@endcan
<flux:tooltip :content="__('View todos')">
<flux:button
size="sm"
@@ -519,42 +792,48 @@ new #[Title('Folders')] class extends Component
data-test="view-todos-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Add todo')">
<flux:button
size="sm"
variant="ghost"
icon="document-plus"
wire:click="startTodoCreate({{ $node['folder']->id }})"
data-test="add-todo-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Add sub-folder')">
<flux:button
size="sm"
variant="ghost"
icon="folder-plus"
wire:click="startChildCreate({{ $node['folder']->id }})"
data-test="add-child-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Rename')">
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
wire:click="startRename({{ $node['folder']->id }})"
data-test="rename-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Delete')">
<flux:button
size="sm"
variant="ghost"
icon="trash"
wire:click="confirmFolderDelete({{ $node['folder']->id }})"
data-test="delete-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@can('create', [App\Models\Todo::class, $node['folder']])
<flux:tooltip :content="__('Add todo')">
<flux:button
size="sm"
variant="ghost"
icon="document-plus"
wire:click="startTodoCreate({{ $node['folder']->id }})"
data-test="add-todo-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@endcan
@can('createChild', $node['folder'])
<flux:tooltip :content="__('Add sub-folder')">
<flux:button
size="sm"
variant="ghost"
icon="folder-plus"
wire:click="startChildCreate({{ $node['folder']->id }})"
data-test="add-child-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@endcan
@can('update', $node['folder'])
<flux:tooltip :content="__('Rename')">
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
wire:click="startRename({{ $node['folder']->id }})"
data-test="rename-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
<flux:tooltip :content="__('Delete')">
<flux:button
size="sm"
variant="ghost"
icon="trash"
wire:click="confirmFolderDelete({{ $node['folder']->id }})"
data-test="delete-folder-{{ $node['folder']->id }}"
/>
</flux:tooltip>
@endcan
</div>
@endif
</div>
@@ -739,4 +1018,129 @@ new #[Title('Folders')] class extends Component
</div>
</div>
</flux:modal>
<flux:modal name="folder-share" class="md:w-2xl">
@if ($this->sharingFolder !== null)
<div class="space-y-6" data-test="folder-share-panel">
<div>
<flux:heading size="lg">{{ __('Share folder') }}</flux:heading>
<flux:text class="mt-2">
{{ __('Manage who can access ":name" and what they can do.', ['name' => $this->sharingFolder->name]) }}
</flux:text>
</div>
@if ($this->sharingFolder->organization !== null && ! $this->sharingFolder->organization->isPersonal())
<flux:field>
<flux:label>{{ __('Visibility') }}</flux:label>
<flux:select wire:model.live="shareVisibility" data-test="visibility-select">
@foreach (FolderVisibility::cases() as $visibility)
<flux:select.option value="{{ $visibility->value }}">{{ $visibility->label() }}</flux:select.option>
@endforeach
</flux:select>
<flux:error name="shareVisibility" />
<flux:description>
{{ __('Workspace folders are visible to every member of :name.', ['name' => $this->sharingFolder->organization->name]) }}
</flux:description>
</flux:field>
@else
<flux:callout icon="lock-closed" data-test="personal-share-notice">
{{ __('This folder is only visible to you. Personal workspaces cannot hold other members.') }}
</flux:callout>
@endif
<div class="space-y-3">
<flux:heading size="sm">{{ __('Collaborators') }}</flux:heading>
<div class="flex items-center justify-between gap-3" data-test="folder-owner-row">
<flux:text>{{ $this->sharingFolder->user?->name }}</flux:text>
<flux:badge size="sm" color="blue">{{ __('Owner') }}</flux:badge>
</div>
@forelse ($this->sharingCollaborators as $collaborator)
<div class="flex flex-wrap items-center justify-between gap-3" data-test="collaborator-row-{{ $collaborator->id }}">
<div class="flex min-w-0 items-center gap-2">
<flux:text class="truncate">{{ $collaborator->name }}</flux:text>
<flux:text size="sm" class="truncate text-zinc-500 dark:text-zinc-400">{{ $collaborator->email }}</flux:text>
</div>
<div class="flex items-center gap-2">
<flux:select
size="sm"
wire:model.live="collaboratorRoles.{{ $collaborator->id }}"
data-test="collaborator-role-{{ $collaborator->id }}"
>
@foreach (FolderRole::assignable() as $role)
<flux:select.option value="{{ $role->value }}">{{ $role->label() }}</flux:select.option>
@endforeach
</flux:select>
<flux:button
size="sm"
variant="ghost"
icon="trash"
wire:click="removeCollaborator({{ $collaborator->id }})"
data-test="remove-collaborator-{{ $collaborator->id }}"
/>
</div>
</div>
@empty
<flux:text size="sm" class="text-zinc-500 dark:text-zinc-400">{{ __('No one else has access yet.') }}</flux:text>
@endforelse
</div>
<form wire:submit="inviteCollaborator" class="space-y-3">
<flux:heading size="sm">{{ __('Invite by email') }}</flux:heading>
<div class="flex flex-wrap items-start gap-3">
<div class="min-w-56 flex-1">
<flux:input
wire:model="inviteEmail"
type="email"
placeholder="name@example.com"
data-test="invite-email-input"
/>
<flux:error name="inviteEmail" />
</div>
<div>
<flux:select wire:model="inviteRole" data-test="invite-role-select">
@foreach (FolderRole::assignable() as $role)
<flux:select.option value="{{ $role->value }}">{{ $role->label() }}</flux:select.option>
@endforeach
</flux:select>
<flux:error name="inviteRole" />
</div>
<flux:button type="submit" variant="primary" icon="paper-airplane" data-test="invite-collaborator-button">
{{ __('Send invite') }}
</flux:button>
</div>
</form>
@if ($this->sharingInvitations->isNotEmpty())
<div class="space-y-3">
<flux:heading size="sm">{{ __('Pending invitations') }}</flux:heading>
@foreach ($this->sharingInvitations as $invitation)
<div class="flex flex-wrap items-center justify-between gap-3" data-test="invitation-row-{{ $invitation->id }}">
<div class="flex min-w-0 items-center gap-2">
<flux:text class="truncate">{{ $invitation->email }}</flux:text>
<flux:badge size="sm">{{ $invitation->roleLabel() }}</flux:badge>
</div>
<flux:button
size="sm"
variant="ghost"
icon="x-mark"
wire:click="revokeFolderInvitation({{ $invitation->id }})"
data-test="revoke-invitation-{{ $invitation->id }}"
>
{{ __('Revoke') }}
</flux:button>
</div>
@endforeach
</div>
@endif
<div class="flex justify-end gap-3">
<flux:button variant="ghost" wire:click="closeShare" data-test="close-share-button">
{{ __('Done') }}
</flux:button>
</div>
</div>
@endif
</flux:modal>
</div>
+83 -56
View File
@@ -52,7 +52,7 @@ new #[Title('Todos')] class extends Component
$folderId = (int) $folderId;
if (Folder::query()->forUser(auth()->id())->whereKey($folderId)->exists()) {
if (Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($folderId)->exists()) {
$this->folderId = $folderId;
}
}
@@ -90,7 +90,7 @@ new #[Title('Todos')] class extends Component
public function openEditForm(int $todoId): void
{
$todo = $this->todoForUser($todoId);
$todo = $this->todoForAction($todoId, 'update');
$this->editingTodoId = $todo->id;
$this->description = $todo->description;
@@ -110,11 +110,20 @@ new #[Title('Todos')] class extends Component
'todoStatus' => ['required', Rule::enum(TodoStatus::class)],
'dueDate' => ['nullable', 'date'],
'notes' => ['nullable', 'string', 'max:'.auth()->user()->notesLimit()],
'formFolderId' => ['required', 'integer', Rule::exists('folders', 'id')->where('user_id', auth()->id())],
'formFolderId' => ['required', 'integer', Rule::exists('folders', 'id')->whereIn(
'id',
Folder::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->select('folders.id')
)],
]);
$folder = Folder::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->findOrFail($validated['formFolderId']);
abort_unless(auth()->user()->can('create', [Todo::class, $folder]), 403);
$data = [
'folder_id' => $validated['formFolderId'],
'folder_id' => $folder->id,
'description' => $validated['description'],
'status' => TodoStatus::from($validated['todoStatus']),
'due_date' => filled($validated['dueDate']) ? Carbon::parse($validated['dueDate']) : null,
@@ -122,11 +131,11 @@ new #[Title('Todos')] class extends Component
];
if ($this->editingTodoId !== null) {
$this->todoForUser($this->editingTodoId)->update($data);
$this->todoForAction($this->editingTodoId, 'update')->update($data);
} else {
Todo::create($data + [
'user_id' => auth()->id(),
'position' => Todo::nextPositionFor(auth()->user()),
'position' => Todo::nextPositionFor($folder),
]);
}
@@ -135,7 +144,7 @@ new #[Title('Todos')] class extends Component
public function toggleComplete(int $todoId): void
{
$todo = $this->todoForUser($todoId);
$todo = $this->todoForAction($todoId, 'markComplete');
$data = ['completed_at' => $todo->completed_at === null ? now() : null];
@@ -150,7 +159,7 @@ new #[Title('Todos')] class extends Component
public function confirmTodoDelete(int $todoId): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'delete');
$this->confirmingTodoDelete = $todoId;
$this->modal('confirm-todo-delete')->show();
@@ -158,7 +167,7 @@ new #[Title('Todos')] class extends Component
public function deleteTodo(): void
{
$this->todoForUser($this->confirmingTodoDelete)->delete();
$this->todoForAction($this->confirmingTodoDelete ?? 0, 'delete')->delete();
$this->confirmingTodoDelete = null;
$this->modal('confirm-todo-delete')->close();
@@ -181,7 +190,7 @@ new #[Title('Todos')] class extends Component
#[Computed]
public function folderTree(): Collection
{
return Folder::treeForUser(auth()->id());
return Folder::treeForVisibleTo(auth()->id(), $this->activeWorkspaceId());
}
#[Computed]
@@ -243,7 +252,7 @@ new #[Title('Todos')] class extends Component
public function moveTodo(int $todoId, string $direction): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'update');
$ids = $this->orderedTodoIds;
$currentIndex = array_search($todoId, $ids, true);
@@ -263,7 +272,7 @@ new #[Title('Todos')] class extends Component
public function handleSort(int $todoId, int $position): void
{
$this->todoForUser($todoId);
$this->todoForAction($todoId, 'update');
$this->reorderTodo($todoId, $position);
}
@@ -294,7 +303,7 @@ new #[Title('Todos')] class extends Component
unset($visibleOrder[$currentIndex]);
array_splice($visibleOrder, $newIndex, 0, $todoId);
$ids = Todo::query()->forUser(auth()->id())->priority()->pluck('id')->all();
$ids = Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->priority()->pluck('id')->all();
$slots = array_keys(array_intersect($ids, $visibleIds));
@@ -303,7 +312,7 @@ new #[Title('Todos')] class extends Component
}
foreach ($ids as $position => $id) {
Todo::query()->forUser(auth()->id())->whereKey($id)->update(['position' => $position]);
Todo::query()->visibleTo(auth()->id(), $this->activeWorkspaceId())->whereKey($id)->update(['position' => $position]);
}
unset($this->todos, $this->orderedTodoIds);
@@ -315,7 +324,7 @@ new #[Title('Todos')] class extends Component
private function filteredTodoQuery(): Builder
{
return Todo::query()
->forUser(auth()->id())
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->when(
$this->search !== '',
fn (Builder $query) => $query->where('description', 'like', '%'.$this->search.'%')
@@ -346,9 +355,21 @@ new #[Title('Todos')] class extends Component
return $ids;
}
private function todoForUser(int $todoId): Todo
private function todoForAction(int $todoId, string $ability): Todo
{
return Todo::query()->forUser(auth()->id())->findOrFail($todoId);
$todo = Todo::query()
->visibleTo(auth()->id(), $this->activeWorkspaceId())
->with('folder')
->findOrFail($todoId);
abort_unless(auth()->user()->can($ability, $todo), 403);
return $todo;
}
private function activeWorkspaceId(): int
{
return (int) auth()->user()->current_organization_id;
}
}; ?>
@@ -424,18 +445,22 @@ new #[Title('Todos')] class extends Component
@forelse ($this->todos as $todo)
<flux:table.row :key="$todo->id" wire:sort:item="{{ $todo->id }}" data-test="todo-row">
<flux:table.cell class="py-0 text-zinc-300 dark:text-zinc-600">
<div wire:sort:handle class="inline-flex cursor-grab" title="{{ __('Reorder') }}">
<flux:icon.chevrons-up-down variant="micro" />
</div>
@can('update', $todo)
<div wire:sort:handle class="inline-flex cursor-grab" title="{{ __('Reorder') }}">
<flux:icon.chevrons-up-down variant="micro" />
</div>
@endcan
</flux:table.cell>
<flux:table.cell class="py-0">
<flux:checkbox
:checked="$todo->completed_at !== null"
:title="$todo->completed_at ? __('Mark as not done') : __('Mark as done')"
wire:click="toggleComplete({{ $todo->id }})"
data-test="toggle-complete-{{ $todo->id }}"
/>
@can('markComplete', $todo)
<flux:checkbox
:checked="$todo->completed_at !== null"
:title="$todo->completed_at ? __('Mark as not done') : __('Mark as done')"
wire:click="toggleComplete({{ $todo->id }})"
data-test="toggle-complete-{{ $todo->id }}"
/>
@endcan
</flux:table.cell>
<flux:table.cell :variant="$todo->completed_at ? 'strong' : 'default'" data-test="todo-description-{{ $todo->id }}">
@@ -491,44 +516,46 @@ new #[Title('Todos')] class extends Component
<flux:table.cell class="py-0">
<div class="flex justify-end gap-1">
@if ($todo->id !== ($this->orderedTodoIds[0] ?? null))
@can('update', $todo)
@if ($todo->id !== ($this->orderedTodoIds[0] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-up"
:title="__('Move up')"
wire:click="moveTodo({{ $todo->id }}, 'up')"
data-test="move-todo-up-{{ $todo->id }}"
/>
@endif
@if ($todo->id !== ($this->orderedTodoIds[array_key_last($this->orderedTodoIds)] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-down"
:title="__('Move down')"
wire:click="moveTodo({{ $todo->id }}, 'down')"
data-test="move-todo-down-{{ $todo->id }}"
/>
@endif
<flux:button
size="sm"
variant="ghost"
icon="chevron-up"
:title="__('Move up')"
wire:click="moveTodo({{ $todo->id }}, 'up')"
data-test="move-todo-up-{{ $todo->id }}"
icon="pencil-square"
:title="__('Edit todo')"
wire:click="openEditForm({{ $todo->id }})"
data-test="edit-todo-{{ $todo->id }}"
/>
@endif
@if ($todo->id !== ($this->orderedTodoIds[array_key_last($this->orderedTodoIds)] ?? null))
<flux:button
size="sm"
variant="ghost"
icon="chevron-down"
:title="__('Move down')"
wire:click="moveTodo({{ $todo->id }}, 'down')"
data-test="move-todo-down-{{ $todo->id }}"
icon="trash"
:title="__('Delete todo')"
wire:click="confirmTodoDelete({{ $todo->id }})"
data-test="delete-todo-{{ $todo->id }}"
/>
@endif
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
:title="__('Edit todo')"
wire:click="openEditForm({{ $todo->id }})"
data-test="edit-todo-{{ $todo->id }}"
/>
<flux:button
size="sm"
variant="ghost"
icon="trash"
:title="__('Delete todo')"
wire:click="confirmTodoDelete({{ $todo->id }})"
data-test="delete-todo-{{ $todo->id }}"
/>
@endcan
</div>
</flux:table.cell>
</flux:table.row>