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

10 Reasons Why Your Business Should Invest in eCommerce

Hire Developer Apr 9th, 2025

10 Reasons Why Your Business Should Invest in eCommerce...

Read more
Best Practices for a Successful OneStream Implementation: Avoiding Common Pitfalls

Hire Developer Mar 31st, 2025

Best Practices for a Successful OneStream Implementatio...

Read more
ESG Reporting & Planning in OneStream: A Guide to Sustainability Metrics

Hire Developer Mar 24th, 2025

ESG Reporting & Planning in OneStream: A Guide to ...

Read more

INQUIRY

Let's get in touch

UNITED STATES

4411 Suwanee Dam road,
Bld. 300 Ste. 350
Suwanee GA, 30024

Sales: +1 (415) 230 0051

UNITED KINGDOM

Kemp House 160 City Road, London,United Kingdom EC1V 2NX

Sales: +44 7404 607567

INDIA

101, Kalasagar Shopping Hub, Sattadhar, Gujarat 380061

Sales: +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