package stroom.security.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import stroom.security.SecurityContext;

import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Api(
        value = "authorisation - /v1",
        description = "Stroom Authorisation API")
@Path("/authorisation/v1")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class AuthorisationResource {
    private final SecurityContext securityContext;

    @Inject
    public AuthorisationResource(final SecurityContext securityContext) {
        this.securityContext = securityContext;
    }

    /**
     * Authenticates using JWT
     */
    @POST
    @Path("isAuthorised")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
        value = "Submit a request to verify if the user has the requested permission on a 'document'",
        response = Response.class)
    public Response isAuthorised(@ApiParam("AuthorisationRequest") final AuthorisationRequest authorisationRequest) {

        boolean result = securityContext.hasDocumentPermission(
            authorisationRequest.getDocRef().getType(),
            authorisationRequest.getDocRef().getUuid(),
            authorisationRequest.getPermission());

        return result
            ? Response
            .ok()
            .build()
            : Response
            .status(Response.Status.UNAUTHORIZED)
            .build();
    }

    @POST
    @Path("canManageUsers")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response canManageUsers(UserPermissionRequest userPermissionRequest) {
        // TODO what happens if the permission is bad? What's the result of this method call and how should we handle it?
        boolean result = securityContext.hasAppPermission(userPermissionRequest.getPermission());
        // The user here will be the one logged in by the JWT.
        return result ? Response.ok().build() : Response.status(Response.Status.UNAUTHORIZED).build();
    }
}

@@ -156,86 +156,98 @@
 ort 
-stroom.security.SecurityContext;%0A%0Aimport javax.validation.constraints.NotNull;
+org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport stroom.security.SecurityContext;%0A
 %0Aimp
@@ -354,32 +354,8 @@
 es;%0A
-import javax.ws.rs.GET;%0A
 impo
@@ -425,24 +425,55 @@
 s.Produces;%0A
+import javax.ws.rs.QueryParam;%0A
 import javax
@@ -754,45 +754,184 @@
 ate 
-final SecurityContext securityContext
+static final Logger LOGGER = LoggerFactory.getLogger(AuthorisationResource.class);%0A%0A    private final SecurityContext securityContext;%0A    private final UserService userService
 ;%0A%0A 
@@ -1011,16 +1011,41 @@
 yContext
+, UserService userService
 ) %7B%0A    
@@ -1084,24 +1084,64 @@
 ityContext;%0A
+        this.userService = userService;%0A
     %7D%0A%0A    /
@@ -2587,11 +2587,677 @@
 );%0A    %7D
+%0A%0A    /**%0A     * This function is used by the Users UI to create a Stroom user for authorisation purposes.%0A     * It solves the problem of Users having to log in before they're available to assign permissions to.%0A     */%0A    @POST%0A    @Path(%22createUser%22)%0A    @Consumes(MediaType.APPLICATION_JSON)%0A    @Produces(MediaType.APPLICATION_JSON)%0A    public Response createUser(@QueryParam(%22id%22) String userId) %7B%0A        try%7B%0A            userService.createUser(userId);%0A            return Response.ok().build();%0A        %7D%0A        catch(Exception e)%7B%0A            LOGGER.error(%22Unable to create user: %7B%7D%22, e);%0A            return Response.serverError().build();%0A        %7D%0A    %7D
 %0A%7D%0A

Add an endpoint for the creation of internal users