Home > Alfresco > Adding Permissions using Alfresco Javascript API

Adding Permissions using Alfresco Javascript API

ADDING PERMISSIONS…

On my last post, I have discussed how permissions can be applied in a folder using the Alfresco web client. This is very helpful for first time users of Alfresco. Permission plays an important part in Alfresco Administration as this can be used to control access of users to folders or documents. However, for advanced users some might require adding them automatically or even might have a requirement where you add permissions without going through to many clicks in the Alfresco web client. Applying permissions is also possible using JAVA APIs exposed by Alfresco. But, there is also another way for non-Java guys by using Alfresco JavaScript API.

In this post, I will show you how you can add a permission using JavaScript API. I will be using a simple web script to add permissions to a given folder name.

CREATING THE PERMISSION  WEB SCRIPT

1. Log in as  admin in Alfresco and go to Company Home/Data Dictionary/Web Scripts Extensions

Figure 1: Web Scripts Extensions Page

2. Create a folder/space that  will contain our web script files e.g. setPermission folder

3. In the created folder, create 3 files that will complete our web script.

a. setPermission.get.desc.xml – the descriptor file that will register our web script, example:

<webscript>
    <shortname>Set Permission</shortname>
    <description>Set Permission using javascript API</description>
    <url>/set/folder/permission/?folderName={folderName}</url>
    <format default=”html”/>
    <authentication>admin</authentication>
</webscript>

b.  setPermission.get.html.ftl –  the file that will display the response from Alfresco, example:

<html>
<body>
<p>${myStatus}</p>
</body>
</html>

c. setPermission.get.js – java-script that will contain the logic of our code. Here, we will use Alfresco java-script API for  setting the permission of  a folder, example:

/*Set permission to a folder using Alfresco javascript API*/

//search for the folder node using lucene search
var folderNode =  search.luceneSearch(“TYPE:\”{http://www.alfresco.org/model/content/1.0}folder\” AND @cm\\:name:”+args.folderName);

//make sure we only get one node

if(folderNode.length == 1){
//set permission “Contributor” to user with username “anythingalfresco” and group “ANYTHINGALFRESCOGROUP”
folderNode[0].setPermission(“Contributor”, “anythingalfresco”);
folderNode[0].setPermission(“Contributor”, “GROUP_ANYTHINGALFRESCOGROUP”);
model.myStatus = “Permission was set successfully!”;
}else  if (folderNode.length == 0){
//no node was found
model.myStatus = “Folder not found”;
}else{
//either greater than two  was found
model.myStatus = “Duplicate folder found”;
}

Below is the API for  applying permissions:

void setPermission(string permission, string authority) 
Parameters:
string permission – a string representation of  the permission or role you want to assign to a user or a group. Note that this permission should exist in Alfresco otherwise you will get an error like the one below:

Figure 2: Permission Error

string authority – string representation of the username or a group name you want to have permissions. For groups, append “GROUP_” before the group name. Alfresco will not produce error if a username or group does  not exist (at least in my test!).

4. Go to web scripts home (http://localhost:8080/alfresco/service/index) and click Refresh Web Scripts button
5. On a new tab, invoke your web script:
          http://localhost:8080/alfresco/set/folder/permission/?folderName=Folder A
If all goes well, you should be able to  see a response “Permission was set successfully!”

Figure 3: Permission Applied

Now we are done! Note that this is just a simple code. You are free to experiment with the code at your own risk 🙂
I  hope this helps!
Advertisements
  1. August 18, 2011 at 5:44 am

    I liked your article is an interesting technology
    thanks to google I found you

  2. Ilija job
    September 2, 2011 at 4:10 pm

    hi,

    Thanks for tutorial.

    http://localhost:8080/alfresco/set/folder/permission/?folderName=Folder A

    could you please explain url?

  3. Kader
    October 12, 2011 at 4:57 pm

    Hi,

    Thanks for this great tutorial.

    Could you please tell me how can I use your script for setting the permission to more than one folder with the same name :

    folderName=Customer1/FolderA
    folderName=Customer2/FolderA

    folderName=CustomerX/FolderA

    Thanks.

    • October 12, 2011 at 10:20 pm

      Hi Kader,

      Thanks for reading this post. For your question, are you trying to add SAME permission for all folders? If so, you can just iterate all lucene search results by:

      //search.luceneSearch(“your query”) –>search the whole Alfresco repository and returns an array list containing Noderefs
      var folderNode = search.luceneSearch(“TYPE:\”{http://www.alfresco.org/model/content/1.0}folder\” AND @cm\\:name:”+args.folderName);

      for(var i = 0; i<folderNode.length; i++){
      //apply permission here
      folderNode[i].setPermission();
      }

      I hope I have answered your question.

      Thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: