/** * List all resources by group * * @param array $group Resource group * * @return array */ public function listResourcesByGroup($group) { $result = null; $select = new Select(); $select->from(array('uar' => 'user_acl_resource'))->join(array('uap' => 'user_acl_permission'), 'uar.id = uap.user_acl_resource_id')->where->equalTo('uar.resource', $group); $group = $this->roleTable->fetchAll($select); foreach ($group as $key => $value) { if ($this->isAllowed($this->user->getRole()->getName(), $value['resource'], $value['permission'])) { $result[] = $value['permission']; } } return $result; }
/** * Get Roles * * @param boolean $forceReload Force reload * * @return array \Gc\User\Role\Model */ public function getRoles($forceReload = false) { if (empty($this->roles) or $forceReload === true) { $rows = $this->fetchAll($this->select(function (Select $select) { $select->order('name'); })); $roles = array(); foreach ($rows as $row) { $roles[] = Model::fromArray((array) $row); } $this->roles = $roles; } return $this->roles; }
/** * Test * * @return void */ public function testGetUserPermissions() { $this->assertInternalType('array', $this->object->getUserPermissions()); }
/** * Check acl * * @param string $resource Resource name * @param string $permission Permission name * * @return boolean */ public function __invoke($resource, $permission) { return $this->acl->isAllowed($this->roleName, $resource, $permission); }
/** * Get User Role * * @param boolean $forceReload Force reload * * @return \Gc\User\Role\Model */ public function getRole($forceReload = false) { $role = $this->getData('role'); if (empty($role) or !empty($forceReload)) { $role = Role\Model::fromId($this->getUserAclRoleId()); $this->setData('role', $role); } return $this->getData('role'); }
/** * Edit role * * @return \Zend\View\Model\ViewModel|array */ public function editAction() { $roleId = $this->getRouteMatch()->getParam('id'); $roleModel = Role\Model::fromId($roleId); if (empty($roleModel) or $roleModel->getName() === Role\Model::PROTECTED_NAME) { $this->flashMessenger()->addErrorMessage("Can't edit this role"); return $this->redirect()->toRoute('config/user/role'); } $form = new RoleForm(); $form->initPermissions($roleModel->getUserPermissions()); $form->setAttribute('action', $this->url()->fromRoute('config/user/role/edit', array('id' => $roleId))); $form->loadValues($roleModel); if ($this->getRequest()->isPost()) { $post = $this->getRequest()->getPost()->toArray(); $form->setData($post); if ($form->isValid()) { $roleModel->addData($form->getInputFilter()->getValues()); $roleModel->save(); $this->flashMessenger()->addSuccessMessage('Role saved!'); return $this->redirect()->toRoute('config/user/role/edit', array('id' => $roleId)); } $this->flashMessenger()->addErrorMessage('Role can not saved!'); $this->useFlashMessenger(); } return array('form' => $form); }
/** * Initiliaze from id * * @param integer $userRoleId User role id * * @return \Gc\User\Role\Model */ public static function fromId($userRoleId) { $roleTable = new Model(); $row = $roleTable->fetchRow($roleTable->select(array('id' => (int) $userRoleId))); $roleTable->events()->trigger(__CLASS__, 'before.load', $roleTable); if (!empty($row)) { $roleTable->setData((array) $row); $roleTable->setOrigData(); $roleTable->events()->trigger(__CLASS__, 'after.load', $roleTable); return $roleTable; } else { $roleTable->events()->trigger(__CLASS__, 'after.load.failed', $roleTable); return false; } }
/** * Test * * @return void */ public function testEditActionWithWrongPostData() { $roleModel = RoleModel::fromArray(array('name' => 'RoleTest', 'description' => 'Description')); $roleModel->save(); $this->dispatch('/admin/config/user/role/edit/' . $roleModel->getId(), 'POST', array()); $this->assertResponseStatusCode(200); $this->assertModuleName('GcConfig'); $this->assertControllerName('RoleController'); $this->assertControllerClass('RoleController'); $this->assertMatchedRouteName('config/user/role/edit'); $roleModel->delete(); }