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.

13 thoughts on “Using the Javascript Console: Creating users with avatar images

  1. You can adopt their styles to suit yours; this is one of the best ways to
    determine the right fashion for you. The Unitron website includes
    detailed description of the features and benefits from
    this product. The cable breaking system is also new feature added to the line of
    three wheel strollers.

  2. The brand of Moda in pelle provides extremely stylish,
    durable and comfortable shoes. They are going to almost any extent to look good and attractive.
    Wearing an elegant UGG boot with tight jeans can give you a classy and beautiful look.

  3. Also, they have ruled the market due to their anatomical constructions of shoes that
    make the shoes anti-stress with special shock absorption system.
    The Unitron website includes detailed description of the features and
    benefits from this product. A new kind of wire mixed with natural fabrics, like cotton,
    provided the perfect adjustment to the body.

  4. After his death, the business was successfully carried on by
    his three sons. Style- Most of us looks at the design and
    style while buying any item. The Fashion Industry is totally focused on how people look and
    larger people do not figure on mainstream fashion’s radar.

  5. Just wish to say your article is as surprising.
    The clearness in your post is simply nice and i can assume you’re an expert on this subject.
    Well with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please continue the enjoyable work.

  6. Many of these new creations are about shape and texture as
    much as color and style. They are going to almost any extent to look good and attractive.

    The Fashion Industry is totally focused on how people look and larger people do not figure on mainstream fashion’s radar.

  7. Can I simply just say what a comfort to find someone that genuinely knows what they are discussing
    on the internet. You definitely realize how to bring an issue to
    light and make it important. A lot more people ought
    to check this out and understand this side of your story.

    I can’t believe you’re not more popular given that you surely have the gift.

  8. Taper Roller Bearings: Taper Roller bearings are drafted in such a way that it can carry large amount
    of thrust and radial loads. If the bearing explodes, the wheel will flop up and down on the spindle and cause greater damage than if you just
    replaced the bearing at first alert. With the bottlebrush or a sponge, plus the bucket of soapy water clean under both the saddle, bottom bracket, brakes and forks if you need to use the scrubbing brush to remove
    any stubborn dirt and then with a soapy sponge clean off all the
    degreaser and dirt that has been loosened.

  9. oh and lying and cheating of course, doesn’t everyone know that’s
    how it works. They can also be filled with drama and make strong
    political statements. Sky is the limit to the kinds of movies that are available
    online.

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>