/**
  * @param bool $json
  * @return string|array
  */
 public function getUsers($json = false)
 {
     if ($this->getRequest()->getParam('in_role_user') != "") {
         return $this->getRequest()->getParam('in_role_user');
     }
     $roleId = $this->getRequest()->getParam('rid') > 0 ? $this->getRequest()->getParam('rid') : $this->_coreRegistry->registry('RID');
     $users = $this->_roleFactory->create()->setId($roleId)->getRoleUsers();
     if (sizeof($users) > 0) {
         if ($json) {
             $jsonUsers = [];
             foreach ($users as $usrid) {
                 $jsonUsers[$usrid] = 0;
             }
             return $this->_jsonEncoder->encode((object) $jsonUsers);
         } else {
             return array_values($users);
         }
     } else {
         if ($json) {
             return '{}';
         } else {
             return [];
         }
     }
 }
Beispiel #2
0
    /**
     * Create role for provided user of provided type
     *
     * @param int $parentId
     * @param ModelUser $user
     * @return void
     */
    protected function _createUserRole($parentId, ModelUser $user)
    {
        if ($parentId > 0) {
            /** @var \Magento\Authorization\Model\Role $parentRole */
            $parentRole = $this->_roleFactory->create()->load($parentId);
        } else {
            $role = new \Magento\Framework\DataObject();
            $role->setTreeLevel(0);
        }

        if ($parentRole->getId()) {
            $data = new \Magento\Framework\DataObject(
                [
                    'parent_id' => $parentRole->getId(),
                    'tree_level' => $parentRole->getTreeLevel() + 1,
                    'sort_order' => 0,
                    'role_type' => RoleUser::ROLE_TYPE,
                    'user_id' => $user->getId(),
                    'user_type' => UserContextInterface::USER_TYPE_ADMIN,
                    'role_name' => $user->getFirstname(),
                ]
            );

            $insertData = $this->_prepareDataForTable($data, $this->getTable('authorization_role'));
            $this->getConnection()->insert($this->getTable('authorization_role'), $insertData);
            $this->_aclCache->clean();
        }
    }
Beispiel #3
0
 /**
  * Initialize role model by passed parameter in request
  *
  * @param string $requestVariable
  * @return \Magento\Authorization\Model\Role
  */
 protected function _initRole($requestVariable = 'rid')
 {
     $role = $this->_roleFactory->create()->load($this->getRequest()->getParam($requestVariable));
     // preventing edit of relation role
     if ($role->getId() && $role->getRoleType() != RoleGroup::ROLE_TYPE) {
         $role->unsetData($role->getIdFieldName());
     }
     $this->_coreRegistry->register('current_role', $role);
     return $this->_coreRegistry->registry('current_role');
 }
 /**
  * @return void
  */
 public function testGetAclRole()
 {
     $roles = ['role'];
     $result = 1;
     $roleMock = $this->getMockBuilder('Magento\\Authorization\\Model\\Role')->disableOriginalConstructor()->setMethods([])->getMock();
     $this->roleFactoryMock->expects($this->once())->method('create')->willReturn($roleMock);
     $this->resourceMock->expects($this->once())->method('getRoles')->with($this->model)->willReturn($roles);
     $roleMock->expects($this->once())->method('load')->with($roles[0]);
     $roleMock->expects($this->once())->method('getId')->willReturn($result);
     $this->assertEquals($result, $this->model->getAclRole());
 }
Beispiel #5
0
 /**
  * Get admin role model
  *
  * @return \Magento\Authorization\Model\Role
  */
 public function getRole()
 {
     if (null === $this->_role) {
         $this->_role = $this->_roleFactory->create();
         $roles = $this->getRoles();
         if ($roles && isset($roles[0]) && $roles[0]) {
             $this->_role->load($roles[0]);
         }
     }
     return $this->_role;
 }
Beispiel #6
0
 public function testAfterSave()
 {
     $roleId = 123;
     $methodUserMock = $this->getMockBuilder('\\Magento\\User\\Model\\User')->disableOriginalConstructor()->setMethods(['hasRoleId', 'getRoleId'])->getMock();
     $methodUserMock->expects($this->once())->method('hasRoleId')->willReturn(true);
     $methodUserMock->expects($this->once())->method('getRoleId')->willReturn($roleId);
     $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->dbAdapterMock);
     $this->roleFactoryMock->expects($this->once())->method('create')->willReturn($this->roleMock);
     $this->roleMock->expects($this->once())->method('load')->willReturn($this->roleMock);
     $this->roleMock->expects($this->atLeastOnce())->method('getId')->willReturn($roleId);
     $this->dbAdapterMock->expects($this->once())->method('describeTable')->willReturn([1, 2, 3]);
     $this->assertInstanceOf('\\Magento\\User\\Model\\ResourceModel\\User', $this->invokeMethod($this->model, '_afterSave', [$methodUserMock]));
 }
 public function testGetUsersNoRolesAndJsonFalse()
 {
     $roleId = 1;
     $roles = [];
     /** @var \Magento\Authorization\Model\Role|\PHPUnit_Framework_MockObject_MockObject */
     $roleModelMock = $this->getMockBuilder('Magento\\Authorization\\Model\\Role')->disableOriginalConstructor()->setMethods([])->getMock();
     $this->requestInterfaceMock->expects($this->at(0))->method('getParam')->willReturn("");
     $this->requestInterfaceMock->expects($this->at(1))->method('getParam')->willReturn($roleId);
     $this->requestInterfaceMock->expects($this->at(2))->method('getParam')->willReturn($roleId);
     $this->roleFactoryMock->expects($this->once())->method('create')->willReturn($roleModelMock);
     $roleModelMock->expects($this->once())->method('setId')->willReturnSelf();
     $roleModelMock->expects($this->once())->method('getRoleUsers')->willReturn($roles);
     $this->assertEquals($roles, $this->model->getUsers());
 }
Beispiel #8
0
 /**
  * Creates role model
  *
  * @return \Magento\Authorization\Model\Role
  */
 public function createRole()
 {
     return $this->_roleFactory->create();
 }
 /**
  * Remove integration role. This deletes the cascading permissions
  *
  * @param int $integrationId
  * @return \Magento\Authorization\Model\Role
  */
 protected function _deleteRole($integrationId)
 {
     $roleName = UserContextInterface::USER_TYPE_INTEGRATION . $integrationId;
     $role = $this->_roleFactory->create()->load($roleName, 'role_name');
     return $role->delete();
 }