Run Javascript Function as Admin Privileges

Sep 23rd, 2013

Run Javascript Function as Admin Privileges

Share

  • In Alfresco, create group, user, add user to group, set permission are allowed only to Admin group users.
  • Many times we get a requirement where we need to create group and users dynamically on some action.
  • Now javascript API are available to create group but works only for Admin group users.
  • I had a requirement to create groups dynamically when rule is executed. But rule was executed action taken by non-admin user.
  • So I created custom javascript API to run entire javascript function as Admin privileges.
  • For this you have to create one Java class which extends BaseScopableProcessorExtension. BaseScopableProcessorExtension class is extended whenever you want to create custom javascript API.
  • Register your javascript API in *-context.xml
  • Then use your API in your javascript.

public class RunAsAdminUtil extends BaseScopableProcessorExtension {
/**
* This method runs javascript function with Admin privileges
* @param func
*
*/
public void runAsAdmin(final Function func) {
final Context cx = Context.getCurrentContext(); final Scriptable scope = getScope();
RunAsWork raw = new RunAsWork() {
public Object doWork() throws Exception {
func.call(cx, scope, scope, new Object[] {}); return null;
}
};
AuthenticationUtil.runAs(raw,
AuthenticationUtil.getSystemUserName());
}
}
Below is the *-contex.xml entry,

runAsAdminUtil

Below is the code of javascript where you would be using custom created javascript api to run function with admin privileges

function main() { if (!people.isAdmin(person)) { //if current logged in user is not admin run function with admin privileges runAsAdminUtil.runAsAdmin(createGroupsDynamically); }else{ createFolderStructureFromTemplate(); } } var createGroupsDynamically = function createGroupsDynamically() { var name = document.properties.name; var TEST_READ = name + ‘_read’; var TEST_WRITE = name + ‘_write’; people.createGroup(TEST_READ); people.createGroup(TEST_WRITE); } main();

Comments are closed.

Let's Discuss Your Project

Get free consultation and let us know your project idea to turn
it into an amazing digital product.

Let’s talk

NEWS & BLOG

Related Blogs

Why CFOs Are Moving from Hyperion to OneStream: A Complete Hyperion to OneStream Migration Guide

Hire Developer Jun 23rd, 2025

Why CFOs Are Moving from Hyperion to OneStream: A Compl...

Read more
AI Use Cases Across Industries: Examples in the Real World

Hire Developer Jun 2nd, 2025

AI Use Cases Across Industries: Examples in the Real Wo...

Read more
AI Development in 2025: Trends That Will Define the Next Generation of Business

Hire Developer May 14th, 2025

AI Development in 2025: Trends That Will Define the Nex...

Read more

INQUIRY

Let's get in touch

UNITED STATES

31236 Meadowview Square,
Delmar,
DE 19940, USA

Sales: +1 (415) 230 0051

UNITED KINGDOM

13 Layton Road,
Hounslow,
London, TW3 1YJ

Sales: +44 7404 607567

INDIA

101, Kalasagar Shopping Hub, Sattadhar, Gujarat 380061

+91 999-894-5667

For Project Inquiries

Please enable JavaScript in your browser to complete this form.
emailsales@solutionanalysts.com emailinfo@solutionanalysts.com emailcareer@solutionanalysts.com skypebiz.solutionanalysts