Exemplo n.º 1
0
  public Collection findMembershipsByGroupId(String groupId) throws Exception {
    if (log.isTraceEnabled()) {
      Tools.logMethodIn(
          log, LogLevel.TRACE, "findMembershipsByGroup", new Object[] {"groupId", groupId});
    }

    orgService.flush();

    String plGroupName = getPLIDMGroupName(getGroupNameFromId(groupId));

    String gid =
        getIdentitySession()
            .getPersistenceManager()
            .createGroupKey(plGroupName, getGroupTypeFromId(groupId));

    Collection<Role> roles = new HashSet();

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

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

    Group g = orgService.getGroupHandler().findGroupById(groupId);

    for (Role role : roles) {
      if (isCreateMembership(role.getRoleType().getName(), g.getId())) {
        MembershipImpl m = new MembershipImpl();
        m.setGroupId(g.getId());
        m.setUserName(role.getUser().getId());
        m.setMembershipType(role.getRoleType().getName());
        memberships.add(m);
      }
    }

    if (isAssociationMapped()) {

      Collection<org.picketlink.idm.api.User> users = new HashSet();

      try {
        users = getIdentitySession().getRelationshipManager().findAssociatedUsers(gid, false, null);
      } catch (Exception e) {
        // TODO:
        handleException("Identity operation error: ", e);
      }

      for (org.picketlink.idm.api.User user : users) {
        MembershipImpl m = new MembershipImpl();
        m.setGroupId(groupId);
        m.setUserName(user.getId());
        m.setMembershipType(getAssociationMapping());
        memberships.add(m);
      }
    }

    // TODO: Exo UI has harcoded casts to List
    List<MembershipImpl> results = new LinkedList<MembershipImpl>(memberships);

    if (orgService.getConfiguration().isSortMemberships()) {
      Collections.sort(results);
    }

    if (log.isTraceEnabled()) {
      Tools.logMethodOut(log, LogLevel.TRACE, "findMembershipsByGroupId", results);
    }

    return results;
  }
Exemplo n.º 2
0
  public Membership findMembershipByUserGroupAndType(String userName, String groupId, String type)
      throws Exception {

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

    orgService.flush();

    String plGroupName = getPLIDMGroupName(getGroupNameFromId(groupId));

    String gid =
        getIdentitySession()
            .getPersistenceManager()
            .createGroupKey(plGroupName, getGroupTypeFromId(groupId));

    boolean hasMembership = false;

    boolean associated = false;

    try {
      associated = getIdentitySession().getRelationshipManager().isAssociatedByKeys(gid, userName);
    } catch (Exception e) {
      // TODO:
      handleException("Identity operation error: ", e);
    }

    if (isAssociationMapped() && getAssociationMapping().equals(type) && associated) {
      hasMembership = true;
    }

    Role role = null;

    try {
      role = getIdentitySession().getRoleManager().getRole(type, userName, gid);
    } catch (Exception e) {
      // TODO:
      handleException("Identity operation error: ", e);
    }

    if (role != null
        && (!isAssociationMapped()
            || !getAssociationMapping().equals(role.getRoleType())
            || !ignoreMappedMembershipType(groupId))) {
      hasMembership = true;
    }

    Membership result = null;

    if (hasMembership) {

      MembershipImpl m = new MembershipImpl();
      m.setGroupId(groupId);
      m.setUserName(userName);
      m.setMembershipType(type);

      result = m;
    }

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

    return result;
  }
Exemplo n.º 3
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;
  }
Exemplo n.º 4
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);
  }