GithubHelp home page GithubHelp logo

dynamic-acl's Introduction

Dynamic ACL

Dynamic ACL is a package that handles Access Control Level on your Laravel Application. It's fast to run and simple to use. Install and enjoy ;)

Features

Installation

Prerequisite:

  • Make your authentication (session-based) system.
  • Define a name for your routes.
composer require iya30n/dynamic-acl

Publish config file

php artisan vendor:publish

Migrate roles table

php artisan migrate

Don't worry about relationships; We handle them for you.

Run make:admin command

This command makes your first admin a super admin with a full-access level.

php artisan make:admin --role

Usage

Just run your application and visit locahost:8000/admin/roles. You'll see a list of your roles. You can create a new one, edit or delete them.

Configuration

After publishing the vendor, you can change the configuration in the config/dynamicACL.php file.

It has the following options:

  • alignment: Changes UI alignment. It can be eigther RTL or LTR. Also, when you change your lang, CRUD roles will change in (fa, en).
  • controllers_path: Namespace of your controllers.
  • ignore_list: List of routes to be ignored on permission check.

How to use the ACL?

Just add dynamicAcl middleware to your routes.

now you'll see list of the routes with dynamicAcl middleware on localhost:8000/admin/roles/create.

also, this middleware will check your admin access to the current route.

Access to the roles

You can write your queries with the Role model to get the list of roles and use it on your admin/user CRUD views.

use Iya30n\DynamicAcl\Models\Role;

Sync user roles

You can use sync, attach, and detach methods to assign user roles.

$user->roles()->sync([1, 2, 3,...]);

Get user roles

$user->roles()->get();

Check user access manually

Call the hasPermission method on the user and pass the route name:

auth()->user()->hasPermission('admin.articles.create');

Check whether the user has access to any routes of an entity:

auth()->user()->hasPermission('admin.articles.*')

Also, you can check if the user has access to his entity:

$user->hasPermission('admin.articles.update', $article);

// Or with a custom relation_key (default is 'user_id')
$user->hasPermission('admin.articles.update', $article, 'owner_id');

Check Ownership

Check manually whether the user is the owner of an entity:

$user->isOwner($article);

// Or with a custom relation_key
$user->isOwner($article, 'owner_id');

// Or pass it as ['model' => id]
$user->isOwner(['article' => $article->id]);

// Or with a custom model path
$user->isOwner(['App\\Article' => $article->id]);

Dynamic Policy

Using dynamic policy is straightforward too. Just add authorize middleware to your routes. You can pass the foreign key as a parameter (default is user_id). This middleware checks the foreign key of your entity.

NOTE: It is necessary to use route model binding on your controllers.

dynamic-acl's People

Contributors

alibagherifam avatar iya30n avatar reziamini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.