@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\OrganizationRole;
|
||||
|
||||
class FolderPolicy
|
||||
{
|
||||
/**
|
||||
* Anyone with a role anywhere on the folder's ancestry may see it.
|
||||
*/
|
||||
public function view(User $user, Folder $folder): bool
|
||||
{
|
||||
return $folder->effectiveRoleFor($user) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only workspace admins create folders at the top of a workspace.
|
||||
*/
|
||||
public function create(User $user, Organization $organization): bool
|
||||
{
|
||||
$role = $this->organizationRoleFor($user, $organization);
|
||||
|
||||
return $role !== null && $role->canManageWorkspace();
|
||||
}
|
||||
|
||||
/**
|
||||
* Folder admins create folders beneath an existing folder.
|
||||
*/
|
||||
public function createChild(User $user, Folder $folder): bool
|
||||
{
|
||||
$role = $folder->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canManage();
|
||||
}
|
||||
|
||||
public function update(User $user, Folder $folder): bool
|
||||
{
|
||||
$role = $folder->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canManage();
|
||||
}
|
||||
|
||||
public function delete(User $user, Folder $folder): bool
|
||||
{
|
||||
$role = $folder->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canManage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Only folder admins invite collaborators and change visibility.
|
||||
*/
|
||||
public function manageShares(User $user, Folder $folder): bool
|
||||
{
|
||||
$role = $folder->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canManage();
|
||||
}
|
||||
|
||||
private function organizationRoleFor(User $user, Organization $organization): ?OrganizationRole
|
||||
{
|
||||
return OrganizationRole::tryFrom((string) $organization->users()
|
||||
->where('users.id', $user->id)
|
||||
->withPivot('role')
|
||||
->first()?->pivot->role);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Invitation;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
|
||||
class InvitationPolicy
|
||||
{
|
||||
/**
|
||||
* Inviting people into a folder or workspace is gated by the inviteable's
|
||||
* own policies.
|
||||
*/
|
||||
public function create(User $user, Organization|Folder $inviteable): bool
|
||||
{
|
||||
if ($inviteable instanceof Folder) {
|
||||
return $user->can('manageShares', $inviteable);
|
||||
}
|
||||
|
||||
return $user->can('manageMembers', $inviteable);
|
||||
}
|
||||
|
||||
/**
|
||||
* The invitation may be revoked by whoever sent it or by an admin of the
|
||||
* thing it invites into.
|
||||
*/
|
||||
public function delete(User $user, Invitation $invitation): bool
|
||||
{
|
||||
return $user->id === $invitation->inviter_id
|
||||
|| $user->can('delete', $invitation->inviteable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\OrganizationRole;
|
||||
|
||||
class OrganizationPolicy
|
||||
{
|
||||
public function view(User $user, Organization $organization): bool
|
||||
{
|
||||
return $this->roleFor($user, $organization) !== null;
|
||||
}
|
||||
|
||||
public function update(User $user, Organization $organization): bool
|
||||
{
|
||||
return $this->canManage($user, $organization);
|
||||
}
|
||||
|
||||
public function manageMembers(User $user, Organization $organization): bool
|
||||
{
|
||||
return $this->canManage($user, $organization);
|
||||
}
|
||||
|
||||
public function manageBilling(User $user, Organization $organization): bool
|
||||
{
|
||||
return $this->canManage($user, $organization);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only the owner may dissolve a workspace.
|
||||
*/
|
||||
public function delete(User $user, Organization $organization): bool
|
||||
{
|
||||
return $this->roleFor($user, $organization) === OrganizationRole::Owner;
|
||||
}
|
||||
|
||||
private function roleFor(User $user, Organization $organization): ?OrganizationRole
|
||||
{
|
||||
return OrganizationRole::tryFrom((string) $organization->users()
|
||||
->where('users.id', $user->id)
|
||||
->withPivot('role')
|
||||
->first()?->pivot->role);
|
||||
}
|
||||
|
||||
private function canManage(User $user, Organization $organization): bool
|
||||
{
|
||||
return $user->isOrganizationAdmin($organization);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Folder;
|
||||
use App\Models\Todo;
|
||||
use App\Models\User;
|
||||
|
||||
class TodoPolicy
|
||||
{
|
||||
public function view(User $user, Todo $todo): bool
|
||||
{
|
||||
return $todo->folder?->effectiveRoleFor($user) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Workspace folders are shared with editors so they can add todos to them.
|
||||
*/
|
||||
public function create(User $user, Folder $folder): bool
|
||||
{
|
||||
$role = $folder->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canEditTodos();
|
||||
}
|
||||
|
||||
public function update(User $user, Todo $todo): bool
|
||||
{
|
||||
return $this->canEdit($user, $todo);
|
||||
}
|
||||
|
||||
public function delete(User $user, Todo $todo): bool
|
||||
{
|
||||
return $this->canEdit($user, $todo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Markers and above may flip a todo's completion state.
|
||||
*/
|
||||
public function markComplete(User $user, Todo $todo): bool
|
||||
{
|
||||
$role = $todo->folder?->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canMarkComplete();
|
||||
}
|
||||
|
||||
private function canEdit(User $user, Todo $todo): bool
|
||||
{
|
||||
$role = $todo->folder?->effectiveRoleFor($user);
|
||||
|
||||
return $role !== null && $role->canEditTodos();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user