use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class AccountManageServiceCommonImpl method login.
/**
* {@inheritDoc}
*
* @see AccountManageService#login(String, String, boolean)
*/
@Override
public LoginHistory login(String accountCode, String password, boolean forced) {
Account account = accessor.getByCode(accountCode, Account.class);
if (account == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
try {
if (!DigestUtils.md5(password).equals(account.getPassword())) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED);
}
} catch (NoSuchAlgorithmException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_UNSUPPORTED_OPERATE);
}
List<GeneralAccessor.ConditionTuple> tuples = Arrays.asList(new GeneralAccessor.ConditionTuple("account", account), new GeneralAccessor.ConditionTuple("online", true));
List<LoginHistory> loginHistories = accessor.find(tuples, LoginHistory.class);
LoginHistory loginHistory;
if (loginHistories != null && !loginHistories.isEmpty()) {
// 已经登录
if (forced) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("The account[%s] has login, now login again.", accountCode));
}
// 强制重新登录
if (loginHistories.size() > 1) {
// 根据登录时间排序
Collections.sort(loginHistories);
}
loginHistory = loginHistories.get(0);
} else {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_ALREADY_LOGINED);
}
} else {
// 新登录
loginHistory = EntityFactory.createEntity(LoginHistory.class);
loginHistory.setAccount(account);
}
loginHistory.setLoginTime(new Date().getTime());
loginHistory.setOnline(true);
// 设置令牌
loginHistory.setToken(jwtService.sign(account.getCode()));
loginHistory = accessor.save(loginHistory, false);
if (operateLogService != null) {
operateLogService.writeLog(String.format("账户[code=%s, name=%s]登录系统成功。", account.getCode(), account.getName()));
}
return loginHistory;
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class AccreditManageServiceCommonImpl method accredit.
/**
* {@inheritDoc}
*
* @see AccreditManageService#accredit(AccreditInfo)
*/
@Override
public Accredit accredit(AccreditInfo accreditInfo) {
if (accreditInfo == null || StringUtils.isBlank(accreditInfo.getSrcAccountId()) || StringUtils.isBlank(accreditInfo.getTarAccountId()) || accreditInfo.getRoleIds() == null || accreditInfo.getRoleIds().isEmpty()) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
// 判断是否存在相同的有效授权
if (hasSameAccredit(accreditInfo)) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_SAME_FOUND);
}
Account src = accessor.getById(accreditInfo.getSrcAccountId(), Account.class);
if (src == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
Account tar = accessor.getById(accreditInfo.getTarAccountId(), Account.class);
if (tar == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
Set<Role> roles = new HashSet<>();
for (String roleId : accreditInfo.getRoleIds()) {
Role role = accessor.getById(roleId, Role.class);
if (role == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
}
roles.add(role);
}
Accredit accredit = EntityFactory.createEntity(Accredit.class);
accredit.setSrc(src);
accredit.setTar(tar);
accredit.setRoles(roles);
accredit.setStartTime(new Date(accreditInfo.getStartTime()));
if (accreditInfo.getEndTime() > 0 && accreditInfo.getEndTime() > accreditInfo.getStartTime()) {
accredit.setEndTime(new Date(accreditInfo.getEndTime()));
}
accredit.setValid(true);
accredit.setDesc(accreditInfo.getDesc());
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 UserManageServiceCommonImpl method allocateAccount.
/**
* {@inheritDoc}
*
* @see UserManageService#allocateAccount(AccountManageService.AccountInfo)
*/
@Override
public Account allocateAccount(AccountManageService.AccountInfo accountInfo) {
if (accountInfo == null || StringUtils.isBlank(accountInfo.getOwnerId()) || StringUtils.isBlank(accountInfo.getCode())) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
User user = accessor.getById(accountInfo.getOwnerId(), User.class);
if (user == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
}
Account account = accessor.getByCode(accountInfo.getCode(), Account.class);
if (account != null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST);
}
account = accountManageService.saveAccount(accountInfo);
if (operateLogService != null) {
operateLogService.writeLog(String.format("为用户[%s]分配账户[%s]成功。", user.getFullName(), account.getCode()));
}
return account;
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class UserManageServiceCommonImpl method saveUser.
/**
* {@inheritDoc}
*
* @see UserManageService#saveUser(UserInfo)
*/
@Override
public User saveUser(UserInfo userInfo) {
if (userInfo == null) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
String userId = userInfo.getUserId();
User user;
if (!StringUtils.isBlank(userId)) {
user = accessor.getById(userId, User.class);
if (user == null) {
if (logger.isErrorEnabled()) {
logger.error(String.format("The User entity[%s] not found.", userId));
}
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
}
} else {
user = EntityFactory.createEntity(User.class);
}
if (!StringUtils.isBlank(userInfo.getDepartId())) {
Department depart = accessor.getById(userInfo.getDepartId(), Department.class);
if (depart == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.DEPARTMENT_NOT_FOUND);
}
user.setDepartment(depart);
} else {
user.setDepartment(null);
}
long birthday = userInfo.getBirthday();
if (birthday > 0) {
user.setBirthday(new Date(birthday));
}
user.setDesc(userInfo.getDesc());
user.setFirstName(userInfo.getFirstName());
user.setMiddleName(userInfo.getMiddleName());
user.setLastName(userInfo.getLastName());
user.setSex(userInfo.getSex());
user.setStation(userInfo.getStation());
user.setValid(userInfo.isValid());
user = this.save(user);
if (operateLogService != null) {
operateLogService.writeLog(String.format("保存用户[name=%s]信息成功。", user.getFullName()));
}
return user;
}
use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.
the class TestAccredit method testAccredit.
@Test
public void testAccredit() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
AccreditManageService accreditService = context.getBean(AccreditManageService.class);
assertNotNull(service);
UserManageService userManageService = context.getBean(UserManageService.class);
assertNotNull(userManageService);
AccountManageService accountManageService = context.getBean(AccountManageService.class);
assertNotNull(accountManageService);
RoleManageService roleManageService = context.getBean(RoleManageService.class);
assertNotNull(roleManageService);
try {
TestUser.testInsertUser(service, userManageService);
TestUser.testEditUser(service, userManageService);
assertEquals(3, service.count(User.class));
TestAccount.testInsertAccount(service, accountManageService);
TestAccount.testEditAccount(service, accountManageService);
TestRole.testInsertRole(service, roleManageService);
TestRole.testEditRole(service, roleManageService);
assertEquals(3, service.count(Account.class));
assertEquals(3, service.count(Role.class));
assertEquals(0, service.count(Accredit.class));
Account account1 = service.getById(TestAccount.account1Id, Account.class);
assertNotNull(account1);
Account account2 = service.getById(TestAccount.account2Id, Account.class);
assertNotNull(account2);
Role role1 = service.getById(TestRole.role1Id, Role.class);
assertNotNull(role1);
Role role2 = service.getById(TestRole.role2Id, Role.class);
assertNotNull(role2);
Role role3 = service.getById(TestRole.role3Id, Role.class);
assertNotNull(role3);
long startTime = new Date().getTime();
AccreditManageService.AccreditInfo accreditInfo;
// 测试没有设置源
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf("", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceSystemErrorException ex) {
assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceSystemErrorException ex) {
assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, null, startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceSystemErrorException ex) {
assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceSystemErrorException ex) {
assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf("abcde", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "abcde", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
}
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, "abcdef", TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND.getErrorCode(), ex.getErrorCode());
}
// 测试正常授权
long endTime = new Date().getTime() + 500;
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, endTime, "desc");
Accredit accredit = accreditService.accredit(accreditInfo);
assertEquals(1, service.count(Accredit.class));
assertNotNull(accredit);
assertEquals(account1, accredit.getSrc());
assertEquals(account2, accredit.getTar());
assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
assertEquals(startTime, accredit.getStartTime().getTime());
assertEquals(endTime, accredit.getEndTime().getTime());
assertTrue(accredit.isValid());
assertEquals("desc", accredit.getDesc());
accredit = service.getById(accredit.getId(), Accredit.class);
assertNotNull(accredit);
assertEquals(account1, accredit.getSrc());
assertEquals(account2, accredit.getTar());
assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
assertEquals(startTime, accredit.getStartTime().getTime());
assertEquals(endTime, accredit.getEndTime().getTime());
assertTrue(accredit.isValid());
assertEquals("desc", accredit.getDesc());
// 测试重复授权
try {
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
accreditService.accredit(accreditInfo);
fail("Here need a exception");
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_SAME_FOUND.getErrorCode(), ex.getErrorCode());
}
// 测试自动时间到达后关闭
Thread.sleep(600);
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
accredit = accreditService.accredit(accreditInfo);
assertEquals(2, service.count(Accredit.class));
assertEquals(2, service.count(Accredit.class, false));
accredit = service.getById(accredit.getId(), Accredit.class);
assertNotNull(accredit);
assertEquals(account1, accredit.getSrc());
assertEquals(account2, accredit.getTar());
assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
assertEquals(startTime, accredit.getStartTime().getTime());
assertNull(accredit.getEndTime());
assertTrue(accredit.isValid());
assertEquals("desc", accredit.getDesc());
// 测试关闭
accreditService.closeAccredit(accredit.getId());
assertEquals(1, service.count(Accredit.class));
assertEquals(2, service.count(Accredit.class, false));
// 再次授权
accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
accredit = accreditService.accredit(accreditInfo);
assertEquals(2, service.count(Accredit.class));
assertEquals(3, service.count(Accredit.class, false));
assertNotNull(accredit);
accredit = service.getById(accredit.getId(), Accredit.class);
assertNotNull(accredit);
assertEquals(account1, accredit.getSrc());
assertEquals(account2, accredit.getTar());
assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
assertEquals(startTime, accredit.getStartTime().getTime());
assertNull(accredit.getEndTime());
assertTrue(accredit.isValid());
assertEquals("desc", accredit.getDesc());
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
Aggregations