Skip to content

Commit

Permalink
fix: Only updating the required fields in User while generating usage…
Browse files Browse the repository at this point in the history
… pulse to avoid overwriting default fields. (#38030)

… 

## Description
> [!TIP]  
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/test all

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12229443145>
> Commit: 9f3eebb
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12229443145&attempt=4"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.All`
> Spec:
> <hr>Mon, 09 Dec 2024 08:27:39 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

- **New Features**
- Introduced methods to update user records directly by ID, enhancing
user management capabilities.
- Added functionality to update user information without permission
checks for administrative purposes.

- **Bug Fixes**
- Improved error handling for user updates, ensuring robustness in user
management operations.

- **Documentation**
- Updated documentation to reflect new methods and their functionalities
in user services.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
trishaanand committed Dec 9, 2024
1 parent 5f9f0fb commit 299ce06
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 299ce06

Please sign in to comment.