Skip to content

Instantly share code, notes, and snippets.

@crynobone
Last active September 19, 2015 16:11
Show Gist options
  • Save crynobone/e46d22bbbbbe2c6bcda6 to your computer and use it in GitHub Desktop.
Save crynobone/e46d22bbbbbe2c6bcda6 to your computer and use it in GitHub Desktop.
https://github.com/orchestral/authorization usage outside of Laravel or Orchestra Platform
{
"name": "crynobone/standalone-authorization",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "crynobone",
"email": "[email protected]"
}
],
"autoload": {
"classmap": [
"UserRoles.php"
]
},
"require": {
"orchestra/authorization": "~3.2"
},
"minimum-stability": "dev"
}
<?php
require "vendor/autoload.php";
$memory = new Orchestra\Memory\Provider(new Orchestra\Memory\Handlers\Runtime('app', []));
$acl = new Orchestra\Authorization\Authorization('app', $memory);
$acl->setUser(new UserRoles(['Administrator', 'Editor']));
$acl->roles()->attach(['Administrator', 'Editor', 'User']);
$acl->actions()->attach(['Manage User', 'Manage Post', 'Create Post', 'Edit Post']);
$acl->allow('Administrator', 'Manage User');
$acl->allow(['Administrator', 'Editor'], 'Manage Post');
$acl->allow('User', ['Create Post', 'Edit Post']);
echo $acl->can('Manage User') ? '✓ manage user' : '✗ manage user';
echo PHP_EOL;
echo $acl->can('Manage Post') ? '✓ manage post' : '✗ manage post';
echo PHP_EOL;
echo $acl->can('Create Post') ? '✓ create post' : '✗ create post';
echo PHP_EOL;
echo $acl->can('Edit Post') ? '✓ edit post' : '✗ edit post';
echo PHP_EOL;
<?php
class UserRoles implements Orchestra\Contracts\Authorization\Authorizable
{
protected $roles;
public function __construct(array $roles)
{
$this->roles = $roles;
}
public function getRoles()
{
return $this->roles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment