Skip to content

Commit

Permalink
fix: updateWithoutPermission updates user instead of saving it (#36206)
Browse files Browse the repository at this point in the history
## Description
> UserService$updateWithoutPermission will now update the user instead
of saving it.

Fixes #36063

## Automation

/ok-to-test tags="@tag.Sanity"

### 🔍 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/10786655302>
> Commit: 0eb7f18
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10786655302&attempt=1"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Sanity`
> Spec:
> <hr>Tue, 10 Sep 2024 06:20:46 UTC
<!-- end of auto-generated comment: Cypress test results  -->


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


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


## Summary by CodeRabbit

- **New Features**
	- Enhanced user update functionality for more efficient processing.
- Added a mechanism to ensure user policies remain unchanged during
updates.

- **Bug Fixes**
- Implemented tests to verify that user name updates do not affect
associated policies.

- **Tests**
- Introduced new test methods to validate user update scenarios within
the correct security context.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Nilesh Sarupriya <[email protected]>
  • Loading branch information
nsarupr and nsarupr authored Sep 10, 2024
1 parent 182dbe7 commit 316f914
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.appsmith.server.services.ce;

import com.appsmith.external.helpers.AppsmithBeanUtils;
import com.appsmith.external.helpers.EncryptionHelper;
import com.appsmith.server.acl.AclPermission;
import com.appsmith.server.configurations.CommonConfig;
Expand Down Expand Up @@ -576,8 +575,7 @@ private Mono<User> update(User existingUser, User userUpdate) {
userUpdate.setPassword(passwordEncoder.encode(userUpdate.getPassword()));
}

AppsmithBeanUtils.copyNewFieldValuesIntoOldObject(userUpdate, existingUser);
return repository.save(existingUser);
return repository.updateById(existingUser.getId(), userUpdate, null);
}

private boolean validateName(String name) {
Expand All @@ -603,6 +601,8 @@ public Mono<User> updateCurrentUser(final UserUpdateDTO allUpdates, ServerWebExc
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.NAME));
}
updates.setName(inputName);
// Set policies to null to avoid overriding them.
updates.setPolicies(null);
updatedUserMono = sessionUserService
.getCurrentUser()
.flatMap(user -> updateWithoutPermission(user.getId(), updates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.appsmith.server.domains.TenantConfiguration;
import com.appsmith.server.domains.User;
import com.appsmith.server.domains.UserData;
import com.appsmith.server.domains.UserState;
import com.appsmith.server.domains.Workspace;
import com.appsmith.server.dtos.InviteUsersDTO;
import com.appsmith.server.dtos.ResendEmailVerificationDTO;
Expand All @@ -39,6 +40,9 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -748,4 +752,61 @@ public void updateNameProficiencyAndUseCaseOfUser() {
})
.verifyComplete();
}

private <I> Mono<I> runAs(Mono<I> input, User user, String password) {
log.info("Running as user: {}", user.getEmail());
return input.contextWrite((ctx) -> {
SecurityContext securityContext = new SecurityContextImpl(
new UsernamePasswordAuthenticationToken(user, password, user.getAuthorities()));
return ctx.put(SecurityContext.class, Mono.just(securityContext));
});
}

@Test
@WithUserDetails(value = "api_user")
public void testUpdateCurrentUser_shouldNotUpdatePolicies() {
String testName = "testUpdateName_shouldNotUpdatePolicies";
User user = new User();
user.setEmail(testName + "@test.com");
user.setPassword(testName);
User createdUser = userService.create(user).block();
Set<Policy> policies = createdUser.getPolicies();

assertThat(createdUser.getName()).isNull();
assertThat(createdUser.getPolicies()).isNotEmpty();

UserUpdateDTO updateUser = new UserUpdateDTO();
updateUser.setName("Test Name");

User userUpdatedPostNameUpdate = runAs(userService.updateCurrentUser(updateUser, null), createdUser, testName)
.block();

assertThat(userUpdatedPostNameUpdate.getName()).isEqualTo("Test Name");
userUpdatedPostNameUpdate.getPolicies().forEach(policy -> {
assertThat(policies).contains(policy);
});
}

@Test
@WithUserDetails(value = "api_user")
public void testUpdateWithoutPermission_shouldUpdateChangedFields() {
String testName = "testUpdateWithoutPermission_shouldUpdateChangedFields";
User user = new User();
user.setEmail(testName + "@test.com");
user.setPassword(testName);
User createdUser = userService.create(user).block();
Set<Policy> policies = createdUser.getPolicies();

User update = new User();
update.setName("Test Name");
update.setState(UserState.ACTIVATED);
User updatedUser =
userService.updateWithoutPermission(createdUser.getId(), update).block();

assertThat(updatedUser.getName()).isEqualTo("Test Name");
assertThat(updatedUser.getState()).isEqualTo(UserState.ACTIVATED);
policies.forEach(policy -> {
assertThat(updatedUser.getPolicies()).contains(policy);
});
}
}

0 comments on commit 316f914

Please sign in to comment.