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());
}
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());
});
}
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);
}
Aggregations