/** * Attempt to find an action on the resource with the given name * * @param ResourceInterface $resource * @param $action_name * @return ActionInterface|null */ public function findAction(ResourceInterface $resource, $action_name) { $found_action = null; foreach ($resource->getActions() as &$action) { if ($action->getName() == $action_name) { $found_action = $action; break; } } return $found_action; }
protected function getAction(ResourceInterface $resource) { $action = null; foreach ($resource->getActions() as $resourceAction) { if ($resourceAction->getName() == '*') { $action = $resourceAction; } } if ($action === null) { $action = $resource->addAction('*'); } return $action; }
/** * Store a newly created resource in storage. * * @return Response */ public function store(RoleInterface $role, ResourceInterface $privilege, $action_id) { $response = Redirect::back(); // find the action we are trying to give tot he role $action = null; foreach ($privilege->getActions() as &$check_action) { if ($check_action->getId() == $action_id) { $action = $check_action; break; } } if ($action) { $container = $action->getResource()->getName(); // This decides which add permission bootstrap accordion is open upon load $response->with('permission_add_container_open', $container); // This decides which permission bootstrap accordion is open upon load $response->with('permission_container_open', $container); if ($role->addPermission($action, 0)) { $response->with('permission', trans('roles::permission.add success', ['representation' => $action->getResource()->getName() . '.' . $action->getName()])); } else { $errors = new MessageBag(); $errors->add('permission', trans('roles::permission.add fail', ['representation' => $action->getResource()->getName() . '.' . $action->getName(), 'action_id' => $action_id])); $response->withErrors($errors); } } else { $errors = new MessageBag(); $errors->add('permission', trans('roles::action.not found', ['action_id' => $action_id])); $response->withErrors($errors); } // Info to the edit view: open privilege tab of the accordion $response->with('privilege_open', true); return $response; }
/** * @param ResourceInterface $resource * @param $action_id * @return \Illuminate\Http\RedirectResponse */ public function destroyAction(ResourceInterface $resource, $action_id) { $response = Redirect::back(); $action = null; foreach ($resource->getActions() as $check_action) { if ($check_action->getId() == $action_id) { $action = $check_action; break; } } if (Auth::user()->can("remove action", $resource)) { if (!is_null($action)) { if ($resource->removeAction($action)) { } else { $errors = new MessageBag(); $errors->add('error', trans('roles::action.delete failed')); $response->withErrors($errors); } } else { $errors = new MessageBag(); $errors->add('error', trans('roles::action.delete find failed')); $response->withErrors($errors); } } else { $errors = new MessageBag(); $errors->add('error', trans('roles::action.delete permission denied')); $response->withErrors($errors); } return $response; }