• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    PHP
  • Created over 4 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Because in the Maravelous univer every user deserves super power

Grant Users the superpower of Marvel characters

inani-user2

Download

composer require inani/maravel-permissions

Installation

Then include the service provider inside config/app.php. (You can skipp it if it's in Laravel 5.5 or higher)

'providers' => [
    ...
    Inani\Maravel\Providers\MaravelServiceProvider::class,
    ...
];

Publish resources, and migrate

php artisan vendor:publish

PS : You can edit 2020_05_27_221346_add_role_id_to_users migration to link it with the correct user table

Edit the config/maravels.php with the correct values

<?php

return [
    // Define the list of actions to be checked against
    'actions' => [
        'add', 'delete', 'create', 'search', 'update'
    ],

    // define the class path for the entities
    'entities' => [
        \App\Models\User::class,
    ],
];

And then migrate

php artisan migrate

Setup a Model

To setup the user model, all you have to do is add (and import) the IsMarvel trait.

use Inani\Maravel\HasRole;

class User extends Model
{
    use HasRole;
    ...
}

Usage

All roles are role and permissions are powers

Because every user deserves to be a hero, The Maravel API is based on the Marvel Jargon, and here are how it can be used

// Having a user
$user = User::first();

// Create a new role, description is not mandotary
$userManager = RoleBuilder::create('User Manager', 'The role to manage users')
                ->havingPower([
                   'name' => 'can_update',
                   'description' => 'The abilitiy to update a user',
                   'action' => 'update',
                   'entity' => \App\Models\User::class,
               ]);

// we can grant a power to it
$userManager = RoleBuilder::of($userManager)
                        ->grant([
                             'name' => 'can_create',
                             'description' => 'The abilitiy to create a user',
                             'action' => 'create',
                             'entity' => \App\Models\User::class,
                         ]);

// Or take it off
$ability = Ability::first();

$storm = RoleBuilder::of($userManager)->takeOff($ability);


// bless the user with the abilities of the role
$user->roleManager()->blessWith($storm);


// check if it has the ability
$user->roleManager()->owns($ability);

// check if it has one of the provided abilities
$user->roleManager()->ownsOneOf([$ability, $anOtherAbility]);

// make it human again (remove its role)
$user->roleManager()->humanize();

You can also manage the instances directly

// Create Ability
$ability = Ability::create([
    'name' => 'post_write',
    'description' => 'Abitlity to create new Posts',
    'action' => 'add',
    'entity' => \App\Models\Post::class,
]);

// Create a Marvel
$writer = Role::create([
    'name' => 'Webmaster',
    'description' => 'A Role that allows you create new posts'
]);

// Grant the ability
$writer->grant($ability);

// remove a certain ability
$writer->takeOff($ability);

// remove all and keep only those
$abilities = [1, 2]; // or the models
$writer->keep($abilities);

// bless it to our user
$user = \App\Models\User::first();

$user->roleManager()->blessWith($writer);

Am I missing something?

Submit a PR or issue with details!

More Repositories

1

larapoll

A Laravel package to manage your polls
PHP
260
star
2

messager

A convenient way to handle messages between users in a simple way
PHP
153
star
3

laravel-nova-configuration

Use Configuration inputs instead of the env/config files. Good news for the clients!
Vue
40
star
4

chibi

A mini PHP framework
PHP
37
star
5

oxfordapi-wrapper

A PHP/Laravel Wrapper for oxford dictionary API
PHP
27
star
6

nova-resource-maker

A Nova tool that will help you to generate fields array for the resource.
PHP
25
star
7

LazyBelongsToMany

A lightweight implementation of Laravel's belongs To many
PHP
24
star
8

PHPValidate

A light library to validate inputs
PHP
11
star
9

Dental

A Dental App built using Laravel 5.2
HTML
11
star
10

laravel-same-request

The Missing check on Laravel Request Unicity.
PHP
10
star
11

corona-wash-reminder

A mobile app to remind you to wash your hand
TypeScript
8
star
12

geeksblabla

This is my Nuxt Js version of Geeksblabla of #DevC
JavaScript
8
star
13

laravel-controllers-generator

A Laravel package that will generate for you controllers used in routes
PHP
7
star
14

vuejs-101

HTML
4
star
15

parser

PHP
4
star
16

filters

PHP
3
star
17

chibi-orm

PHP
2
star
18

chibi-console

PHP
2
star
19

psd-to-html

PSDs converted to HTML/CSS
CSS
1
star
20

php-array-every-day

A repo where I put every day some code to understand and get used to the important arrays method
PHP
1
star
21

The-Coding-Train

Notes&Codes of The Coding Train Tutorials
Processing
1
star
22

docker-101

PHP
1
star
23

webpack-101

JavaScript
1
star
24

Uploader

a simple Class to make your life easier while handling Uploads
1
star
25

chibi-app

The basic app to be used with chibi framework
PHP
1
star
26

presta-module

PHP
1
star
27

realtime-with-socket-io

Real time chat with Socketio & Vuejs
HTML
1
star