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));
}
}
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));
}
}
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));
}
}
}
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());
}
}
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));
}
}
Aggregations