private MembershipUser toMembershipUser(Membership membership) throws Exception {
   OrganizationService service = getApplicationComponent(OrganizationService.class);
   String userName = membership.getUserName();
   UserHandler handler = service.getUserHandler();
   User user = handler.findUserByName(userName);
   if (user == null) return null;
   return new MembershipUser(user, membership.getMembershipType(), membership.getId());
 }
 /**
  * initialize administrators, called from {@link #getAdministratorsList()}
  *
  * @throws Exception
  */
 public void initAdministrators() throws Exception {
   SpaceService spaceService = getSpaceService();
   Space space = spaceService.getSpaceByUrl(SpaceUtils.getSpaceUrl());
   administratorsList = new ArrayList<User>();
   OrganizationService orgSrc = getApplicationComponent(OrganizationService.class);
   UserHandler userHandler = orgSrc.getUserHandler();
   String[] managers = space.getManagers();
   if (managers != null) {
     for (String name : managers) {
       administratorsList.add(userHandler.findUserByName(name));
     }
   }
 }
  /*
   * (non-Javadoc)
   * @see
   * org.exoplatform.services.security.Authenticator#validateUser(org.exoplatform
   * .services.security.Credential[])
   */
  public String validateUser(Credential[] credentials) throws LoginException, Exception {
    String username = null;
    String password = null;
    Map<String, String> passwordContext = null;
    for (Credential cred : credentials) {
      if (cred instanceof UsernameCredential) {
        username = ((UsernameCredential) cred).getUsername();
      }
      if (cred instanceof PasswordCredential) {
        password = ((PasswordCredential) cred).getPassword();
        passwordContext = ((PasswordCredential) cred).getPasswordContext();
      }
    }
    if (username == null || password == null)
      throw new LoginException("Username or Password is not defined");

    if (this.encrypter != null) password = new String(encrypter.encrypt(password.getBytes()));

    begin(orgService);
    boolean success;
    try {
      UserHandler userHandler = orgService.getUserHandler();
      if (passwordContext != null && userHandler instanceof ExtendedUserHandler) {
        PasswordEncrypter pe = new DigestPasswordEncrypter(username, passwordContext);
        success = ((ExtendedUserHandler) userHandler).authenticate(username, password, pe);
      } else {
        success = userHandler.authenticate(username, password);
      }
      // No exception occurred
      lastExceptionOnValidateUser.remove();
    } catch (DisabledUserException e) {
      lastExceptionOnValidateUser.set(e);
      throw new LoginException(
          "The user account " + username.replace("\n", " ").replace("\r", " ") + " is disabled");
    } catch (Exception e) {
      lastExceptionOnValidateUser.set(e);
      throw e;
    } finally {
      end(orgService);
    }

    if (!success)
      throw new LoginException(
          "Login failed for " + username.replace("\n", " ").replace("\r", " "));

    return username;
  }
  public void testGetIdentitiesByName() throws Exception {
    User user = userHandler.createUserInstance("alex");
    user.setFirstName("");
    user.setLastName("");
    user.setEmail("");
    userHandler.createUser(user, true);
    User found = userHandler.findUserByName("alex");
    assertNotNull(found);
    String providerId = OrganizationIdentityProvider.NAME;

    Identity identity = new Identity(providerId, "alex");
    identityManager.saveIdentity(identity);
    Profile profile = new Profile(identity);
    profile.setProperty(Profile.USERNAME, "alex");
    profile.setProperty(Profile.FIRST_NAME, "Mary");
    profile.setProperty(Profile.LAST_NAME, "Williams");
    profile.setProperty(Profile.FULL_NAME, "Mary " + "Williams");
    profile.setProperty(Profile.POSITION, "developer");
    profile.setProperty(Profile.GENDER, "female");
    identityManager.saveProfile(profile);
    identity.setProfile(profile);
    tearDownIdentityList.add(identity);

    ProfileFilter pf = new ProfileFilter();

    // Search by name full name
    pf.setName("Mary");
    ListAccess<Identity> idsListAccess =
        identityManager.getIdentitiesByProfileFilter(providerId, pf, false);
    assertEquals(1, idsListAccess.getSize());
    pf.setName("Williams");
    idsListAccess = identityManager.getIdentitiesByProfileFilter(providerId, pf, false);
    assertEquals(1, idsListAccess.getSize());
    pf.setName("Mary Williams");
    idsListAccess = identityManager.getIdentitiesByProfileFilter(providerId, pf, false);
    assertEquals(1, idsListAccess.getSize());

    // update profile name
    profile.setProperty(Profile.FIRST_NAME, "Mary-James");
    profile.setProperty(Profile.FULL_NAME, "Mary-James Williams");
    identityManager.updateProfile(profile);
    Identity alex =
        identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, "alex", true);
    assertEquals("Mary-James Williams", alex.getProfile().getFullName());

    pf.setName("Mary-James Williams");
    idsListAccess = identityManager.getIdentitiesByProfileFilter(providerId, pf, false);
    assertEquals(1, idsListAccess.getSize());

    //
    List<ExoSocialActivity> activities =
        activityManager.getActivitiesWithListAccess(identity).loadAsList(0, 20);
    for (ExoSocialActivity act : activities) {
      List<ExoSocialActivity> comments =
          activityManager.getCommentsWithListAccess(act).loadAsList(0, 20);
      for (ExoSocialActivity cmt : comments) {
        activityManager.deleteComment(act, cmt);
      }
      activityManager.deleteActivity(act);
    }
    userHandler.removeUser(user.getUserName(), false);
  }