View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.actions.activation;
18  
19  import java.util.Map;
20  
21  import javax.jcr.Node;
22  import javax.jcr.Property;
23  import javax.jcr.Session;
24  import javax.jcr.Value;
25  
26  import org.exoplatform.services.cms.actions.ActionPlugin;
27  import org.exoplatform.services.cms.actions.ActionServiceContainer;
28  import org.exoplatform.services.cms.actions.impl.ScriptActionPlugin;
29  import org.exoplatform.services.jcr.RepositoryService;
30  import org.exoplatform.services.log.ExoLogger;
31  import org.exoplatform.services.log.Log;
32  import org.exoplatform.services.security.Identity;
33  import org.exoplatform.services.security.IdentityConstants;
34  import org.exoplatform.services.security.IdentityRegistry;
35  import org.exoplatform.services.security.MembershipEntry;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  import org.quartz.Job;
38  import org.quartz.JobDataMap;
39  import org.quartz.JobExecutionContext;
40  import org.quartz.JobExecutionException;
41  
42  /**
43   * Created by The eXo Platform SAS
44   * Author : Pham Xuan Hoa
45   *          hoa.pham@exoplatform.com
46   * Dec 21, 2006
47   */
48  public class ScriptActionActivationJob implements Job {
49  
50    final private static String COUNTER_PROP = "exo:counter" ;
51    private static final Log LOG  = ExoLogger.getLogger(ScriptActionActivationJob.class.getName());
52  
53    public void execute(JobExecutionContext context) throws JobExecutionException {
54      RepositoryService repositoryService = WCMCoreUtils.getService(RepositoryService.class);
55      ActionServiceContainer actionServiceContainer = WCMCoreUtils.getService(ActionServiceContainer.class);
56      IdentityRegistry identityRegistry = WCMCoreUtils.getService(IdentityRegistry.class);
57      ActionPlugin scriptActionService = actionServiceContainer.getActionPlugin(ScriptActionPlugin.ACTION_TYPE) ;
58  
59      Session jcrSession = null;
60      Node actionNode = null ;
61      JobDataMap jdatamap = context.getJobDetail().getJobDataMap() ;
62      String userId = jdatamap.getString("initiator") ;
63      String srcWorkspace = jdatamap.getString("srcWorkspace") ;
64      String srcPath = jdatamap.getString("srcPath") ;
65      String actionName = jdatamap.getString("actionName") ;
66      String executable = jdatamap.getString("executable") ;
67      Map variables = jdatamap.getWrappedMap() ;
68      try {
69        jcrSession = repositoryService.getCurrentRepository().getSystemSession(srcWorkspace);
70        Node node = (Node) jcrSession.getItem(srcPath);
71        actionNode = actionServiceContainer.getAction(node, actionName);
72        Property rolesProp = actionNode.getProperty("exo:roles");
73        Value[] roles = rolesProp.getValues();
74        boolean hasPermission = checkExcetuteable(userId, roles, identityRegistry);
75        if (!hasPermission) {
76          jcrSession.logout();
77          return;
78        }
79        scriptActionService.activateAction(userId, executable, variables);
80        int currentCounter = (int)actionNode.getProperty(COUNTER_PROP).getValue().getLong() ;
81        actionNode.setProperty(COUNTER_PROP,currentCounter +1) ;
82        actionNode.save() ;
83        jcrSession.save() ;
84      } catch (Exception e) {
85        if (LOG.isErrorEnabled()) {
86          LOG.error("Unexpected error", e);
87        }
88      } finally {
89        if(jcrSession != null) jcrSession.logout();
90      }
91    }
92  
93    private boolean checkExcetuteable(String userId,Value[] roles, IdentityRegistry identityRegistry) throws Exception {
94      if(IdentityConstants.SYSTEM.equalsIgnoreCase(userId)) {
95        return true ;
96      }
97      Identity identity = identityRegistry.getIdentity(userId);
98      if(identity == null) {
99        return false ;
100     }
101     for (int i = 0; i < roles.length; i++) {
102       String role = roles[i].getString();
103       if("*".equalsIgnoreCase(role)) return true ;
104       MembershipEntry membershipEntry = MembershipEntry.parse(role) ;
105       if(identity.isMemberOf(membershipEntry)) {
106         return true ;
107       }
108     }
109     return false ;
110   }
111 }