composer require inani/maravel-permissions
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_marvel_id_to_users
migration to link it with the correct user table
And then migrate
php artisan migrate
You will have a MarvelCan
Middleware, you can tweak it to adapt to your needs and use it such as
// Check if he owns one of those two abilities or both
Route::middleware(['auth:api', 'marvelCan:create_post,edit_post'])
->post('/posts', 'PostsController@store');
To setup the user model, all you have to do is add (and import) the IsMarvel
trait.
use Inani\Maravel\IsMarvel;
class User extends Model
{
use IsMarvel;
...
}
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 marvel, description is not mandotary
$storm = Spectre::create('storm', 'A Superhero that can do crazy things')->havingPower([
'weather_manipulation',
'earth_telepathy',
'high_sens',
'see_the_future'
]);
// we can grant a power to it
$storm = Spectre::of($storm)->grant('flying');
// Or take it off
$storm = Spectre::of($storm)->takeOff('see_the_future');
// bless the user with the abilities of the marvel
$user->cerebro()->blessWith($storm);
// check if it has the ability
$user->cerebro()->owns('weather_manipulation');
// check if it has one of the provided abilities
$user->cerebro()->ownsOneOf([
'earth_telepathy',
'flying',
'x-ray',
]);
// make it human again (remove its role)
$user->cerebro()->humanize();
You can also manage the instances directly
// Create Ability
$ability = Ability::create([
'super_power' => 'speed',
'description' => 'Run faster than anyone else'
]);
// Create a Marvel
$marvel = Marvel::create([
'name' => 'Cristiano Ronaldo',
'description' => 'A super footballer'
]);
// Grant the ability
$marvel->grant($ability);
// remove a certain ability
$marvel->takeOff($ability);
// remove all and keep only those
$marvel->keep($abilities);
// bless it to our user
$user->cerebro()->blessWith($marvel);
Submit a PR or issue with details!