This commit is contained in:
Lucio Lelii 2019-04-11 14:38:41 +00:00
parent 8876b9f446
commit 609418a131
2 changed files with 69 additions and 81 deletions

View File

@ -15,7 +15,9 @@ import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.jackrabbit.api.JackrabbitSession;
@ -99,7 +101,7 @@ public class GroupManager {
Group createdGroup = usrManager.createGroup(group);
groupId = createdGroup.getID();
//TODO: A VREFolder must be created
createVreFolder(groupId, session);
session.save();
}catch(Exception e) {
@ -114,20 +116,23 @@ public class GroupManager {
}
@DELETE
@Path("{id}")
@Path("{group}")
@AuthorizationControl(allowed={"lucio.lelii"}, exception=MyAuthException.class)
public String deleteGroup(@PathParam("id") String id){
public String deleteGroup(@PathParam("group") String group){
JackrabbitSession session = null;
String userId = null;
try {
session = (JackrabbitSession) repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
org.apache.jackrabbit.api.security.user.UserManager usrManager = session.getUserManager();
//TODO: the VREFolder must be deleted
Authorizable authorizable = usrManager.getAuthorizable(id);
try {
Node sharedRootNode = session.getNode(Constants.SHARED_FOLDER_PATH);
sharedRootNode.getNode(group).removeSharedSet();
}catch (Exception e) {
log.warn("vreFolder {} not found, removing only the group", group);
}
Authorizable authorizable = usrManager.getAuthorizable(group);
if (authorizable.isGroup())
authorizable.remove();
session.save();
@ -139,7 +144,7 @@ public class GroupManager {
session.logout();
}
return userId;
return group;
}
@PUT
@ -159,6 +164,21 @@ public class GroupManager {
success = group.addMember(user);
String folderName = group.getPrincipal().getName();
Node sharedRootNode = session.getNode(Constants.SHARED_FOLDER_PATH);
Node folder = sharedRootNode.getNode(folderName);
AccessControlManager acm = session.getAccessControlManager();
JackrabbitAccessControlList acls = AccessControlUtils.getAccessControlList(acm, folder.getPath());
Privilege[] usersPrivileges = new Privilege[] { acm.privilegeFromName(AccessType.WRITE_OWNER.getValue()) };
String userPath = String.format("%s%s/%s",Utils.getWorkspacePath(user.getPrincipal().getName()).toPath(),Constants.VRE_FOLDER_PARENT_NAME, folderName);
log.debug("creating folder in user path {}", userPath );
session.getWorkspace().clone(session.getWorkspace().getName(), folder.getPath(),userPath , false);
acls.addAccessControlEntry(user.getPrincipal(), usersPrivileges );
acm.setPolicy(folder.getPath(), acls);
session.save();
}catch(Exception e) {
log.error("jcr error adding user {} to group {}", userId, groupId, e);
@ -202,6 +222,7 @@ public class GroupManager {
@GET
@Path("{groupId}/users")
@Produces(MediaType.APPLICATION_JSON)
@AuthorizationControl(allowed={"lucio.lelii"}, exception=MyAuthException.class)
public List<String> getUsersOfGroup(@PathParam("groupId") String groupId){
@ -233,18 +254,7 @@ public class GroupManager {
return users;
}
@POST
@Path("{groupId}/createVREFolder")
@AuthorizationControl(allowed={"lucio.lelii"}, exception=MyAuthException.class)
public Response createVreFolder(@PathParam("groupId") String groupId){
JackrabbitSession session = null;
try {
session = (JackrabbitSession) repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
org.apache.jackrabbit.api.security.user.UserManager usrManager = session.getUserManager();
Group group = (Group)usrManager.getAuthorizable(groupId);
private void createVreFolder(String groupId, JackrabbitSession session) throws Exception{
Node sharedRootNode = session.getNode(Constants.SHARED_FOLDER_PATH);
@ -264,29 +274,6 @@ public class GroupManager {
Privilege[] adminPrivileges = new Privilege[] { acm.privilegeFromName(AccessType.ADMINISTRATOR.getValue()) };
acls.addAccessControlEntry(AccessControlUtils.getPrincipal(session, AuthorizationProvider.instance.get().getClient().getId()), adminPrivileges );
Privilege[] usersPrivileges = new Privilege[] { acm.privilegeFromName(AccessType.WRITE_OWNER.getValue()) };
Iterator<Authorizable> it = group.getMembers();
while (it.hasNext()) {
Authorizable user = it.next();
String userPath = String.format("%s%s/%s",Utils.getWorkspacePath(user.getPrincipal().getName()).toPath(),Constants.VRE_FOLDER_PARENT_NAME, name);
log.debug("creating folder in user path {}", userPath );
session.getWorkspace().clone(session.getWorkspace().getName(), folder.getPath(),userPath , false);
}
acls.addAccessControlEntry(group.getPrincipal(), usersPrivileges );
acm.setPolicy(folder.getPath(), acls);
session.save();
}catch(Exception e) {
log.error("jcr error creating vreFolder {}", groupId, e);
GXOutboundErrorResponse.throwException(new BackendGenericError(e));
} finally {
if (session!=null)
session.logout();
}
return Response.ok().build();
}
}

View File

@ -112,6 +112,7 @@ public class WorkspaceManager {
org.gcube.common.storagehub.model.Path trashPath = Paths.append(Utils.getWorkspacePath(), Constants.TRASH_ROOT_FOLDER_NAME);
if (!ses.nodeExists(trashPath.toPath())) {
Utils.createFolderInternally(ses, ses.getNode(Utils.getWorkspacePath().toPath()) , Constants.TRASH_ROOT_FOLDER_NAME, "trash of "+user, false, user, null);
ses.save();
}
log.trace("time to connect to repo {}",(System.currentTimeMillis()-start));