Search in sources :

Example 16 with UserInterfaceSystemErrorException

use of org.mx.error.UserInterfaceSystemErrorException 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 17 with UserInterfaceSystemErrorException

use of org.mx.error.UserInterfaceSystemErrorException 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)

Example 18 with UserInterfaceSystemErrorException

use of org.mx.error.UserInterfaceSystemErrorException 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;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) Accredit(org.mx.comps.rbac.dal.entity.Accredit) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Date(java.util.Date) HashSet(java.util.HashSet)

Example 19 with UserInterfaceSystemErrorException

use of org.mx.error.UserInterfaceSystemErrorException 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;
}
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) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException)

Example 20 with UserInterfaceSystemErrorException

use of org.mx.error.UserInterfaceSystemErrorException 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;
}
Also used : Department(org.mx.comps.rbac.dal.entity.Department) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Date(java.util.Date)

Aggregations

UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)40 UserInterfaceException (org.mx.error.UserInterfaceException)30 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)30 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)27 DataVO (org.mx.service.rest.vo.DataVO)23 Account (org.mx.comps.rbac.dal.entity.Account)12 User (org.mx.comps.rbac.dal.entity.User)10 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)10 Role (org.mx.comps.rbac.dal.entity.Role)9 Accredit (org.mx.comps.rbac.dal.entity.Accredit)7 Pagination (org.mx.dal.Pagination)7 Department (org.mx.comps.rbac.dal.entity.Department)6 Privilege (org.mx.comps.rbac.dal.entity.Privilege)5 UserVO (org.mx.comps.rbac.rest.vo.UserVO)5 Date (java.util.Date)4 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)4 AccreditVO (org.mx.comps.rbac.rest.vo.AccreditVO)4 DepartmentVO (org.mx.comps.rbac.rest.vo.DepartmentVO)4 PrivilegeVO (org.mx.comps.rbac.rest.vo.PrivilegeVO)4 RoleVO (org.mx.comps.rbac.rest.vo.RoleVO)4