Search in sources :

Example 1 with LoginHistory

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());
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 2 with LoginHistory

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));
    }
}
Also used : Pagination(org.mx.dal.Pagination) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 3 with LoginHistory

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));
    }
}
Also used : LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 4 with LoginHistory

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;
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) GeneralAccessor(org.mx.dal.service.GeneralAccessor) Date(java.util.Date)

Example 5 with 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;
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) GeneralAccessor(org.mx.dal.service.GeneralAccessor) Date(java.util.Date)

Aggregations

LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)6 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)4 Account (org.mx.comps.rbac.dal.entity.Account)3 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)3 UserInterfaceException (org.mx.error.UserInterfaceException)3 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Date (java.util.Date)2 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 DataVO (org.mx.service.rest.vo.DataVO)2 Test (org.junit.Test)1 User (org.mx.comps.rbac.dal.entity.User)1 AccountManageService (org.mx.comps.rbac.service.AccountManageService)1 UserManageService (org.mx.comps.rbac.service.UserManageService)1 Pagination (org.mx.dal.Pagination)1 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)1