使用DTO时如何限制对Jhipster中其他用户数据的访问?

时间:2018-07-12 11:19:06

标签: jhipster

当我需要限制访问Jhipster中其他用户的数据时,我使用SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)这样修改服务,并相应地创建方法:

@Transactional(readOnly = true)
public Page<ProductOrder> findAll(Pageable pageable) {
    log.debug("Request to get all ProductOrders");
    if (SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        return productOrderRepository.findAll(pageable);
    } else
        return productOrderRepository.findAllByCustomerUserLogin(
            SecurityUtils.getCurrentUserLogin().get(),
            pageable);
}

但是,如果我像JHipster自动创建的那样使用DTO,该怎么办?因为PostServiceImplementation没有与存储库进行对话

这是PostServiceImpl:

@Service
@Transactional
public class PostServiceImpl implements PostService {
private final Logger log = LoggerFactory.getLogger(PostServiceImpl.class);

private final PostRepository postRepository;

private final PostMapper postMapper;

public PostServiceImpl(PostRepository postRepository, PostMapper postMapper) {
    this.postRepository = postRepository;
    this.postMapper = postMapper;
}


@Override
public PostDTO save(PostDTO postDTO) {
    log.debug("Request to save Post : {}", postDTO);
    Post post = postMapper.toEntity(postDTO);
    post = postRepository.save(post);
    return postMapper.toDto(post);
}

@Override
@Transactional(readOnly = true)
public Page<PostDTO> findAll(Pageable pageable) {
    log.debug("Request to get all Posts");
    return postRepository.findAll(pageable)
        .map(postMapper::toDto);
}

@Override
@Transactional(readOnly = true)
public Optional<PostDTO> findOne(Long id) {
    log.debug("Request to get Post : {}", id);
    return postRepository.findById(id)
        .map(postMapper::toDto);
}

@Override
public void delete(Long id) {
    log.debug("Request to delete Post : {}", id);
    postRepository.deleteById(id);
}
}

这是PostService:

public interface PostService {

PostDTO save(PostDTO postDTO);

Page<PostDTO> findAll(Pageable pageable);

Optional<PostDTO> findOne(Long id);

void delete(Long id);
}

但是我看不到如何包括 ** SecurityUtils.getCurrentUserLogin()。get(),可分页**
进入PostQueryService条件!

 @Service
@Transactional(readOnly = true)
public class PostQueryService extends QueryService<Post> {

private final Logger log = LoggerFactory.getLogger(PostQueryService.class);

private final PostRepository postRepository;

private final PostMapper postMapper;

public PostQueryService(PostRepository postRepository, PostMapper postMapper) {
    this.postRepository = postRepository;
    this.postMapper = postMapper;
}

/**
 * Return a {@link List} of {@link PostDTO} which matches the criteria from the database
 * @param criteria The object which holds all the filters, which the entities should match.
 * @return the matching entities.
 */
@Transactional(readOnly = true)
public List<PostDTO> findByCriteria(PostCriteria criteria) {
    log.debug("find by criteria : {}", criteria);
    final Specification<Post> specification = createSpecification(criteria);
    return postMapper.toDto(postRepository.findAll(specification));
}

/**
 * Return a {@link Page} of {@link PostDTO} which matches the criteria from the database
 * @param criteria The object which holds all the filters, which the entities should match.
 * @param page The page, which should be returned.
 * @return the matching entities.
 */
@Transactional(readOnly = true)
public Page<PostDTO> findByCriteria(PostCriteria criteria, Pageable page) {
    log.debug("find by criteria : {}, page: {}", criteria, page);
    final Specification<Post> specification = createSpecification(criteria);
    return postRepository.findAll(specification, page)
        .map(postMapper::toDto);
}

/**
 * Function to convert PostCriteria to a {@link Specification}
 */
private Specification<Post> createSpecification(PostCriteria criteria) {
    Specification<Post> specification = Specification.where(null);
    if (criteria != null) {
        if (criteria.getId() != null) {
            specification = specification.and(buildSpecification(criteria.getId(), Post_.id));
        }
        if (criteria.getCreationDate() != null) {
            specification = specification.and(buildRangeSpecification(criteria.getCreationDate(), Post_.creationDate));
        }
        if (criteria.getPublicationDate() != null) {
            specification = specification.and(buildRangeSpecification(criteria.getPublicationDate(), Post_.publicationDate));
        }
        if (criteria.getHeadline() != null) {
            specification = specification.and(buildStringSpecification(criteria.getHeadline(), Post_.headline));
        }
        if (criteria.getLeadtext() != null) {
            specification = specification.and(buildStringSpecification(criteria.getLeadtext(), Post_.leadtext));
        }
        if (criteria.getBodytext() != null) {
            specification = specification.and(buildStringSpecification(criteria.getBodytext(), Post_.bodytext));
        }
        if (criteria.getQuote() != null) {
            specification = specification.and(buildStringSpecification(criteria.getQuote(), Post_.quote));
        }
        if (criteria.getConclusion() != null) {
            specification = specification.and(buildStringSpecification(criteria.getConclusion(), Post_.conclusion));
        }
        if (criteria.getCommentId() != null) {
            specification = specification.and(buildReferringEntitySpecification(criteria.getCommentId(), Post_.comments, Comment_.id));
        }
        if (criteria.getBlogId() != null) {
            specification = specification.and(buildReferringEntitySpecification(criteria.getBlogId(), Post_.blog, Blog_.id));
        }
        if (criteria.getTagId() != null) {
            specification = specification.and(buildReferringEntitySpecification(criteria.getTagId(), Post_.tags, Tag_.id));
        }
        if (criteria.getTopicId() != null) {
            specification = specification.and(buildReferringEntitySpecification(criteria.getTopicId(), Post_.topics, Topic_.id));
        }
    }
    return specification;
}
}

0 个答案:

没有答案