Search in sources :

Example 11 with Role

use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.

the class RoleManageResource method saveRole.

@Path("roles/new")
@POST
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<RoleVO> saveRole(@QueryParam("userCode") String userCode, RoleInfoVO roleInfoVO) {
    sessionDataStore.setCurrentUserCode(userCode);
    try {
        roleInfoVO.setId(null);
        Role role = roleManageService.saveRole(roleInfoVO.getRoleInfo());
        RoleVO vo = RoleVO.transform(role, true);
        sessionDataStore.removeCurrentUserCode();
        return new DataVO<>(vo);
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Save role fail.", ex);
        }
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) RoleVO(org.mx.comps.rbac.rest.vo.RoleVO) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 12 with Role

use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.

the class RoleManageResource method roles.

@Path("roles")
@POST
@AuthenticateAround(returnValueClass = PaginationDataVO.class)
public PaginationDataVO<List<RoleVO>> roles(Pagination pagination) {
    if (pagination == null) {
        pagination = new Pagination();
    }
    try {
        List<Role> roles = accessor.list(pagination, Role.class);
        List<RoleVO> vos = RoleVO.transform(roles);
        return new PaginationDataVO<>(pagination, vos);
    } catch (UserInterfaceException ex) {
        return new PaginationDataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("List roles fail.", ex);
        }
        return new PaginationDataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Pagination(org.mx.dal.Pagination) RoleVO(org.mx.comps.rbac.rest.vo.RoleVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 13 with Role

use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.

the class InitializeAdminAccountTask method createAccount.

/**
 * 初始化指定的账户
 *
 * @param accessor 实体访问器
 * @param code     代码
 * @param name     名称
 * @param password 密码
 * @param desc     描述
 * @param roleCode 角色代码
 */
private void createAccount(GeneralDictAccessor accessor, String code, String name, String password, String desc, String... roleCode) {
    Set<Role> roles = new HashSet<>();
    if (roleCode != null && roleCode.length > 0) {
        for (int index = 0; index < roleCode.length; index++) {
            Role role = accessor.getByCode(roleCode[index], Role.class);
            if (role == null) {
                if (logger.isErrorEnabled())
                    logger.error(String.format("The role for %s is not existed.", roleCode));
                return;
            }
            roles.add(role);
        }
    }
    Account admin = accessor.getByCode(code, Account.class);
    if (admin == null) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("The account for %s not exist, will create it.", code));
        }
        try {
            admin = EntityFactory.createEntity(Account.class);
            admin.setCode(code);
            admin.setName(name);
            admin.setPassword(DigestUtils.md5(password));
            admin.setRoles(roles);
            admin.setDesc(desc);
            admin.setValid(true);
            accessor.save(admin);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Create the %s account successfully.", code));
            }
        } catch (NoSuchAlgorithmException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Create the %s account fail.", code), ex);
            }
        }
    } else {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("The account for %s has existed, this task will ignored.", code));
        }
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HashSet(java.util.HashSet)

Example 14 with Role

use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.

the class TestPrivilege method testPrivilegeRoles.

@Test
public void testPrivilegeRoles() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    RoleManageService roleManageService = context.getBean(RoleManageService.class);
    assertNotNull(roleManageService);
    try {
        TestRole.testInsertRole(service, roleManageService);
        TestRole.testEditRole(service, roleManageService);
        testInsertPrivilege(service);
        testEditPrivilege(service);
        assertEquals(3, service.count(Role.class));
        assertEquals(3, service.count(Privilege.class));
        Privilege p1 = service.getById(p1Id, Privilege.class);
        assertNotNull(p1);
        assertTrue(p1.getRoles().isEmpty());
        Role role1 = service.getById(TestRole.role1Id, Role.class);
        Role role2 = service.getById(TestRole.role2Id, Role.class);
        Role role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role1);
        assertTrue(role1.getPrivileges().isEmpty());
        assertNotNull(role2);
        assertTrue(role2.getPrivileges().isEmpty());
        assertNotNull(role3);
        assertTrue(role3.getPrivileges().isEmpty());
        p1.getRoles().add(role1);
        p1.getRoles().add(role2);
        p1.getRoles().add(role3);
        service.save(p1);
        p1 = service.getById(p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(3, p1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), p1.getRoles());
        role1 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(1, role1.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1)), role1.getPrivileges());
        role2 = service.getById(TestRole.role2Id, Role.class);
        assertNotNull(role2);
        assertEquals(1, role2.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1)), role2.getPrivileges());
        role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role3);
        assertEquals(1, role3.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1)), role3.getPrivileges());
        p1.getRoles().remove(role2);
        service.save(p1);
        p1 = service.getById(p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(2, p1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), p1.getRoles());
        role1 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(1, role1.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1)), role1.getPrivileges());
        role2 = service.getById(TestRole.role2Id, Role.class);
        assertNotNull(role2);
        assertEquals(0, role2.getPrivileges().size());
        role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role3);
        assertEquals(1, role3.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1)), role3.getPrivileges());
        p1.getRoles().clear();
        service.save(p1);
        p1 = service.getById(p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(0, p1.getRoles().size());
        role1 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(0, role1.getPrivileges().size());
        role2 = service.getById(TestRole.role2Id, Role.class);
        assertNotNull(role2);
        assertEquals(0, role2.getPrivileges().size());
        role3 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role3);
        assertEquals(0, role3.getPrivileges().size());
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) RoleManageService(org.mx.comps.rbac.service.RoleManageService) Privilege(org.mx.comps.rbac.dal.entity.Privilege) Test(org.junit.Test)

Example 15 with Role

use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.

the class RoleManageResource method getRole.

@Path("roles/{id}")
@GET
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<RoleVO> getRole(@PathParam("id") String id) {
    try {
        Role role = accessor.getById(id, Role.class);
        RoleVO vo = RoleVO.transform(role, true);
        return new DataVO<>(vo);
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Get role fail.", ex);
        }
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) RoleVO(org.mx.comps.rbac.rest.vo.RoleVO) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Aggregations

Role (org.mx.comps.rbac.dal.entity.Role)22 Account (org.mx.comps.rbac.dal.entity.Account)9 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)9 RoleManageService (org.mx.comps.rbac.service.RoleManageService)7 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)6 Test (org.junit.Test)5 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashSet (java.util.HashSet)4 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)4 Accredit (org.mx.comps.rbac.dal.entity.Accredit)4 Privilege (org.mx.comps.rbac.dal.entity.Privilege)4 User (org.mx.comps.rbac.dal.entity.User)4 RoleVO (org.mx.comps.rbac.rest.vo.RoleVO)4 UserInterfaceException (org.mx.error.UserInterfaceException)4 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)4 AccountManageService (org.mx.comps.rbac.service.AccountManageService)3 UserManageService (org.mx.comps.rbac.service.UserManageService)3 DataVO (org.mx.service.rest.vo.DataVO)3 ArrayList (java.util.ArrayList)2