Skip to content
Permalink
Browse files
Merge pull request #38 from isururanawaka/sharing_service_impl
Add sharing handler and bug fix in Resource update
  • Loading branch information
isururanawaka committed Aug 12, 2021
2 parents a96e649 + efe0e5c commit 54dc4bda71bb17b0e85f14363fe992ac5e358b65
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 0 deletions.
@@ -307,6 +307,14 @@ public void updateResource(ResourceUpdateRequest
serializedMap.put("lastModifiedTime", exEntity.get().getCreatedAt());
serializedMap.put("owner", exEntity.get().getOwnerId());
serializedMap.putAll(request.getResource().getPropertiesMap());
if (serializedMap.containsKey("properties") && serializedMap.get("properties") instanceof List) {
List propertiesList = (List) serializedMap.get("properties");
propertiesList.forEach(property -> {
MapEntry entry = (MapEntry) property;
serializedMap.put(entry.getKey().toString(), entry.getValue());
});
}
serializedMap.remove("properties");

if (!parentId.isEmpty()) {
this.neo4JConnector.mergeNodesWithParentChildRelationShip(serializedMap, new HashMap<>(),
@@ -0,0 +1,239 @@
package org.apache.airavata.drms.api.handlers;


import com.google.protobuf.Empty;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import org.apache.airavata.datalake.drms.AuthenticatedUser;
import org.apache.airavata.datalake.drms.sharing.*;
import org.apache.custos.clients.CustosClientProvider;
import org.apache.custos.sharing.management.client.SharingManagementClient;
import org.apache.custos.sharing.service.Entity;
import org.apache.custos.sharing.service.PermissionType;
import org.apache.custos.sharing.service.SharingRequest;
import org.lognet.springboot.grpc.GRpcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@GRpcService
public class SharingServiceHandler extends DRMSSharingServiceGrpc.DRMSSharingServiceImplBase {
private static final Logger LOGGER = LoggerFactory.getLogger(SharingServiceHandler.class);

@Autowired
private CustosClientProvider custosClientProvider;


@Override
public void shareEntityWithUser(ShareEntityWithUserRequest request, StreamObserver<Empty> responseObserver) {
try {

AuthenticatedUser authenticatedUser = request.getAuthToken().getAuthenticatedUser();
String username = authenticatedUser.getUsername();
String tenantId = authenticatedUser.getTenantId();

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeEditor)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status status = sharingManagementClient
.userHasAccess(tenantId, sharingRequestEditor);
SharingRequest sharingRequestAdmin = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeAdmin)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status statusAdmin = sharingManagementClient
.userHasAccess(tenantId, sharingRequestAdmin);
if (status.getStatus() || statusAdmin.getStatus()) {
SharingRequest shrRequest = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(PermissionType.newBuilder().setId(request.getPermissionId()).build())
.addOwnerId(request.getSharedUserId()).build();
sharingManagementClient.shareEntityWithUsers(tenantId, shrRequest);
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();

} else {
String msg = "You don't have permission to manage sharing";
LOGGER.error(msg);
responseObserver.onError(Status.PERMISSION_DENIED.withDescription(msg).asRuntimeException());
}

} catch (Exception ex) {
LOGGER.error("Error occurred while sharing entity with user {}", request.getSharedUserId());
String msg = "Error occurred while sharing entity with user {}" + request.getSharedUserId();
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}

}

@Override
public void shareEntityWithGroup(ShareEntityWithGroupRequest request, StreamObserver<Empty> responseObserver) {
try {

AuthenticatedUser authenticatedUser = request.getAuthToken().getAuthenticatedUser();
String username = authenticatedUser.getUsername();
String tenantId = authenticatedUser.getTenantId();

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeEditor)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status status = sharingManagementClient
.userHasAccess(tenantId, sharingRequestEditor);
SharingRequest sharingRequestAdmin = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeAdmin)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status statusAdmin = sharingManagementClient
.userHasAccess(tenantId, sharingRequestAdmin);
if (status.getStatus() || statusAdmin.getStatus()) {
SharingRequest shrRequest = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(PermissionType.newBuilder().setId(request.getPermissionId()).build())
.addOwnerId(request.getSharedGroupId()).build();
sharingManagementClient.shareEntityWithGroups(tenantId, shrRequest);
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();

} else {
String msg = "You don't have permission to manage sharing";
LOGGER.error(msg);
responseObserver.onError(Status.PERMISSION_DENIED.withDescription(msg).asRuntimeException());
}


} catch (Exception ex) {
LOGGER.error("Error occurred while sharing entity with group {}", request.getSharedGroupId());
String msg = "Error occurred while sharing entity with group {}" + request.getSharedGroupId();
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}

@Override
public void revokeEntitySharingFromUser(RevokeEntityWithUserRequest request, StreamObserver<Empty> responseObserver) {
try {
AuthenticatedUser authenticatedUser = request.getAuthToken().getAuthenticatedUser();
String username = authenticatedUser.getUsername();
String tenantId = authenticatedUser.getTenantId();

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeEditor)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status status = sharingManagementClient
.userHasAccess(tenantId, sharingRequestEditor);
SharingRequest sharingRequestAdmin = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeAdmin)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status statusAdmin = sharingManagementClient
.userHasAccess(tenantId, sharingRequestAdmin);
if (status.getStatus() || statusAdmin.getStatus()) {
SharingRequest shrRequest = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(PermissionType.newBuilder().setId(request.getPermissionId()).build())
.addOwnerId(request.getRevokedUserId()).build();
sharingManagementClient.revokeEntitySharingFromUsers(tenantId, shrRequest);
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();

} else {
String msg = "You don't have permission to manage sharing";
LOGGER.error(msg);
responseObserver.onError(Status.PERMISSION_DENIED.withDescription(msg).asRuntimeException());
}


} catch (Exception ex) {
LOGGER.error("Error occurred while revoking entity with user {}", request.getRevokedUserId());
String msg = "Error occurred while revoking entity with user {}" + request.getRevokedUserId();
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}

