diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/UserController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/UserController.java index c72d9e2129..e641907bff 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/UserController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/controllers/UserController.java @@ -9,7 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -34,7 +33,7 @@ public class UserController extends BaseController { @PutMapping("/addOrganization/{orgId}") public Mono> addUserToOrganization(@PathVariable String orgId) { - return service.addUserToOrganization(orgId) + return service.addUserToOrganization(orgId, null) .map(user -> new ResponseDTO<>(HttpStatus.OK.value(), user, null)); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java index 2ce088164f..7dd8c79100 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserService.java @@ -9,7 +9,7 @@ public interface UserService extends CrudService { Mono switchCurrentOrganization(String orgId); - Mono addUserToOrganization(String orgId); + Mono addUserToOrganization(String orgId, User user); Mono forgotPasswordTokenGenerate(String email); diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java index ea6f99b68d..b1aa90a049 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/services/UserServiceImpl.java @@ -1,5 +1,6 @@ package com.appsmith.server.services; +import com.appsmith.server.domains.Organization; import com.appsmith.server.domains.PasswordResetToken; import com.appsmith.server.domains.User; import com.appsmith.server.exceptions.AppsmithError; @@ -109,30 +110,46 @@ public class UserServiceImpl extends BaseService i * and operate inside them independently. * * @param orgId The organizationId being added to the user. + * @param user * @return */ @Override - public Mono addUserToOrganization(String orgId) { + public Mono addUserToOrganization(String orgId, User user) { + + Mono currentUserMono; + if (user == null) { + currentUserMono = sessionUserService.getCurrentUser(); + } else { + currentUserMono = Mono.just(user); + } return organizationService.findById(orgId) .switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "organization", orgId))) - .flatMap(org -> sessionUserService.getCurrentUser()) - .map(user -> { - Set organizationIds = user.getOrganizationIds(); + .zipWith(currentUserMono) + .map(tuple -> { + Organization organization = tuple.getT1(); + User user1 = tuple.getT2(); + log.debug("Adding organization {} with id {} to user {}", organization.getName(), organization.getId(), user.getEmail()); + return user1; + }) + .map(user1 -> { + Set organizationIds = user1.getOrganizationIds(); if (organizationIds == null) { organizationIds = new HashSet<>(); - if (user.getCurrentOrganizationId() != null) { + if (user1.getCurrentOrganizationId() != null) { // If the list of organizationIds for a user is null, add the current user org // to the new list as well - organizationIds.add(user.getCurrentOrganizationId()); + organizationIds.add(user1.getCurrentOrganizationId()); } } if (!organizationIds.contains(orgId)) { // Only add to the organizationIds array if it's not already present organizationIds.add(orgId); - user.setOrganizationIds(organizationIds); + user1.setOrganizationIds(organizationIds); } - return user; + // Set the current organization to the newly added organization + user1.setCurrentOrganizationId(orgId); + return user1; }) .flatMap(repository::save); } @@ -231,9 +248,32 @@ public class UserServiceImpl extends BaseService i @Override public Mono create(User user) { user.setPassword(this.passwordEncoder.encode(user.getPassword())); + + Organization personalOrg = new Organization(); + String name; + if (user.getName() != null) { + name = user.getName(); + } else { + name = user.getEmail(); + } + personalOrg.setName(name+"'s Personal Workspace"); + + Mono savedOrganizationMono = organizationService.create(personalOrg); + Mono savedUserMono = super.create(user); - return savedUserMono + + return Mono.zip(savedOrganizationMono, savedUserMono) + //Once the two monos finish emitting, the user and the organization have been saved to the db + .flatMap(tuple -> { + Organization savedOrg = tuple.getT1(); + User savedUser = tuple.getT2(); + + // At this point both the user and the organization have been saved. Now add the newly created + // organization to the newly created user. + return addUserToOrganization(savedOrg.getId(), savedUser); + }) .flatMap(analyticsService::trackNewUser); + } @Override