From 5b09427b6c534a4e60079f927f524329a3010b6e Mon Sep 17 00:00:00 2001 From: Arpit Mohan Date: Mon, 24 Feb 2020 14:27:26 +0530 Subject: [PATCH] Removing the aspect and trying to implement the application repository directly --- .../appsmith/server/aspects/AclAspect.java | 4 +- .../SoftDeleteMongoQueryLookupStrategy.java | 22 ++++----- .../controllers/ApplicationController.java | 1 + .../repositories/ApplicationRepository.java | 19 ++++++-- .../ApplicationRepositoryImpl.java | 46 +++++++++++++++++++ .../repositories/BaseRepositoryImpl.java | 8 ++-- 6 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepositoryImpl.java diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/aspects/AclAspect.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/aspects/AclAspect.java index a2dad280a8..90f7ebd42c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/aspects/AclAspect.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/aspects/AclAspect.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -@Aspect -@Component +//@Aspect +//@Component @Slf4j public class AclAspect { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/mongo/SoftDeleteMongoQueryLookupStrategy.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/mongo/SoftDeleteMongoQueryLookupStrategy.java index ba37c38802..7f9c2a1450 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/mongo/SoftDeleteMongoQueryLookupStrategy.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/configurations/mongo/SoftDeleteMongoQueryLookupStrategy.java @@ -90,17 +90,17 @@ public class SoftDeleteMongoQueryLookupStrategy implements QueryLookupStrategy { @Override protected Query createQuery(ConvertingParameterAccessor accessor) { - SecurityContext securityContext = SecurityContextHolder.getContext(); -// User userPrincipal = (User) ReactiveSecurityContextHolder.getContext() -// .switchIfEmpty(Mono.error(new Exception("no context"))) -// .map(ctx -> ctx.getAuthentication()) -// .map(auth -> auth.getPrincipal()) -// .map(principal -> { -// if (principal instanceof User) { -// return (User) principal; -// } -// return new User(); -// }).block(); +// SecurityContext securityContext = SecurityContextHolder.getContext(); + User userPrincipal = ReactiveSecurityContextHolder.getContext() + .switchIfEmpty(Mono.error(new Exception("no context"))) + .map(ctx -> ctx.getAuthentication()) + .map(auth -> auth.getPrincipal()) + .map(principal -> { + if (principal instanceof User) { + return (User) principal; + } + return new User(); + }).block(); AclPermission aclPermission = method.getAnnotation(AclPermission.class); if (aclPermission != null) { log.debug("Got principal: {}", aclPermission.principal()); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java index 8b919f479c..e557b0cb3c 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/ApplicationController.java @@ -9,6 +9,7 @@ import com.appsmith.server.services.ApplicationService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepository.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepository.java index 3ae56d8892..553c6a404b 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepository.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepository.java @@ -3,23 +3,32 @@ package com.appsmith.server.repositories; import com.appsmith.server.domains.Application; import com.appsmith.server.services.AclEntity; import org.springframework.data.domain.Example; +import org.springframework.data.repository.NoRepositoryBean; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.ReactiveSecurityContextHolder; import org.springframework.stereotype.Repository; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -@Repository +@NoRepositoryBean @AclEntity("applications") public interface ApplicationRepository extends BaseRepository { - default Mono findByIdAndOrganizationId(String id, String orgId){ + default Mono findByIdAndOrganizationId(String id, String orgId) { System.out.println("In the custom implementation"); - return Mono.empty(); + return ReactiveSecurityContextHolder.getContext() + .map(ctx -> ctx.getAuthentication()) + .map(auth -> auth.getPrincipal()) + .flatMap(principal -> { + System.out.println("Got principal: " + principal); + return Mono.empty(); + }); } Mono findByName(String name); - @Override - Flux findAll(Example example); +// @Override +// Flux findAll(Example example); @Override Mono findById(String id); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepositoryImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepositoryImpl.java new file mode 100644 index 0000000000..60735c2c37 --- /dev/null +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ApplicationRepositoryImpl.java @@ -0,0 +1,46 @@ +package com.appsmith.server.repositories; + +import com.appsmith.server.domains.Application; +import lombok.NonNull; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; +import org.springframework.data.mongodb.core.ReactiveMongoOperations; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.lang.annotation.Annotation; + +import static org.springframework.data.mongodb.core.query.Criteria.where; + +@Component +public class ApplicationRepositoryImpl extends BaseRepositoryImpl implements ApplicationRepository { + + @Autowired + public ApplicationRepositoryImpl(@NonNull MongoEntityInformation entityInformation, + @NonNull ReactiveMongoOperations mongoOperations) { + super(entityInformation, mongoOperations); + } + + @Override + public Mono findByName(String name) { + Query query = new Query(); + query.addCriteria(notDeleted()); + + Annotation[] annotations = entityInformation.getJavaType().getAnnotations(); + return mongoOperations.query(entityInformation.getJavaType()) + .inCollection(entityInformation.getCollectionName()) + .matching(query) + .one(); + } + + @Override + public Flux findAll(Example example) { + Query query = new Query(notDeleted()); + Annotation[] annotations = entityInformation.getJavaType().getAnnotations(); + return mongoOperations.find(query, entityInformation.getJavaType(), entityInformation.getCollectionName()); + } +} diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java index 940fec1c0b..1f04401e0a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/BaseRepositoryImpl.java @@ -38,8 +38,8 @@ import static org.springframework.data.mongodb.core.query.Criteria.where; public class BaseRepositoryImpl extends SimpleReactiveMongoRepository implements BaseRepository { - private final MongoEntityInformation entityInformation; - private final ReactiveMongoOperations mongoOperations; + protected final MongoEntityInformation entityInformation; + protected final ReactiveMongoOperations mongoOperations; public BaseRepositoryImpl(@NonNull MongoEntityInformation entityInformation, @NonNull ReactiveMongoOperations mongoOperations) { @@ -48,14 +48,14 @@ public class BaseRepositoryImpl e this.mongoOperations = mongoOperations; } - private Criteria notDeleted() { + protected Criteria notDeleted() { return new Criteria().orOperator( where("deleted").exists(false), where("deleted").is(false) ); } - private Criteria getIdCriteria(Object id) { + protected Criteria getIdCriteria(Object id) { return where(entityInformation.getIdAttribute()).is(id); }