Skip to content

HasAuthorizable ​

Namespace: Unusualify\Modularous\Entities\Traits\HasAuthorizable

Attaches a per-record Authorization morph record, enabling fine-grained ownership and access control on individual model instances. Integrates with Spatie Permissions for role-based access checks.


Boot Behavior ​

EventAction
savingIf authorized_id is set and valid, marks model as authorizing; clears fillable helpers
savedCreates/updates the Authorization record; touches updated_at if model wasn't otherwise dirty
deleting / forceDeletingDeletes the associated Authorization record

Relationships ​

php
public function authorizationRecord(): MorphOne     // → Authorization
public function authorizedUser(): HasOneThrough     // → User through Authorization

Computed Attributes ​

AttributeTypeDescription
is_authorizedbooltrue if an authorization record exists
authorization_record_existsboolPre-computed via withExists('authorizationRecord') global scope

Fillable Helpers ​

FieldDescription
authorized_idSet to a user ID to create/update the authorization on save
authorized_typeSet to a model class string to override the authorized model type

Scopes ​

ScopeDescription
scopeHasAuthorization($user)Records authorized for the given user (or current auth user)
scopeIsAuthorizedToYou($user)Records authorized specifically to the given user's ID
scopeIsAuthorizedToYourRole($user)Records authorized to any user sharing the current user's roles
scopeHasAnyAuthorization()Records with any non-null authorization
scopeUnauthorized()Records with no authorization record

Methods ​

MethodSignatureDescription
getAuthorizedModel(): stringReturns the authorized model class (from record or default)
getDefaultAuthorizedModel(): string (static)Returns the default model class (default: User::class, override via $defaultAuthorizedModel)
hasAuthorizationUsage(?mixed $user = null): boolReturns true if the current user has permission to manage authorizations on this record

Global Scopes ​

Registers authorization_record_exists via addGlobalScopesHasAuthorizable():

  • Adds withExists('authorizationRecord') so $model->is_authorized avoids a lazy-load.

Configuration ​

PropertyTypeDescription
$defaultAuthorizedModelstringModel class to use when no authorization record exists (default: User::class)
$authorizableRolesToCheckarraySpatie roles that bypass the authorization filter entirely

Usage ​

php
use Unusualify\Modularous\Entities\Traits\HasAuthorizable;

class Report extends Model
{
    use HasAuthorizable;
}

// Assign authorization
$report->authorized_id = $user->id;
$report->save();

// Read
$report->authorizationRecord;
$report->authorizedUser;
$report->is_authorized;           // true

// Query
Report::isAuthorizedToYou()->get();
Report::hasAuthorization()->get();
Report::unauthorized()->get();