use of org.mx.comps.rbac.dal.entity.LoginHistory in project main by JohnPeng739.
the class TestAccount method testLoginAndLogout.
@Test
public void testLoginAndLogout() {
GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
assertNotNull(service);
AccountManageService accountService = context.getBean(AccountManageService.class);
assertNotNull(service);
UserManageService userManageService = context.getBean(UserManageService.class);
assertNotNull(userManageService);
try {
TestUser.testInsertUser(service, userManageService);
TestUser.testEditUser(service, userManageService);
assertEquals(3, service.count(User.class));
testInsertAccount(service, accountService);
testEditAccount(service, accountService);
assertEquals(3, service.count(Account.class));
// 测试正常流程
LoginHistory login = accountService.login("account1", "password", false);
assertNotNull(login);
Account account1 = service.getById(account1Id, Account.class);
assertNotNull(account1);
assertEquals(account1, login.getAccount());
// 测试用户重复登录
login = accountService.login("account1", "password", true);
assertNotNull(login);
assertEquals(account1, login.getAccount());
try {
accountService.login("account1", "password", false);
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_ALREADY_LOGINED.getErrorCode(), ex.getErrorCode());
}
// 测试正常登出
login = accountService.logout(account1.getId());
assertNotNull(login);
assertEquals(account1, login.getAccount());
// 测试用户不存在
try {
accountService.login("abc", "adasd", false);
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorMessage(), ex.getErrorMessage());
}
// 测试密码不正确
try {
accountService.login("account1", "adfasd", false);
} catch (UserInterfaceRbacErrorException ex) {
assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED.getErrorMessage(), ex.getErrorMessage());
}
} catch (Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}
use of org.mx.comps.rbac.dal.entity.LoginHistory in project main by JohnPeng739.
the class AccountManageResource method loginHistories.
@Path("loginHistories")
@POST
@AuthenticateAround(returnValueClass = PaginationDataVO.class)
public PaginationDataVO<List<LoginHistoryVO>> loginHistories(Pagination pagination) {
if (pagination == null) {
pagination = new Pagination();
}
try {
List<LoginHistory> histories = accessor.list(pagination, LoginHistory.class);
List<LoginHistoryVO> vos = LoginHistoryVO.transform(histories);
return new PaginationDataVO(pagination, vos);
} catch (UserInterfaceException ex) {
return new PaginationDataVO<>(ex);
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error("List login histories fail.", ex);
}
return new PaginationDataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
}
}
use of org.mx.comps.rbac.dal.entity.LoginHistory in project main by JohnPeng739.
the class AccountManageResource method logout.
@Path("logout/{id}")
@GET
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<LoginHistoryVO> logout(@PathParam("id") String id, @QueryParam("userCode") String userCode, @Context Request request) {
sessionDataStore.setCurrentUserCode(userCode);
try {
LoginHistory loginHistory = accountManageService.logout(id);
LoginHistoryVO loginHistoryVO = LoginHistoryVO.transform(loginHistory);
sessionDataStore.removeCurrentUserCode();
return new DataVO<>(loginHistoryVO);
} catch (UserInterfaceException ex) {
return new DataVO<>(ex);
} catch (Exception ex) {
if (logger.isErrorEnabled()) {
logger.error(String.format("User[%s] logout fail.", userCode), ex);
}
return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
}
}
use of org.mx.comps.rbac.dal.entity.LoginHistory in project main by JohnPeng739.
the class AccountManageServiceCommonImpl method logout.
/**
* {@inheritDoc}
*
* @see AccountManageService#logout(String)
*/
@Override
public LoginHistory logout(String accountId) {
Account account = accessor.getById(accountId, Account.class);
if (account == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
List<GeneralAccessor.ConditionTuple> tuples = Arrays.asList(new GeneralAccessor.ConditionTuple("account", account), new GeneralAccessor.ConditionTuple("online", true));
List<LoginHistory> loginHistories = accessor.find(tuples, LoginHistory.class);
if (loginHistories == null || loginHistories.isEmpty()) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_LOGIN);
} else {
if (loginHistories.size() > 1) {
// 根据登录时间排序
Collections.sort(loginHistories);
}
LoginHistory loginHistory = loginHistories.get(0);
loginHistory.setLogoutTime(new Date().getTime());
loginHistory.setOnline(false);
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.dal.entity.LoginHistory 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;
}
Aggregations