/** * 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; }
/** * 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; }
/** * 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; }
private ErrorCollection errorIfPermissionCheckFailed(boolean hasPermission, String errorMessage) { return hasPermission ? ErrorCollections.empty() : ErrorCollections.create(errorMessage, ErrorCollection.Reason.FORBIDDEN); }
@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)); }
public ErrorCollection validateGrants(ApplicationUser user, Iterable<PermissionGrantInput> grants) { ErrorCollection result = empty(); for (PermissionGrantInput grant : grants) { result.addErrorCollection(validateGrant(user, grant)); } return result; }
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(); } }
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(); }
@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 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 boolean isValidProjectKey(JiraServiceContext serviceContext, String key) { final ErrorCollection errorCollection = ErrorCollections.empty(); final I18nHelper i18nBean = serviceContext.getI18nBean(); validateProjectKey(null, key, errorCollection, i18nBean); if (errorCollection.hasAnyErrors()) { serviceContext.getErrorCollection().addErrorCollection(errorCollection); return false; } return true; }
private ErrorCollection initialValidation(PermissionGrantInput grant) { ErrorCollection result = empty(); result.addErrorCollection(validatePermission(grant.getPermission())); result.addErrorCollection(validateHolder(grant.getHolder())); return result; }
/** * 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; }
@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 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; }
@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); }
@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()); }
@Override public boolean isValidRequiredProjectData(JiraServiceContext serviceContext, ProjectCreationData projectCreationData) { final ErrorCollection errorCollection = ErrorCollections.empty(); final I18nHelper i18nBean = serviceContext.getI18nBean(); validateProjectName(projectCreationData.getName(), errorCollection, i18nBean); validateProjectKey(null, projectCreationData.getKey(), errorCollection, i18nBean); validateProjectLead(Option.option(projectCreationData.getLead()), errorCollection, i18nBean); validateProjectType(serviceContext.getLoggedInApplicationUser(), projectCreationData.getProjectTypeKey(), errorCollection, i18nBean); if (errorCollection.hasAnyErrors()) { serviceContext.getErrorCollection().addErrorCollection(errorCollection); return false; } return true; }
@Override public boolean isValidAllProjectData(JiraServiceContext serviceContext, ProjectCreationData projectCreationData) { final ErrorCollection errorCollection = ErrorCollections.empty(); final I18nHelper i18nHelper = serviceContext.getI18nBean(); isValidRequiredProjectData(getServiceContext(serviceContext.getLoggedInApplicationUser(), errorCollection), projectCreationData); validateProjectUrl(projectCreationData.getUrl(), errorCollection, i18nHelper); validateProjectAssigneeType(projectCreationData.getAssigneeType(), errorCollection, i18nHelper); validateAvatarId(projectCreationData.getAvatarId(), projectManager.getProjectObjByKey(projectCreationData.getKey()), errorCollection, i18nHelper); validateProjectTemplate(projectCreationData.getProjectTemplateKey(), errorCollection, i18nHelper); if (errorCollection.hasAnyErrors()) { serviceContext.getErrorCollection().addErrorCollection(errorCollection); return false; } return true; }
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(); } }