Blog

Latest technology news, trends and developers guide for business application. Visit this space for more tech updates.

Let’s work on your exciting new project together!

Run Javascript Function as Admin Privileges

alfresco, javascript

  • 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();

Why Should You Hire Full-Stack Developers for Developing MVP

Every excellent and creative idea needs implementation. But then, startups and even small companies find...

8 Valuable Tips to Master Best Code Practices in Node.JS

As one of the most popular open source platforms for developing server applications, Node.JS has...

Google Uncovers Angular 5 JavaScript and Here’s what It Promises

Google has unleashed the Angular 5 after two major upgrades in less than last 2...

We are always happy to hear from you - Share your business requirements. Our business experts will reach you shortly

 

Select Your Requirement

Secure : We'll ensure, all the information provided by you will not be shared with anyone and your details are completely safe.

We respect your privacy. *Privacy Policy

Select Your Requirement

Secure : We'll ensure, all the information provided by you will not be shared with anyone and your details are completely safe.

We respect your privacy. *Privacy Policy