package io.subutai.core.hubmanager.impl.environment.state.destroy;


import io.subutai.common.peer.EnvironmentId;
import io.subutai.core.hubmanager.impl.environment.state.Context;
import io.subutai.core.hubmanager.impl.environment.state.StateHandler;
import io.subutai.hub.share.dto.environment.EnvironmentPeerDto;


public class DeletePeerStateHandler extends StateHandler
{
    public DeletePeerStateHandler( Context ctx )
    {
        super( ctx, "Deleting peer" );
    }


    @Override
    protected Object doHandle( EnvironmentPeerDto peerDto ) throws Exception
    {
        logStart();

        EnvironmentId envId = new EnvironmentId( peerDto.getEnvironmentInfo().getId() );

        ctx.localPeer.cleanupEnvironment( envId );

        ctx.envUserHelper.handleEnvironmentOwnerDeletion( peerDto );

        logEnd();

        return null;
    }


    /**
     * Instead of environment owner token, a peer token is needed to remove a Hub user.
     */
    @Override
    protected String getToken( EnvironmentPeerDto peerDto )
    {
        return peerDto.getPeerToken();
    }


    @Override
    protected void post( EnvironmentPeerDto peerDto, Object body )
    {
        ctx.restClient.delete( path( "/rest/v1/environments/%s/peers/%s", peerDto ) );
    }
}
@@ -62,16 +62,93 @@
 troy;%0A%0A%0A
+import java.security.PrivilegedAction;%0A%0Aimport javax.security.auth.Subject;%0A%0A
 import i
@@ -321,16 +321,67 @@
 andler;%0A
+import io.subutai.core.identity.api.model.Session;%0A
 import i
@@ -643,16 +643,22 @@
 oHandle(
+ final
  Environ
@@ -877,285 +877,303 @@
     
-ctx.envUserHelper.handleEnvironmentOwnerDeletion( peerDto );%0A%0A        logEnd();%0A%0A        return null;%0A    %7D%0A%0A%0A    /**%0A     * Instead of environment owner token, a peer token is needed to remove a Hub user.
+Session session = ctx.identityManager.login( %22token%22, peerDto.getPeerToken() );%0A%0A        Subject.doAs( session.getSubject(), new PrivilegedAction%3CVoid%3E()%0A        %7B%0A            @Override%0A            public Void run()%0A            %7B
 %0A     
-*/%0A
     
-@Override%0A    protected String getToken( EnvironmentPeerDto
+       ctx.envUserHelper.handleEnvironmentOwnerDeletion(
  pee
@@ -1178,23 +1178,26 @@
 eerDto )
-%0A
+;%0A  
     
-%7B%0A
+  
         
@@ -1203,38 +1203,88 @@
  return 
-peerDto.getPeerToken()
+null;%0A            %7D%0A        %7D );%0A%0A        logEnd();%0A%0A        return null
 ;%0A    %7D%0A

Refactor delete env owner, change session to peer owner