Removing Permission of a User or Group in Alfresco Using Javascript

August 12, 2011 1 comment

REMOVING PERMISSIONS IN ALFRESCO…

In my last post, I have discussed how you can grant permissions to a user or group in a certain space or folder. This can be done by either using the Alfresco Web Client (Alfresco UI) or by using the permission service JavaScript API provided by Alfresco. By creating a JavaScript web script, one can add or delete permissions of a user or group in a specific folder/space. This post is  related to my previous post I created,  but in this section we will  discuss about removing the permission applied using JavaScript API.

THE JAVASCRIPT API…

Alfresco provided a javascript API we can use to remove set permissions. Below are the two methods we can use:

1. void removePermission(string permission) – Remove a permission for ALL users from the node.

2. void removePermission(string permission, string authority) – Remove a permission for the specified authority (e.g. username or group) from the node.

As an example (taken from my previous post), we can also create  another web scripts which removes permission on a node:

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

<webscript>
    <shortname>Remove Permission</shortname>
    <description>Remove Permission of a User or  Group from a Folder or Space</description>
    <url>/remove/folder/permission/?folderName={folderName}</url>
    <format default=”html”/>
    <authentication>admin</authentication>
</webscript>

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

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

c. removePermission.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:

/*Remove 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){
//remove permission “Contributor” from all users or group
folderNode[0].removePermission(“Contributor”);
//remove permission “Contributor” from group ANYTHINGALFRESCOGROUP

folderNode[0].removePermission(“Contributor”, “GROUP_ANYTHINGALFRESCOGROUP”);
model.myStatus = “Permission was removed 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”;
}

You can try the above code on your Alfresco installation and see if it works. Just follow the steps on how  to create a web script and upload it in Web Scripts Extension space. I hope this helps! =P

Adding Permissions using Alfresco Javascript API

August 4, 2011 5 comments

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!

3 Easy Steps: Applying Permissions Using Alfresco Web Client

July 28, 2011 1 comment

HOW PERMISSIONS ARE APPLIED?

In Alfresco, you apply permissions to certain space/folder or documents to restrict  a user to certain actions that he/she can perform on a folder or document. For example, you don’t want everyone to to have a capability to create documents or folders in Folder A. What you can do is to apply a READ ONLY permission to a user to  Folder A. In this way, a user can only VIEW the contents of Folder A.

For beginners, below  are the steps  to apply permission using Alfresco  web client:

1. Log in to Alfresco and proceed to the space/folder you want permissions to be applied. Click More Actions –> Manage Space Users.

Figure 1: Manage Space Users Link

2. Click  Invite. You’ll proceed to Invite Users Wizard page. In this page, you can invite a user or a group by selecting from the drop down list of Specify Users/Groups. Enter a group name or username and then click the search button. Select a user or group from the search result and then select a Role you want a user to have as seen below:

Figure 2: Invite User

Note: To further understand what Roles are, you can visit this link.

3. Click Add to List button. Click Next. You will be directed to a page which will ask you if you want to send an email to the invited user, for  now select No and click Finish button and you’re done! You should be able to see that the user has now a Role in the specified folder.

Figure 3: User with Role

Now we are done! Please note that applied permissions will only affect the space to which it is applied. You can also apply permission using java-script API’s provided by Alfresco. On my next post, I will concentrate on using java-script to apply permissions. =P

5 Easy Steps To Get All Root Groups in Alfresco Using Web Scripts

July 25, 2011 1 comment

Get All Root Groups in Alfresco

I was too curious and wonder how I can get or visibly see all the groups in Alfresco aside from using the Admin Console functionality.  I found out that it is possible using web scripts.

The Problem

I want to display in a web browser the list of all groups in Alfresco using a simple java-script web script.

The Solution

NOTE: I will assume you know how to create a web script, or at least have an idea. =P

1. Log in to your Alfresco as admin.

2. Locate ‘Web Scripts Extensions‘ folder (Company Home > Data Dictionary > Web Scripts Extensions)

Figure 1: Web Scripts Extensions Page

This folder is where we usually put our custom web scripts. By default, custom web scripts should be pit in this folder.

3. Create a folder (any name will do, e.g. getAllGroups) inside the Web Scripts Extensions folder.

4. Under the folder created in step 3, we create the 3 files below:

a. getAllGroups.get.desc.xml – the descriptor file that is responsible for registering our web script in Alfresco. This file will containt the following code

<webscript>
    <shortname>Gets All the Groups  in Alfresco</shortname>
    <description>This  is a sample web script</description>
    <url>/get/all/groups</url>
    <format default=”html”/>
    <authentication>admin</authentication>
    <transaction>required</transaction>
</webscript>

The explanation for each tags can be found here.

b. getAllGroups.get.html.ftl – free-marker template, used to represent the response data from Alfresco. Below is an example with formatting.

<html>
   <body>
       <table>
       <thead>
           <tr>
               <th>Short Name</th>
               <th>Full Name</th>
               <th>Display Name</th>
           </tr>
       </thead>
       <#list alfrescoGroups as g>
           <tr>
               <th>${g.getShortName()}</th>
               <th>${g.getFullName()}</th>
               <th>${g.getDisplayName()}</th>
           </tr>
       </#list>
       <table>
   </body>
</html>

c. getAllGroups.get.js – this is where the logic of the code is located. Alfresco exposed some javascript  API’s which developers can use to access Alfresco repository. For groups and authority related API, we will use the Groups (Authority Service). Below is a sample code using groups API:

/*@author – anythingalfresco: Gets all the root groups in Alfresco*/

//javascript API that gets all the root groups in Alfresco
var myGroups = groups.getAllRootGroups();

model.alfrescoGroups = myGroups;

5. After creating these 3 files, go to the web scripts index page at http://localhost:8080/alfresco/service/index and click Refresh Web Scripts button. Invoke the web script uri (http://localhost:8080/alfresco/service/get/all/groups) and the result should be like the one below:

Figure 2: Sample Result

Now we are done! It’s quite easy? You can try the code in your Alfresco installation and see if it works. BTW, I’ve used Alfresco Enterprise 3.2r in this example. But, it should work in higher versions too. If you want to know more about about Alfresco web scripts, you can visit the Alfresco wiki. =P

It’s been a while!

Wow! Time passed by so fast, and before I know it, its already 2011! Hmmm, its more than a year since I’ve posted something in this site/blog. Oh well, I’ve been busy with projects and work related Alfresco stuff at the office and I have to say I have learned a lot from that project.

Hhmmm, what else can I say??? Oh yeah, the things that I learned during the project duration e.g. creating custom Alfresco permissions, java-backed web scripts, custom Alfresco behaviour, Alfresco javascripts, building Alfresco project with Maven and deploying it to Tomcat, hiding some buttons in Alfresco Share, Alfresco Records Management and the latest is changing the  header and footer of  WCM Quick Start or Alfresco Quick Start.

I will be making some posts regarding the stuff that I mentioned above so stay tuned, if there are any subscribers🙂

I hope to add some images too, for your visualization.

Good day!

Categories: News and Updates

Alfresco Multi-tenancy: Exporting and Importing Tenants

February 27, 2010 2 comments

This post is the continuation of my previous post Alfresco Multi-tenancy Feature. If you haven’t read it, you can read it here. This post will provide easy steps on exporting and importing tenants in Alfresco Enterprise 3.0.

Exporting a tenant in Alfresco is one of the ways to back-up a tenant. The export will produce one or more .acp (Alfresco Content Package) files. It is like a zip file that contains xml files that describes the exported folders or contents. it also contains meta data of documents and folders. On the other hand, importing a tenant is the reverse of export wherein the information held in the .acp file is copied to the repository.

Exporting a tenant is normally like a complete repository export, which can be performed in previous Alfresco versions. Below are the steps to export a tenant:

1. Start Alfresco. Log-in as super admin.(Only the super admin can perform the tenant export)

2. Go to the tenant admin console.

3. In the text field, enter the command:

export <tenant domain> <destination directory>

Ex. export tenanta C:\exported_tenants

4. Go to the location where you specified the tenant to b0 exported and check if you six .acp files below:

  • <tenant domain>_users.acp
  • <tenant domain>_spaces.acp
  • <tenant domain>_spaces_archive.acp
  • <tenant domain>_model.acp
  • <tenant domain>_versions2.acp

Now you have successfully exported your tenant. The next step is to import the tenant. Reminder, importing should be done on the same version of Alfresco where you performed the export, otherwise you will encounter some errors. Below are the steps:

1. In a multi-tenant enabled Alfresco, log in as super admin.

2. Open the tenant admin console.

3. In the text field, enter command:

import <tenant domain> <source directory> <root contentstore directory>

Ex: import tenanta C:\exported_tenants C:\Alfresco\alf_data\tenantstore\a

NOTE: During this process, I have encountered errors regarding the version2Store does not exist. This is because I have versioned files in the version history(Allow versioning functionality in Alfresco is turned on). I guess this is a bug or an issue that is needed to be resolve.

If all goes well and you haven’t encountered errors, you can check the imported tenants, log in as tenant admin. Congatulations, you have now successfully imported the tenant! =)

For more information about Alfresco Multi-tenancy, visit the wiki page.

Categories: Alfresco Features

Alfresco Multi-Tenancy Feature

February 27, 2010 1 comment

This post will provide information about the Multi-tenancy functionality of Alfresco Enterprise 3.0. I have decided to make this article because currently, I have been researching for this feature for a project requirement. I decided to divide this in several parts like enabling, creating, exporting and importing a tenant. Now, let’s get started!

The Multi-Tenancy Feature enables Alfresco to run in a single-instance (meaning using only one Alfresco WAR, DB and same port) multi-tenant environment. So how is it enabled, below are the steps:

1. If it is your first time to run Alfresco(clean DB and alf_data), I suggest you run it first. (Because for some reason, I can not access the tenant admin console if I enable it here).

2. Stop Alfresco.

3. Locate the three files below located at tomcat/shared/classes/alfresco/extension/mt directory :

  • mt-context.xml.sample
  • mt-admin-context.xml.sample
  • mt-contentstore-context.xml.sample

4. Rename the files:

  • mt-context.xml.sample -> mt-context.xml
  • mt-admin-context.xml.sample ->mt-admin-context.xml
  • mt-contentstore-context.xml.sample -> mt-contentstore-context.xml

5. Restart Alfresco.

6. Open tenant admin console (http://localhost:8080/alfresco/faces/jsp/admin/tenantadmin-console.jsp). If you are able to access this page, multi-tenancy is enabled.

Now that multi-tenancy is enabled, you are now ready to create your first tenant. Below are the steps:

1. Log in as super admin

2. Open tenant admin console

3.  Enter command to create tenant:

create <tenant domain> <tenant admin password> <root contentstore dir>

Ex: create tenanta admina C:\Alfresco\alf_data\tenantstore\a

The above command will create a tenant tenanta with an administrator password admina

4. When creation is successful, you can login as tenant by accessing the login page of Alfresco and typing:

Username : admin@tenanta

Password:   admin

You can now create spaces, content and users inside your tenant. Enjoy!=)

Follow

Get every new post delivered to your Inbox.