Author: jvelociter
Date: 2007-10-25 20:43:49 +0200 (Thu, 25 Oct 2007)
New Revision: 5502

Modified:
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/GroovyJob.java
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginApi.java
   
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
Log:
The calling the scheduler API should not require programming rights. Only the 
edit access level on the job document holder should be required. This commit 
removes getXWikiObjects() operations done by the api on wrapped Objects.



Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/GroovyJob.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/GroovyJob.java
    2007-10-25 16:28:36 UTC (rev 5501)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/GroovyJob.java
    2007-10-25 18:43:49 UTC (rev 5502)
@@ -60,7 +60,7 @@
             // The XWiki context was saved in the Job execution data map. Get 
it as we'll retrieve
             // the script to execute from it.
             Context xcontext = (Context) data.get("context");
-
+ 
             if (xcontext.hasProgrammingRights()) {
 
                 // Get the job XObject to be executed

Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginApi.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginApi.java
   2007-10-25 16:28:36 UTC (rev 5501)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginApi.java
   2007-10-25 18:43:49 UTC (rev 5502)
@@ -24,6 +24,7 @@
 import com.xpn.xwiki.api.Api;
 import com.xpn.xwiki.api.Document;
 import com.xpn.xwiki.api.Object;
+import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.objects.BaseObject;
 import com.xpn.xwiki.plugin.XWikiPluginInterface;
 import org.apache.commons.logging.Log;
@@ -32,7 +33,7 @@
 
 import java.util.Date;
 import java.util.Iterator;
-import java.util.Vector;
+import java.util.List;
 
 /**
  * A Scheduler plugin to plan execution of Jobs from XWiki with cron 
expressions. The plugin uses
@@ -76,7 +77,7 @@
     {
         try {
             return getJobStatus(object.getXWikiObject()).getValue();
-        } catch (SchedulerException e) {
+        } catch (Exception e) {
             context.put("error", e.getMessage());
             return null;
         }
@@ -86,17 +87,45 @@
      * Return the trigger state as a [EMAIL PROTECTED] JobState}, that holds 
both the integer trigger's inner
      * value of the state and a String as a human readable representation of 
that state
      */
-    public JobState getJobStatus(BaseObject object) throws SchedulerException
+    public JobState getJobStatus(BaseObject object) throws SchedulerException, 
SchedulerPluginException
     {
         return plugin.getJobStatus(object);
     }
 
-    public JobState getJobStatus(Object object) throws SchedulerException
+    public JobState getJobStatus(Object object) throws SchedulerException, 
SchedulerPluginException
     {
-        return plugin.getJobStatus(object.getXWikiObject());
+        return plugin.getJobStatus(retrieveBaseObject(object));
     }
 
     /**
+     * This function allow to retrieve a com.xpn.xwiki.objects.BaseObject from 
a
+     * com.xpn.xwiki.api.Object without that the current user needs 
programming rights (as in
+     * com.xpn.xwiki.api.Object#getXWikiObject(). The function is used 
internally by this api class
+     * and allows wiki users to call methods from the scheduler without having 
programming right.
+     * The programming right is only needed at script execution time.
+     *
+     * @return object the unwrapped version of the passed api object
+     */
+    private BaseObject retrieveBaseObject(Object object) throws 
SchedulerPluginException
+    {
+        String docName = object.getName();
+        int objNb = object.getNumber();
+        try
+        {
+
+            XWikiDocument jobHolder = 
context.getWiki().getDocument(docName,context);
+            BaseObject jobObject = 
jobHolder.getObject(SchedulerPlugin.XWIKI_JOB_CLASS,objNb);
+            return jobObject;
+        }
+        catch(XWikiException e)
+        {
+            throw new SchedulerPluginException(
+                
SchedulerPluginException.ERROR_SCHEDULERPLUGIN_UNABLE_TO_RETRIEVE_JOB,
+                "Job in document [" + docName + "] with object number ["+ 
objNb +"] could not be retrieved.", e);
+        }
+    }
+
+    /**
      * Schedule the given XObject to be executed according to its parameters. 
Errors are returned in
      * the context map. Scheduling can be called for example: <code> 
#if($xwiki.scheduler.scheduleJob($job)!=true)
      * #error($context.get("error") #else #info("Job scheduled") #end </code> 
Where $job is an
@@ -107,7 +136,14 @@
      */
     public boolean scheduleJob(Object object)
     {
-        return scheduleJob(object.getXWikiObject());
+        try{
+            return scheduleJob(retrieveBaseObject(object));
+        }
+        catch(Exception e){
+            // we don't need to push the exception message in the context here
+            // as it should already have been pushed by the throwing exception
+            return false;
+        }
     }
 
     public boolean scheduleJob(BaseObject object)
@@ -131,11 +167,20 @@
     public boolean scheduleJobs(Document document)
     {
         boolean result = true;
-        Vector objects = document.getObjects(SchedulerPlugin.XWIKI_JOB_CLASS);
-        for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
-            Object object = (Object) iterator.next();
-            result &= scheduleJob(object.getXWikiObject());
+        try
+        {
+            XWikiDocument doc = 
context.getWiki().getDocument(document.getFullName(),context);
+            List objects = doc.getObjects(SchedulerPlugin.XWIKI_JOB_CLASS);
+            for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
+                Object object = (Object) iterator.next();
+                result &= scheduleJob(object);
+            }
         }
+        catch(Exception e)
+        {
+            context.put("error", e.getMessage());
+            return false;
+        }
         return result;
     }
 
