/** * Applies a configuration set to a dependency injector. * * @param Injector $injector */ public function apply(Injector $injector) { // Create a new Manager instance. $manager = new Manager(new ArrayDriver()); // Managers can do everything $manager->role('manager')->allow('all'); // $manager->role('employee')->allow('guest', 'read'); $injector->share($manager); }
public function __construct() { // Prepare parent::__construct(); $manager = new Manager(new ArrayDriver()); $this->__lock = $manager->caller(new Path()); $this->__lock->deny('all'); // REST $this->middleware(function ($middlewares) { if (isset($_POST['_METHOD'])) { $_SERVER['REQUEST_METHOD'] = strtoupper($_POST['_METHOD']); unset($_POST['_METHOD']); } $middlewares->next(); }); // Return allowed files $this->middleware(function ($middlewares) { $path = $this->_formatPath($this['request']->getResourceUri()); if (file_exists($path) && is_file($path) && $this->__lock->can($this->_formatPath($path))) { // Get mime type $mime = mimetype($path); // Try some more extensions if ($mime == 'text/plain') { if (strpos($path, '.css')) { $mime = 'text/css'; } if (strpos($path, '.js')) { $mime = 'application/javascript'; } if (strpos($path, '.json')) { $mime = 'application/json'; } } // Set content type if ($mime != 'text/plain') { header('Content-Type: ' . $mime); } // Print file echo file_get_contents($path); exit; } $middlewares->next(); }); }
/** * Return a collection of role based permissions. * * @param $roleName * * @return Collection */ protected function getRolePermissions($roleName) { $role = new SimpleRole($roleName); $permissions = $this->manager->getDriver()->getRolePermissions($role); $collection = new Collection(); foreach ($permissions as $permission) { if ($permission instanceof Restriction) { continue; } $collection->allow($permission->getAction(), $permission->getResourceType(), $permission->getResourceId()); } return $collection; }
/** @test */ final function it_can_make_a_role_lock_aware() { $this->getRoleLock('admin')->allow('create', 'users'); $role = $this->manager->makeRoleLockAware('admin'); $this->assertTrue($role->can('create', 'users')); }
/** * The current driver provided by the manager * * @return \BeatSwitch\Lock\Drivers\Driver */ public function getDriver() { return $this->manager->getDriver(); }