PHP code example of defrostedtuna / alnus

1. Go to this page and download the library: Download defrostedtuna/alnus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

defrostedtuna / alnus example snippets


'providers' => [

    // Lots of providers here
    
    DefrostedTuna\Alnus\AlnusServiceProvider::class,
    
    // Some other jargon afterwards
    
],



namespace App\Models\User;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use DefrostedTuna\Alnus\Traits\RolesAndPermissions;

class User extends Authenticatable
{
    use Notifiable;
    use RolesAndPermissions;


// String, Role object, or an array of either.
$role = "administrator";

$user->assignRole($role);
$user->revokeRole($role);
$user->revokeAllRoles();
$user->syncRoles($role); // Also accepts ids as well.

// String, Role/Permission object, or an array of either.
$role = "moderator";
$permission = "update_post";

$user->hasRole($role);
$user->isA($role); // $user->isA('moderator');
$user->isAn($role); //$user->isAn('administrator');
$user->isAbleTo($permission);

$role = new DefrostedTuna\Alnus\Models\Role();

$role->name = 'administrator';
$role->label = 'the site administrator';

$role->save();

$permission = new DefrostedTuna\Alnus\Models\Permission();

$permission->name = 'update_post';
$permission->label = 'Permission to update post';

$permission->save();

$role->givePermission($permission);

// PostPolicy.php

public function before($user, $ability)
{
    if ($user->isAn('administrator') {
        return true;
    }
}

public function update(User $user, Post $post)
{
    return ($user->isAbleTo('update_post') || $user->owns($post)) true : false;
}

// routes/web.php

Route::get('posts/{post}/edit', [
    'uses' => 'PostController@edit',
    'middleware' => 'can:update,post'
]);

php artisan vendor:publish --provider="DefrostedTuna\Alnus\AlnusServiceProvider"