/**
 * 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.rest.api.management.rest.resource;

import io.gravitee.common.http.MediaType;
import io.gravitee.rest.api.management.rest.security.Permission;
import io.gravitee.rest.api.management.rest.security.Permissions;
import io.gravitee.rest.api.model.MembershipMemberType;
import io.gravitee.rest.api.model.MembershipReferenceType;
import io.gravitee.rest.api.model.permissions.RolePermission;
import io.gravitee.rest.api.model.permissions.RolePermissionAction;
import io.gravitee.rest.api.model.permissions.RoleScope;
import io.gravitee.rest.api.service.MembershipService;
import io.gravitee.rest.api.service.common.GraviteeContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.PathParam;

/**
 * @author Nicolas GERAUD (nicolas.geraud at graviteesource.com)
 * @author GraviteeSource Team
 */
@Api(tags = {"Roles"})
public class RoleUserResource extends AbstractResource  {

    @Inject
    private MembershipService membershipService;

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Delete the role for a given user",
            notes = "User must have the MANAGEMENT_ROLE[DELETE] permission to use this service")
    @ApiResponses({
            @ApiResponse(code = 204, message = "Role successfully removed"),
            @ApiResponse(code = 500, message = "Internal server error")})
    @Permissions({
            @Permission(value = RolePermission.ORGANIZATION_ROLE, acls = RolePermissionAction.DELETE)
    })
    public void deleteRoleForUser(@PathParam("scope")RoleScope scope,
                       @PathParam("role") String role,
                       @PathParam("userId") String userId) {
        if (RoleScope.ORGANIZATION.equals(scope)) {
            membershipService.deleteReferenceMember(
                    MembershipReferenceType.ORGANIZATION,
                    GraviteeContext.getCurrentOrganization(),
                    MembershipMemberType.USER,
                    userId);
        } else if (RoleScope.ENVIRONMENT.equals(scope)) {
            membershipService.deleteReferenceMember(
                    MembershipReferenceType.ENVIRONMENT,
                    GraviteeContext.getCurrentEnvironment(),
                    MembershipMemberType.USER,
                    userId);
        }
    }
}

@@ -964,24 +964,70 @@
 erenceType;%0A
+import io.gravitee.rest.api.model.RoleEntity;%0A
 import io.gr
@@ -1068,32 +1068,32 @@
 RolePermission;%0A
-
 import io.gravit
@@ -1596,16 +1596,43 @@
 thParam;
+%0Aimport java.util.Optional;
 %0A%0A/**%0A *
@@ -2567,24 +2567,219 @@
 g userId) %7B%0A
+        final Optional%3CRoleEntity%3E roleToRemove = roleService.findByScopeAndName(scope, role);%0A        if (roleToRemove.isPresent()) %7B%0A            String roleId = roleToRemove.get().getId();%0A    
         if (
@@ -2822,32 +2822,36 @@
 ) %7B%0A            
+    
 membershipServic
@@ -2852,39 +2852,32 @@
 Service.
-deleteReferenceMember(%0A
+removeRole(%0A    
         
@@ -2938,32 +2938,36 @@
                 
+  
+  
 GraviteeContext.
@@ -2988,24 +2988,28 @@
 nization(),%0A
+    
             
@@ -3063,33 +3063,72 @@
         
+    
 userId
-);%0A
+,%0A                        roleId);%0A   
         
 %7D else i
@@ -3119,16 +3119,17 @@
         
+ 
 %7D else i
@@ -3174,32 +3174,36 @@
 ) %7B%0A            
+    
 membershipServic
@@ -3208,31 +3208,24 @@
 ice.
-deleteReferenceMember(%0A
+removeRole(%0A    
     
@@ -3289,32 +3289,36 @@
                 
+  
+  
 GraviteeContext.
@@ -3338,24 +3338,28 @@
 ironment(),%0A
+    
             
@@ -3417,16 +3417,66 @@
     
+    
 userId
-);
+,%0A                        roleId);%0A            %7D
 %0A   

remove only the selected role