Skip to content

Instantly share code, notes, and snippets.

@crynobone
Last active Sep 19, 2015
Embed
What would you like to do?
https://github.com/orchestral/authorization usage outside of Laravel or Orchestra Platform
{
"name": "crynobone/standalone-authorization",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "crynobone",
"email": "crynobone@gmail.com"
}
],
"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