All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gravitee.management.rest.resource.UsersResource Maven / Gradle / Ivy

There is a newer version: 1.30.31
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.management.rest.resource;

import io.gravitee.common.data.domain.Page;
import io.gravitee.management.model.NewExternalUserEntity;
import io.gravitee.management.model.UserEntity;
import io.gravitee.management.model.permissions.RolePermission;
import io.gravitee.management.rest.model.Pageable;
import io.gravitee.management.rest.model.PagedResult;
import io.gravitee.management.rest.security.Permission;
import io.gravitee.management.rest.security.Permissions;
import io.gravitee.management.service.UserService;
import io.swagger.annotations.*;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import static io.gravitee.common.http.MediaType.APPLICATION_JSON;
import static io.gravitee.management.model.permissions.RolePermissionAction.CREATE;
import static io.gravitee.management.model.permissions.RolePermissionAction.READ;

/**
 * Defines the REST resources to manage Users.
 *
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
 * @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
@Path("/users")
@Api(tags = {"User"})
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
public class UsersResource extends AbstractResource {

    @Context
    private ResourceContext resourceContext;

    @Inject
    private UserService userService;

    @GET
    @Permissions(@Permission(value = RolePermission.MANAGEMENT_USERS, acls = READ))
    @ApiOperation(
            value = "Search for users using the search engine",
            notes = "User must have the MANAGEMENT_USERS[READ] permission to use this service"
    )
    @ApiResponses({
            @ApiResponse(code = 200, message = "List users matching the query criteria", response = UserEntity.class, responseContainer = "PagedResult"),
            @ApiResponse(code = 500, message = "Internal server error")})
    public PagedResult findAll(
            @ApiParam(name = "q")
            @QueryParam("q") String query,
            @Valid @BeanParam Pageable pageable) {
        Page users = userService.search(query, pageable.toPageable());
        return new PagedResult<>(users, pageable.getSize());
    }

    @POST
    @Permissions(@Permission(value = RolePermission.MANAGEMENT_USERS, acls = CREATE))
    @ApiOperation(
            value = "Create a user",
            notes = "User must have the MANAGEMENT_USERS[CREATE] permission to use this service"
    )
    @ApiResponses({
            @ApiResponse(code = 200, message = "List users matching the query criteria", response = UserEntity.class),
            @ApiResponse(code = 500, message = "Internal server error")})
    public Response createUser(@Valid NewExternalUserEntity newExternalUserEntity) {
        UserEntity newUser = userService.create(newExternalUserEntity);
        if (newUser != null) {
            return Response
                    .ok()
                    .entity(newUser)
                    .build();
        }

        return Response.serverError().build();
    }

    @Path("{id}")
    public UserResource getUserResource() {
        return resourceContext.getResource(UserResource.class);
    }

    @Path("registration")
    public UsersRegistrationResource getUsersRegistrationResource() {
        return resourceContext.getResource(UsersRegistrationResource.class);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy