Staking a claim [Updated x2]

[Update March 2, 2012] The script below has been radically updated and simplified with thanks to Antonio Soler. This makes it much quicker and more reliable.

One of the biggest problems we had with the out-of-the-box implementation of Alfresco is that it can only assign quotas to individual users, not sites.

With thanks to several members of the Alfresco team at the last Developer’s Conference, especially Jared Ottley, I developed a workaround to essentially fake site quota functionality. In short, you use a rule to assign ownership of anything placed in a given site to the user that created that site. At that point, the storage space for an item will be debited against the site owner’s quota, not the quota of the user who uploaded it.

This has a couple of uses:

  • An individual user might use it to make sure that they own any content that is in one of their sites. For example, a professor may wish to make sure he owns anything created by his grad students in one of his Alfresco sites, so that the content remains even after the grad students graduate.
  • An Alfresco administrator may wish to use it to be able to assign a quota to a specific site on the system so that items placed in that site are debited against the site’s quota, not a user’s personal quota.

Here’s how you set up the latter example:

  1. Place the changeowner.js file (below) in Company Home/Data Dictionary/Scripts on your Alfresco instance
  2. Change the {tomcat}/webapps/share/WEB-INF/classes/alfresco/share-form-config.xml file to suppress the display of the “Owner” field, so that other users can’t arbitrarily change it and bypass the intent of this script. This is done by commenting out the <show id="cm:owner" /> line in the cm:ownable aspect section of that file. (Ideally, we’d want to have it visible, but not editable in the document details screen, but I haven’t figured out how to do that yet.)

    Update (7/8/2011): At least in EE 3.4.2, it is possible to have it visible but not editable by changing the line from <show id="cm:owner" /> to <show id="cm:owner" for-mode="view" /> instead of commenting it out.

  3. Create a new user to own the site for which you want to have a site quota, and assign the quota you want for the site to that user. I’ll refer to this user as “site-owner” below.
  4. Create the site for which you want to have a site quota (I’ll call it “demo-site”) using the site-owner user.
  5. Use the repository view to set a rule on Company Home/Sites/demo-site that will run the changeowner.js script for any item entering or created in that site, that DOES apply to subfolders, and will NOT run in the background. That’ll reassign the ownership–and quota usage–to site-owner.

Changeowner.js:

/*
changeowner.js
version of 10/27/2011
by Ian Crew, icrew@berkeley.edu

A script to change the ownership of a new node in a site to be the same as the creator of that site.

This script has only been tested on Alfresco EE 3.3.4 and 3.4.x. 

Legalese
This script is copyright 2011 by the Regents of the University of California.
No warranties are expressed or implied.  Use at your own risk.  This script may be
freely distributed and modified so long as this copyright notice is included in
all derivative versions.
*/

//Note: "document" is the item that was just uploaded, whether it's a folder or a document

var currentOwner=space.getOwner();

if  ((currentOwner=="admin") && (space.properties["name"]=="documentLibrary")) {
	currentOwner=space.properties["creator"];
}

if (document.owner != currentOwner) {
	try	{
		document.setOwner(currentOwner);
	}
	catch (err) {
		//get the email address of the document library owner--this is where we send most error messages
		var docLibOwnerEmail = getEmailAddr(currentOwner);

		var errorText = "An error occurred while "+document.getOwner()+" was trying to change the ownership of the document ";
		errorText += document.name + " (workspace://SpacesStore/"+document.properties["node-uuid"]+") in Research Hub folder ";
		errorText += space.properties["name"]+" from " + document.getOwner() + " to " + currentOwner +".\n";
		errorText += "Error description: " + err.description +  "\nError number: " + err.number + "\n\n";
		
		//Let the admin know about this error
		//sendMail (docLibOwnerEmail, "[Change Owner Script] Can't change ownership of item", errorText, document); 
		sendMail ("foo@bar.com", "[Change Owner Script] Can't change ownership of item", "Tell "+docLibOwnerEmail+" that we can't change ownership of item: \n"+ errorText, document); 	
	}
}

//send off the emailed errors
function sendMail (recipient, subject, contents, node) {
	//send email to the specified recipients

	// create mail action
	var mail = actions.create("mail");
	mail.parameters.to = recipient;
	mail.parameters.subject = subject;
	mail.parameters.from = "hub@berkeley.edu";
	mail.parameters.template = null;
	mail.parameters.text = contents;
	// execute action against a document		
	mail.execute(node);
}

//get an email address from a username
function getEmailAddr (username) {
	
	//Get the person object for this documentOwner
	var newPerson = people.getPerson(username);

	return newPerson.properties["email"]; 
}
Advertisements
This entry was posted in Scripts. Bookmark the permalink.

One Response to Staking a claim [Updated x2]

  1. Pingback: Improving a trail | Wandering Alfresco

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