PHP code example of michaeltintiuc / laravel-permy

1. Go to this page and download the library: Download michaeltintiuc/laravel-permy 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/ */

    

michaeltintiuc / laravel-permy example snippets


    'MichaelT\Permy\PermyServiceProvider'

    'Permy' => 'MichaelT\Permy\PermyFacade'

    use MichaelT\Permy\PermyTrait;

    class User extends Model
    {
        use PermyTrait;
    }

    $table->increments('id');
    $table->string('name');
    $table->string('desc');

    Acme\Controllers\UsersController

php artisan migrate

Route::filter('permy', 'MichaelT\Permy\PermyFilter');

'permy' => 'MichaelT\Permy\PermyMiddleware'

Route::get('/', ['before' => 'permy', 'uses' => 'SomeController@method']);

    Route::group([before' => 'permy'], function () {
        ...
    });

    class SomeController
    {
        public function __construct()
        {
            $this->beforeFilter('permy');                             // checks all methods
            $this->beforeFilter('permy', array('only' => 'index'));   // checks only index method
            $this->beforeFilter('permy', array('except' => 'index')); // checks all but the index method
        }
    }

    Route::get('/', 'SomeController@method')->middleware('permy');

    Route::group([middleware' => 'permy'], function () {
        ...
    });

    class SomeController
    {
        public function __construct()
        {
            $this->middleware('permy');                  // checks all methods
            $this->middleware('permy')->only('index');   // checks only index method
            $this->middleware('permy')->except('index'); // checks all but the index method
        }
    }

    {"index": 1, "someMethod": 0}

boolean can(<array|string|Illuminate\Routing\Route $routes> [, [string $operator = 'and'] [, boolean|callable $extra_check = true]])

    // check single route or controller method
    Permy::can('users.index');
    Permy::can('UsersController@index');

    // check multiple routes or controller methods
    // returns true if ALL routes/methods are allowed
    Permy::can(['users.index', 'users.show']);
    Permy::can(['UsersController@index', 'UsersController@show']);

    // OR returns true if at least 1 route/method is accessible
    Permy::can(['users.index', 'users.show', 'operator' => 'or']);
    Permy::can(['UsersController@index', 'UsersController@show', 'operator' => 'or']);

    // XOR the permission values of each route/method
    Permy::can(['users.index', 'users.show', 'operator' => 'xor']);
    Permy::can(['UsersController@index', 'UsersController@show', 'operator' => 'xor']);

    // Additional check
    $check = SomeClass::checkUser();

    // return true if permissions AND $check are true
    Permy::can('users.index', 'and', $check);

    // At least one should be true
    Permy::can('users.index', 'or', $check);

    // XOR the values of permissions and $check
    Permy::can('users.index', 'xor', $check);

    // Omit the $operator and use the default value
    Permy::can('users.index', $extra_check = $check);

    // Provide a callback function
    // The return value will be type hinted to boolean
    Permy::can('users.index', $extra_check = function () {
        return SomeClass::fetchData();
    });

boolean cant(<array|string|Illuminate\Routing\Route $routes> [, [string $operator = 'and'] [, boolean|callable $extra_check = true]])

    // returns false if access is allowed
    Permy::cant('users.index');

array getList()

    // Generates language file for default locale
    Permy::getList();

    // Generates language file for 'fr' locale
    App::setLocale('fr');
    Permy::getList();

    // When setting locale explicitly - reset it when done
    // Whichever is fine
    App::setLocale(Config::get('app.fallback_locale'));
    App::setLocale('en');

PermyHandler setUser(<Illuminate\Database\Eloquent\Model $user>)

    $user = User::find(123);

    // Check if user ID 123 has access
    Permy::setUser($user)->can('users.index');

    // Next calls will check the authenticated user NOT the one we've set before
    Permy::can('users.index');

Illuminate\Database\Eloquent\Model getUser()

    $user = User::find(123);

    // returns user ID 123
    Permy::setUser($user)->getUser();

    // returns currently authenticated user
    Permy::getUser();

PermyHandler setDebug(<boolean $bool>)

    // Debugging is on
    Permy::setDebug(true)->can('users.index');

    // Debugging is equal to value set in config
    Permy::can('users.index');

PermyHandler setGodmode(<boolean $bool>)

    // Returns true even if access is disallowed
    Permy::setGodmode(true)->can('users.index');

    // Godmode is equal to value set in config
    Permy::can('users.index');

PermyHandler setRolesLogicOperator(<string $operator>)

    // At least one of the permissions assigned allows access to users.index
    Permy::setRolesLogicOperator('or')->can('users.index');

    // Value from config is used now
    Permy::can('users.index');

    [
        'fillable' => ['permy'],
        'guarded' => []
    ]

    return array (
        'Acme::UsersController' =>
        array (
            'name' => 'A name for the non-tech people',
            'desc' => 'In case if anyone reads these, provide some sort of help for managers.',
            'methods' =>
            array (
                'myAwesomeMethod' =>
                array (
                    'name' => 'Managers may think camelCase is weird.',
                    'desc' => '"rm -rf ~" is not a very helpful description.',
                )
            )
        )
    );

    [
        // :controller is replaced with the name-spaced controller name
        'controller' => [
            'name' => '* :controller - please update',
            'desc' => '* The developer was way to busy to care describing the :controller class',
        ],
        // :controller is replaced with the name-spaced controller name
        // :method is replaced with the controller method name
        'method' => [
            'name' => '* :controller@:method - please update',
            'desc' => '* The developer was way to busy to care describing the :method method of :controller class',
        ],
    ];
shell
    php artisan migrate:publish michaeltintiuc/laravel-permy
shell
    php artisan vendor:publish --provider="MichaelT\Permy\PermyServiceProvider" --tag="migrations"
shell
    php artisan config:publish michaeltintiuc/laravel-permy
shell
    php artisan vendor:publish --provider="MichaelT\Permy\PermyServiceProvider" --tag="config"
shell
    php artisan vendor:publish --provider="MichaelT\Permy\PermyServiceProvider" --tag="translations"