Search in sources :

Example 1 with NewForm

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);
}
Also used : ApplicationService(io.gravitee.am.service.ApplicationService) Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) DomainService(io.gravitee.am.service.DomainService) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) ApiResponses(io.swagger.annotations.ApiResponses) Valid(javax.validation.Valid) Acl(io.gravitee.am.model.Acl) ApiOperation(io.swagger.annotations.ApiOperation) User(io.gravitee.am.identityprovider.api.User) Form(io.gravitee.am.model.Form) ReferenceType(io.gravitee.am.model.ReferenceType) Api(io.swagger.annotations.Api) URI(java.net.URI) NewForm(io.gravitee.am.service.model.NewForm) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) NotNull(javax.validation.constraints.NotNull) Suspended(javax.ws.rs.container.Suspended) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Template(io.gravitee.am.model.Template) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) FormService(io.gravitee.am.service.FormService) ApplicationNotFoundException(io.gravitee.am.service.exception.ApplicationNotFoundException) User(io.gravitee.am.identityprovider.api.User) ApplicationNotFoundException(io.gravitee.am.service.exception.ApplicationNotFoundException) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with NewForm

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());
}
Also used : Response(javax.ws.rs.core.Response) User(io.gravitee.am.identityprovider.api.User) Form(io.gravitee.am.model.Form) NewForm(io.gravitee.am.service.model.NewForm) NewForm(io.gravitee.am.service.model.NewForm) Domain(io.gravitee.am.model.Domain) Ignore(org.junit.Ignore) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 3 with NewForm

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);
}
Also used : Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) Valid(javax.validation.Valid) Acl(io.gravitee.am.model.Acl) User(io.gravitee.am.identityprovider.api.User) Form(io.gravitee.am.model.Form) ReferenceType(io.gravitee.am.model.ReferenceType) io.swagger.annotations(io.swagger.annotations) URI(java.net.URI) NewForm(io.gravitee.am.service.model.NewForm) Context(javax.ws.rs.core.Context) Permissions.of(io.gravitee.am.management.service.permissions.Permissions.of) AsyncResponse(javax.ws.rs.container.AsyncResponse) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) NotNull(javax.validation.constraints.NotNull) Suspended(javax.ws.rs.container.Suspended) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Template(io.gravitee.am.model.Template) ResourceContext(javax.ws.rs.container.ResourceContext) FormService(io.gravitee.am.service.FormService) Permissions.or(io.gravitee.am.management.service.permissions.Permissions.or) User(io.gravitee.am.identityprovider.api.User) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException)

Example 4 with NewForm

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)));
}
Also used : Date(java.util.Date) FormRepository(io.gravitee.am.repository.management.api.FormRepository) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) FormAlreadyExistsException(io.gravitee.am.service.exception.FormAlreadyExistsException) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) Form(io.gravitee.am.model.Form) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) NewForm(io.gravitee.am.service.model.NewForm) Action(io.gravitee.am.common.event.Action) Logger(org.slf4j.Logger) FormTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.FormTemplateAuditBuilder) EventType(io.gravitee.am.common.audit.EventType) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) RandomString(io.gravitee.am.common.utils.RandomString) Component(org.springframework.stereotype.Component) List(java.util.List) FormNotFoundException(io.gravitee.am.service.exception.FormNotFoundException) Template(io.gravitee.am.model.Template) Payload(io.gravitee.am.model.common.event.Payload) UpdateForm(io.gravitee.am.service.model.UpdateForm) FormService(io.gravitee.am.service.FormService) Lazy(org.springframework.context.annotation.Lazy) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Form(io.gravitee.am.model.Form) NewForm(io.gravitee.am.service.model.NewForm) UpdateForm(io.gravitee.am.service.model.UpdateForm) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString) FormTemplateAuditBuilder(io.gravitee.am.service.reporter.builder.management.FormTemplateAuditBuilder) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Aggregations

User (io.gravitee.am.identityprovider.api.User)4 Form (io.gravitee.am.model.Form)4 NewForm (io.gravitee.am.service.model.NewForm)4 ReferenceType (io.gravitee.am.model.ReferenceType)3 Template (io.gravitee.am.model.Template)3 FormService (io.gravitee.am.service.FormService)3 Maybe (io.reactivex.Maybe)3 Response (javax.ws.rs.core.Response)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)2 Acl (io.gravitee.am.model.Acl)2 Permission (io.gravitee.am.model.permissions.Permission)2 DomainService (io.gravitee.am.service.DomainService)2 DomainNotFoundException (io.gravitee.am.service.exception.DomainNotFoundException)2 MediaType (io.gravitee.common.http.MediaType)2 URI (java.net.URI)2 Valid (javax.validation.Valid)2 NotNull (javax.validation.constraints.NotNull)2 javax.ws.rs (javax.ws.rs)2 AsyncResponse (javax.ws.rs.container.AsyncResponse)2