Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
rhbz867657 - Add the ability to run an async process as a given ident…
…ity.

This prevents some permissions errors when running a process asynchrnously as the process doesn't inherit the same session context it was started with.
  • Loading branch information
Carlos Munoz committed Oct 18, 2012
1 parent 714ba95 commit 2c5ef50
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Expand Up @@ -25,6 +25,7 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.zanata.security.ZanataIdentity;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -45,6 +46,15 @@ public <H extends ProcessHandle> void runAsynchronously(RunnableProcess<H> proce
{
try
{
// Authenticate as the provided credentials
if( process.getRunAsUsername() != null )
{
ZanataIdentity identity = ZanataIdentity.instance();
identity.getCredentials().setUsername( process.getRunAsUsername() );
identity.setApiKey( process.getRunAsApiKey() );
identity.login();
}

process.run(handle);
}
catch( Throwable t )
Expand Down
30 changes: 30 additions & 0 deletions zanata-war/src/main/java/org/zanata/process/RunnableProcess.java
Expand Up @@ -20,6 +20,8 @@
*/
package org.zanata.process;

import org.zanata.security.ZanataIdentity;

import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -33,6 +35,10 @@
public abstract class RunnableProcess<H extends ProcessHandle>
{

private String runAsUsername;

private String runAsApiKey;

/**
* This method contains the logic to execute.
*
Expand Down Expand Up @@ -65,4 +71,28 @@ protected void handleThrowable( H handle, Throwable t )
{
handle.setError(t);
}

String getRunAsUsername()
{
return runAsUsername;
}

String getRunAsApiKey()
{
return runAsApiKey;
}

/**
* Tells the process to run with a specific Identity.
*
* @param identity Identity to execute the process as.
* @return The runnable process itself.
*/
public RunnableProcess<H> withIdentity( ZanataIdentity identity )
{
// TODO Only detects API identities.
this.runAsUsername = identity.getCredentials().getUsername();
this.runAsApiKey = identity.getApiKey();
return this;
}
}
Expand Up @@ -125,7 +125,7 @@ public ProcessStatus startSourceDocCreation(final @PathParam("id") String idNoSl

if( errorMessage == null )
{
processManagerServiceImpl.startProcess(
RunnableProcess<ProcessHandle> process =
new RunnableProcess<ProcessHandle>()
{
@Override
Expand All @@ -137,9 +137,9 @@ protected void run(ProcessHandle handle) throws Throwable
projectSlug, iterationSlug, resource, extensions, copytrans, true, handle);
handle.setCurrentProgress( handle.getMaxProgress() ); // TODO This should update with real progress
}
},
handle
);
}.withIdentity(identity);

processManagerServiceImpl.startProcess(process,handle);

//response.setStatus(Response.Status.ACCEPTED.getStatusCode());
return this.getProcessStatus(handle.getId());
Expand Down Expand Up @@ -170,7 +170,7 @@ public ProcessStatus startSourceDocCreationOrUpdate(final @PathParam("id") Strin

if( errorMessage == null )
{
processManagerServiceImpl.startProcess(
RunnableProcess<ProcessHandle> process =
new RunnableProcess<ProcessHandle>()
{
@Override
Expand All @@ -195,9 +195,9 @@ protected void handleThrowable(ProcessHandle handle, Throwable t)
AsynchronousProcessResourceService.log.error("Error pushing source document", t);
super.handleThrowable(handle, t); //To change body of overridden methods use File | Settings | File Templates.
}
},
handle
);
}.withIdentity(identity);

processManagerServiceImpl.startProcess(process,handle);

//response.setStatus(Response.Status.ACCEPTED.getStatusCode());
return this.getProcessStatus(handle.getId());
Expand Down Expand Up @@ -244,7 +244,7 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(final @PathParam("id") S

if( errorMessage == null )
{
processManagerServiceImpl.startProcess(
RunnableProcess<MessagesProcessHandle> process =
new RunnableProcess<MessagesProcessHandle>()
{
@Override
Expand All @@ -264,9 +264,9 @@ protected void handleThrowable(MessagesProcessHandle handle, Throwable t)
AsynchronousProcessResourceService.log.error("Error pushing translations", t);
super.handleThrowable(handle, t); //To change body of overridden methods use File | Settings | File Templates.
}
},
handle
);
}.withIdentity(identity);

processManagerServiceImpl.startProcess(process,handle);

return this.getProcessStatus(handle.getId());
}
Expand Down

0 comments on commit 2c5ef50

Please sign in to comment.