@Override public Either<Project, ErrorCollection> updateProjectType(ApplicationUser user, Project project, ProjectTypeKey newProjectType) { I18nHelper i18nBean = getI18nBean(user); ErrorCollection errorCollection = new SimpleErrorCollection(); if (!checkActionPermission(user, project, ProjectAction.EDIT_PROJECT_CONFIG)) { errorCollection.addErrorMessage(i18nBean.getText("admin.errors.project.type.update.no.permission"), Reason.FORBIDDEN); } validateProjectType(user, newProjectType, errorCollection, i18nBean); if (errorCollection.hasAnyErrors()) { return Either.right(errorCollection); } return executeUnderClusterLock(getLockNameFor(project), () -> { Project updatedProject = projectManager.updateProjectType(user, project, newProjectType); return notifyProjectTypeUpdated(user, updatedProject, project.getProjectTypeKey(), newProjectType); }); }
@Override public ServiceOutcome<List<Project>> getAllProjects(final ApplicationUser user) { return getAllProjectsForAction(user, ProjectAction.VIEW_ISSUES); }
@Override public GetProjectResult getProjectById(Long id) { return getProjectById(jiraAuthenticationContext.getUser(), id); }
private ErrorCollection validateUpdateProjectData(ApplicationUser user, String name, Project oldProject, ApplicationUser lead, String url, Long assigneeType, Long avatarId) { final ErrorCollection errorCollection = ErrorCollections.empty(); final I18nHelper i18nHelper = getI18nBean(user); validateProjectNameForUpdate(name, oldProject.getKey(), errorCollection, i18nHelper); validateProjectLead(Option.option(lead), errorCollection, i18nHelper); validateProjectUrl(url, errorCollection, i18nHelper); validateProjectAssigneeType(assigneeType, errorCollection, i18nHelper); validateAvatarId(avatarId, oldProject, errorCollection, i18nHelper); return errorCollection; }
private UpdateProjectValidationResult validateUpdateProject(final ApplicationUser user, final String name, final String oldKey, final String newKey, final String description, final String leadName, final String url, final Long assigneeType, final Long avatarId) final I18nHelper i18nBean = getI18nBean(user); final GetProjectResult getProjectResult = getProjectByKeyForAction(user, oldKey, ProjectAction.EDIT_PROJECT_CONFIG); if (!getProjectResult.isValid() || getProjectResult.getProject() == null) if (!StringUtils.equals(oldKey, newKey)) if (!checkActionPermission(user, project, ProjectAction.EDIT_PROJECT_KEY)) validateProjectKey(project, newKey, errorCollection, i18nBean); if (errorCollection.hasAnyErrors()) final ApplicationUser lead = getUserByName(leadName); final ErrorCollection validationErrors = validateUpdateProjectData(user, name, project, lead, url, assigneeType, avatarId);
@Override public @Nonnull CreateProjectValidationResult validateCreateProject(ApplicationUser user, @Nonnull ProjectCreationData projectCreationData) { final JiraServiceContext serviceContext = getServiceContext(user, ErrorCollections.empty()); final I18nHelper i18nBean = getI18nBean(user); if (!globalPermissionManager.hasPermission(GlobalPermissionKey.ADMINISTER, user)) { serviceContext.getErrorCollection().addErrorMessage(i18nBean.getText("admin.projects.service.error.no.admin.permission"), Reason.FORBIDDEN); return new CreateProjectValidationResult(serviceContext.getErrorCollection()); } ProjectCreationData overridenProjectCreationData = overrideProjectTypeUsingTypeFromProjectTemplate(projectCreationData); isValidAllProjectData(serviceContext, overridenProjectCreationData); if (serviceContext.getErrorCollection().hasAnyErrors()) { return new CreateProjectValidationResult(serviceContext.getErrorCollection()); } return new CreateProjectValidationResult(serviceContext.getErrorCollection(), user, overridenProjectCreationData); }
@Nonnull @Override public CreateProjectValidationResult validateCreateProjectBasedOnExistingProject( final ApplicationUser user, @Nonnull final Long existingProjectId, @Nonnull ProjectCreationData input ) { final JiraServiceContext serviceContext = getServiceContext(user, ErrorCollections.empty()); final I18nHelper i18nBean = getI18nBean(user); if (!globalPermissionManager.hasPermission(GlobalPermissionKey.ADMINISTER, user)) { serviceContext.getErrorCollection().addErrorMessage(i18nBean.getText("admin.projects.service.error.no.admin.permission"), Reason.FORBIDDEN); return new CreateProjectValidationResult(serviceContext.getErrorCollection()); } final GetProjectResult projectById = getProjectByIdForAction(user, existingProjectId, ProjectAction.EDIT_PROJECT_CONFIG); if (!projectById.isValid()) { return new CreateProjectValidationResult(projectById.getErrorCollection()); } final ProjectCreationData creationData = new ProjectCreationData.Builder().fromExistingProject(projectById.get(), input).build(); isValidAllProjectData(serviceContext, creationData); return new CreateProjectValidationResult(serviceContext.getErrorCollection(), user, creationData, Optional.of(existingProjectId)); }
private void validateProjectKey(final Project oldProject, final String key, final ErrorCollection errorCollection, final I18nHelper i18nBean) if (!isProjectKeyValid(key)) else if (isReservedKeyword(key)) if (key.length() > getMaximumKeyLength()) errorCollection.addError(PROJECT_KEY, i18nBean.getText("admin.errors.project.key.too.long", getMaximumKeyLength()));
@Override public Project createProject(CreateProjectValidationResult result) { Consumer<Project> projectConsumer; if (shouldCreateProjectBasedOnExisting(result)) { projectConsumer = (newProject) -> { final Optional<Project> existing = ofNullable(projectManager.getProjectObj(result.getExistingProjectId().get())); final Project existingProject = existing.orElseThrow(() -> new IllegalStateException("Could not retrieve existing project.")); final Optional<ProjectCategory> projectCategory = ofNullable(existingProject.getProjectCategory()); projectCategory.ifPresent(category -> projectManager.setProjectCategory(newProject, category)); projectSchemeAssociationManager.associateSchemesOfExistingProjectWithNewProject(newProject, existingProject); }; } else { projectConsumer = projectSchemeAssociationManager::associateDefaultSchemesWithProject; } return createProjectInternal(result, projectConsumer); }
private Project createProjectInternal(final CreateProjectValidationResult result, final Consumer<Project> projectConsumer) { if (result == null) { throw new IllegalArgumentException("You can not create a project with a null validation result."); } if (!result.isValid()) { throw new IllegalStateException("You can not create a project with an invalid validation result."); } if (result.getExistingProjectId().isPresent()) { ofNullable(projectManager.getProjectObj(result.getExistingProjectId().get())).orElseThrow(() -> new IllegalStateException("Existing project could not be found.")); } final ApplicationUser user = result.getUser(); final ProjectCreationData projectCreationData = result.getProjectCreationData(); final Project newProject = projectManager.createProject(user, new ProjectCreationData.Builder() .from(projectCreationData) .withKey(projectCreationData.getKey().toUpperCase()) .withLead(projectCreationData.getLead()) .withAssigneeType(effectiveAssigneeType(projectCreationData.getAssigneeType())) .build()); projectConsumer.accept(newProject); // Refresh the workflow cache workflowSchemeManager.clearWorkflowCache(); notifyHandlersOfProjectCreated(user, newProject, result); projectEventManager.dispatchProjectCreated(user, newProject); return newProject; }
@Override public DeleteProjectValidationResult validateDeleteProject(ApplicationUser user, String key) { final I18nHelper i18nBean = getI18nBean(user); if (!globalPermissionManager.hasPermission(GlobalPermissionKey.ADMINISTER, user)) { return new DeleteProjectValidationResult(ErrorCollections.create(i18nBean.getText("admin.projects.service.error.no.admin.permission"), Reason.FORBIDDEN)); } //check if the project exists. If not return with an error. final GetProjectResult oldProjectResult = getProjectByKeyForAction(user, key, ProjectAction.EDIT_PROJECT_CONFIG); if (!oldProjectResult.isValid() || oldProjectResult.getProject() == null) { return new DeleteProjectValidationResult(ErrorCollections.copyOf(oldProjectResult.getErrorCollection())); } return new DeleteProjectValidationResult(ErrorCollections.empty(), oldProjectResult.getProject()); }
final JiraServiceContext serviceContext = getServiceContext(user, errorCollection); final I18nHelper i18nBean = getI18nBean(user);
@Override public boolean apply(@Nullable Project input) { return (input != null) && checkActionPermission(user, input, action); } });
/** * Creates a new ErrorCollection instance for a single error message. If the reason "forbidden" and the user is * null, this method adds an additional error message along the lines of "You are not logged in, please log in". * * @param user the ApplicationUser for whom the message will be i18n'ed * @param i18nKey a String containing an i18n key * @param reason a Reason code * @param params a String array containing the i18n params @return a new ErrorCollection * @return a new ErrorCollection * @since v6.1 */ protected final ErrorCollection makeErrorCollection(ApplicationUser user, String i18nKey, ErrorCollection.Reason reason, String... params) { final ErrorCollection errors = ErrorCollections.empty(); final I18nHelper i18nBean = getI18nBean(user); errors.addErrorMessage(i18nBean.getText(i18nKey, params), reason); // lack of permissions may be due to the user not being logged in if (reason == Reason.FORBIDDEN && user == null) { errors.addErrorMessage(i18nBean.getText("common.forms.ajax.unauthorised.alert"), Reason.NOT_LOGGED_IN); } return errors; }
private void notifyHandlersOfProjectCreated(ApplicationUser user, Project newProject, CreateProjectValidationResult result) { final ProjectCreatedData.Builder projectCreatedData = new ProjectCreatedData.Builder().withProject(newProject); Optional.ofNullable(result.getProjectCreationData().getProjectTemplateKey()).ifPresent(projectCreatedData::withProjectTemplateKey); result.getExistingProjectId().ifPresent(projectCreatedData::withExistingProjectId); boolean notificationWasSuccessful = projectCreateNotifier.notifyAllHandlers(projectCreatedData.build()); if (!notificationWasSuccessful) { deleteProject(user, new DeleteProjectValidationResult(ErrorCollections.empty(), newProject)); throw new RuntimeException("An error occurred while notifying that a project was created"); } }
@Override public GetProjectResult getProjectByIdForAction(ApplicationUser user, Long id, ProjectAction action) { Project project = projectManager.getProjectObj(id); if (project == null || !checkActionPermission(user, project, ProjectAction.VIEW_PROJECT)) { return new ProjectNotFoundResult(user, "admin.errors.project.not.found.for.id", String.valueOf(id)); } if (!checkActionPermission(user, project, action)) { return new PermissionErrorResult(user, action); } return new GetProjectResult(project); }
final I18nHelper i18nBean = getI18nBean(user);
@Override public GetProjectResult getProjectByKeyForAction(ApplicationUser user, String key, ProjectAction action) { Assertions.notNull("action", action); Project project = projectManager.getProjectObjByKey(key); if (project == null || !checkActionPermission(user, project, ProjectAction.VIEW_PROJECT)) { return new ProjectNotFoundResult(user, "admin.errors.project.not.found.for.key", key); } if (!checkActionPermission(user, project, action)) { return new PermissionErrorResult(user, action); } return new GetProjectResult(project); }