Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

3.3 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Policies;

use App\Enums\Post\Status;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\Response;

class PostPolicy
{
    /**
     * Authorize viewing a post. The post must live in the user's current
     * workspace; cross-workspace lookups deny as 404 so we don't leak
     * existence across tenants.
     */
    public function view(User $user, Post $post): bool|Response
    {
        if ($this->isHidden($user, $post)) {
            return Response::denyAsNotFound();
        }

        return true;
    }

    /**
     * Authorize updating a post: tenancy guard (404 across tenants) then the
     * `createPost` gate (any member). Whether the change needs approval is
     * decided by PostApproval when it is saved.
     */
    public function update(User $user, Post $post): bool|Response
    {
        if ($this->isHidden($user, $post)) {
            return Response::denyAsNotFound();
        }

        return $user->can('createPost', $user->currentWorkspace);
    }

    /**
     * Authorize deleting a post: tenancy guard (404 across tenants), the
     * `createPost` gate, and a member who needs approval may delete only the
     * posts they wrote, or another member's request they made while it is still pending approval.
     */
    public function delete(User $user, Post $post): bool|Response
    {
        if ($this->isHidden($user, $post)) {
            return Response::denyAsNotFound();
        }

        if (! $user->can('createPost', $user->currentWorkspace)) {
            return false;
        }

        return ! $user->requiresApprovalIn($user->currentWorkspace)
            || $post->user_id === $user->id
            || ($post->status === Status::PendingApproval && $post->approval_requested_by === $user->id);
    }

    /**
     * Authorize duplicating a post into the user's current workspace as a
     * fresh draft. The post must live in the user's current workspace
     * (404 otherwise — tenancy guard, see `view()`) AND the user must
     * have permission to create posts there (403 otherwise).
     */
    public function duplicate(User $user, Post $post): bool|Response
    {
        if ($this->isHidden($user, $post)) {
            return Response::denyAsNotFound();
        }

        return $user->can('createPost', $user->currentWorkspace);
    }

    /**
     * Authorize approving or rejecting a pending post: tenancy guard (404 across
     * tenants) then the approver gate (members who publish directly).
     */
    public function approve(User $user, Post $post): bool|Response
    {
        if ($post->workspace_id !== $user->current_workspace_id) {
            return Response::denyAsNotFound();
        }

        return $user->can('approvePosts', $user->currentWorkspace);
    }

    /**
     * A post outside the user's current workspace, or another member's pending
     * request seen by someone who cannot approve it, is hidden as not found.
     */
    private function isHidden(User $user, Post $post): bool
    {
        if ($post->workspace_id !== $user->current_workspace_id) {
            return true;
        }

        if ($post->status !== Status::PendingApproval || $user->can('approvePosts', $user->currentWorkspace)) {
            return false;
        }

        return $post->approvalRequester()?->id !== $user->id;
    }
}