Skip to content

Commit

Permalink
chore: Minor code refactor for seat based pricing (#38359)
Browse files Browse the repository at this point in the history
## 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 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/12490386991>
> Commit: a06c916
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12490386991&attempt=2"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Sanity`
> Spec:
> <hr>Wed, 25 Dec 2024 07:41:48 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

- **New Features**
- Introduced methods for user retrieval and signup conditions, enhancing
user management capabilities.
	- Added functionality to retrieve system-generated user emails.

- **Bug Fixes**
- Improved user lookup process by centralizing logic into dedicated
methods.

- **Documentation**
- Updated method visibility for better accessibility of user-related
functionalities.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
trishaanand authored Dec 25, 2024
1 parent 65391ab commit f078ec0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ private Mono<User> checkAndCreateUser(OAuth2User oAuth2User, OAuth2UserRequest u

String username = oAuth2User.getName();

return repository
.findByEmail(username)
.switchIfEmpty(repository.findFirstByEmailIgnoreCaseOrderByCreatedAtDesc(username))
return this.findByUsername(username)
.switchIfEmpty(Mono.defer(() -> {
User newUser = new User();
newUser.setName(oAuth2User.getName());
Expand All @@ -76,4 +74,10 @@ private Mono<User> checkAndCreateUser(OAuth2User oAuth2User, OAuth2UserRequest u
error -> new AppsmithOAuth2AuthenticationException(
new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), "")));
}

protected Mono<User> findByUsername(String email) {
return repository
.findByEmail(email)
.switchIfEmpty(repository.findFirstByEmailIgnoreCaseOrderByCreatedAtDesc(email));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ public Mono<User> checkAndCreateUser(OidcUser oidcUser, OidcUserRequest userRequ

String username = (!StringUtils.isEmpty(oidcUser.getEmail())) ? oidcUser.getEmail() : oidcUser.getName();

return repository
.findByEmail(username)
.switchIfEmpty(repository.findFirstByEmailIgnoreCaseOrderByCreatedAtDesc(username))
return this.findByUsername(username)
.switchIfEmpty(Mono.defer(() -> {
User newUser = new User();
if (oidcUser.getUserInfo() != null) {
Expand Down Expand Up @@ -82,4 +80,10 @@ public Mono<User> checkAndCreateUser(OidcUser oidcUser, OidcUserRequest userRequ
error -> new AppsmithOAuth2AuthenticationException(
new OAuth2Error(error.getAppErrorCode().toString(), error.getMessage(), "")));
}

protected Mono<User> findByUsername(String email) {
return repository
.findByEmail(email)
.switchIfEmpty(repository.findFirstByEmailIgnoreCaseOrderByCreatedAtDesc(email));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import reactor.core.publisher.Mono;

import java.util.Set;

public interface CustomUserRepositoryCE extends AppsmithRepository<User> {

Mono<User> findByEmail(String email, AclPermission aclPermission);
Expand All @@ -14,5 +16,7 @@ public interface CustomUserRepositoryCE extends AppsmithRepository<User> {

Mono<Boolean> isUsersEmpty();

Set<String> getSystemGeneratedUserEmails();

Mono<Integer> updateById(String id, UpdateDefinition updateObj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public Mono<Boolean> isUsersEmpty() {
.map(count -> count == 0);
}

protected Set<String> getSystemGeneratedUserEmails() {
@Override
public Set<String> getSystemGeneratedUserEmails() {
Set<String> systemGeneratedEmails = new HashSet<>();
systemGeneratedEmails.add(FieldName.ANONYMOUS_USER);
return systemGeneratedEmails;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public interface UserServiceCE extends CrudService<User, String> {

Flux<User> getAllByEmails(Set<String> emails, AclPermission permission);

Mono<User> signupIfAllowed(User user);

Mono<User> updateWithoutPermission(String id, User update);

Mono<Boolean> resendEmailVerification(ResendEmailVerificationDTO resendEmailVerificationDTO, String redirectUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ public Mono<UserSignupDTO> createUser(User user) {
* @param user User object representing the user to be created/enabled.
* @return Publishes the user object, after having been saved.
*/
private Mono<User> signupIfAllowed(User user) {
@Override
public Mono<User> signupIfAllowed(User user) {
boolean isAdminUser = false;

if (!commonConfig.getAdminEmails().contains(user.getEmail())) {
Expand Down

0 comments on commit f078ec0

Please sign in to comment.