use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.
the class FfeeAccountManageServiceImpl method regist.
/**
* 使用用户名、密码方式注册账户。
*
* @see FfeeAccountManageService#regist(String, String, String)
*/
@Override
@Transactional()
public FfeeAccount regist(String code, String name, String password) {
if (StringUtils.isBlank(code) || StringUtils.isBlank(name) || StringUtils.isBlank(password)) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
Account account = accessor.getByCode(code, Account.class);
if (account != null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST);
}
Role role = accessor.getByCode("user", Role.class);
if (role == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
}
account = EntityFactory.createEntity(Account.class);
account.setCode(code);
account.setName(name);
try {
account.setPassword(DigestUtils.md5(password));
} catch (NoSuchAlgorithmException ex) {
if (logger.isErrorEnabled()) {
logger.error("Digest the password fail.", ex);
}
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
}
account = accessor.save(account, false);
FfeeAccount ffeeAccount = EntityFactory.createEntity(FfeeAccount.class);
ffeeAccount.setAccount(account);
ffeeAccount.setSourceType(FfeeAccount.AccountSourceType.NORMAL);
ffeeAccount = accessor.save(ffeeAccount, false);
if (operateLogService != null) {
operateLogService.writeLog(String.format("常规账户[%s]注册成功。", name));
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Create a normal registry FFEE account[%s - %s] successfully.", code, name));
}
return ffeeAccount;
}
use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.
the class TestRole method testRoleAccounts.
@Test
public void testRoleAccounts() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
RoleManageService roleService = context.getBean(RoleManageService.class);
assertNotNull(service);
UserManageService userManageService = context.getBean(UserManageService.class);
assertNotNull(userManageService);
AccountManageService accountManageService = context.getBean(AccountManageService.class);
assertNotNull(accountManageService);
try {
TestUser.testInsertUser(service, userManageService);
TestUser.testEditUser(service, userManageService);
assertEquals(3, service.count(User.class));
TestAccount.testInsertAccount(service, accountManageService);
TestAccount.testEditAccount(service, accountManageService);
testInsertRole(service, roleService);
testEditRole(service, roleService);
assertEquals(3, service.count(Account.class));
assertEquals(3, service.count(Role.class));
Role role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertEquals(0, role1.getAccounts().size());
Account account1 = service.getById(TestAccount.account1Id, Account.class);
Account account2 = service.getById(TestAccount.account2Id, Account.class);
Account account3 = service.getById(TestAccount.account3Id, Account.class);
assertNotNull(account1);
assertNotNull(account2);
assertNotNull(account3);
RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(account1.getId(), account2.getId(), account3.getId()), Arrays.asList(), role1.isValid());
roleService.saveRole(roleInfo);
assertEquals(3, service.count(Role.class));
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertNotNull(role1.getAccounts());
assertEquals(3, role1.getAccounts().size());
assertEquals(new HashSet<>(Arrays.asList(account1, account2, account3)), role1.getAccounts());
roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(account1.getId(), account3.getId()), Arrays.asList(), role1.isValid());
roleService.saveRole(roleInfo);
assertEquals(3, service.count(Role.class));
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertNotNull(role1.getAccounts());
assertEquals(2, role1.getAccounts().size());
assertEquals(new HashSet<>(Arrays.asList(account1, account3)), role1.getAccounts());
roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(), role1.isValid());
roleService.saveRole(roleInfo);
role1 = service.getById(role1Id, Role.class);
assertNotNull(role1);
assertEquals(0, role1.getAccounts().size());
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
use of org.mx.comps.rbac.dal.entity.Role in project main by JohnPeng739.
the class TestRole method testInsertRole.
public static void testInsertRole(GeneralDictAccessor service, RoleManageService roleService) {
RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf("role1", "role1", "desc");
Role role1 = roleService.saveRole(roleInfo);
assertEquals(1, service.count(Role.class));
role1 = service.getById(role1.getId(), Role.class);
assertNotNull(role1);
assertNotNull(role1.getId());
role1Id = role1.getId();
assertEquals("role1", role1.getCode());
assertEquals("role1", role1.getName());
assertEquals("desc", role1.getDesc());
assertTrue(role1.isValid());
roleInfo = RoleManageService.RoleInfo.valueOf("role2", "role2", "desc");
Role role2 = roleService.saveRole(roleInfo);
assertEquals(2, service.count(Role.class));
role2 = service.getById(role2.getId(), Role.class);
assertNotNull(role2);
role2Id = role2.getId();
assertEquals("role2", role2.getCode());
assertEquals("role2", role2.getName());
assertTrue(role2.isValid());
}
use of org.mx.comps.rbac.dal.entity.Role 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.Role in project main by JohnPeng739.
the class TestRole method testDeleteRole.
private void testDeleteRole(GeneralDictAccessor service) {
List<Role> roles = service.list(Role.class);
int roleNum = roles.size();
for (Role role : roles) {
service.remove(role);
assertEquals(--roleNum, service.count(Role.class));
assertEquals(roles.size(), service.count(Role.class, false));
}
roleNum = roles.size();
for (Role role : roles) {
service.remove(role, false);
assertEquals(--roleNum, service.count(Role.class, false));
}
}
Aggregations