PHP code example of efficiently / authority-controller

1. Go to this page and download the library: Download efficiently/authority-controller 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/ */

    

efficiently / authority-controller example snippets


     Efficiently\AuthorityController\AuthorityControllerServiceProvider::class,
 

     'Params'    => Efficiently\AuthorityController\Facades\Params::class,
     'Authority' => Efficiently\AuthorityController\Facades\Authority::class,
 

 Authority::can('update', SomeModel::class);
 

    //app/User.php
    public function roles()
    {
        return $this->belongsToMany(Authority\Role::class)->withTimestamps();
    }

    public function permissions()
    {
        return $this->hasMany(Authority\Permission::class);
    }

    public function hasRole($key)
    {
        $hasRole = false;
        foreach ($this->roles as $role) {
            if ($role->name === $key) {
                $hasRole = true;
                break;
            }
        }

        return $hasRole;
    }

    //app/Authority/Role.php
    

    namespace App\Authority;

    use Illuminate\Database\Eloquent\Model;

    class Role extends Model {}

    //app/Authority/Permission.php
    

    namespace App\Authority;

    use Illuminate\Database\Eloquent\Model;

    class Permission extends Model {}



namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Efficiently\AuthorityController\ControllerAdditions as AuthorityControllerAdditions;

class Controller extends BaseController
{
    // use AuthorizesRequests;
    use DispatchesJobs, ValidatesRequests;
    use AuthorityControllerAdditions;
    //code...
}


//code...
class Controller extends BaseController
{
    use DispatchesJobs, ValidatesRequests;
    use AuthorizesRequests, AuthorityControllerAdditions {
        AuthorityControllerAdditions::authorize insteadof AuthorizesRequests;
        AuthorizesRequests::authorize as illuminateAuthorize;
        AuthorizesRequests::authorizeResource as illuminateAuthorizeResource;
    }
    //code...
}

//config/authority-controller.php


$serializer = new SuperClosure\Serializer;
return [
    'initialize' => $serializer->serialize(function ($authority) {
        $user = auth()->guest() ? new App\User : $authority->getCurrentUser();

        // Action aliases. For example:
        $authority->addAlias('moderate', ['read', 'update', 'delete']);

        // Define abilities for the passed in user here. For example:
        if ($user->hasRole('admin')) {
            $authority->allow('manage', 'all');
        } else {
            $authority->allow('read', 'all');
        }
    })
];

public function show($id)
{
    $this->article = App\Article::find($id);
    $this->authorize('read', $this->article);
}



namespace App\Http\Controllers;

class ArticlesController extends Controller
{

    public function __construct()
    {
        $this->loadAndAuthorizeResource();
    }

    public function show($id)
    {
        // $this->article is already loaded and authorized
    }
}

Authority::authorize('read', 'App\Product', 'Unable to read this product.');

//app/Exceptions/Handler.php

   /**
    * Render an exception into an HTTP response.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Exception  $e
    * @return \Illuminate\Http\Response
    */
    public function render($request, Exception $e)
    {
        //code...
        if ($e instanceof \Efficiently\AuthorityController\Exceptions\AccessDenied) {
            $msg = $e->getMessage();
            \Log::error('Access denied! '.$msg);

            return redirect('/home')->with('flash_alert', $msg);
        }

        return parent::render($request, $e);
    }

    //code...



namespace App\Http\Controllers;

class ProductsController extends Controller
{
    //code...

    public function update($id)
    {
        $this->params['id'] == $id;//-> true
        $this->params['product'];//-> ["name" => "Best movie"]
        $this->params['controller'];//-> 'products'
        $this->params['action'];//-> 'update'
        //code...
    }

    //code...
}

$authority->allow('update', 'App\Product', function ($self, $product) {
    return $product->available === true;
});
bash
php artisan vendor:publish --provider="Efficiently\AuthorityController\AuthorityControllerServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Efficiently\AuthorityController\AuthorityControllerServiceProvider" --tag="config"