How to run execute script as admin?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2014 03:22 AM
I've created a javascript file :
The destination folder is where the user have no restriction.
I understand that I can use
in a workflow script, but how do I code this directly in the javascript file?
Thank you in advance!
var dest = companyhome.childByNamePath("Sites/test-script/documentLibrary/dari");document.move(dest);
The destination folder is where the user have no restriction.
I understand that I can use
<runas>admin</runas>
in a workflow script, but how do I code this directly in the javascript file?
Thank you in advance!
Labels:
- Labels:
-
Archive
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2014 04:04 AM
To do this in javascript, you will have to write a custom javascript extension. Here is a link to a forum post which explains the steps to create a custom javascript extension.
http://forums.alfresco.com/comment/146025#comment-146025
You can take this steps as reference and create your own custom javascript extension in which write a method and set the authentication to run as admin/system user.
Hope this helps.
http://forums.alfresco.com/comment/146025#comment-146025
You can take this steps as reference and create your own custom javascript extension in which write a method and set the authentication to run as admin/system user.
Hope this helps.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2014 09:21 AM
I thought it can be done simply through execute script.
Thank you for the link!
Thank you for the link!
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2014 08:14 PM
-
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2016 03:31 PM
To answer your question for others that might come on this page like I did…
- The XML <runas>admin</runas> is for webscript in javascript. It's not usable when you are doing plain Javascript ( repo/data dicionnary/scripts ).
If you want to execute your Javascript method with admin rights, you will need to create an extension to the Javascript API. This API has been make in Java, so you will have to do some Java ( but not too much don't worry ). Note this extension is also called Javascript Root Object.
To deploy, you will have no other solution ( at the moment ) than doing an AMP file, which is the common way to deploy our stuff in Alfresco.
This has been tested on Alfresco 5.1 and should work on older version as well.
I will provide some links, for further information, but you should be able to achieve your goal without using them.
That said, let's go :
1) Create an AMP
To create an amp, you will have to use maven command.
This link will help you for further info : http://docs.alfresco.com/5.1/tasks/alfresco-sdk-tutorials-share-amp-archetype.html
Basically you use :
<strong>mvn archetype:generate -Dfilter=org.alfresco: </strong>
Choose option 1
Use latest version, give your groupId… let's say we use here, com.example and artifactId runAsUtil
2) Update the source
Now we gonna inject some code in your AMP. So we will give a java file, and a context.
Here is the official and easier example if you want to go through it first [ up to you ] : http://docs.alfresco.com/5.1/references/dev-extension-points-javascript-root-objects.html
- Java :
File Name : RunAsAdminUtil.java
Location :
Create a folder Jscript ( you can change the name, but if you do, update all reference I will provide later ) as
<strong><where_you_created_the_folder_with_maven>\runAsUtil\runAsUtil-repo-amp\src\main\java\com\example\jscript</strong>
Note that com\example might be different for you if you used a different groupId
Here is the full content of the Java file :
<java>
package com.example.jscript;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class RunAsAdminUtil extends BaseScopableProcessorExtension {
public void runAsAdmin(final Function func, final Object args[]) {
final Context cx = Context.getCurrentContext();
final Scriptable scope = getScope();
AuthenticationUtil.RunAsWork raw = new AuthenticationUtil.RunAsWork() {
public Object doWork() throws Exception {
func.call(cx, scope, scope, (args != null ? args : new Object[] {}));
return null;
}
};
AuthenticationUtil.runAs(raw, AuthenticationUtil.getSystemUserName());
}
}
</java>
- Context file :
FileName : service-context.xml
Location : C:\Users\bertrand.barraud\Desktop\Alfresco TEST\runAsUtil\runAsUtil-repo-amp\src\main\amp\config\alfresco\module\runAsUtil-repo-amp\context
Note that runAsUtil is our artifactId
Content to add :
Full file content :
Note :
- value= is your artifactId
- id = [artifactId]-repo-amp.customRootObject
Ect..
Make sure your bean is well configurated. I advice to use the provided example before changing the naming
3) Compilation and Deployment
- Compilation :
Go to the root folder of the project ( folder generated by maven ) and use the command : <strong>mvn clean install -e</strong>
At this point if you have error, check all the steps and all your naming
- Deployment :
Get the amp file in <strong><rootFolder>/runAsUtil-repo-amp/target</strong> should be named something like <strong>runAsUtil-repo-amp-1.0-SNAPSHOT.amp</strong>
Drop the file into your alfresco instance in the amp folder, should be located on your server somewhere like :
<strong>/opt/alfresco-one/amps</strong> ( on unix )
Apply the amp :
<strong>sudo sh /opt/alfresco-one/bin/apply_amps.sh</strong>
Note : You should see your amp in the list visible in the console
Restart alfresco :
<strong>sudo sh /opt/alfresco-one/alfresco.sh restart</strong>
—————-
You deployed the AMP ! Nice
Now you just need to know how to use this in your javascript !
4) Javascript
You just need to use the artifactId ( in your bean value= ) as the package/object name then the method you want to use. No import, no include nothing, it's directly usable from the API.
Example :
<javascript>
runAsUtil.runAsAdmin(moveDocument, [moveLocation]);
</javascript>
Note :
In this example, I give a parameter to my function. Do not forget the runAsAdmin method expect an array as second arg ! Else you will have an error, saying that the software can't find runAsAdmin.
Hope this help someone to not waste 2 days like I did
Good luck !!!
PS: Do not forget to share your solution when you find it
- The XML <runas>admin</runas> is for webscript in javascript. It's not usable when you are doing plain Javascript ( repo/data dicionnary/scripts ).
If you want to execute your Javascript method with admin rights, you will need to create an extension to the Javascript API. This API has been make in Java, so you will have to do some Java ( but not too much don't worry ). Note this extension is also called Javascript Root Object.
To deploy, you will have no other solution ( at the moment ) than doing an AMP file, which is the common way to deploy our stuff in Alfresco.
This has been tested on Alfresco 5.1 and should work on older version as well.
I will provide some links, for further information, but you should be able to achieve your goal without using them.
That said, let's go :
1) Create an AMP
To create an amp, you will have to use maven command.
This link will help you for further info : http://docs.alfresco.com/5.1/tasks/alfresco-sdk-tutorials-share-amp-archetype.html
Basically you use :
<strong>mvn archetype:generate -Dfilter=org.alfresco: </strong>
Choose option 1
Use latest version, give your groupId… let's say we use here, com.example and artifactId runAsUtil
2) Update the source
Now we gonna inject some code in your AMP. So we will give a java file, and a context.
Here is the official and easier example if you want to go through it first [ up to you ] : http://docs.alfresco.com/5.1/references/dev-extension-points-javascript-root-objects.html
- Java :
File Name : RunAsAdminUtil.java
Location :
Create a folder Jscript ( you can change the name, but if you do, update all reference I will provide later ) as
<strong><where_you_created_the_folder_with_maven>\runAsUtil\runAsUtil-repo-amp\src\main\java\com\example\jscript</strong>
Note that com\example might be different for you if you used a different groupId
Here is the full content of the Java file :
<java>
package com.example.jscript;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class RunAsAdminUtil extends BaseScopableProcessorExtension {
public void runAsAdmin(final Function func, final Object args[]) {
final Context cx = Context.getCurrentContext();
final Scriptable scope = getScope();
AuthenticationUtil.RunAsWork raw = new AuthenticationUtil.RunAsWork() {
public Object doWork() throws Exception {
func.call(cx, scope, scope, (args != null ? args : new Object[] {}));
return null;
}
};
AuthenticationUtil.runAs(raw, AuthenticationUtil.getSystemUserName());
}
}
</java>
- Context file :
FileName : service-context.xml
Location : C:\Users\bertrand.barraud\Desktop\Alfresco TEST\runAsUtil\runAsUtil-repo-amp\src\main\amp\config\alfresco\module\runAsUtil-repo-amp\context
Note that runAsUtil is our artifactId
Content to add :
<xml> <bean id="runAsUtil-repo-amp.customRootObject" class="com.example.jscript.RunAsAdminUtil" parent="baseJavaScriptExtension"> <property name="extensionName" value="runAsUtil"/> </bean></xml>
Full file content :
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><!– Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.–><beans> <!– A simple class that is initialized by Spring –> <bean id="custom-repo-amp.exampleBean" class="com.fiducial.demoamp.Demo" init-method="init" /> <!– A simple module component that will be executed once. Note. this module component will only be executed once, and then there will be an entry for it in the Repo. So doing for example $ mvn clean install -Prun twice will only execute this component the first time. You need to remove /alf_data_dev for it to be executed again. –> <bean id="custom-repo-amp.exampleComponent" class="com.fiducial.demoamp.DemoComponent" parent="module.baseComponent" > <property name="moduleId" value="custom-repo-amp" /> <!– See module.properties –> <property name="name" value="exampleComponent" /> <property name="description" value="A demonstration component" /> <property name="sinceVersion" value="1.0" /> <property name="appliesFromVersion" value="1.0" /> <property name="nodeService" ref="NodeService" /> <property name="nodeLocatorService" ref="nodeLocatorService" /> </bean> <bean id="runAsUtil-repo-amp.customRootObject" class="com.example.jscript.RunAsAdminUtil" parent="baseJavaScriptExtension"> <property name="extensionName" value="runAsUtil"/> </bean> </beans>
Note :
- value= is your artifactId
- id = [artifactId]-repo-amp.customRootObject
Ect..
Make sure your bean is well configurated. I advice to use the provided example before changing the naming

