use of org.mx.comps.rbac.dal.entity.Privilege in project main by JohnPeng739.
the class TestRole method testRolePrivileges.
@Test
public void testRolePrivileges() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
RoleManageService roleService = context.getBean(RoleManageService.class);
assertNotNull(service);
try {
TestPrivilege.testInsertPrivilege(service);
TestPrivilege.testEditPrivilege(service);
testInsertRole(service, roleService);
testEditRole(service, roleService);
assertEquals(3, service.count(Role.class));
assertEquals(3, service.count(Privilege.class));
Role role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertTrue(role1.getPrivileges().isEmpty());
Privilege p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
assertNotNull(p1);
assertTrue(p1.getRoles().isEmpty());
Privilege p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
assertNotNull(p2);
assertTrue(p2.getRoles().isEmpty());
Privilege p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
assertNotNull(p3);
assertTrue(p3.getRoles().isEmpty());
RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(p1.getId(), p2.getId(), p3.getId()), role1.isValid());
roleService.saveRole(roleInfo);
assertEquals(3, service.count(Role.class));
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertEquals(3, role1.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1, p2, p3)), role1.getPrivileges());
p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(1, p1.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1)), p1.getRoles());
p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
assertNotNull(p2);
assertEquals(1, p2.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1)), p2.getRoles());
p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
assertNotNull(p3);
assertEquals(1, p3.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1)), p3.getRoles());
roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(p1.getId(), p3.getId()), role1.isValid());
roleService.saveRole(roleInfo);
assertEquals(3, service.count(Role.class));
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertEquals(2, role1.getPrivileges().size());
assertEquals(new HashSet<>(Arrays.asList(p1, p3)), role1.getPrivileges());
p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(1, p1.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1)), p1.getRoles());
p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
assertNotNull(p2);
assertEquals(0, p2.getRoles().size());
p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
assertNotNull(p3);
assertEquals(1, p3.getRoles().size());
assertEquals(new HashSet<>(Arrays.asList(role1)), p3.getRoles());
roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(), role1.isValid());
roleService.saveRole(roleInfo);
assertEquals(3, service.count(Role.class));
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertEquals(0, role1.getPrivileges().size());
p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
assertNotNull(p1);
assertEquals(0, p1.getRoles().size());
p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
assertNotNull(p2);
assertEquals(0, p2.getRoles().size());
p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
assertNotNull(p3);
assertEquals(0, p3.getRoles().size());
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
use of org.mx.comps.rbac.dal.entity.Privilege in project main by JohnPeng739.
the class PrivilegeManageResource method savePrivilege.
@Path("privilege/{id}")
@DELETE
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<PrivilegeVO> savePrivilege(@QueryParam("userCode") String userCode, @PathParam("id") String id) {
sessionDataStore.setCurrentUserCode(userCode);
try {
Privilege privilege = accessor.remove(id, Privilege.class);
PrivilegeVO vo = PrivilegeVO.transform(privilege, true);
sessionDataStore.removeCurrentUserCode();
return new DataVO<>(vo);
} catch (UserInterfaceException ex) {
return new DataVO<>(ex);
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("Save privilege fail.", ex);
}
return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
}
}
use of org.mx.comps.rbac.dal.entity.Privilege in project main by JohnPeng739.
the class PrivilegeManageResource method privileges.
@Path("privileges")
@POST
@AuthenticateAround(returnValueClass = PaginationDataVO.class)
public PaginationDataVO<List<PrivilegeVO>> privileges(Pagination pagination) {
if (pagination == null) {
pagination = new Pagination();
}
try {
List<Privilege> privileges = accessor.list(pagination, Privilege.class);
List<PrivilegeVO> vos = PrivilegeVO.transform(privileges);
return new PaginationDataVO<>(pagination, vos);
} catch (UserInterfaceException ex) {
return new PaginationDataVO<>(ex);
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("List privileges fail.", ex);
}
return new PaginationDataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
}
}
use of org.mx.comps.rbac.dal.entity.Privilege in project main by JohnPeng739.
the class PrivilegeManageResource method savePrivilegeInfo.
private DataVO<PrivilegeVO> savePrivilegeInfo(PrivilegeVO privilegeVO) {
try {
Privilege privilege = EntityFactory.createEntity(Privilege.class);
PrivilegeVO.transform(privilegeVO, privilege);
privilege = accessor.save(privilege);
PrivilegeVO vo = PrivilegeVO.transform(privilege, true);
sessionDataStore.removeCurrentUserCode();
return new DataVO<>(vo);
} catch (UserInterfaceException ex) {
return new DataVO<>(ex);
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("Save privilege fail.", ex);
}
return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
}
}
use of org.mx.comps.rbac.dal.entity.Privilege 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());
}
}
Aggregations