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

Commit

Permalink
rhbz1021357 - Insert the authenticated person into the Security Rules…
Browse files Browse the repository at this point in the history
…' context for asynchronous processes.
  • Loading branch information
Carlos A. Munoz committed Oct 22, 2013
1 parent f1f4304 commit 9154034
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -24,12 +24,15 @@

import javax.security.auth.Subject;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.security.RunAsOperation;
import org.zanata.action.AuthenticationEvents;
import org.zanata.dao.AccountDAO;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -51,13 +54,14 @@ public class AsynchronousTaskExecutor {
@Asynchronous
public <V, H extends AsyncTaskHandle<V>> void runAsynchronously(
final AsyncTask<V, H> task, final Principal runAsPpal,
final Subject runAsSubject) {
final Subject runAsSubject, final String username) {
AsyncUtils.outject(task.getHandle(), ScopeType.EVENT);

RunAsOperation runAsOp = new RunAsOperation() {
@Override
public void execute() {
try {
prepareSecurityContext(username);
V returnValue = task.call();
task.getHandle().set(returnValue);
} catch (Exception t) {
Expand All @@ -82,4 +86,21 @@ public Subject getSubject() {
runAsOp.run();
}

/**
* Prepares the Drools security context so that it contains all the
* necessary facts for security checking.
*/
private static void prepareSecurityContext(String username) {
/*
* TODO This should be changed to not need the username. There should be
* a way to simulate a login for asyn tasks, or at least to inherit the
* caller's context
*/
AccountDAO accountDAO =
(AccountDAO) Component.getInstance(AccountDAO.class);
AuthenticationEvents authEvts =
(AuthenticationEvents) Component
.getInstance(AuthenticationEvents.class);
authEvts.loginSuccessful(accountDAO.getByUsername(username));

This comment has been minimized.

Copy link
@seanf

seanf Oct 22, 2013

Contributor

Which invokes log.info("Account {0} authenticated", account.getUsername()); So. Many. Log. Statements.

I really wish we could do away with the need for authenticatedPerson in security.drl, but in the meantime, perhaps we could separate the idea of "login successful" from inserting authenticatedPerson into the security context.

This comment has been minimized.

Copy link
@carlosmunoz

carlosmunoz Oct 22, 2013

Member

We could duplicate the code that injects the account into the security context. I was trying to reuse what was already there. But it seems we are going to have to do that anyway as we might need to check for null in this case.

This comment has been minimized.

Copy link
@seanf

seanf Oct 22, 2013

Contributor

Rather than duplicate anything, can't we just extract the code that injects the account into the security context into its own method?

}
}
5 changes: 3 additions & 2 deletions zanata-war/src/main/java/org/zanata/async/TaskExecutor.java
Expand Up @@ -61,8 +61,9 @@ public <V, H extends AsyncTaskHandle<V>> AsyncTaskHandle<V> startTask(
}

Identity identity = Identity.instance();
asynchronousTaskExecutor.runAsynchronously(task,
identity.getPrincipal(), identity.getSubject());
asynchronousTaskExecutor.runAsynchronously(task, identity
.getPrincipal(), identity.getSubject(), identity
.getCredentials().getUsername());
return handle;
}

Expand Down

0 comments on commit 9154034

Please sign in to comment.