Giter VIP home page Giter VIP logo

ladder's Introduction

Build Status Total Downloads Latest Stable Version License

Ladder ๐Ÿชœ

Ladder simplifies role and permission management for your Laravel project by avoiding storing everything in the database. Inspired by Laravel Jetstream, it offers a static approach, reducing queries and ensuring immutability for easy modifications.

Install

This package requires Laravel 10 and above.

composer require eneadm/ladder

Once Ladder is installed, create a new LadderServiceProvider to manage roles and permissions. You can do so effortlessly with this command:

php artisan ladder:install

Lastly, execute the migration command to create a single pivot user_role table, assigning roles to users.

php artisan migrate

Use

Before using Ladder add the HasRoles trait to your App\Models\User model. By doing so this trait will provide the necessary methods to manage roles and permissions.

use Ladder\HasRoles;
 
class User extends Authenticatable
{
    use HasRoles;
}

HasRoles trait in detail

// Access all of user's roles...
$user->roles : Illuminate\Database\Eloquent\Collection

// Determine if the user has the given role... 
$user->hasRole(string $role) : bool

// Access all permissions for a given role belonging to the user...
$user->rolePermissions(string $role) : ?array

// Determine if the user role has a given permission...
$user->hasRolePermission(string $role, string $permission) : bool

// Determine if the user has a given permission...
$user->hasPermission(string $permission) : bool

Roles & Permissions

Users can receive roles with permissions defined in App\Providers\LadderServiceProvider using Ladder::role method. This involves specifying a role's slug, name, permissions, and description. For instance, in a blog app, role definitions could be:

Ladder::role('admin', 'Administrator', [
    'create',
    'read',
    'update',
    'delete',
])->description('Administrator users can perform any action.');

Ladder::role('editor', 'Editor', [
    'post:read',
    'post:create',
    'post:update',
])->description('Editor users have the ability to read, create, and update posts.');

Assign Roles

You may assign roles to the user using the roles relationship that is provided by the Ladder\HasRoles trait:

use App\Models\User;

$user = User::find(1);

$user->roles()->updateOrCreate(['role' => 'admin']);

Authorization

For request authorization, utilize the Ladder\HasRoles trait's hasPermission method to check user's role permissions. Generally, verifying granular permissions is more important than roles. Roles group permissions and are mainly for presentation. Use the hasPermission method within authorization policies.

/**
 * Determine whether the user can update a post.
 */
public function update(User $user, Post $post): bool
{
    return $user->hasPermission('post:update');
}

License

Ladder is free software distributed under the terms of the MIT license.

ladder's People

Contributors

eneakh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.