yuriy-martini/laravel-authorization

There is no license information available for the latest version (v0.1.1) of this package.

v0.1.1 2023-10-16 12:56 UTC

This package is not auto-updated.

Last update: 2024-04-30 16:21:18 UTC


README

Latest Version MIT License

Laravel Authorization

https://laravel.com/docs/master/authorization#introduction

Installation

composer require yuriy-martini/laravel-authorization

Usage

https://laravel.com/docs/master/authorization#authorizing-actions-using-policies

  1. Authorizable (User)
  2. Config
  3. Policies

U/ Authorizable

U/ A/ Example

-+ app/Models/User.php:

<?php

namespace App\Models;

class User
    implements \YuriyMartini\Laravel\Authorization\Contracts\Authorizable
{
    use \YuriyMartini\Laravel\Authorization\Concerns\Authorizable;
}

U/ Config

U/ C/ Defaults

php artisan vendor:publish --tag=authorization-defaults-config

U/ C/ Models

php artisan vendor:publish --tag=authorization-models-config

U/ C/ M/ Example

+ config/authorization/models.php:

<?php

return [
    \App\Models\User::class => [
        \App\Models\User::class => [
            YuriyMartini\Laravel\Authorization\Enums\Permission::view,
        ],
    ],
];

U/ Models

U/ M/ Example

+ app/Models/User/Authorizations.php:

<?php

namespace App\Models\User;

trait Authorizations
{
    public function isViewable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isUpdatable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isDeletable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isRestorable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isForceDeletable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }
}

-+ app/Models/User.php:

<?php

namespace App\Models;

class User
    implements \YuriyMartini\Laravel\Authorization\Contracts\Model
{
    use \App\Models\User\Authorizations;
}

U/ Policies

https://laravel.com/docs/master/authorization#generating-policies

U/ P/ Example

-+ app/Policies/UserPolicy.php:

<?php
 
namespace App\Policies;
 
class UserPolicy
    extends \YuriyMartini\Laravel\Authorization\Policy
{
    protected static function getModel(): string
    {
        return \App\Models\User::class;
    }
}

Changelog

Please see Changelog File for more information on what has changed recently.

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.