3) Compilation and Deployment
- Compilation :
Go to the root folder of the project ( folder generated by maven ) and use the command : <strong>mvn clean install -e</strong>
At this point if you have error, check all the steps and all your naming
- Deployment :
Get the amp file in <strong><rootFolder>/runAsUtil-repo-amp/target</strong> should be named something like <strong>runAsUtil-repo-amp-1.0-SNAPSHOT.amp</strong>
Drop the file into your alfresco instance in the amp folder, should be located on your server somewhere like :
<strong>/opt/alfresco-one/amps</strong> ( on unix )
Apply the amp :
<strong>sudo sh /opt/alfresco-one/bin/apply_amps.sh</strong>
Note : You should see your amp in the list visible in the console
Restart alfresco :
<strong>sudo sh /opt/alfresco-one/alfresco.sh restart</strong>
—————-
You deployed the AMP ! Nice

Now you just need to know how to use this in your javascript !
4) Javascript
You just need to use the artifactId ( in your bean value= ) as the package/object name then the method you want to use. No import, no include nothing, it's directly usable from the API.
Example :
<javascript>
runAsUtil.runAsAdmin(moveDocument, [moveLocation]);
</javascript>
Note :
In this example, I give a parameter to my function. Do not forget the runAsAdmin method expect an array as second arg ! Else you will have an error, saying that the software can't find runAsAdmin.
Hope this help someone to not waste 2 days like I did

Good luck !!!
PS: Do not forget to share your solution when you find it