@Override
public void revokeEntitySharingFromGroup(RevokeEntityFromGroupRequest request, StreamObserver<Empty> responseObserver) {
try {
AuthenticatedUser authenticatedUser = request.getAuthToken().getAuthenticatedUser();
String username = authenticatedUser.getUsername();
String tenantId = authenticatedUser.getTenantId();

SharingManagementClient sharingManagementClient = custosClientProvider.getSharingManagementClient();
Entity entity = Entity.newBuilder().setId(request.getEntityId()).build();
PermissionType permissionTypeEditor = PermissionType.newBuilder().setId("EDITOR").build();
PermissionType permissionTypeAdmin = PermissionType.newBuilder().setId("ADMIN").build();

SharingRequest sharingRequestEditor = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeEditor)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status status = sharingManagementClient
.userHasAccess(tenantId, sharingRequestEditor);
SharingRequest sharingRequestAdmin = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(permissionTypeAdmin)
.addOwnerId(username).build();
org.apache.custos.sharing.service.Status statusAdmin = sharingManagementClient
.userHasAccess(tenantId, sharingRequestAdmin);
if (status.getStatus() || statusAdmin.getStatus()) {
SharingRequest shrRequest = SharingRequest
.newBuilder()
.setClientId(tenantId)
.setEntity(entity)
.setPermissionType(PermissionType.newBuilder().setId(request.getPermissionId()).build())
.addOwnerId(request.getRevokedGroupId()).build();
sharingManagementClient.revokeEntitySharingFromGroups(tenantId, shrRequest);
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();

} else {
String msg = "You don't have permission to manage sharing";
LOGGER.error(msg);
responseObserver.onError(Status.PERMISSION_DENIED.withDescription(msg).asRuntimeException());
}

} catch (Exception ex) {
LOGGER.error("Error occurred while revoking entity with user {}", request.getRevokedGroupId());
String msg = "Error occurred while revoking entity with user {}" + request.getRevokedGroupId();
responseObserver.onError(Status.INTERNAL.withDescription(msg).asRuntimeException());
}
}
}
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

syntax = "proto3";

option java_multiple_files = true;
package org.apache.airavata.datalake.drms.sharing;

import "google/api/annotations.proto";
import "Common.proto";
import "google/protobuf/empty.proto";


message ShareEntityWithUserRequest {
org.apache.airavata.datalake.drms.DRMSServiceAuthToken auth_token = 1;
string client_id = 2;
string entity_id = 3;
string permission_id = 4;
string shared_user_id = 5;
}


message ShareEntityWithGroupRequest {
org.apache.airavata.datalake.drms.DRMSServiceAuthToken auth_token = 1;
string client_id = 2;
string entity_id = 3;
string permission_id = 4;
string shared_group_id = 5;
}


message RevokeEntityWithUserRequest {
org.apache.airavata.datalake.drms.DRMSServiceAuthToken auth_token = 1;
string entity_id = 2;
string permission_id = 3;
string revoked_user_id = 4;

}


message RevokeEntityFromGroupRequest {
org.apache.airavata.datalake.drms.DRMSServiceAuthToken auth_token = 1;
string entity_id = 2;
string permission_id = 3;
string revoked_group_id = 4;

}


service DRMSSharingService {

rpc shareEntityWithUser (ShareEntityWithUserRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v1.0/api/drms/users/share"
};
}

rpc shareEntityWithGroup (ShareEntityWithGroupRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
post: "/v1.0/api/drms/groups/share"
};
}

rpc revokeEntitySharingFromUser (RevokeEntityWithUserRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1.0/api/drms/users/share"
};
}

rpc revokeEntitySharingFromGroup (RevokeEntityFromGroupRequest) returns (google.protobuf.Empty) {
option (google.api.http) = {
delete: "/v1.0/api/drms/groups/share"
};
}

}

0 comments on commit 54dc4bd

Please sign in to comment.