Siren

Roles & Permissions

How current_user_can maps to Siren's action-based permission system and auth middleware.

WordPress plugin

Last updated: October 26, 2026

In WordPress, you check permissions with current_user_can('capability_name'). Siren uses an action-based permission system where you check whether a user can perform a specific action (Create, Read, Update, Delete) on a specific model class.

// WordPress: string-based capability checks
if (current_user_can('manage_options')) {
    // User is an admin
}

if (current_user_can('edit_post', $post_id)) {
    // User can edit this specific post
}
// Siren outside a REST request: resolve the current user and check actions
use PHPNomad\Auth\Interfaces\CurrentUserResolverStrategy;
use PHPNomad\Auth\Models\Action;
use Siren\Programs\Core\Models\Program;
use PHPNomad\Auth\Enums\ActionTypes;

class MyService
{
    protected CurrentUserResolverStrategy $userResolver;

    public function __construct(CurrentUserResolverStrategy $userResolver)
    {
        $this->userResolver = $userResolver;
    }

    public function checkAccess(): void
    {
        $user = $this->userResolver->getCurrentUser();

        if ($user->canDoAction(new Action(ActionTypes::Update, Program::class))) {
            // User can update programs
        }

        if ($user->canDoAction(new Action(ActionTypes::Read, Program::class))) {
            // User can read programs
        }
    }
}
// Siren inside a REST controller: user is on the request, middleware handles auth

use PHPNomad\Auth\Enums\ActionTypes;
use PHPNomad\Auth\Models\Action;
use PHPNomad\Auth\Traits\CanCreateAuthMiddlewareFromCurrentContext;
use PHPNomad\Http\Interfaces\Request;
use PHPNomad\Http\Interfaces\Response;
use PHPNomad\Rest\Exceptions\AuthorizationException;
use PHPNomad\Rest\Interfaces\Controller;
use PHPNomad\Rest\Interfaces\HasMiddleware;
use Siren\Programs\Core\Models\Program;

class UpdateProgramController implements Controller, HasMiddleware
{
    use CanCreateAuthMiddlewareFromCurrentContext;

    // The constructor must inject CurrentContextResolverStrategy $currentContextResolver
    // and AuthPolicyEvaluatorService $authPolicyEvaluatorService for the trait.
    // getEndpoint() and getMethod() are left out.

    public function getMiddleware(Request $request): array
    {
        return [
            // Middleware checks auth before getResponse() runs
            // Answers 400 if not signed in or not authorized
            $this->createAuthMiddlewareFromCurrentContext(ActionTypes::Update, Program::class),
        ];
    }

    public function getResponse(Request $request): Response
    {
        // If you reach here, the user is authenticated and authorized
        $user = $request->getUser();

        // Additional checks if needed
        if (!$user->canDoAction(new Action(ActionTypes::Delete, Program::class))) {
            // Answers 403 in Siren's {"error": {"message", "context"}} shape
            throw new AuthorizationException('Cannot delete programs', [], 403);
        }

        // ... handle the request
    }
}

How does the action system work?

Siren’s permission model is built on two concepts: action types and model classes.

ActionTypes is an enum with four values:

ActionTypeEquivalent WordPress capability pattern
ActionTypes::Createcreate_posts, create_users
ActionTypes::Readread_post, list_users
ActionTypes::Updateedit_post, edit_users
ActionTypes::Deletedelete_post, delete_users

The model class specifies what the action applies to. Instead of WordPress’s string-based capability names like 'edit_post', Siren uses new Action(ActionTypes::Update, Program::class). This is type-safe. Your IDE catches typos and you can refactor model class names without breaking permission checks.

Under the hood, Siren maps these action/model pairs to WordPress capability strings. An action like new Action(ActionTypes::Read, Program::class) resolves to a capability string like siren_action__read__Siren\Programs\Core\Models\Program. WordPress’s role system stores and checks these capabilities, so Siren’s permission model layers on top of WordPress rather than replacing it.

Program::class is one of many permissioned models. The same pattern applies to every model in Siren’s managed set, including the Plus-tier Siren\Plus\Core\Groups\Models\CollaboratorGroup. So new Action(ActionTypes::Update, CollaboratorGroup::class) is a real capability check, the same as it is for programs. Full Create, Read, Update, and Delete on CollaboratorGroup is granted to the administrator and siren_platform_manager roles. Other siren_* roles do not hold model-level capabilities on CollaboratorGroup today. If you build a custom collaborator-group admin surface or REST controller, gate it on these actions and expect only administrators and platform managers to pass. For the full list of permissioned models, see Enums & Constants.

How do REST controllers handle auth?

Inside REST controllers, you typically do not check permissions manually. The createAuthMiddlewareFromCurrentContext() middleware handles authentication and authorization before your getResponse() method runs:

  1. Authentication verifies the user has a valid auth token.
  2. Authorization checks that the authenticated user has the required capabilities for the controller’s action.

If either check fails, the request answers 400 with “You don’t have permission to do that.” This middleware can’t take another status. On WordPress, a 401 or 403 thrown by middleware becomes WordPress’s {code, message, data} error, with rest_not_logged_in or rest_forbidden. To refuse that way, write your own middleware that throws with the status, as Error Handling shows. The same exception thrown from getResponse() answers with its status in Siren’s {"error": {"message", "context"}} shape.

By the time your handler executes, the user is guaranteed to be authenticated and authorized. Use $request->getUser() to access the authenticated user object for any additional logic.

When do I need manual permission checks?

Manual canDoAction() checks are useful in two cases:

For conditional logic within a handler, the middleware grants access to the endpoint, but you may need finer-grained checks inside:

public function handle(Request $request): Response
{
    $user = $request->getUser();

    // The user can access this endpoint, but can they update THIS program?
    if (!$user->canDoAction(new Action(ActionTypes::Update, Program::class))) {
        // Show read-only view instead
    }
}

For code outside REST controllers, listeners, services, and admin hooks do not have middleware. Use the CurrentUserResolverStrategy to get the current user and check permissions directly.

Where can I learn more?

For the full list of action types, model classes, and the generated capability strings, see Enums & Constants.

For the full PHPNomad framework documentation on auth, see Auth.

Your privacy choices

Account, checkout and chat you request work without optional tracking. Choose whether to allow these other uses on this website.

You can change or withdraw your choices at any time using “Privacy choices.” We remember your choice for 180 days. Read our privacy policy.