/** * 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); }
private ErrorCollection errorIfPermissionCheckFailed(boolean hasPermission, String errorMessage) { return hasPermission ? ErrorCollections.empty() : ErrorCollections.create(errorMessage, ErrorCollection.Reason.FORBIDDEN); }
/** * 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()); } }
@Override public Response notLoggedInResponse() { return errorResponse(ErrorCollections.create(i18nBean.getText("rest.authentication.no.user.logged.in"), ErrorCollection.Reason.NOT_LOGGED_IN)); }
@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); }
private Either<Response, String> withUserId(final String userKey, final String username) { if ((username == null) == (userKey == null)) { return Either.left(responseFactory.errorResponse(ErrorCollections.create(i18n.getText("rest.user.properties.id.not.specified"), Reason.VALIDATION_FAILED))); } final Optional<UserIdentity> userIdentity = username != null ? userManager.getUserIdentityByUsername(username) : userManager.getUserIdentityByKey(userKey); return userIdentity.isPresent() ? Either.<Response, String>right(userIdentity.get().getId().toString()) : Either.<Response, String>left(responseFactory.errorResponse(ErrorCollections.create(i18n.getText("rest.user.error.not.found.general"), Reason.NOT_FOUND))); } }
public Response asAdmin(RestAction action) { if (globalPermissionManager.hasPermission(GlobalPermissionKey.ADMINISTER, getUser())) { return action.perform(); } else { return responseFactory.errorResponse(ErrorCollections.create(i18n.getText("rest.permissionscheme.forbidden"), ErrorCollection.Reason.FORBIDDEN)); } }
private Either<ErrorCollection, PermissionGrantInput> validateThatNewPermissionDoesNotAlreadyExist(final PermissionScheme oldScheme, final PermissionGrantInput newPermission) { if (contains(transform(oldScheme.getPermissions(), TO_PURE_DATA), PermissionGrantAsPureData.of(newPermission))) { return Either.left(ErrorCollections.create(i18n.getText("rest.permissionscheme.permission.already.exists", newPermission.getPermission().permissionKey(), newPermission.getHolder().getType().toString(), newPermission.getHolder().getParameter().getOrNull(), oldScheme.getId().toString()), VALIDATION_FAILED)); } else { return Either.right(newPermission); } }
@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()); }
return new UpdateProjectValidationResult(ErrorCollections.create(PROJECT_KEY, i18nBean.getText(ProjectAction.EDIT_PROJECT_KEY.getErrorKey()), Reason.FORBIDDEN));
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(); } }