Skip to content

Commit

Permalink
Merge pull request #38047 from appsmithorg/cherry-pick/idToken-missing
Browse files Browse the repository at this point in the history
fix: Only updating the required fields in User while generating usage…
  • Loading branch information
trishaanand authored Dec 9, 2024
2 parents 5f9f0fb + 299ce06 commit 2199333
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> {
Expand All @@ -12,4 +13,6 @@ public interface CustomUserRepositoryCE extends AppsmithRepository<User> {
Mono<User> findByEmailAndTenantId(String email, String tenantId);

Mono<Boolean> isUsersEmpty();

Mono<Integer> updateById(String id, UpdateDefinition updateObj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,4 +53,12 @@ protected Set<String> getSystemGeneratedUserEmails() {
systemGeneratedEmails.add(FieldName.ANONYMOUS_USER);
return systemGeneratedEmails;
}

@Override
public Mono<Integer> updateById(String id, UpdateDefinition updateObj) {
if (id == null) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ID));
}
return queryBuilder().byId(id).updateFirst(updateObj);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,19 +85,21 @@ public Mono<UsagePulse> 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));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,8 @@ public interface UserServiceCE extends CrudService<User, String> {

Mono<User> userCreate(User user, boolean isAdminUser);

Mono<Integer> updateWithoutPermission(String id, UpdateDefinition updateObj);

Mono<User> updateCurrentUser(UserUpdateDTO updates, ServerWebExchange exchange);

Mono<Boolean> isUsersEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -568,6 +569,15 @@ public Mono<User> updateWithoutPermission(String id, User update) {
return userFromRepository.flatMap(existingUser -> this.update(existingUser, update));
}

@Override
public Mono<Integer> updateWithoutPermission(String id, UpdateDefinition updateObj) {
Mono<User> 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<User> update(User existingUser, User userUpdate) {

// The password is being updated. Hash it first and then store it
Expand Down

0 comments on commit 2199333

Please sign in to comment.