Search in sources :

Example 1 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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 3 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.

the class AccountManageServiceCommonImpl method saveAccount.

/**
 * {@inheritDoc}
 *
 * @see AccountManageService#saveAccount(AccountInfo)
 */
@Override
public Account saveAccount(AccountInfo accountInfo) {
    if (accountInfo == null) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    try {
        String accountId = accountInfo.getAccountId();
        Account account;
        if (!StringUtils.isBlank(accountId)) {
            account = accessor.getById(accountId, Account.class);
            if (account == null) {
                if (logger.isErrorEnabled()) {
                    logger.error(String.format("The Account entity[%s] not found.", accountId));
                }
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
            }
        // 这里不允许修改密码,密码必须通过另外途径进行修改
        } else {
            String password = accountInfo.getPassword();
            if (StringUtils.isBlank(password)) {
                password = "ds110119";
            }
            account = EntityFactory.createEntity(Account.class);
            account.setPassword(DigestUtils.md5(password));
        }
        account.setCode(accountInfo.getCode());
        if (StringUtils.isBlank(accountInfo.getOwnerId())) {
            if (!"admin".equals(accountInfo.getCode())) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOALLOCATE_USER);
            }
        } else {
            User owner = accessor.getById(accountInfo.getOwnerId(), User.class);
            if (owner == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
            }
            account.setOwner(owner);
            account.setName(owner.getFullName());
        }
        account.setDesc(accountInfo.getDesc());
        if (account.getRoles() != null && !account.getRoles().isEmpty()) {
            account.getRoles().clear();
        }
        for (String roleId : accountInfo.getRoleIds()) {
            Role role = accessor.getById(roleId, Role.class);
            if (role == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
            }
            account.getRoles().add(role);
        }
        account.setValid(accountInfo.isValid());
        account = this.save(account);
        if (operateLogService != null) {
            operateLogService.writeLog(String.format("保存账户[code=%s, name=%s]成功。", account.getCode(), account.getName()));
        }
        return account;
    } catch (UserInterfaceDalErrorException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    } catch (NoSuchAlgorithmException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.

the class AccountManageServiceCommonImpl method changePersonal.

/**
 * {@inheritDoc}
 *
 * @see AccountManageService#changePersonal(AccountPersonalInfo)
 */
@Override
public Account changePersonal(AccountPersonalInfo accountPersonalInfo) {
    Account account = accessor.getById(accountPersonalInfo.getId(), Account.class);
    if (account == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
    }
    account.setFavoriteTools(accountPersonalInfo.getFavoriteTools());
    account = this.save(account);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("修改账户[code=%s, name=%s]的个性化信息成功。", account.getCode(), account.getName()));
    }
    return account;
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException)

Example 5 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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)

Aggregations

UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)16 Account (org.mx.comps.rbac.dal.entity.Account)12 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)10 User (org.mx.comps.rbac.dal.entity.User)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 Date (java.util.Date)5 Role (org.mx.comps.rbac.dal.entity.Role)5 Test (org.junit.Test)3 Accredit (org.mx.comps.rbac.dal.entity.Accredit)3 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)3 AccountManageService (org.mx.comps.rbac.service.AccountManageService)3 UserManageService (org.mx.comps.rbac.service.UserManageService)3 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)3 Department (org.mx.comps.rbac.dal.entity.Department)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (java.text.ParseException)1 HashSet (java.util.HashSet)1 Privilege (org.mx.comps.rbac.dal.entity.Privilege)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1