use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class TestUser method testAllocateAccount.
@Test
public void testAllocateAccount() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
UserManageService userService = context.getBean(UserManageService.class);
assertNotNull(service);
AccountManageService accountManageService = context.getBean(AccountManageService.class);
assertNotNull(accountManageService);
try {
testInsertUser(service, userService);
testEditUser(service, userService);
assertEquals(3, service.count(User.class));
User john = service.getById(joshId, User.class);
assertNotNull(john);
assertEquals(0, service.count(Account.class));
// 用户不存在
AccountManageService.AccountInfo accountInfo = AccountManageService.AccountInfo.valueOf("john---", "password", "desc", "", "asdfasd", Arrays.asList(), true);
try {
userService.allocateAccount(accountInfo);
fail("here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND.getErrorCode(), ex.getErrorCode());
}
// 正常创建
accountInfo = AccountManageService.AccountInfo.valueOf("John.Peng", "edmund!@#123", "desc", "", john.getId(), Arrays.asList(), true);
Account account = userService.allocateAccount(accountInfo);
assertNotNull(account);
assertEquals(3, service.count(User.class));
assertEquals(1, service.count(Account.class));
account = service.getByCode("John.Peng", Account.class);
assertNotNull(account);
assertNotNull(account.getOwner());
assertEquals(john, account.getOwner());
assertEquals(DigestUtils.md5("edmund!@#123"), account.getPassword());
assertEquals(john.getFullName(), account.getName());
assertEquals("desc", account.getDesc());
assertEquals(0, account.getRoles().size());
// 账户已存在
try {
userService.allocateAccount(accountInfo);
fail("here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST.getErrorCode(), ex.getErrorCode());
}
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class JwtService method init.
/**
* 初始化
*/
private void init() {
if (hasInitialized) {
return;
}
String algorithmName = env.getProperty("auth.algorithm", "HS256");
issue = env.getProperty("auth.issue", "mx institute");
subject = env.getProperty("auth.subject", "everyone");
String expired = env.getProperty("auth.expired", "2 Sec");
expiredClock = env.getProperty("auth.expiredClock", Integer.class, -1);
timePeriod = StringUtils.stirng2TimePeriod(expired, StringUtils.DAY);
try {
String secret = "john_73_9@hotmail.com";
switch(algorithmName) {
case "HS256":
algorithm = Algorithm.HMAC256(secret);
break;
case "HS384":
algorithm = Algorithm.HMAC384(secret);
break;
case "HS512":
algorithm = Algorithm.HMAC512(secret);
break;
default:
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.UNSUPPORTED_ALGORITHM);
}
verifier = JWT.require(algorithm).withIssuer(issue).withSubject(subject).acceptLeeway(1).acceptExpiresAt(1).build();
hasInitialized = true;
if (logger.isDebugEnabled()) {
logger.debug(String.format("Initialize a JWT instance, algorithm: %s, issue: %s, subject: %s.", algorithmName, issue, subject));
}
} catch (UnsupportedEncodingException ex) {
if (logger.isErrorEnabled()) {
logger.error("Initialize JWT fail.", ex);
}
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.UNSUPPORTED_ALGORITHM);
}
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class AccountManageServiceCommonImpl method changePassword.
/**
* {@inheritDoc}
*
* @see AccountManageService#changePassword(String, String, String)
*/
@Override
public Account changePassword(String accountId, String oldPassword, String newPassword) {
Account account = accessor.getById(accountId, Account.class);
if (account == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
try {
if (account.getPassword().equals(DigestUtils.md5(oldPassword))) {
// the old password is matched.
account.setPassword(DigestUtils.md5(newPassword));
account = this.save(account);
if (operateLogService != null) {
operateLogService.writeLog(String.format("修改账户[code=%s, name=%s]的密码成功。", account.getCode(), account.getName()));
}
return account;
} else {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED);
}
} catch (NoSuchAlgorithmException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
}
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class AccreditManageServiceCommonImpl method closeAccredit.
/**
* {@inheritDoc}
*
* @see AccreditManageService#closeAccredit(String)
*/
@Override
public Accredit closeAccredit(String accreditId) {
if (StringUtils.isBlank(accreditId)) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
Accredit accredit = accessor.getById(accreditId, Accredit.class);
if (accredit == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_NOT_FOUND);
}
if (accredit.isClosed()) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_HAS_CLOSED);
}
accredit.setValid(false);
accredit = this.save(accredit);
if (operateLogService != null) {
operateLogService.writeLog(String.format("关闭授权[%s=>%s]成功。", accredit.getSrc().getName(), accredit.getTar().getName()));
}
return accredit;
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class DepartmentManageServiceCommonImpl method saveDepartment.
/**
* {@inheritDoc}
*
* @see DepartmentManageService#saveDepartment(DepartInfo)
*/
@Override
public Department saveDepartment(DepartInfo departInfo) {
if (departInfo == null) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
String id = departInfo.getId();
Department department;
if (!StringUtils.isBlank(id)) {
department = accessor.getById(id, Department.class);
if (department == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.DEPARTMENT_NOT_FOUND);
}
} else {
department = EntityFactory.createEntity(Department.class);
}
department.setCode(departInfo.getCode());
department.setName(departInfo.getName());
department.setDesc(departInfo.getDesc());
if (StringUtils.isBlank(departInfo.getManagerId())) {
department.setManager(null);
} else {
User manager = accessor.getById(departInfo.getManagerId(), User.class);
if (manager == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
}
department.setManager(manager);
}
if (department.getEmployees() != null && !department.getEmployees().isEmpty()) {
department.getEmployees().clear();
}
if (departInfo.getEmployeeIds() != null) {
for (String employeeId : departInfo.getEmployeeIds()) {
User employee = accessor.getById(employeeId, User.class);
if (employee == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
}
department.getEmployees().add(employee);
}
}
department.setValid(departInfo.isValid());
department = this.save(department);
if (operateLogService != null) {
operateLogService.writeLog(String.format("保存部门[code=%s, name=%s]信息成功。", departInfo.getCode(), departInfo.getName()));
}
return department;
}
Aggregations