@@ -148,7 +193,17 @@
      */
     public boolean pauseJob(Object object)
     {
-        return pauseJob(object.getXWikiObject());
+        try
+        {
+            return pauseJob(retrieveBaseObject(object));
+        }
+        catch(Exception e)
+        {
+            // we don't need to push the exception message in the context here
+            // as it should already have been pushed by the throwing exception
+            return false;
+        }
+
     }
 
     public boolean pauseJob(BaseObject object)
@@ -164,7 +219,7 @@
     }
 
     /**
-     * Resume a XObject job that is in a [EMAIL PROTECTED] JobState#PAUSED} 
state. Can be called the same
+     * Resume a XObject job that is in a [EMAIL PROTECTED] 
JobState#STATE_PAUSED} state. Can be called the same
      * way as [EMAIL PROTECTED] #scheduleJob}
      *
      * @param object the wrapped XObject Job to be paused
@@ -172,7 +227,14 @@
      */
     public boolean resumeJob(Object object)
     {
-        return resumeJob(object.getXWikiObject());
+        try{
+            return resumeJob(retrieveBaseObject(object));
+        }
+        catch(Exception e){
+            // we don't need to push the exception message in the context here
+            // as it should already have been pushed by the throwing exception
+            return false;
+        }
     }
 
     public boolean resumeJob(BaseObject object)
@@ -196,7 +258,14 @@
      */
     public boolean unscheduleJob(Object object)
     {
-        return unscheduleJob(object.getXWikiObject());
+        try{
+            return unscheduleJob(retrieveBaseObject(object));
+        }
+        catch(Exception e){
+            // we don't need to push the exception message in the context here
+            // as it should already have been pushed by the throwing exception
+            return false;
+        }
     }
 
     public boolean unscheduleJob(BaseObject object)
@@ -224,7 +293,14 @@
      */
     public Date getNextFireTime(Object object)
     {
-        return getNextFireTime(object.getXWikiObject());
+        try{
+            return getNextFireTime(retrieveBaseObject(object));
+        }
+        catch(Exception e){
+            // we don't need to push the exception message in the context here
+            // as it should already have been pushed by the throwing exception
+            return null;
+        }
     }
 
     public Date getNextFireTime(BaseObject object)

Modified: 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
===================================================================
--- 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
     2007-10-25 16:28:36 UTC (rev 5501)
+++ 
xwiki-platform/xwiki-plugins/trunk/scheduler/src/main/java/com/xpn/xwiki/plugin/scheduler/SchedulerPluginException.java
     2007-10-25 18:43:49 UTC (rev 5502)
@@ -45,6 +45,8 @@
 
     protected static final int ERROR_SCHEDULERPLUGIN_RESTORE_EXISTING_JOBS = 
90009;
 
+    protected static final int ERROR_SCHEDULERPLUGIN_UNABLE_TO_RETRIEVE_JOB = 
90010;
+
     public SchedulerPluginException(int code, String message)
     {
         super(SchedulerPlugin.class, code, message);

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to