/support/cas-server-support-consent-mongo/src/main/java/org/apereo/cas/consent/MongoDbConsentRepository.java

https://github.com/Jasig/cas · Java · 73 lines · 54 code · 13 blank · 6 comment · 0 complexity · 9d48d4a2e26637f3485f5023422669c6 MD5 · raw file

  1. package org.apereo.cas.consent;
  2. import org.apereo.cas.authentication.Authentication;
  3. import org.apereo.cas.authentication.principal.Service;
  4. import org.apereo.cas.services.RegisteredService;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.val;
  7. import org.springframework.data.mongodb.core.MongoOperations;
  8. import org.springframework.data.mongodb.core.query.Criteria;
  9. import org.springframework.data.mongodb.core.query.Query;
  10. import java.util.Collection;
  11. /**
  12. * This is {@link MongoDbConsentRepository}.
  13. *
  14. * @author Misagh Moayyed
  15. * @since 5.2.0
  16. */
  17. @RequiredArgsConstructor
  18. public class MongoDbConsentRepository implements ConsentRepository {
  19. private static final long serialVersionUID = 7734163279139907616L;
  20. private final transient MongoOperations mongoTemplate;
  21. private final String collectionName;
  22. @Override
  23. public ConsentDecision findConsentDecision(final Service service,
  24. final RegisteredService registeredService,
  25. final Authentication authentication) {
  26. val query = new Query(Criteria.where("service").is(service.getId())
  27. .and("principal").is(authentication.getPrincipal().getId()));
  28. return this.mongoTemplate.findOne(query, ConsentDecision.class, this.collectionName);
  29. }
  30. @Override
  31. public Collection<? extends ConsentDecision> findConsentDecisions(final String principal) {
  32. val query = new Query(Criteria.where("principal").is(principal));
  33. return this.mongoTemplate.find(query, ConsentDecision.class, this.collectionName);
  34. }
  35. @Override
  36. public Collection<? extends ConsentDecision> findConsentDecisions() {
  37. return this.mongoTemplate.findAll(ConsentDecision.class, this.collectionName);
  38. }
  39. @Override
  40. public ConsentDecision storeConsentDecision(final ConsentDecision decision) {
  41. return this.mongoTemplate.save(decision, this.collectionName);
  42. }
  43. @Override
  44. public boolean deleteConsentDecision(final long decisionId, final String principal) {
  45. val query = new Query(Criteria.where("id").is(decisionId).and("principal").is(principal));
  46. val result = this.mongoTemplate.remove(query, ConsentDecision.class, this.collectionName);
  47. return result.getDeletedCount() > 0;
  48. }
  49. @Override
  50. public boolean deleteConsentDecisions(final String principal) {
  51. val query = new Query(Criteria.where("principal").is(principal));
  52. val result = this.mongoTemplate.remove(query, ConsentDecision.class, this.collectionName);
  53. return result.getDeletedCount() > 0;
  54. }
  55. @Override
  56. public void deleteAll() {
  57. val query = new Query(Criteria.where("principal").exists(true));
  58. mongoTemplate.remove(query, ConsentDecision.class, this.collectionName);
  59. }
  60. }