En fait j'ai créé un espace de réception de documents dans chaque espace de service.
package lpr.alfresco.repo.workflow.jbpm;
import java.util.List;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.security.authority.AuthorityDAO;
import org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript;
import org.alfresco.repo.workflow.jbpm.JBPMNode;
import org.alfresco.repo.workflow.jbpm.JBPMSpringActionHandler;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.OwnableService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.workflow.WorkflowException;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import org.jbpm.graph.exe.ExecutionContext;
import org.springframework.beans.factory.BeanFactory;
//
//Required to change Workflow permission on web-client-config-workflow-actions.xml in WEB-INF\classes\alfresco like this
//
//<action id="start_workflow">
// <permissions>
// <permission allow="true">Write</permission>
// </permissions>
// …
//
//and add workflow user as adminUsers in WEB-INF\classes\alfresco\authority-services-context.xml
public class SetPermissions extends JBPMSpringActionHandler {
private static final long serialVersionUID = 5L;
private static final Logger logger = Logger.getLogger(SetPermissions.class);
private BeanFactory factory;
private ServiceRegistry services;
private NodeService nodeService;
private NamespaceService namespaceService;
private AuthorityDAO authorityDAO;
private PermissionService permissionService;
private OwnableService ownableService;
// Property names
public static final QName PROP_SAVE_OWNER = QName.createQName("lpr.model","SaveOwner");
public static final String WORKFLOW_USER = "workflow";
public static final String WORKFLOW_PASSWORD = "password";
// Patameters
private Element actor;
private String permission;
private String pooledactors;
@Override
protected void initialiseHandler(BeanFactory factory) {
this.factory = factory;
services = (ServiceRegistry)this.factory.getBean(ServiceRegistry.SERVICE_REGISTRY);
authorityDAO = (AuthorityDAO)this.factory.getBean("authorityDAO");
nodeService = (NodeService)this.factory.getBean("nodeService");
namespaceService = (NamespaceService)this.factory.getBean("namespaceService");
permissionService = (PermissionService)this.factory.getBean("permissionService");
ownableService = (OwnableService)this.factory.getBean("ownableService");
}
public void execute(ExecutionContext executionContext) throws Exception {
System.out.println("Set Permissions…");
if (actor == null && pooledactors == null && (permission.equals("set") || permission.equals("unset") ||
(permission.equals("setEditor") || permission.equals("unsetEditor")))) {
throw new WorkflowException("actor has not been provided");
}
if (permission == null) {
throw new WorkflowException("permission has not been provided");
}
// Get the Actor ID
String assignedActor = null;
if (actor != null)
{
String actorValStr = actor.getTextTrim();
if (actorValStr != null && actorValStr.length() > 0)
{
if (actorValStr.startsWith("#{"))
{
String expression = actorValStr.substring(2, actorValStr.length() -1);
Object eval = AlfrescoJavaScript.executeScript(executionContext, services, expression, null);
if (eval == null) {
throw new WorkflowException("actor expression '" + actorValStr + "' evaluates to null");
}
String actor = null;
if (eval instanceof String) {
actor = (String)eval;
}
else if (eval instanceof JBPMNode) {
actor = mapAuthorityToName((JBPMNode)eval, false);
}
if (actor == null) {
throw new WorkflowException("actor expression must evaluate to a person");
}
assignedActor = actor;
}
else {
assignedActor = actorValStr;
}
}
}
if (pooledactors != null) {
assignedActor = pooledactors;
}
try {
Object res = executionContext.getContextInstance().getVariable("bpm_package");
if(res == null) {
//never enters here
logger.fatal("bpm_package is null");
return;
}
final NodeRef nodeRef = ((JBPMNode) res).getNodeRef();
if(nodeRef == null) {
//never enters here
logger.fatal("NodeRef is null");
return;
}
String DocPath = null;
List<ChildAssociationRef> childRefs = nodeService.getChildAssocs(nodeRef);
for (ChildAssociationRef tchildRef : childRefs) {
final NodeRef childRef = tchildRef.getChildRef();
DocPath = nodeService.getPath(childRef).toPrefixString(namespaceService);
}
Repository repository = (Repository)this.factory.getBean("JCR.Repository");
Session session = null;
try {
// Think to add workflow as administrator on the "authority-service-contxt.xml" file on "class\alfresco"
session = repository.login(new SimpleCredentials(WORKFLOW_USER,WORKFLOW_PASSWORD.toCharArray()));
// access the page via JCR
Node rootNode = session.getRootNode();
Node curentNodeRef = rootNode.getNode(DocPath);
// Convert the JCR Node to an Alfresco Node Reference
NodeRef AdminNodeRef = JCRNodeRef.getNodeRef(curentNodeRef);
// Change permission
if (permission.equals("remove")) {
removePermissions(AdminNodeRef);
} else if (permission.equals("restore")) {
restorePermissions(AdminNodeRef);
} else if (permission.equals("set")) {
// Add CONSUMER permission for the current workflow actor
permissionService.setPermission(AdminNodeRef, assignedActor,PermissionService.CONSUMER,true);
} else if (permission.equals("unset")) {
// Remove CONSUMER permission for the current workflow actor
permissionService.deletePermission(AdminNodeRef, assignedActor, PermissionService.CONSUMER);
} else if (permission.equals("setEditor")) {
// Add EDIOTOR permission for the current workflow actor
permissionService.setPermission(AdminNodeRef, assignedActor,PermissionService.EDITOR,true);
} else if (permission.equals("unsetEditor")) {
// Remove EDITOR permission for the current workflow actor
permissionService.deletePermission(AdminNodeRef, assignedActor,PermissionService.EDITOR);
} else {
throw new WorkflowException("permission has not been provided");
}
session.save();
// Debug only
/*Collection<AccessPermission> permissions = permissionService.getAllSetPermissions(AdminNodeRef);
Iterator iterator = permissions.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
System.out.println("——> Permissions(Result): " + element);
} */
// Debug Node only
//System.out.println("Node Path: " + nodeService.getPath(nodeRef));
/*Collection<AccessPermission> nodepermissions = permissionService.getAllSetPermissions(nodeRef);
Iterator nodeiterator = nodepermissions.iterator();
while (nodeiterator.hasNext()) {
Object nodeelement = nodeiterator.next();
System.out.println("——> Permissions(Result): " + nodeelement);
} */
}
finally {
session.save();
session.logout();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String mapAuthorityToName(ScriptNode authority, boolean allowGroup) {
String name = null;
QName type = authority.getType();
if (type.equals(ContentModel.TYPE_PERSON)) {
name = (String)authority.getProperties().get(ContentModel.PROP_USERNAME);
} else if (type.equals(ContentModel.TYPE_AUTHORITY)) {
name = authorityDAO.getAuthorityName(authority.getNodeRef());
} else if (allowGroup && type.equals(ContentModel.TYPE_AUTHORITY_CONTAINER)) {
name = authorityDAO.getAuthorityName(authority.getNodeRef());
}
return name;
}
private void removePermissions(NodeRef nodeRef) {
//System.out.println("**************** remove Permissions ****************");
// Disabled all Permissions
Set<AccessPermission> Permissions = permissionService.getAllSetPermissions(nodeRef);
for (AccessPermission permission : Permissions){
String authority = permission.getAuthority();
String Permission = permission.getPermission();
/*AccessStatus status = permission.getAccessStatus();
System.out.println("——> Permissions(Name / Permission / Status): " +
authority + " / " +
Permission + " / " +
status);*/
permissionService.setPermission(nodeRef,authority,Permission,false);
}
// Save current Owner and Take Ownership
String owner = ownableService.getOwner(nodeRef);
nodeService.setProperty(nodeRef,PROP_SAVE_OWNER,owner);
//System.out.println("Owner : " + owner);
//session.save();
ownableService.takeOwnership(nodeRef);
// Add CONSUMER permission for old Owner
permissionService.setPermission(nodeRef, owner,PermissionService.CONSUMER,true);
}
private void restorePermissions(NodeRef nodeRef) {
//System.out.println("**************** restore Permissions ****************");
// Get saved Owner and set the document with this one
String saveOwner = (String)nodeService.getProperty(nodeRef, PROP_SAVE_OWNER);
ownableService.setOwner(nodeRef, saveOwner);
//System.out.println("Save Owner : " + saveOwner);
// Remove CONSUMER permission for the saved Owner
permissionService.deletePermission(nodeRef, saveOwner, PermissionService.CONSUMER);
// Enabled all Permissions
Set<AccessPermission> Permissions = permissionService.getAllSetPermissions(nodeRef);
for (AccessPermission permission : Permissions){
String authority = permission.getAuthority();
String Permission = permission.getPermission();
/*AccessStatus status = permission.getAccessStatus();
System.out.println("——> Permissions(Name / Permission / Status): " +
authority + " / " +
Permission + " / " +
status);*/
permissionService.setPermission(nodeRef,authority,Permission,true);
}
}
}
<!– Set read permission for the initiator –>
<action class="lpr.alfresco.repo.workflow.jbpm.SetPermissions">
<actor>#{initiator}</actor>
<permission>set</permission>
</action>
Content from pre 2016 and from language groups that have been closed.
Content is read-only.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.