private function registerDirectives() { Blade::directive('allowed', function ($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return "<?php if (app('rbac')->checkAccess(\\Auth::user(), {$expression})): ?>"; }); if (Config::get('rbac.shortDirectives')) { foreach (Rbac::getRepository() as $name => $item) { $directiveName = $item->type == Item::TYPE_PERMISSION ? 'allowed' : 'is'; $directiveName .= Str::studly(str_replace('.', ' ', $name)); Blade::directive($directiveName, function ($expression) use($name) { $expression = trim($expression, '()'); if (!empty($expression)) { $expression = ', ' . $expression; } return "<?php if (app('rbac')->checkAccess(\\Auth::user(), '{$name}'{$expression})): ?>"; }); } } Blade::directive('endallowed', function ($expression) { return "<?php endif; ?>"; }); }
private function resolvePermissions($route) { $rbacActions = $this->manager->getActions(); $rbacControllers = $this->manager->getControllers(); $action = $route->getAction(); $actionNameSlash = str_replace($action['namespace'], '', $action['uses']); $actionName = ltrim($actionNameSlash, '\\'); $actionParts = explode('@', $actionName); if (isset($rbacActions[$actionName])) { $permissionNames = $rbacActions[$actionName]; } elseif (isset($rbacControllers[$actionParts[0]])) { $permissionNames = $rbacControllers[$actionParts[0]] . '.' . $actionParts[1]; } else { $permissionNames = $this->dotStyle($actionName); } return $permissionNames; }
public function allowed($itemName, $params = []) { return Rbac::checkAccess($this, $itemName, $params); }