package edu.harvard.iq.dataverse.engine.command.impl;

import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.RoleAssignment;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.authorization.users.User;
import edu.harvard.iq.dataverse.engine.command.AbstractVoidCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;

/**
 * Revokes a role for a user on a dataverse.
 * @author michael
 */
@RequiredPermissions( Permission.AssignRole )
public class RevokeRoleCommand extends AbstractVoidCommand {
	
	private final RoleAssignment toBeRevoked;

	public RevokeRoleCommand(RoleAssignment toBeRevoked, User aUser) {
		super(aUser, toBeRevoked.getDefinitionPoint());
		this.toBeRevoked = toBeRevoked;
	}
	
	@Override
	protected void executeImpl(CommandContext ctxt) throws CommandException {
		ctxt.roles().revoke(toBeRevoked);
	}
	
}

@@ -48,16 +48,58 @@
 .impl;%0A%0A
+import edu.harvard.iq.dataverse.DataFile;%0A
 import e
@@ -432,76 +432,8 @@
 xt;%0A
-import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;%0A
 impo
@@ -502,16 +502,90 @@
 ception;
+%0Aimport java.util.Collections;%0Aimport java.util.Map;%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -654,53 +654,73 @@
  */%0A
-@RequiredPermissions( Permission.AssignRole )
+// no annotations here, since permissions are dynamically decided
 %0Apub
@@ -895,222 +895,800 @@
 ) %7B%0A
-%09%09super(aUser, toBeRevoked.getDefinitionPoint());%0A%09%09this.toBeRevoked = toBeRevoked;%0A%09%7D%0A%09%0A%09@Override%0A%09protected void executeImpl(CommandContext ctxt) throws CommandException %7B%0A%09%09ctxt.roles().revoke(toBeRevoked);%0A%09%7D%0A
+        // for data file check permission on owning dataset%0A        super(aUser, toBeRevoked.getDefinitionPoint() instanceof DataFile ? toBeRevoked.getDefinitionPoint().getOwner() : toBeRevoked.getDefinitionPoint());%0A%09%09this.toBeRevoked = toBeRevoked;%0A%09%7D%0A%09%0A%09@Override%0A%09protected void executeImpl(CommandContext ctxt) throws CommandException %7B%0A%09%09ctxt.roles().revoke(toBeRevoked);%0A%09%7D%0A        %0A    @Override%0A    public Map%3CString, Set%3CPermission%3E%3E getRequiredPermissions() %7B%0A        // for data file check permission on owning dataset%0A        return Collections.singletonMap(%22%22,%0A                toBeRevoked.getDefinitionPoint() instanceof Dataverse ? Collections.singleton(Permission.ManageDataversePermissions)%0A                : Collections.singleton(Permission.ManageDatasetPermissions));%0A    %7D
 %09%0A%7D%0A

use new, simplified permissions (and set dynamically)