Ejemplo n.º 1
0
 /**
  * Create a new Role
  *
  * After role saved into database, forward to editRole to add users/groups/roles
  *
  * @view /views/scripts/role/add.phtml
  * @access public
  */
 public function addAction()
 {
     $role = new Admin_Model_DbRow_Role(array('name' => $this->getRequest()->getParam('name', ''), 'description' => $this->getRequest()->getParam('description', ''), 'enabled' => 0));
     foreach ($this->dbGroup->fetchAll() as $row) {
         $groups[] = new Admin_Model_DbRow_Group($row);
     }
     foreach ($this->dbUser->fetchAll() as $row) {
         $users[] = new Admin_Model_DbRow_User($row);
     }
     foreach ($this->dbRole->fetchAll() as $row) {
         $inherit = new Admin_Model_DbRow_Role($row);
         if ($inherit->get('id') !== $role->get('id')) {
             $inhterits[] = $inherit;
         }
     }
     $form = new Admin_Form_Role_Add($role, $groups, $users, $inhterits);
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($this->getRequest()->getParams())) {
             $this->dbRole->insert($role->toDbArray());
             // get the last insert id for redirect to edit mode
             $role->set('id', $this->dbRole->getAdapter()->lastInsertId());
             $selectedGroups = $form->getValue('groups');
             $selectedUsers = $form->getValue('users');
             $roleInheritance = $form->getValue('inherit');
             foreach ($roleInheritance as $inherit) {
                 // dont insert "no inheritance" in the database or self as inheritance
                 if ($inherit == 0 || $inherit == $role->get('id')) {
                     continue;
                 }
                 $this->dbRoleInherit->insert($role->get('id'), $inherit);
             }
             foreach ($selectedGroups as $group) {
                 $this->dbRoleMember->insert($role->get('id'), $group, Admin_Model_DbTable_Acl_RoleMember::MEMBER_TYPE_GROUP);
             }
             foreach ($selectedUsers as $user) {
                 $this->dbRoleMember->insert($role->get('id'), $user, Admin_Model_DbTable_Acl_RoleMember::MEMBER_TYPE_USER);
             }
             $this->_redirect('admin/role/index');
         } else {
             $form->addError('Please check if you filled the form correctly.');
         }
     }
     $this->view->form = $form;
 }
Ejemplo n.º 2
0
 /**
  * Save a new Role in the database, if all tests passed
  *
  * @return array
  */
 public function saveNewRoleAction()
 {
     $pUsers = Zend_Json_Decoder::decode($this->request->getParam('users', array()));
     $pGroups = Zend_Json_Decoder::decode($this->request->getParam('groups', array()));
     $pRoles = Zend_Json_Decoder::decode($this->request->getParam('roles', array()));
     $roleModel = new Admin_Model_DbTable_Acl_Role();
     $roleMembers = new Admin_Model_DbTable_Acl_RoleMember();
     $roleInherits = new Admin_Model_DbTable_Acl_RoleInherit();
     $roleRow = $roleModel->fetchAll($roleModel->select()->where('LOWER(uar_name) = ?', strtolower($this->request->getParam('name'))));
     if ($roleRow->count() === 0) {
         $roleRow = new Admin_Model_DbRow_Role(array('name' => $this->request->getParam('name', ''), 'enabled' => $this->request->getParam('enabled', 'false') == 'true' ? 1 : 0, 'description' => $this->request->getParam('description', '')));
         $roleModel->insert($roleRow->toDbArray());
         $lastInsertId = $roleModel->getAdapter()->lastInsertId();
         foreach ($pGroups as $group) {
             $roleMembers->insert($lastInsertId, $group, Admin_Model_DbTable_Acl_RoleMember::MEMBER_TYPE_GROUP);
         }
         foreach ($pUsers as $user) {
             $roleMembers->insert($lastInsertId, $user, Admin_Model_DbTable_Acl_RoleMember::MEMBER_TYPE_USER);
         }
         foreach ($pRoles as $inheritRole) {
             $roleInherits->insert($lastInsertId, $inheritRole);
         }
         return $this->responseSuccess();
     }
     return $this->responseFailure('Failed saving informations', 'This Rolename is already in use');
 }