diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCE.java index d071b5e04e7..6ceef7aed4a 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCE.java @@ -3,6 +3,7 @@ import com.appsmith.server.acl.AclPermission; import com.appsmith.server.domains.User; import com.appsmith.server.repositories.AppsmithRepository; +import org.springframework.data.mongodb.core.query.UpdateDefinition; import reactor.core.publisher.Mono; public interface CustomUserRepositoryCE extends AppsmithRepository { @@ -12,4 +13,6 @@ public interface CustomUserRepositoryCE extends AppsmithRepository { Mono findByEmailAndTenantId(String email, String tenantId); Mono isUsersEmpty(); + + Mono updateById(String id, UpdateDefinition updateObj); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCEImpl.java index 8508431a725..b09ac27cc25 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomUserRepositoryCEImpl.java @@ -3,11 +3,14 @@ import com.appsmith.server.acl.AclPermission; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.User; +import com.appsmith.server.exceptions.AppsmithError; +import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.helpers.ce.bridge.Bridge; import com.appsmith.server.helpers.ce.bridge.BridgeQuery; import com.appsmith.server.projections.IdOnly; import com.appsmith.server.repositories.BaseAppsmithRepositoryImpl; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.mongodb.core.query.UpdateDefinition; import reactor.core.publisher.Mono; import java.util.HashSet; @@ -50,4 +53,12 @@ protected Set getSystemGeneratedUserEmails() { systemGeneratedEmails.add(FieldName.ANONYMOUS_USER); return systemGeneratedEmails; } + + @Override + public Mono updateById(String id, UpdateDefinition updateObj) { + if (id == null) { + return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ID)); + } + return queryBuilder().byId(id).updateFirst(updateObj); + } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UsagePulseServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UsagePulseServiceCEImpl.java index 1d15be8c900..55027b06933 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UsagePulseServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UsagePulseServiceCEImpl.java @@ -7,6 +7,8 @@ import com.appsmith.server.dtos.UsagePulseDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; +import com.appsmith.server.helpers.ce.bridge.Bridge; +import com.appsmith.server.helpers.ce.bridge.BridgeUpdate; import com.appsmith.server.repositories.UsagePulseRepository; import com.appsmith.server.services.ConfigService; import com.appsmith.server.services.SessionUserService; @@ -83,19 +85,21 @@ public Mono createPulse(UsagePulseDTO usagePulseDTO) { return save(usagePulse); } usagePulse.setIsAnonymousUser(false); - User updateUser = new User(); + BridgeUpdate updateUserObj = Bridge.update(); + String hashedEmail = user.getHashedEmail(); if (StringUtils.isEmpty(hashedEmail)) { hashedEmail = DigestUtils.sha256Hex(user.getEmail()); // Hashed user email is stored to user for future mapping of user and pulses - updateUser.setHashedEmail(hashedEmail); + updateUserObj.set(User.Fields.hashedEmail, hashedEmail); } usagePulse.setUser(hashedEmail); - updateUser.setLastActiveAt(Instant.now()); - // Avoid updating policies - updateUser.setPolicies(null); - return userService.updateWithoutPermission(user.getId(), updateUser).then(save(usagePulse)); + updateUserObj.set(User.Fields.lastActiveAt, Instant.now()); + + return userService + .updateWithoutPermission(user.getId(), updateUserObj) + .then(save(usagePulse)); }); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCE.java index 3c489065891..2bdb85f6780 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCE.java @@ -8,6 +8,7 @@ import com.appsmith.server.dtos.UserSignupDTO; import com.appsmith.server.dtos.UserUpdateDTO; import com.appsmith.server.services.CrudService; +import org.springframework.data.mongodb.core.query.UpdateDefinition; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -30,6 +31,8 @@ public interface UserServiceCE extends CrudService { Mono userCreate(User user, boolean isAdminUser); + Mono updateWithoutPermission(String id, UpdateDefinition updateObj); + Mono updateCurrentUser(UserUpdateDTO updates, ServerWebExchange exchange); Mono isUsersEmpty(); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java index 9cede7c2f62..35d24c352aa 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/ce/UserServiceCEImpl.java @@ -43,6 +43,7 @@ import org.apache.hc.core5.http.message.BasicNameValuePair; import org.apache.hc.core5.net.WWWFormCodec; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.query.UpdateDefinition; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.ReactiveSecurityContextHolder; @@ -568,6 +569,15 @@ public Mono updateWithoutPermission(String id, User update) { return userFromRepository.flatMap(existingUser -> this.update(existingUser, update)); } + @Override + public Mono updateWithoutPermission(String id, UpdateDefinition updateObj) { + Mono userFromRepository = repository + .findById(id) + .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, FieldName.USER, id))); + + return userFromRepository.flatMap(existingUser -> repository.updateById(id, updateObj)); + } + private Mono update(User existingUser, User userUpdate) { // The password is being updated. Hash it first and then store it