Alfresco – Run as system / admin user (without credentials)

From Java

1. By using AuthenticationUtil.RunAsWork

AuthenticationUtil.runAsSystem(
        new AuthenticationUtil.RunAsWork<Object>() {

            public Object doWork() throws Exception {

                // ...code to be run as Admin...
                return null;

            }
        }
);

2. By using AuthenticationUtil.setRunAsUserSystem

AuthenticationUtil.pushAuthentication();
try {
    AuthenticationUtil.setRunAsUserSystem();
    // ...code to be run as Admin...
} finally {
    AuthenticationUtil.popAuthentication();
}

3. Alternative source code for AuthenticationUtil.setRunAsUserSystem

try {
    AuthenticationUtil.setRunAsUserSystem();
    // ...code to be run as Admin...
} finally {
    AuthenticationUtil.clearCurrentSecurityContext();
}

From JavaScript (server side)

By using custom RunAs script (source code based on Fabio Strozzi)

1. Implement a JavaScript root object called RunAs

package es.keensoft.alfresco.script.RunAs;

public class RunAs extends BaseScopableProcessorExtension {
    public void work(final Function func) {
        final Context cx = Context.getCurrentContext();
        final Scriptable scope = getScope();

        RunAsWork<Object> raw = new RunAsWork<Object>() {
            public Object doWork() throws Exception {
                func.call(cx, scope, scope, new Object[] {});
                return null;
            }
        };

        AuthenticationUtil.runAs(raw, AuthenticationUtil.getAdminUserName());
    }
}

2. Declare Spring bean in order to expose it to Rhino JavaScript

<beans>
    <bean id="RunAs" parent="baseJavaScriptExtension" class="es.keensoft.alfresco.script.RunAs">
        <property name="extensionName">
            <value>runAs</value>
        </property>
    </bean>
</beans>

3. Use it on any JavaScript

function main() {
    var f = function() {
        // ...code to be run as Admin...
    };
    runAs.work(f);
}

main();

From WebScript (declarative)

<webscript>
  <shortname>Test</shortname>
  <description>Test</description>
  <url>/test</url>
  <format default="html">argument</format>
  <!-- Use runas="admin" attribute -->
  <authentication runas="admin">user</authentication>
  <transaction>required</transaction>
</webscript>

From JavaScript (client side)

Not supported.

From CMIS (client side)

Not supported, but using Alfresco tickets and some SSO integration (CMIS-887) can do the trick.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s