Search in sources :

Example 1 with NewScope

use of io.gravitee.am.service.model.NewScope in project gravitee-access-management by gravitee-io.

the class ScopesResourceTest method shouldCreate.

@Test
public void shouldCreate() {
    final String domainId = "domain-1";
    final Domain mockDomain = new Domain();
    mockDomain.setId(domainId);
    NewScope newScope = new NewScope();
    newScope.setKey("scope-key");
    newScope.setName("scope-name");
    newScope.setDescription("scope-description");
    Scope scope = new Scope();
    scope.setId("scope-id");
    scope.setKey("scope-key");
    scope.setName("scope-name");
    doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
    doReturn(Single.just(scope)).when(scopeService).create(eq(domainId), any(NewScope.class), any());
    final Response response = target("domains").path(domainId).path("scopes").request().post(Entity.json(newScope));
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) NewScope(io.gravitee.am.service.model.NewScope) Scope(io.gravitee.am.model.oauth2.Scope) NewScope(io.gravitee.am.service.model.NewScope) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 2 with NewScope

use of io.gravitee.am.service.model.NewScope in project gravitee-access-management by gravitee-io.

the class ScopeUpgrader method createScope.

private Single<Scope> createScope(String domain, String scopeKey) {
    return scopeService.findByDomain(domain, 0, Integer.MAX_VALUE).flatMap(scopes -> {
        Optional<Scope> optScope = scopes.getData().stream().filter(scope -> scope.getKey().equalsIgnoreCase(scopeKey)).findFirst();
        if (!optScope.isPresent()) {
            logger.info("Create a new scope key[{}] for domain[{}]", scopeKey, domain);
            NewScope scope = new NewScope();
            scope.setKey(scopeKey);
            scope.setName(Character.toUpperCase(scopeKey.charAt(0)) + scopeKey.substring(1));
            scope.setDescription("Default description for scope " + scopeKey);
            return scopeService.create(domain, scope);
        }
        return Single.just(optScope.get());
    });
}
Also used : Ordered(org.springframework.core.Ordered) io.gravitee.am.service(io.gravitee.am.service) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Domain(io.gravitee.am.model.Domain) NewScope(io.gravitee.am.service.model.NewScope) Single(io.reactivex.Single) ArrayList(java.util.ArrayList) Component(org.springframework.stereotype.Component) List(java.util.List) Optional(java.util.Optional) Observable(io.reactivex.Observable) Scope(io.gravitee.am.model.oauth2.Scope) SCOPE_UPGRADER(io.gravitee.am.management.service.impl.upgrades.UpgraderOrder.SCOPE_UPGRADER) NewScope(io.gravitee.am.service.model.NewScope) Scope(io.gravitee.am.model.oauth2.Scope) NewScope(io.gravitee.am.service.model.NewScope)

Example 3 with NewScope

use of io.gravitee.am.service.model.NewScope in project gravitee-access-management by gravitee-io.

the class ScopesResource method create.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a scope", notes = "User must have the DOMAIN_SCOPE[CREATE] permission on the specified domain " + "or DOMAIN_SCOPE[CREATE] permission on the specified environment " + "or DOMAIN_SCOPE[CREATE] permission on the specified organization")
@ApiResponses({ @ApiResponse(code = 201, message = "Scope 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 = "scope", required = true) @Valid @NotNull final NewScope newScope, @Suspended final AsyncResponse response) {
    final User authenticatedUser = getAuthenticatedUser();
    checkAnyPermission(organizationId, environmentId, domain, Permission.DOMAIN_SCOPE, Acl.CREATE).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMapSingle(irrelevant -> scopeService.create(domain, newScope, authenticatedUser).map(scope -> Response.created(URI.create("/organizations/" + organizationId + "/environments/" + environmentId + "/domains/" + domain + "/scopes/" + scope.getId())).entity(scope).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) NewScope(io.gravitee.am.service.model.NewScope) Valid(javax.validation.Valid) Acl(io.gravitee.am.model.Acl) User(io.gravitee.am.identityprovider.api.User) ScopeService(io.gravitee.am.service.ScopeService) ReferenceType(io.gravitee.am.model.ReferenceType) Observable(io.reactivex.Observable) io.swagger.annotations(io.swagger.annotations) URI(java.net.URI) Scope(io.gravitee.am.model.oauth2.Scope) Page(io.gravitee.am.model.common.Page) 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) Collectors(java.util.stream.Collectors) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ResourceContext(javax.ws.rs.container.ResourceContext) Comparator(java.util.Comparator) User(io.gravitee.am.identityprovider.api.User) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException)

Aggregations

Scope (io.gravitee.am.model.oauth2.Scope)3 NewScope (io.gravitee.am.service.model.NewScope)3 Domain (io.gravitee.am.model.Domain)2 Observable (io.reactivex.Observable)2 List (java.util.List)2 Response (javax.ws.rs.core.Response)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 User (io.gravitee.am.identityprovider.api.User)1 JerseySpringTest (io.gravitee.am.management.handlers.management.api.JerseySpringTest)1 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)1 SCOPE_UPGRADER (io.gravitee.am.management.service.impl.upgrades.UpgraderOrder.SCOPE_UPGRADER)1 Acl (io.gravitee.am.model.Acl)1 ReferenceType (io.gravitee.am.model.ReferenceType)1 Page (io.gravitee.am.model.common.Page)1 Permission (io.gravitee.am.model.permissions.Permission)1 io.gravitee.am.service (io.gravitee.am.service)1 DomainService (io.gravitee.am.service.DomainService)1 ScopeService (io.gravitee.am.service.ScopeService)1 DomainNotFoundException (io.gravitee.am.service.exception.DomainNotFoundException)1 MediaType (io.gravitee.common.http.MediaType)1