use of io.gravitee.am.service.model.NewForm in project gravitee-access-management by gravitee-io.
the class ApplicationFormsResource method create.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a form for an application", notes = "User must have APPLICATION_FORM[CREATE] permission on the specified application " + "or APPLICATION_FORM[CREATE] permission on the specified domain " + "or APPLICATION_FORM[CREATE] permission on the specified environment " + "or APPLICATION_FORM[CREATE] permission on the specified organization")
@ApiResponses({ @ApiResponse(code = 201, message = "Form successfully created"), @ApiResponse(code = 500, message = "Internal server error") })
public void create(@PathParam("organizationId") String organizationId, @PathParam("environmentId") String environmentId, @PathParam("domain") String domain, @PathParam("application") String application, @ApiParam(name = "email", required = true) @Valid @NotNull final NewForm newForm, @Suspended final AsyncResponse response) {
final User authenticatedUser = getAuthenticatedUser();
checkAnyPermission(organizationId, environmentId, domain, application, Permission.APPLICATION_FORM, Acl.CREATE).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMap(irrelevant -> applicationService.findById(application)).switchIfEmpty(Maybe.error(new ApplicationNotFoundException(application))).flatMapSingle(irrelevant -> formService.create(domain, application, newForm, authenticatedUser)).map(form -> Response.created(URI.create("/organizations/" + organizationId + "/environments/" + environmentId + "/domains/" + domain + "/applications/" + application + "/forms/" + form.getId())).entity(form).build())).subscribe(response::resume, response::resume);
}
use of io.gravitee.am.service.model.NewForm in project gravitee-access-management by gravitee-io.
the class FormsResourceTest method shouldCreate.
@Test
@Ignore
public void shouldCreate() {
final String domainId = "domain-1";
final Domain mockDomain = new Domain();
mockDomain.setId(domainId);
NewForm newForm = new NewForm();
newForm.setTemplate(Template.LOGIN);
newForm.setContent("content");
doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
doReturn(Single.just(new Form())).when(formService).create(eq(domainId), any(), any(User.class));
final Response response = target("domains").path(domainId).path("forms").request().post(Entity.json(newForm));
assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
use of io.gravitee.am.service.model.NewForm in project gravitee-access-management by gravitee-io.
the class FormsResource method create.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a form", notes = "User must have the DOMAIN_FORM[CREATE] permission on the specified domain " + "or DOMAIN_FORM[CREATE] permission on the specified environment " + "or DOMAIN_FORM[CREATE] permission on the specified organization")
@ApiResponses({ @ApiResponse(code = 201, message = "Form successfully created"), @ApiResponse(code = 500, message = "Internal server error") })
public void create(@PathParam("organizationId") String organizationId, @PathParam("environmentId") String environmentId, @PathParam("domain") String domain, @ApiParam(name = "form", required = true) @Valid @NotNull final NewForm newForm, @Suspended final AsyncResponse response) {
final User authenticatedUser = getAuthenticatedUser();
checkAnyPermission(organizationId, environmentId, domain, Permission.DOMAIN_FORM, Acl.CREATE).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMapSingle(irrelevant -> formService.create(domain, newForm, authenticatedUser).map(form -> Response.created(URI.create("/organizations/" + organizationId + "/environments/" + environmentId + "/domains/" + domain + "/forms/" + form.getId())).entity(form).build()))).subscribe(response::resume, response::resume);
}
use of io.gravitee.am.service.model.NewForm in project gravitee-access-management by gravitee-io.
the class FormServiceImpl method create0.
private Single<Form> create0(ReferenceType referenceType, String referenceId, String client, NewForm newForm, User principal) {
String formId = RandomString.generate();
// check if form is unique
return checkFormUniqueness(referenceType, referenceId, client, newForm.getTemplate().template()).flatMap(irrelevant -> {
Form form = new Form();
form.setId(formId);
form.setReferenceType(referenceType);
form.setReferenceId(referenceId);
form.setClient(client);
form.setEnabled(newForm.isEnabled());
form.setTemplate(newForm.getTemplate().template());
form.setContent(newForm.getContent());
form.setAssets(newForm.getAssets());
form.setCreatedAt(new Date());
form.setUpdatedAt(form.getCreatedAt());
return formRepository.create(form);
}).flatMap(page -> {
// create event for sync process
Event event = new Event(Type.FORM, new Payload(page.getId(), page.getReferenceType(), page.getReferenceId(), Action.CREATE));
return eventService.create(event).flatMap(__ -> Single.just(page));
}).onErrorResumeNext(ex -> {
if (ex instanceof AbstractManagementException) {
return Single.error(ex);
}
LOGGER.error("An error occurs while trying to create a form", ex);
return Single.error(new TechnicalManagementException("An error occurs while trying to create a form", ex));
}).doOnSuccess(form -> auditService.report(AuditBuilder.builder(FormTemplateAuditBuilder.class).principal(principal).type(EventType.FORM_TEMPLATE_CREATED).form(form))).doOnError(throwable -> auditService.report(AuditBuilder.builder(FormTemplateAuditBuilder.class).principal(principal).type(EventType.FORM_TEMPLATE_CREATED).throwable(throwable)));
}
Aggregations