PHP code example of patrickhanna / laravel-warden

1. Go to this page and download the library: Download patrickhanna/laravel-warden library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

patrickhanna / laravel-warden example snippets


use Illuminate\Database\Eloquent\Model;
use Warrant\HasWarrantSchema;

class Timesheet extends Model
{
    use HasWarrantSchema;

    public static function warrantSchema(): string
    {
        return \App\Warrant\TimesheetSchema::class;
    }
}

namespace App\Warrant;

use App\Models\Timesheet;
use Illuminate\Contracts\Database\Query\Builder;
use Warrant\Schema\Ability;
use Warrant\Schema\GlobalCondition;
use Warrant\Schema\Conditions\GlobalConditionContext;
use Warrant\Schema\Conditions\RowConditionContext;
use Warrant\Schema\WarrantSchema;
use Warrant\Schema\RowCondition;

class TimesheetSchema extends WarrantSchema
{
    public const model = Timesheet::class;

    #[Ability] public const VIEW    = 'view';
    #[Ability] public const UPDATE  = 'update';
    #[Ability] public const DELETE  = 'delete';
    #[Ability] public const APPROVE = 'approve';

    // Row: narrows WHICH timesheet rows the user matches.
    #[RowCondition]
    public function isSelf(RowConditionContext $c): Builder
    {
        return $c->query->whereRaw('timesheets.user_id = ?', [$c->user->getAuthIdentifier()]);
    }

    // DSL arguments are declared as parameters after the context object:
    // the context is always first, then parameter #2 binds argument[0],
    // #3 binds argument[1], and so on. has_status('approved') -> $status.
    #[RowCondition]
    public function hasStatus(RowConditionContext $c, string $status): Builder
    {
        return $c->query->whereRaw('timesheets.status = ?', [$status]);
    }

    // A variadic parameter collects a list argument:
    // in_department(?, ?, ?) -> $departmentIds === ['a', 'b', 'c'].
    #[RowCondition]
    public function inDepartment(RowConditionContext $c, string ...$departmentIds): Builder
    {
        return $c->query->whereIn('timesheets.department_id', $departmentIds);
    }

    // Global: a plain yes/no about the user, independent of any row.
    #[GlobalCondition]
    public function isAdmin(GlobalConditionContext $c): bool
    {
        return (bool) $c->user->is_admin;
    }
}

namespace App\Warrant;

use Warrant\Rules\RuleResolutionContext;
use Warrant\Rules\RuleResolver;
use Warrant\Rules\WarrantRuleSet;

class DatabaseRuleResolver implements RuleResolver
{
    public function resolve(RuleResolutionContext $context): WarrantRuleSet
    {
        // Load the raw rule string + any binding values for this user/resource.
        [$syntax, $bindings] = MyRuleStore::for(
            user: $context->user,
            resource: $context->schemaKey, // 'timesheets'
        );

        return WarrantRuleSet::fromSyntax($syntax, $context->schemaKey, $bindings);
    }
}

'rule_resolver' => App\Warrant\DatabaseRuleResolver::class,
'schemas' => ['timesheets' => App\Warrant\TimesheetSchema::class],

// Which timesheets can the current user update?
$editable = Timesheet::query()->userHasAbility('update')->get();

// Can this user approve this specific timesheet?
if (Warrant::can('approve', $timesheet)) { /* ... */ }

// Attach the per-row ability list, e.g. for rendering buttons.
$rows = Timesheet::query()->selectUserAbilities()->get();
$rows->first()->abilities; // e.g. ['view', 'update']

// Or go through Laravel's Gate — Warrant resolves these too:
$user->can('approve', $timesheet);
bash
php artisan vendor:publish --tag=warrant-config
text
if is_self they can view, update, delete
if in_department(?, ?) they can view, approve
if is_admin they can *