Using the Javascript Console: Creating users with avatar images

Automatically creating users has become easier with the new CSV user import features in Alfresco 4.0 or the Create Bulk Users extension from share-extras. If you want to be really flexible and set all the user properties or even set custom properties, using the Javascript API to create users can still be very useful. That’s why I’ll show you how to create users using the Javascript Console.

Let’s start with a short video preview on how the user and avatar creation works.

Creating users

Here is a function that creates a single user using the createPerson function of the global people object. The additional properties location and jobtitle are also filled here for the user. The cm:person object has several more properties to offer. To see a list of all properties available have a look at the cm:person type in the contentModel.xml.

function createUser(username, firstname, lastname, email, location, 
  jobtitle) {

  var password = username; // use for testing only!

  var p = people.createPerson(username, firstname, lastname, email,
    password, true);

  p.properties["cm:location"] = location;
  p.properties["cm:jobtitle"] = jobtitle;
  p.save();
}

It is useful to have this custom createUser function to easily create multiple users. Here is a list of some users we want to add to Alfresco.

createUser("homer", "Homer", "Simpson", "h.simpson@springfield.fox", 
    "Springfield", "Nuclear Safety Inspector");

createUser("marge", "Marge", "Simpson", "m.simpson@springfield.fox",
    "Springfield", "Housewife");

createUser("bart",  "Bart",  "Simpson", "b.simpson@springfield.fox",
    "Springfield", "Kid");

createUser("lisa",  "Lisa",  "Simpson", "l.simpson@springfield.fox",
    "Springfield", "Kid");

createUser("maggie","Maggie","Simpson",
    "maggie.simpson@springfield.fox", "Springfield", "Baby");

Assigning avatar images

Now we have created a few users. The only thing that is missing are the avatar images for each of the users. Wouldn’t it be great to just throw a bunch of images in a repository folder and have them automatically assigned to each user without having to log in as each of these users and assign an avatar image manually?

for each (imageNode in space.children) {
  var name = "" + imageNode.name;  // convert to javascript string
  name = name.replace(/\..*/, ""); // remove file extension

  var user = people.getPerson(name);

  if (user) {
    var avatarAssoc = user.assocs["cm:avatar"];

    if (avatarAssoc) {
      var currentAvatar = avatarAssoc[0];

      if ((""+imageNode.nodeRef) != (""+currentAvatar.nodeRef)) {
        logger.log("changing avatar for " + name + " to " +
            imageNode.displayPath + "/" + imageNode.name);
        user.removeAssociation(currentAvatar, "cm:avatar");
        user.createAssociation(imageNode, "cm:avatar");
      }
      else {
        logger.log("no change for user " + name);
      }
    }
    else {
        logger.log("setting new avatar for " + name + " to " +
            imageNode.displayPath + "/" + imageNode.name);
        user.createAssociation(imageNode, "cm:avatar");
    }
  }
}

This script does exactly that. It assumes that the space variable points to a repository folder that contains an avatar image for each user. Here is how you use it.

  • Create a folder somewhere in the repository, e.g. /Data Dictionary/User Images would be good name to use.
  • Upload your avatar images to that folder. Square aspect ratio would be best but image resolution doesn’t matter.
  • You avatar images should be named by username, e.g. “homer.jpg” or “marge.png” if you have the users “homer” and “marge”.
  • Select the “User Images” folder as space in the Javascript Console and run the script you see above.

A few things to note

  • The avatar image is stored with a cm:avatar association on each cm:person object that means that the image can be anywhere in the repository.
  • The avatar image that is assigned by the cm:avatar association will be converted into a 64×64 pixel thumbnail using the rendition service internally. This means you can use an image of any format and size that the rendition service can convert.
  • There is this odd comparison in the script:
    (""+imageNode.nodeRef) != (""+currentAvatar.nodeRef)

    Comparing nodeRefs can be really tricky in the Alfresco Javascript API, because they are Java objects. Here nodeRefs are converted into Javascript strings which can be compared with == or !=. Another way would be to use the Java equals() method like this:

    imageNode.nodeRef.equals(currentAvatar.nodeRef)

    but what you should not do, is directly comparing nodeRefs with == it won’t work:

    imageNode.nodeRef == currentAvatar.nodeRef
  • Of course you can use the avatar assignment script regardless of how you created the users. Even if the users have been imported with the LDAP synchronization you can still have the repository folder with the user images an have them assigned automatically. You could even create a rule that runs this script as soon as a new image is uploaded to the “User Images” folder.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>