пятница, 4 сентября 2015 г.

Sudo like tool for Alfresco webscripts with scope and parmeters

I want to add some notes to the following blog post:  http://fabiostrozzi.eu/2009/11/23/sudo-like-tool-for-alfresco-webscripts/. It is really useful solution, but it doesn't allow to call functions with parameters and functions from another JS object...

My implementation of Sudo class:

import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

public class Sudo extends BaseScopableProcessorExtension {


    public Object su(final Function func) {

        final Scriptable scope = getScope();

        return su(func,scope);
    }

    public Object su(final Function func, final Scriptable scope, final Object... objects) {

        final Context cx = Context.getCurrentContext();

        AuthenticationUtil.RunAsWork&lgObject&gt raw = new AuthenticationUtil.RunAsWork&lgObject&gt() {
            public Object doWork() throws Exception {
                return func.call(cx, ScriptableObject.getTopLevelScope(scope), scope, objects);
            }
        };

        return AuthenticationUtil.runAs(raw, AuthenticationUtil.getSystemUserName());
    }
}

Initializing of Spring bean:

    
        
        
    

Using sudo service in webscripts:

var SudoExample = {

   writeToLog : function(a,b) {

      logger.getSystem().out("First parameter = " + a);
      logger.getSystem().out("Second parameter = " + a);

   },

   execute: function() {

      sudo.su(this.writeToLog,this, "param#1", "param#2");
   }
}

SudoExample.execute();

Комментариев нет :

Отправить комментарий