7.2.1. Managing users

This section shows you how to create, update, and remove users from the portal via two ways:

You can follow the snippet below to list users and some information of the users:

 try {

      ListAccess<User> users = userHandler.findAllUsers();
      for (User user : users.load(0, users.getSize())) {
        writer.append("<dl class='dl-horizontal'>");
        //Print username and email
        writer.append("<dt>Username:</dt>");
        writer.append("<dd>" + user.getUserName() + "</dd>");
        writer.append("<dt>Email:</dt>");
        writer.append("<dd>" + user.getEmail() + "</dd>");
        //Print user profile
        UserProfile profile = profileHandler.findUserProfileByName(user.getUserName());
        for (Iterator<Map.Entry<String, String>> i =profile.getUserInfoMap().entrySet().iterator(); i.hasNext(); ) {
          Map.Entry<String, String> entry = i.next();
          writer.append("<dt>" + entry.getKey() + "</dt>");
          writer.append("<dd>" + entry.getValue() + "</dd>");
        }
        //Print group and membership of user
        Collection<Group> groups = groupHandler.findGroupsOfUser(user.getUserName());
        writer.append("<dt>Roles:</dt>");
        for (Group group : groups) {
          Collection<Membership> memberships =  membershipHandler.findMembershipsByUserAndGroup(user.getUserName(), group.getId());
          StringBuilder sb = new StringBuilder();
          for (Iterator<Membership> i = memberships.iterator(); i.hasNext(); ) {
            sb.append(i.next().getMembershipType());
            if (i.hasNext()) sb.append(',');
          }
          writer.append("<dd>" + sb.toString() + " in group <i>" + group.getId() + "</i></dd>");
        }
        writer.append("</dl>");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

The users list is displayed like this in your product:

Creating a user

Create a portlet action, then add the snippet below to it.

User newUser = userHandler.createUserInstance("newuser");

newUser.setPassword("exo");
newUser.setOrganizationId("/platform/users");
newUser.setDisplayName("New User");
newUser.setEmail("user@exoplatform.com");
userHandler.createUser(newUser, false);
Group group = groupHandler.findGroupById("/platform/users");
membershipHandler.linkMembership(newUser, group, membershipTypeHandler.findMembershipType("member"), false);
        

Executing the portlet action allows creating a user called newuser whose role is member:/platform/users.

Removing a user

Create a portlet action which executes the snippet below to remove an initialized user, for example, newuser.

userHandler.removeUser("newuser", false);

Removing a membership

Create a portlet action which executes the snippet below to remove a membership of the custom user:

Membership membership = membershipHandler.findMembershipByUserGroupAndType("custom", "/organization/custom", "custom-membership");

    membershipHandler.removeMembership(membership.getId(), false);
Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus