{"id":20138,"date":"2013-09-23T12:03:23","date_gmt":"2013-09-23T17:33:23","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/run-javascript-function-as-admin-privileges\/"},"modified":"2023-08-16T23:05:43","modified_gmt":"2023-08-17T04:35:43","slug":"run-javascript-function-admin-privileges","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/run-javascript-function-admin-privileges\/","title":{"rendered":"Run Javascript Function as Admin Privileges"},"content":{"rendered":"<ul>\n<li>In Alfresco, create group, user, add user to group, set permission are allowed only to Admin group users.<\/li>\n<li>Many times we get a requirement where we need to create group and users dynamically on some action.<\/li>\n<li>Now javascript API are available to create group but works only for Admin group users.<\/li>\n<li>I had a requirement to create groups dynamically when rule is executed. But rule was executed action taken by non-admin user.<\/li>\n<li>So I created custom javascript API to run entire javascript function as Admin privileges.<\/li>\n<li>For this you have to create one Java class which extends BaseScopableProcessorExtension. BaseScopableProcessorExtension class is extended whenever you want to create custom javascript API.<\/li>\n<li>Register your javascript API in *-context.xml<\/li>\n<li>Then use your API in your javascript.<\/li>\n<\/ul>\n<p><code>public class RunAsAdminUtil extends BaseScopableProcessorExtension {<br \/>\n\/**<br \/>\n* This method runs javascript function with Admin privileges<br \/>\n* @param func<br \/>\n*<br \/>\n*\/<br \/>\npublic void runAsAdmin(final Function func) {<br \/>\nfinal  Context cx = Context.getCurrentContext(); final Scriptable scope = getScope();<br \/>\nRunAsWork raw = new RunAsWork() {<br \/>\npublic Object doWork() throws Exception {<br \/>\nfunc.call(cx, scope, scope, new Object[] {}); return null;<br \/>\n}<br \/>\n};<br \/>\nAuthenticationUtil.runAs(raw,<br \/>\nAuthenticationUtil.getSystemUserName());<br \/>\n}<br \/>\n}<br \/>\nBelow is the *-contex.xml entry,<\/code><\/p>\n<p>runAsAdminUtil<\/p>\n<p>Below is the code of javascript where you would be using custom created javascript api to run function with admin privileges<\/p>\n<p>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 + &#8216;_read&#8217;; var TEST_WRITE = name + &#8216;_write&#8217;; people.createGroup(TEST_READ); people.createGroup(TEST_WRITE); } main();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20139,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-20138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hire-developer"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20138"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20138\/revisions"}],"predecessor-version":[{"id":38601,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20138\/revisions\/38601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20139"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}