Search in sources :

Example 1 with AlreadyExistsException

use of com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException in project sechub by mercedes-benz.

the class ProjectCreationService method createProject.

@Validated
/* @formatter:off */
@UseCaseAdminCreatesProject(@Step(number = 2, name = "Create project", description = "The service will create the project when not already existing with such name."))
public /* @formatter:on */
void createProject(@NotNull String projectId, @NotNull String description, @NotNull String owner, @NotNull Set<URI> whitelist, @NotNull ProjectMetaData metaData) {
    LOG.info("Administrator {} triggers create of project:{}, having owner:{}", userContext.getUserId(), projectId, owner);
    assertion.assertIsValidProjectId(projectId);
    assertion.assertIsValidUserId(owner);
    assertion.assertIsValidProjectDescription(description);
    /* assert found */
    Optional<Project> foundProject = projectRepository.findById(projectId);
    if (foundProject.isPresent()) {
        throw new AlreadyExistsException("Project '" + projectId + "' already exists");
    }
    Optional<User> foundOwner = userRepository.findById(owner);
    if (!foundOwner.isPresent()) {
        throw new NotFoundException("Owner '" + owner + "' not found");
    }
    /* setup */
    Project project = new Project();
    project.id = projectId;
    project.description = description;
    User ownerUser = foundOwner.get();
    project.owner = ownerUser;
    /**
     * add only accepted/valid URIs - sanitize
     */
    whitelist.stream().filter(uri -> uriValidation.validate(uri).isValid()).forEach(project.getWhiteList()::add);
    List<ProjectMetaDataEntity> metaDataEntities = metaData.getMetaDataMap().entrySet().stream().map(entry -> new ProjectMetaDataEntity(projectId, entry.getKey(), entry.getValue())).collect(Collectors.toList());
    project.metaData.addAll(metaDataEntities);
    /* store */
    persistenceService.saveInOwnTransaction(project);
    sendProjectCreatedEvent(projectId, whitelist);
    sendRefreshUserAuth(ownerUser);
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) User(com.mercedesbenz.sechub.domain.administration.user.User) MessageID(com.mercedesbenz.sechub.sharedkernel.messaging.MessageID) ProjectMessage(com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage) DomainMessage(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage) Service(org.springframework.stereotype.Service) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Step(com.mercedesbenz.sechub.sharedkernel.Step) URI(java.net.URI) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) MessageDataKeys(com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys) UserRepository(com.mercedesbenz.sechub.domain.administration.user.UserRepository) DomainMessageFactory(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) UserInputAssertion(com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) UserContextService(com.mercedesbenz.sechub.sharedkernel.UserContextService) Set(java.util.Set) URIValidation(com.mercedesbenz.sechub.sharedkernel.validation.URIValidation) RoleConstants(com.mercedesbenz.sechub.sharedkernel.RoleConstants) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) IsSendingAsyncMessage(com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage) List(java.util.List) DomainMessageService(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService) ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Optional(java.util.Optional) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Validated(org.springframework.validation.annotation.Validated) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)

Example 2 with AlreadyExistsException

use of com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException in project sechub by mercedes-benz.

the class ProjectUnassignUserService method unassignUserFromProject.

/* @formatter:off */
@UseCaseAdminUnassignsUserFromProject(@Step(number = 2, name = "Unassign user", description = "The service will remove the user to the project. If users has no longer access to projects ROLE_USER will be removed"))
public /* @formatter:on */
void unassignUserFromProject(String userId, String projectId) {
    auditLogService.log("triggers unassignment of user:{} to project:{}", logSanitizer.sanitize(userId, 30), logSanitizer.sanitize(projectId, 30));
    assertion.assertIsValidUserId(userId);
    assertion.assertIsValidProjectId(projectId);
    Project project = projectRepository.findOrFailProject(projectId);
    User user = userRepository.findOrFailUser(userId);
    if (!project.getUsers().remove(user)) {
        throw new AlreadyExistsException("User already not assigned to this project!");
    }
    user.getProjects().remove(project);
    transactionService.saveInOwnTransaction(project, user);
    sendUserRemovedFromProjectEvent(projectId, user);
    sendRequestUserRoleRecalculation(user);
}
Also used : UseCaseAdminUnassignsUserFromProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminUnassignsUserFromProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) UseCaseAdminUnassignsUserFromProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminUnassignsUserFromProject)

Example 3 with AlreadyExistsException

use of com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException in project sechub by mercedes-benz.

the class SchedulerRestartJobService method restartJob.

