private ErrorCollection validatePermission(final ProjectPermissionKey permission) { if (!projectPermissionsManager.exists(permission)) { return validationError("permission", i18n.getText("admin.schemes.permissions.validation.permission.unrecognized", permission)); } else { return empty(); } }
/** * Creates a new error collection with contents copied from the supplied argument. * * @param errorCollection error collection to copy * @return a new instance of error collection with the same contents as argument */ public static ErrorCollection copyOf(ErrorCollection errorCollection) { ErrorCollection result = empty(); result.addErrorCollection(errorCollection); return result; }
@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()); }
private ErrorCollection validationImplementedByTypes(ApplicationUser user, PermissionGrantInput entity) { JiraServiceContext context = new JiraServiceContextImpl(user, empty(), i18n); String key = entity.getHolder().getType().getKey(); SchemeType type = permissionTypeManager.getSchemeType(key); if (type == null) { return validationError("holder.type", i18n.getText("admin.schemes.permissions.validation.holder.type.unrecognized", key)); } if (!type.isValidForPermission(entity.getPermission())) { return create(i18n.getText("admin.permissions.errors.invalid.combination", entity.getPermission().permissionKey(), key), ErrorCollection.Reason.VALIDATION_FAILED); } type.doValidation(key, getParameters(entity.getHolder()), context); return context.getErrorCollection(); } }
/** * Creates a new instance of error collection that indicates there was a validation error for the specified field. * * <p> * This is equivalent to calling {@link #create(String, String, com.atlassian.jira.util.ErrorCollection.Reason)} * with the same field and message arguments and reason {@link com.atlassian.jira.util.ErrorCollection.Reason#VALIDATION_FAILED}. * </p> * * @param field field that failed validation * @param message detailed error message * @return a new error collection with validation reason set to {@link com.atlassian.jira.util.ErrorCollection.Reason#VALIDATION_FAILED} */ public static ErrorCollection validationError(String field, String message) { return create(field, message, ErrorCollection.Reason.VALIDATION_FAILED); }
@Override public ServiceResult validateUpdateProject(final ApplicationUser user, final String key) { //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 ServiceResultImpl(ErrorCollections.copyOf(oldProjectResult.getErrorCollection())); } return new ServiceResultImpl(ErrorCollections.empty()); }
private Either<ErrorCollection, PermissionSchemeBean> validated(final PermissionSchemeBean bean) { if (isNullOrEmpty(bean.getName())) { return left(ErrorCollections.validationError("name", i18n.getText("rest.missing.field", "name"))); } return right(bean); }
private ErrorCollection validateNoSchemeAssociationsForRole(final ProjectRole projectRole) { return ErrorCollections.join( validateNoAssociations(projectRole, this::getAssociatedNotificationSchemes, "rest.role.used.in.notification.schemes"), validateNoAssociations(projectRole, this::getAssociatedIssueSecuritySchemes, "rest.role.used.in.security.schemes"), validateNoAssociations(projectRole, this::getAssociatedPermissionSchemes, "rest.role.used.in.permission.schemes")); }
/** * Creates a new instance of error collection that contains the specified message and reason. * * @param errorMessage error message to be contained in the returned collection * @param reason reason for the message * @return a new error collection with the specified message and reason */ public static ErrorCollection create(String errorMessage, ErrorCollection.Reason reason) { ErrorCollection result = empty(); result.addErrorMessage(errorMessage, reason); return result; }
if (!getProjectResult.isValid() || getProjectResult.getProject() == null) return new UpdateProjectValidationResult(ErrorCollections.copyOf(getProjectResult.getErrorCollection())); return new UpdateProjectValidationResult(ErrorCollections.create(PROJECT_KEY, i18nBean.getText(ProjectAction.EDIT_PROJECT_KEY.getErrorKey()), Reason.FORBIDDEN)); ErrorCollection errorCollection = ErrorCollections.empty(); validateProjectKey(project, newKey, errorCollection, i18nBean); if (errorCollection.hasAnyErrors()) return new UpdateProjectValidationResult(ErrorCollections.empty(), name, newKey, description, leadName, url, assigneeType, avatarId, project, !StringUtils.equals(oldKey, newKey), user);
/** * parses a query parameter and returns either an error collection in case of failure or a list of parsed elements. * * @param expandQueryParameter query parameter, may be null * @return either error collection or list of {@code T} elements */ public Either<ErrorCollection, List<T>> parseExpandQuery(@Nullable final String expandQueryParameter) { ImmutableList.Builder<T> resultBuilder = ImmutableList.builder(); for (String elementStr : SPLITTER.split(Strings.nullToEmpty(expandQueryParameter))) { if (!index.containsKey(elementStr)) { return Either.left(ErrorCollections.create(i18n.getText("rest.error.invalid.expand", elementStr, values.toString()), com.atlassian.jira.util.ErrorCollection.Reason.VALIDATION_FAILED)); } resultBuilder.add(index.get(elementStr)); } return Either.right(resultBuilder.build()); } }
private Either<Response, Iterable<Project>> getMostRecentProjects(Integer count) { if (count < 0) { return Either.left(responses.errorResponse(ErrorCollections.validationError("recent", i18nHelper.getText("rest.validation.error.negative.number")))); } else { List<Project> recentProjects = projectHistoryManager.getProjectHistoryWithPermissionChecks(ProjectAction.VIEW_PROJECT, authContext.getUser()); return Either.right(Iterables.limit(recentProjects, Math.min(count, MAX_RECENT_PROJECTS))); } }
@Override public ServiceResult validateNoRoleUsage(ProjectRole projectRole) { return new ServiceResultImpl( ErrorCollections.join( validateNoSchemeAssociationsForRole(projectRole), validateNoUsageOfRoleInWorkflows(projectRole), validateNoUsageOfRoleInComments(projectRole), validateNoUsageOfRoleInWorklogs(projectRole))); }
/** * Creates a new instance of error collection that contains the {@code message} related to the {@code field} because of {@code reason}. * * @param field field that the error message will pertain to * @param message error message related to the field * @param reason reason for error * @return a new error collection with one message relating to the field */ public static ErrorCollection create(String field, String message, ErrorCollection.Reason reason) { ErrorCollection result = empty(); result.addError(field, message, reason); return result; }
@Override public ServiceOutcome<Set<Worklog>> getWorklogsForIds(final JiraServiceContext jiraServiceContext, final Set<Long> idsOfWorklogs) { if (idsOfWorklogs.size() > WORKLOG_UPDATE_DATA_PAGE_SIZE) { return new ServiceOutcomeImpl<>( ErrorCollections.create(jiraServiceContext.getI18nBean().getText("worklog.service.error.too.many.ids", WORKLOG_UPDATE_DATA_PAGE_SIZE), VALIDATION_FAILED)); } final Set<Worklog> filteredWorklogs = worklogManager.getWorklogsForIds(idsOfWorklogs) .stream() .filter(w -> hasPermissionToView(jiraServiceContext.getLoggedInApplicationUser(), w)) .collect(Collectors.toSet()); return new ServiceOutcomeImpl<>(ErrorCollections.empty(), filteredWorklogs); }
@Override public Response notLoggedInResponse() { return errorResponse(ErrorCollections.create(i18nBean.getText("rest.authentication.no.user.logged.in"), ErrorCollection.Reason.NOT_LOGGED_IN)); }
private ErrorCollection validateCustomField(final String customField) { if (!customFieldManager.exists(customField)) { return validationError("holder.parameter", i18n.getText("admin.schemes.permissions.validation.cf.does.not.exist", customField)); } return empty(); }
private Either<Response, Option<ProjectCategory>> loadProjectCategory(Long categoryId) { if (categoryId != null) { ProjectCategory category = projectManager.getProjectCategoryObject(categoryId); if (category != null) { return Either.right(Option.some(category)); } else { return Either.left(responses.errorResponse( ErrorCollections.validationError("projectCategory", i18nHelper.getText("admin.errors.project.category.does.not.exist")))); } } else { return Either.right(Option.<ProjectCategory>none()); } }
@Override public ServiceOutcome<List<Project>> getAllProjectsForAction(final ApplicationUser user, final ProjectAction action) { final Iterable<Project> projects = Iterables.filter(projectManager.getProjectObjects(), new Predicate<Project>() { @Override public boolean apply(@Nullable Project input) { return (input != null) && checkActionPermission(user, input, action); } }); return new ServiceOutcomeImpl<List<Project>>(ErrorCollections.empty(), Lists.newArrayList(projects)); }