Пример #1
0
  public Collection findMembershipsByUser(String userName) throws Exception {
    if (log.isTraceEnabled()) {
      Tools.logMethodIn(
          log, LogLevel.TRACE, "findMembershipsByUser", new Object[] {"userName", userName});
    }

    orgService.flush();

    Collection<Role> roles = new HashSet();

    try {
      roles = getIdentitySession().getRoleManager().findRoles(userName, null);
    } catch (Exception e) {
      // TODO:
      handleException("Identity operation error: ", e);
    }

    HashSet<MembershipImpl> memberships = new HashSet<MembershipImpl>();

    for (Role role : roles) {
      Group g = ((GroupDAOImpl) orgService.getGroupHandler()).convertGroup(role.getGroup());
      if (isCreateMembership(role.getRoleType().getName(), g.getId())) {
        MembershipImpl m = new MembershipImpl();
        m.setGroupId(g.getId());
        m.setUserName(role.getUser().getId());

        // LDAP store may return raw membership type as role type
        if (role.getRoleType().getName().equals("JBOSS_IDENTITY_MEMBERSHIP")) {
          m.setMembershipType(orgService.getConfiguration().getAssociationMembershipType());
        } else {
          m.setMembershipType(role.getRoleType().getName());
        }

        memberships.add(m);
      }
    }

    if (isAssociationMapped()) {

      Collection<org.picketlink.idm.api.Group> groups = new HashSet();

      try {
        groups = getIdentitySession().getRelationshipManager().findAssociatedGroups(userName, null);
      } catch (Exception e) {
        // TODO:
        handleException("Identity operation error: ", e);
      }

      for (org.picketlink.idm.api.Group group : groups) {
        MembershipImpl m = new MembershipImpl();
        Group g = ((GroupDAOImpl) orgService.getGroupHandler()).convertGroup(group);
        m.setGroupId(g.getId());
        m.setUserName(userName);
        m.setMembershipType(getAssociationMapping());
        memberships.add(m);
      }
    }

    Collection result = new LinkedList(memberships);

    if (log.isTraceEnabled()) {
      Tools.logMethodOut(log, LogLevel.TRACE, "findMembershipsByUser", result);
    }

    return result;
  }
Пример #2
0
  public Collection removeMembershipByUser(String userName, boolean broadcast) throws Exception {

    if (log.isTraceEnabled()) {
      Tools.logMethodIn(
          log,
          LogLevel.TRACE,
          "removeMembershipByUser",
          new Object[] {"userName", userName, "broadcast", broadcast});
    }

    orgService.flush();

    Collection<Role> roles = new HashSet();

    try {
      roles = getIdentitySession().getRoleManager().findRoles(userName, null);
    } catch (Exception e) {
      // TODO:
      handleException("Identity operation error: ", e);
    }

    HashSet<MembershipImpl> memberships = new HashSet<MembershipImpl>();

    for (Role role : roles) {
      MembershipImpl m = new MembershipImpl();
      Group g = ((GroupDAOImpl) orgService.getGroupHandler()).convertGroup(role.getGroup());
      m.setGroupId(g.getId());
      m.setUserName(role.getUser().getId());
      m.setMembershipType(role.getRoleType().getName());
      memberships.add(m);

      if (broadcast) {
        preDelete(m);
      }

      getIdentitySession().getRoleManager().removeRole(role);

      if (broadcast) {
        postDelete(m);
      }
    }

    if (isAssociationMapped()) {

      Collection<org.picketlink.idm.api.Group> groups = new HashSet();

      try {
        groups = getIdentitySession().getRelationshipManager().findAssociatedGroups(userName, null);
      } catch (Exception e) {
        // TODO:
        handleException("Identity operation error: ", e);
      }

      Set<String> keys = new HashSet<String>();
      keys.add(userName);

      for (org.picketlink.idm.api.Group group : groups) {
        try {
          getIdentitySession()
              .getRelationshipManager()
              .disassociateUsersByKeys(group.getKey(), keys);
        } catch (Exception e) {
          // TODO:
          handleException("Identity operation error: ", e);
        }
      }
    }

    // TODO: Exo UI has hardcoded casts to List
    return new LinkedList(memberships);
  }