diff --git a/config/alfresco/action-services-context.xml b/config/alfresco/action-services-context.xml index 86222040db..e25f3915a5 100644 --- a/config/alfresco/action-services-context.xml +++ b/config/alfresco/action-services-context.xml @@ -5,19 +5,16 @@ - + - - - - + - - - - + + + + diff --git a/source/java/org/alfresco/repo/action/ActionImpl.java b/source/java/org/alfresco/repo/action/ActionImpl.java index 585e277bba..b92579f5ae 100644 --- a/source/java/org/alfresco/repo/action/ActionImpl.java +++ b/source/java/org/alfresco/repo/action/ActionImpl.java @@ -85,6 +85,11 @@ public class ActionImpl extends ParameterizedItemImpl */ private String actionDefinitionName; + /** + * The run as user name + */ + private String runAsUserName; + /** * The owning node reference */ @@ -377,4 +382,14 @@ public class ActionImpl extends ParameterizedItemImpl { return actionChain; } + + public String getRunAsUser() + { + return this.runAsUserName; + } + + public void setRunAsUser(String runAsUserName) + { + this.runAsUserName = runAsUserName; + } } diff --git a/source/java/org/alfresco/repo/action/ActionServiceImpl.java b/source/java/org/alfresco/repo/action/ActionServiceImpl.java index 73301c7517..cf325d3c39 100644 --- a/source/java/org/alfresco/repo/action/ActionServiceImpl.java +++ b/source/java/org/alfresco/repo/action/ActionServiceImpl.java @@ -28,7 +28,7 @@ import java.util.Set; import org.alfresco.model.ContentModel; import org.alfresco.repo.action.evaluator.ActionConditionEvaluator; import org.alfresco.repo.action.executer.ActionExecuter; -import org.alfresco.repo.policy.PolicyComponent; +import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.transaction.AlfrescoTransactionSupport; import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.ActionCondition; @@ -46,7 +46,6 @@ import org.alfresco.service.namespace.DynamicNamespacePrefixResolver; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.RegexQNamePattern; -import org.alfresco.service.transaction.TransactionService; import org.alfresco.util.GUID; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -88,16 +87,6 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A * The application context */ private ApplicationContext applicationContext; - - /** - * The transacton service - */ - private TransactionService transactionService; - - /** - * The policy component - */ - private PolicyComponent policyComponent; /** * The node service @@ -108,6 +97,9 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A * The search service */ private SearchService searchService; + + /** The authentication component */ + private AuthenticationComponent authenticationComponent; /** * The asynchronous action execution queue @@ -138,16 +130,6 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A { this.applicationContext = applicationContext; } - - /** - * Set the policy component - * - * @param policyComponent the policy component to register with - */ - public void setPolicyComponent(PolicyComponent policyComponent) - { - this.policyComponent = policyComponent; - } /** * Set the node service @@ -168,17 +150,17 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A { this.searchService = searchService; } - - /** - * Set the transaction service - * - * @param transactionService the transaction service - */ - public void setTransactionService(TransactionService transactionService) - { - this.transactionService = transactionService; - } - + + /** + * Set the authentication component + * + * @param authenticationComponent the authentication component + */ + public void setAuthenticationComponent(AuthenticationComponent authenticationComponent) + { + this.authenticationComponent = authenticationComponent; + } + /** * Set the asynchronous action execution queue * @@ -199,13 +181,6 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A { return asynchronousActionExecutionQueue; } - - /** - * Initialise methods called by Spring framework - */ - public void initialise() - { - } /** * Gets the saved action folder reference @@ -399,7 +374,6 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A try { - //Set currentActionChain = this.currentActionChain.get(); Set origActionChain = null; if (actionChain == null) @@ -457,7 +431,10 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A Action compensatingAction = action.getCompensatingAction(); if (compensatingAction != null) { - // Queue the compensating action ready for execution + // Set the current user + ((ActionImpl)compensatingAction).setRunAsUser(this.authenticationComponent.getCurrentUserName()); + + // Queue the compensating action ready for execution this.asynchronousActionExecutionQueue.executeAction(this, compensatingAction, actionedUponNodeRef, false, null); } } @@ -481,6 +458,12 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A */ public void directActionExecution(Action action, NodeRef actionedUponNodeRef) { + // Debug output + if (logger.isDebugEnabled() == true) + { + logger.debug("The action is being executed as the user: " + this.authenticationComponent.getCurrentUserName()); + } + // Get the action executer and execute ActionExecuter executer = (ActionExecuter)this.applicationContext.getBean(action.getActionDefinitionName()); executer.execute(action, actionedUponNodeRef); @@ -1097,6 +1080,13 @@ public class ActionServiceImpl implements ActionService, RuntimeActionService, A logger.debug("Doing addPostTransactionPendingAction"); } + // Set the run as user to the current user + if (logger.isDebugEnabled() == true) + { + logger.debug("The current user is: " + this.authenticationComponent.getCurrentUserName()); + } + ((ActionImpl)action).setRunAsUser(this.authenticationComponent.getCurrentUserName()); + // Ensure that the transaction listener is bound to the transaction AlfrescoTransactionSupport.bindListener(this.transactionListener); diff --git a/source/java/org/alfresco/repo/action/AsynchronousActionExecutionQueueImpl.java b/source/java/org/alfresco/repo/action/AsynchronousActionExecutionQueueImpl.java index 39d53d5e2d..ac5ffabdd9 100644 --- a/source/java/org/alfresco/repo/action/AsynchronousActionExecutionQueueImpl.java +++ b/source/java/org/alfresco/repo/action/AsynchronousActionExecutionQueueImpl.java @@ -24,9 +24,12 @@ import java.util.concurrent.TimeUnit; import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.transaction.TransactionUtil; import org.alfresco.service.cmr.action.Action; +import org.alfresco.service.cmr.action.ActionServiceException; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.transaction.TransactionService; +import freemarker.log.Logger; + /** * The asynchronous action execution queue implementation * @@ -261,32 +264,37 @@ public class AsynchronousActionExecutionQueueImpl extends ThreadPoolExecutor imp { try { - - // For now run all actions in the background as the system user - ActionExecutionWrapper.this.authenticationComponent - .setCurrentUser(ActionExecutionWrapper.this.authenticationComponent.getSystemUserName()); - try + // Get the run as user name + final String userName = ((ActionImpl)ActionExecutionWrapper.this.action).getRunAsUser(); + if (userName == null) { - TransactionUtil.executeInNonPropagatingUserTransaction(this.transactionService, - new TransactionUtil.TransactionWork() - { - public Object doWork() + throw new ActionServiceException("Cannot execute action asynchronously since run as user is 'null'"); + } + + TransactionUtil.executeInNonPropagatingUserTransaction(this.transactionService, + new TransactionUtil.TransactionWork() + { + public Object doWork() + { + ActionExecutionWrapper.this.authenticationComponent + .setCurrentUser(userName); + + try { - ActionExecutionWrapper.this.actionService.executeActionImpl( - ActionExecutionWrapper.this.action, - ActionExecutionWrapper.this.actionedUponNodeRef, - ActionExecutionWrapper.this.checkConditions, true, - ActionExecutionWrapper.this.actionChain); - - return null; + ActionExecutionWrapper.this.action, + ActionExecutionWrapper.this.actionedUponNodeRef, + ActionExecutionWrapper.this.checkConditions, true, + ActionExecutionWrapper.this.actionChain); } - }); - } - finally - { - ActionExecutionWrapper.this.authenticationComponent.clearCurrentSecurityContext(); - } + finally + { + ActionExecutionWrapper.this.authenticationComponent.clearCurrentSecurityContext(); + } + + return null; + } + }); } catch (Throwable exception) { diff --git a/source/java/org/alfresco/repo/rule/ruletrigger/RuleTriggerAbstractBase.java b/source/java/org/alfresco/repo/rule/ruletrigger/RuleTriggerAbstractBase.java index beec32b61f..e993b289ab 100644 --- a/source/java/org/alfresco/repo/rule/ruletrigger/RuleTriggerAbstractBase.java +++ b/source/java/org/alfresco/repo/rule/ruletrigger/RuleTriggerAbstractBase.java @@ -103,22 +103,9 @@ public abstract class RuleTriggerAbstractBase implements RuleTrigger */ protected void triggerRules(NodeRef nodeRef, NodeRef actionedUponNodeRef) { - String userName = authenticationComponent.getCurrentUserName(); - authenticationComponent.setSystemUserAsCurrentUser(); - try + for (RuleType ruleType : this.ruleTypes) { - for (RuleType ruleType : this.ruleTypes) - { - ruleType.triggerRuleType(nodeRef, actionedUponNodeRef); - } - } - finally - { - authenticationComponent.clearCurrentSecurityContext(); - if(userName != null) - { - authenticationComponent.setCurrentUser(userName); - } + ruleType.triggerRuleType(nodeRef, actionedUponNodeRef); } } }