Search in sources :

Example 1 with Accredit

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

the class AccreditManageResource method deleteAccredit.

@Path("accredits/{id}")
@DELETE
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<AccreditVO> deleteAccredit(@QueryParam("userCode") String userCode, @PathParam("id") String id) {
    if (StringUtils.isBlank(userCode) || StringUtils.isBlank(id)) {
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM));
    }
    sessionDataStore.setCurrentUserCode(userCode);
    try {
        Accredit accredit = accreditManageService.closeAccredit(id);
        AccreditVO vo = AccreditVO.transform(accredit, true);
        sessionDataStore.removeCurrentUserCode();
        return new DataVO<>(vo);
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Delete accredit fail.", ex);
        }
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Accredit(org.mx.comps.rbac.dal.entity.Accredit) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) AccreditVO(org.mx.comps.rbac.rest.vo.AccreditVO) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 2 with Accredit

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

the class AccreditManageResource method accredits.

@Path("accredits")
@POST
@AuthenticateAround(returnValueClass = PaginationDataVO.class)
public PaginationDataVO<List<AccreditVO>> accredits(Pagination pagination) {
    if (pagination == null) {
        pagination = new Pagination();
    }
    try {
        List<Accredit> accredits = accessor.list(pagination, Accredit.class);
        List<AccreditVO> list = AccreditVO.transform(accredits);
        return new PaginationDataVO<>(pagination, list);
    } catch (UserInterfaceException ex) {
        return new PaginationDataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("List accredits fail.", ex);
        }
        return new PaginationDataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Pagination(org.mx.dal.Pagination) Accredit(org.mx.comps.rbac.dal.entity.Accredit) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) AccreditVO(org.mx.comps.rbac.rest.vo.AccreditVO) 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 Accredit

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

the class AccreditManageResource method newAccredit.

@Path("accredits/new")
@POST
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<AccreditVO> newAccredit(@QueryParam("userCode") String userCode, AccreditInfoVO accreditInfoVO) {
    sessionDataStore.setCurrentUserCode(userCode);
    try {
        Accredit accredit = accreditManageService.accredit(accreditInfoVO.getAccreditInfo());
        AccreditVO vo = AccreditVO.transform(accredit, true);
        sessionDataStore.removeCurrentUserCode();
        return new DataVO<>(vo);
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Create accredit fail.", ex);
        }
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Accredit(org.mx.comps.rbac.dal.entity.Accredit) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) AccreditVO(org.mx.comps.rbac.rest.vo.AccreditVO) 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 Accredit

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

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

the class AccreditManageServiceImpl method hasSameAccredit.

/**
 * {@inheritDoc}
 *
 * @see AccreditManageServiceCommonImpl#hasSameAccredit(AccreditInfo)
 */
@Override
protected boolean hasSameAccredit(AccreditInfo accreditInfo) {
    List<GeneralAccessor.ConditionTuple> conditions = new ArrayList<>();
    conditions.add(new GeneralAccessor.ConditionTuple("src", accessor.getById(accreditInfo.getSrcAccountId(), Account.class)));
    conditions.add(new GeneralAccessor.ConditionTuple("tar", accessor.getById(accreditInfo.getTarAccountId(), Account.class)));
    conditions.add(new GeneralAccessor.ConditionTuple("valid", true));
    List<Accredit> list = accessor.find(conditions, Accredit.class);
    List<Accredit> accredits = new ArrayList<>();
    if (list != null && !list.isEmpty()) {
        list.forEach(accredit -> {
            if (!accredit.isClosed()) {
                accredits.add(accredit);
            }
        });
    }
    if (accredits.isEmpty()) {
        return false;
    }
    for (Accredit accredit : accredits) {
        if (!accredit.isClosed()) {
            for (String roleId : accreditInfo.getRoleIds()) {
                boolean found = false;
                for (Role role : accredit.getRoles()) {
                    if (roleId.equals(role.getId())) {
                        found = true;
                    }
                }
                if (!found) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Accredit(org.mx.comps.rbac.dal.entity.Accredit) ArrayList(java.util.ArrayList) GeneralAccessor(org.mx.dal.service.GeneralAccessor)

Aggregations

Accredit (org.mx.comps.rbac.dal.entity.Accredit)9 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)7 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)4 Role (org.mx.comps.rbac.dal.entity.Role)4 AccreditVO (org.mx.comps.rbac.rest.vo.AccreditVO)4 UserInterfaceException (org.mx.error.UserInterfaceException)4 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)4 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)3 DataVO (org.mx.service.rest.vo.DataVO)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Account (org.mx.comps.rbac.dal.entity.Account)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 User (org.mx.comps.rbac.dal.entity.User)1 AccountManageService (org.mx.comps.rbac.service.AccountManageService)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1 RoleManageService (org.mx.comps.rbac.service.RoleManageService)1 UserManageService (org.mx.comps.rbac.service.UserManageService)1