private void restartJob(UUID jobUUID, String ownerEmailAddress, boolean hard) {
    assertion.assertIsValidJobUUID(jobUUID);
    auditLogService.log("triggered restart of job:{}, variant:[hard={}]", jobUUID, hard);
    Optional<ScheduleSecHubJob> optJob = jobRepository.findById(jobUUID);
    if (!optJob.isPresent()) {
        LOG.warn("SecHub job {} not found, so not able to restart!", jobUUID);
        JobDataContext context = new JobDataContext();
        context.sechubJobUUID = jobUUID;
        context.ownerEmailAddress = ownerEmailAddress;
        context.info = "Restart canceled, because job not found!";
        sendJobRestartCanceled(context);
        throw new NotFoundException("Job not found or you have no access");
    }
    /* job exists, so can be restarted - hard or soft */
    ScheduleSecHubJob job = optJob.get();
    if (job.getExecutionResult().hasFinished()) {
        /* already done so just ignore */
        LOG.warn("SecHub job {} has already finished, so not able to restart!", jobUUID);
        sendJobRestartCanceled(job, ownerEmailAddress, "Restart canceled, because job already finished");
        throw new AlreadyExistsException("Job has already finished - restart not necessary");
    }
    /*
         * when we have still running batch jobs we must terminate them as well +
         * abandon
         */
    schedulerCancelJobService.stopAndAbandonAllRunningBatchJobsForSechubJobUUID(jobUUID);
    if (hard) {
        sendPurgeJobResultsSynchronousRequest(job);
    }
    ScheduleSecHubJob secHubJob = optJob.get();
    markJobAsNewExecutedNow(secHubJob);
    sendJobRestartTriggered(secHubJob, ownerEmailAddress);
    launcherService.executeJob(secHubJob);
    String type = (hard ? "hard" : "normal");
    LOG.info("job {} has been {} restarted", jobUUID, type);
}
Also used : AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) ScheduleSecHubJob(com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)

Example 4 with AlreadyExistsException

use of com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException in project sechub by mercedes-benz.

the class ProjectAssignUserService method assignUserToProject.

/* @formatter:off */
@UseCaseAdminAssignsUserToProject(@Step(number = 2, name = "Assign user", description = "The service will add the user to the project. If user does not have ROLE_USER it will obtain it"))
public /* @formatter:on */
void assignUserToProject(String userId, String projectId) {
    LOG.info("User {} triggers assignment of user:{} to project:{}", userContextService.getUserId(), logSanitizer.sanitize(userId, 30), logSanitizer.sanitize(projectId, 30));
    assertion.assertIsValidUserId(userId);
    assertion.assertIsValidProjectId(projectId);
    Project project = projectRepository.findOrFailProject(projectId);
    User user = userRepository.findOrFailUser(userId);
    if (!project.getUsers().add(user)) {
        throw new AlreadyExistsException("User already assigned to this project!");
    }
    user.getProjects().add(project);
    project.getUsers().add(user);
    transactionService.saveInOwnTransaction(project, user);
    sendUserAddedToProjectEvent(projectId, user);
    sendRequestUserRoleRecalculation(user);
}
Also used : UseCaseAdminAssignsUserToProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminAssignsUserToProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) UseCaseAdminAssignsUserToProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminAssignsUserToProject)

Example 5 with AlreadyExistsException

use of com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException in project sechub by mercedes-benz.

the class ProjectChangeOwnerService method changeProjectOwner.

/* @formatter:off */
@UseCaseAdminChangesProjectOwner(@Step(number = 2, name = "Change project owner", description = "The service will set the user as the owner of the project. If user does not have ROLE_OWNER it will obtain it. The old owner will loose project ownership."))
public /* @formatter:on */
void changeProjectOwner(String newOnwerUserId, String projectId) {
    LOG.info("User {} triggers project owner change - user:{} to project:{}", userContextService.getUserId(), logSanitizer.sanitize(newOnwerUserId, 30), logSanitizer.sanitize(projectId, 30));
    assertion.assertIsValidUserId(newOnwerUserId);
    assertion.assertIsValidProjectId(projectId);
    Project project = projectRepository.findOrFailProject(projectId);
    User newOwner = userRepository.findOrFailUser(newOnwerUserId);
    if (project.owner.equals(newOwner)) {
        throw new AlreadyExistsException("User already assigned in the role as owner to this project!");
    }
    User previousOwner = changeProjectOwnerAndReturnPreviousOwner(project, newOwner);
    transactionService.saveInOwnTransaction(project, newOwner, previousOwner);
    sendOwnerChangedForProjectEvent(project, previousOwner, newOwner);
    sendRequestOwnerRoleRecalculation(newOwner);
    sendRequestOwnerRoleRecalculation(previousOwner);
}
Also used : User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) UseCaseAdminChangesProjectOwner(com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminChangesProjectOwner)

Aggregations

AlreadyExistsException (com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException)5 User (com.mercedesbenz.sechub.domain.administration.user.User)4 NotFoundException (com.mercedesbenz.sechub.sharedkernel.error.NotFoundException)2 ProjectMetaData (com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData)1 UserRepository (com.mercedesbenz.sechub.domain.administration.user.UserRepository)1 ScheduleSecHubJob (com.mercedesbenz.sechub.domain.schedule.job.ScheduleSecHubJob)1 RoleConstants (com.mercedesbenz.sechub.sharedkernel.RoleConstants)1 Step (com.mercedesbenz.sechub.sharedkernel.Step)1 UserContextService (com.mercedesbenz.sechub.sharedkernel.UserContextService)1 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)1 DomainMessageFactory (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory)1 DomainMessageService (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService)1 IsSendingAsyncMessage (com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage)1 MessageDataKeys (com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys)1 MessageID (com.mercedesbenz.sechub.sharedkernel.messaging.MessageID)1 ProjectMessage (com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage)1 UseCaseAdminCreatesProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)1 UseCaseAdminAssignsUserToProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminAssignsUserToProject)1 UseCaseAdminChangesProjectOwner (com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminChangesProjectOwner)1 UseCaseAdminUnassignsUserFromProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.user.UseCaseAdminUnassignsUserFromProject)1