From 2cea526bc612ce7d8b01aa0b9e7f4729ee9aadac Mon Sep 17 00:00:00 2001 From: siarhei_hrabko Date: Wed, 14 Aug 2024 23:09:15 +0300 Subject: [PATCH] EPMRPP-91633 update user assign flow --- reportportal-common-api | 2 +- .../impl/UpdateProjectHandlerImpl.java | 32 +++++++++++++++++-- .../ws/controller/OrganizationController.java | 4 +-- .../OrganizationProjectController.java | 5 +-- .../OrganizationUsersController.java | 4 +-- 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/reportportal-common-api b/reportportal-common-api index 740cc9d0e8..3be6ac618a 160000 --- a/reportportal-common-api +++ b/reportportal-common-api @@ -1 +1 @@ -Subproject commit 740cc9d0e8b72281ef1dd8486bd2733f9622fcac +Subproject commit 3be6ac618adf93029ddcd63c5d3732f3129447e4 diff --git a/src/main/java/com/epam/ta/reportportal/core/project/impl/UpdateProjectHandlerImpl.java b/src/main/java/com/epam/ta/reportportal/core/project/impl/UpdateProjectHandlerImpl.java index a0d3a564cc..9dfaced658 100644 --- a/src/main/java/com/epam/ta/reportportal/core/project/impl/UpdateProjectHandlerImpl.java +++ b/src/main/java/com/epam/ta/reportportal/core/project/impl/UpdateProjectHandlerImpl.java @@ -36,6 +36,7 @@ import static com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerStatusCache.AUTO_ANALYZER_KEY; import static com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum.AUTO_PATTERN_ANALYZER_ENABLED; import static com.epam.ta.reportportal.entity.enums.SendCase.findByName; +import static com.epam.ta.reportportal.entity.organization.OrganizationRole.MEMBER; import static com.epam.ta.reportportal.ws.converter.converters.ProjectActivityConverter.TO_ACTIVITY_RESOURCE; import static java.util.Optional.ofNullable; import static java.util.stream.Collectors.toList; @@ -68,6 +69,8 @@ import com.epam.ta.reportportal.dao.ProjectUserRepository; import com.epam.ta.reportportal.dao.UserPreferenceRepository; import com.epam.ta.reportportal.dao.UserRepository; +import com.epam.ta.reportportal.dao.organization.OrganizationRepositoryCustom; +import com.epam.ta.reportportal.dao.organization.OrganizationUserRepository; import com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum; import com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum.Prefix; import com.epam.ta.reportportal.entity.organization.MembershipDetails; @@ -75,6 +78,7 @@ import com.epam.ta.reportportal.entity.project.ProjectRole; import com.epam.ta.reportportal.entity.project.ProjectUtils; import com.epam.ta.reportportal.entity.project.email.SenderCase; +import com.epam.ta.reportportal.entity.user.OrganizationUser; import com.epam.ta.reportportal.entity.user.ProjectUser; import com.epam.ta.reportportal.entity.user.User; import com.epam.ta.reportportal.entity.user.UserRole; @@ -104,6 +108,7 @@ import java.util.Set; import java.util.function.Predicate; import java.util.stream.Collectors; +import lombok.extern.log4j.Log4j2; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.BooleanUtils; @@ -115,6 +120,7 @@ * @author Pavel Bortnik */ @Service +@Log4j2 public class UpdateProjectHandlerImpl implements UpdateProjectHandler { private static final String UPDATE_EVENT = "update"; @@ -130,6 +136,8 @@ public class UpdateProjectHandlerImpl implements UpdateProjectHandler { private final UserPreferenceRepository preferenceRepository; private final ProjectUserRepository projectUserRepository; + private final OrganizationUserRepository organizationUserRepository; + private final OrganizationRepositoryCustom organizationRepositoryCustom; private final MessageBus messageBus; @@ -151,7 +159,9 @@ public class UpdateProjectHandlerImpl implements UpdateProjectHandler { public UpdateProjectHandlerImpl(ProjectExtractor projectExtractor, ProjectAttributeValidator projectAttributeValidator, ProjectRepository projectRepository, UserRepository userRepository, UserPreferenceRepository preferenceRepository, + OrganizationUserRepository organizationUserRepository, MessageBus messageBus, ProjectUserRepository projectUserRepository, + OrganizationRepositoryCustom organizationRepositoryCustom, ApplicationEventPublisher applicationEventPublisher, MailServiceFactory mailServiceFactory, AnalyzerStatusCache analyzerStatusCache, IndexerStatusCache indexerStatusCache, AnalyzerServiceClient analyzerServiceClient, LogIndexer logIndexer, @@ -161,8 +171,10 @@ public UpdateProjectHandlerImpl(ProjectExtractor projectExtractor, this.projectRepository = projectRepository; this.userRepository = userRepository; this.preferenceRepository = preferenceRepository; + this.organizationUserRepository = organizationUserRepository; this.messageBus = messageBus; this.projectUserRepository = projectUserRepository; + this.organizationRepositoryCustom = organizationRepositoryCustom; this.applicationEventPublisher = applicationEventPublisher; this.mailServiceFactory = mailServiceFactory; this.analyzerStatusCache = analyzerStatusCache; @@ -273,9 +285,7 @@ public OperationCompletionRS assignUsers(String projectKey, AssignUsersRQ assign ProjectRole projectRole = ProjectRole.forName(role) .orElseThrow(() -> new ReportPortalException(ROLE_NOT_FOUND, role)); - ProjectRole modifierRole = membershipDetails.getProjectRole(); - expect(modifierRole.sameOrHigherThan(projectRole), BooleanUtils::isTrue).verify( - ACCESS_DENIED); + assignUser(name, projectRole, assignedUsernames, project, user); }); } @@ -389,6 +399,22 @@ private void assignUser(String name, ProjectRole projectRole, List assig formattedSupplier("User '{}' cannot be assigned to project twice.", name) ); + // assign user to organization if not assigned yet + var organization = organizationRepositoryCustom.findById(project.getOrganizationId()) + .orElseThrow(() -> new ReportPortalException(ErrorType.ORGANIZATION_NOT_FOUND, + project.getOrganizationId())); + organizationUserRepository.findByUserIdAndOrganization_Id(modifyingUser.getId(), + project.getOrganizationId()) + .orElseGet(() -> { + log.debug("Assigning user {} to organization {}", + modifyingUser.getLogin(), organization.getName()); + var organizationUser = new OrganizationUser(); + organizationUser.setOrganization(organization); + organizationUser.setUser(modifyingUser); + organizationUser.setOrganizationRole(MEMBER); + return organizationUserRepository.save(organizationUser); + }); + ProjectUser projectUser = new ProjectUser(); projectUser.setProjectRole(projectRole); projectUser.setUser(modifyingUser); diff --git a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationController.java b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationController.java index 2c71fef754..7ededf8ed0 100644 --- a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationController.java +++ b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationController.java @@ -19,7 +19,7 @@ import static com.epam.ta.reportportal.auth.permissions.Permissions.ORGANIZATION_MEMBER; import static com.epam.ta.reportportal.core.widget.content.constant.ContentLoaderConstants.USER; -import com.epam.reportportal.api.OrganizationsApi; +import com.epam.reportportal.api.OrganizationApi; import com.epam.reportportal.api.model.OrganizationProfile; import com.epam.reportportal.api.model.OrganizationProfilesPage; import com.epam.reportportal.api.model.SearchCriteriaRQ; @@ -40,7 +40,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController -public class OrganizationController extends BaseController implements OrganizationsApi { +public class OrganizationController extends BaseController implements OrganizationApi { private final GetOrganizationHandler getOrganizationHandler; private final OrganizationsSearchCriteriaService searchCriteriaService; diff --git a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationProjectController.java b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationProjectController.java index 6a5aafca8a..734989fb3d 100644 --- a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationProjectController.java +++ b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationProjectController.java @@ -20,7 +20,7 @@ import static com.epam.ta.reportportal.auth.permissions.Permissions.ORGANIZATION_MEMBER; import static org.springframework.http.HttpStatus.OK; -import com.epam.reportportal.api.ProjectsApi; +import com.epam.reportportal.api.OrganizationProjectApi; import com.epam.reportportal.api.model.OrganizationProjectInfo; import com.epam.reportportal.api.model.OrganizationProjectsPage; import com.epam.reportportal.api.model.ProjectDetails; @@ -51,7 +51,8 @@ * @author Siarhei Hrabko */ @RestController -public class OrganizationProjectController extends BaseController implements ProjectsApi { +public class OrganizationProjectController extends BaseController implements + OrganizationProjectApi { private final OrganizationProjectHandler organizationProjectHandler; private final OrganizationRepositoryCustom organizationRepositoryCustom; diff --git a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationUsersController.java b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationUsersController.java index 03b405c473..7ddbb755f4 100644 --- a/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationUsersController.java +++ b/src/main/java/com/epam/ta/reportportal/ws/controller/OrganizationUsersController.java @@ -19,7 +19,7 @@ import static com.epam.ta.reportportal.auth.permissions.Permissions.ORGANIZATION_MANAGER; import static com.epam.ta.reportportal.commons.querygen.constant.UserCriteriaConstant.CRITERIA_FULL_NAME; -import com.epam.reportportal.api.UsersApi; +import com.epam.reportportal.api.OrganizationUserApi; import com.epam.reportportal.api.model.OrganizationUsersPage; import com.epam.reportportal.rules.exception.ErrorType; import com.epam.reportportal.rules.exception.ReportPortalException; @@ -38,7 +38,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController -public class OrganizationUsersController extends BaseController implements UsersApi { +public class OrganizationUsersController extends BaseController implements OrganizationUserApi { private final OrganizationRepositoryCustom organizationRepositoryCustom;