Search in sources :

Example 11 with Account

use of org.mx.comps.rbac.dal.entity.Account 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 12 with Account

use of org.mx.comps.rbac.dal.entity.Account 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 13 with Account

use of org.mx.comps.rbac.dal.entity.Account 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 14 with Account

use of org.mx.comps.rbac.dal.entity.Account 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 15 with Account

use of org.mx.comps.rbac.dal.entity.Account in project main by JohnPeng739.

the class InitializeAdminAccountTask method createAccount.

/**
 * 初始化指定的账户
 *
 * @param accessor 实体访问器
 * @param code     代码
 * @param name     名称
 * @param password 密码
 * @param desc     描述
 * @param roleCode 角色代码
 */
private void createAccount(GeneralDictAccessor accessor, String code, String name, String password, String desc, String... roleCode) {
    Set<Role> roles = new HashSet<>();
    if (roleCode != null && roleCode.length > 0) {
        for (int index = 0; index < roleCode.length; index++) {
            Role role = accessor.getByCode(roleCode[index], Role.class);
            if (role == null) {
                if (logger.isErrorEnabled())
                    logger.error(String.format("The role for %s is not existed.", roleCode));
                return;
            }
            roles.add(role);
        }
    }
    Account admin = accessor.getByCode(code, Account.class);
    if (admin == null) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("The account for %s not exist, will create it.", code));
        }
        try {
            admin = EntityFactory.createEntity(Account.class);
            admin.setCode(code);
            admin.setName(name);
            admin.setPassword(DigestUtils.md5(password));
            admin.setRoles(roles);
            admin.setDesc(desc);
            admin.setValid(true);
            accessor.save(admin);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Create the %s account successfully.", code));
            }
        } catch (NoSuchAlgorithmException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(String.format("Create the %s account fail.", code), ex);
            }
        }
    } else {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("The account for %s has existed, this task will ignored.", code));
        }
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HashSet(java.util.HashSet)

Aggregations

Account (org.mx.comps.rbac.dal.entity.Account)26 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)14 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)12 User (org.mx.comps.rbac.dal.entity.User)10 Role (org.mx.comps.rbac.dal.entity.Role)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 AccountManageService (org.mx.comps.rbac.service.AccountManageService)8 Test (org.junit.Test)6 UserManageService (org.mx.comps.rbac.service.UserManageService)6 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)6 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)5 UserInterfaceException (org.mx.error.UserInterfaceException)5 DataVO (org.mx.service.rest.vo.DataVO)5 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)5 Date (java.util.Date)4 HashSet (java.util.HashSet)3 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)3 RoleManageService (org.mx.comps.rbac.service.RoleManageService)3 Accredit (org.mx.comps.rbac.dal.entity.Accredit)2 Privilege (org.mx.comps.rbac.dal.entity.Privilege)2