Java Code Examples for org.keycloak.models.GroupModel#getName()

The following examples show how to use org.keycloak.models.GroupModel#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KeycloakModelUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static GroupModel findSubGroup(String[] segments, int index, GroupModel parent) {
    for (GroupModel group : parent.getSubGroups()) {
        String groupName = group.getName();
        String[] pathSegments = formatPathSegments(segments, index, groupName);

        if (groupName.equals(pathSegments[index])) {
            if (pathSegments.length == index + 1) {
                return group;
            }
            else {
                if (index + 1 < pathSegments.length) {
                    GroupModel found = findSubGroup(pathSegments, index + 1, group);
                    if (found != null) return found;
                } else {
                    return null;
                }
            }

        }
    }
    return null;
}
 
Example 2
Source File: KeycloakModelUtils.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static GroupModel findGroupByPath(RealmModel realm, String path) {
    if (path == null) {
        return null;
    }
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    String[] split = path.split("/");
    if (split.length == 0) return null;
    GroupModel found = null;
    for (GroupModel group : realm.getTopLevelGroups()) {
        String groupName = group.getName();
        String[] pathSegments = formatPathSegments(split, 0, groupName);

        if (groupName.equals(pathSegments[0])) {
            if (pathSegments.length == 1) {
                found = group;
                break;
            }
            else {
                if (pathSegments.length > 1) {
                    found = findSubGroup(pathSegments, 1, group);
                    if (found != null) break;
                }
            }

        }
    }
    return found;
}
 
Example 3
Source File: CachedGroup.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public CachedGroup(Long revision, RealmModel realm, GroupModel group) {
    super(revision, group.getId());
    this.realm = realm.getId();
    this.name = group.getName();
    this.parentId = group.getParentId();
    this.attributes = new DefaultLazyLoader<>(source -> new MultivaluedHashMap<>(source.getAttributes()), MultivaluedHashMap::new);
    this.roleMappings = new DefaultLazyLoader<>(source -> source.getRoleMappings().stream().map(RoleModel::getId).collect(Collectors.toSet()), Collections::emptySet);
    this.subGroups = new DefaultLazyLoader<>(source -> source.getSubGroups().stream().map(GroupModel::getId).collect(Collectors.toSet()), Collections::emptySet);
}
 
Example 4
Source File: GroupMembershipMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void transformAttributeStatement(AttributeStatementType attributeStatement, ProtocolMapperModel mappingModel, KeycloakSession session, UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) {
    String single = mappingModel.getConfig().get(SINGLE_GROUP_ATTRIBUTE);
    boolean singleAttribute = Boolean.parseBoolean(single);

    boolean fullPath = useFullPath(mappingModel);
    AttributeType singleAttributeType = null;
    for (GroupModel group : userSession.getUser().getGroups()) {
        String groupName;
        if (fullPath) {
            groupName = ModelToRepresentation.buildGroupPath(group);
        } else {
            groupName = group.getName();
        }
        AttributeType attributeType = null;
        if (singleAttribute) {
            if (singleAttributeType == null) {
                singleAttributeType = AttributeStatementHelper.createAttributeType(mappingModel);
                attributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(singleAttributeType));
            }
            attributeType = singleAttributeType;
        } else {
            attributeType = AttributeStatementHelper.createAttributeType(mappingModel);
            attributeStatement.addAttribute(new AttributeStatementType.ASTChoiceType(attributeType));
        }
        attributeType.addAttributeValue(groupName);
    }
}
 
Example 5
Source File: GroupLDAPStorageMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void processKeycloakGroupSyncToLDAP(GroupModel kcGroup, Map<String, LDAPObject> ldapGroupsMap, Set<String> ldapGroupNames, SynchronizationResult syncResult) {
    String groupName = kcGroup.getName();

    // extract group attributes to be updated to LDAP
    Map<String, Set<String>> supportedLdapAttributes = new HashMap<>();
    for (String attrName : config.getGroupAttributes()) {
        List<String> kcAttrValues = kcGroup.getAttribute(attrName);
        Set<String> attrValues2 = (kcAttrValues == null || kcAttrValues.isEmpty()) ? null : new HashSet<>(kcAttrValues);
        supportedLdapAttributes.put(attrName, attrValues2);
    }

    LDAPObject ldapGroup = ldapGroupsMap.get(groupName);

    if (ldapGroup == null) {
        ldapGroup = createLDAPGroup(groupName, supportedLdapAttributes);
        syncResult.increaseAdded();
    } else {
        for (Map.Entry<String, Set<String>> attrEntry : supportedLdapAttributes.entrySet()) {
            ldapGroup.setAttribute(attrEntry.getKey(), attrEntry.getValue());
        }

        ldapProvider.getLdapIdentityStore().update(ldapGroup);
        syncResult.increaseUpdated();
    }

    ldapGroupsMap.put(groupName, ldapGroup);
    ldapGroupNames.add(groupName);

    // process KC subgroups
    for (GroupModel kcSubgroup : kcGroup.getSubGroups()) {
        processKeycloakGroupSyncToLDAP(kcSubgroup, ldapGroupsMap, ldapGroupNames, syncResult);
    }
}
 
Example 6
Source File: GroupLDAPStorageMapper.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public void addGroupMappingInLDAP(RealmModel realm, GroupModel kcGroup, LDAPObject ldapUser) {
    String groupName = kcGroup.getName();
    LDAPObject ldapGroup = loadLDAPGroupByName(groupName);

    if (ldapGroup == null) {
        // Needs to partially sync Keycloak groups to LDAP
        if (config.isPreserveGroupsInheritance()) {
            GroupModel groupsPathGroup = getKcGroupsPathGroup(realm);
            GroupModel highestGroupToSync = getHighestPredecessorNotExistentInLdap(groupsPathGroup, kcGroup);

            logger.debugf("Will sync group '%s' and it's subgroups from DB to LDAP", highestGroupToSync.getName());

            Map<String, LDAPObject> syncedLDAPGroups = new HashMap<>();
            processKeycloakGroupSyncToLDAP(highestGroupToSync, syncedLDAPGroups, new HashSet<>(), new SynchronizationResult());
            processKeycloakGroupMembershipsSyncToLDAP(highestGroupToSync, syncedLDAPGroups);

            ldapGroup = loadLDAPGroupByName(groupName);

            // Finally update LDAP membership in the parent group
            if (highestGroupToSync.getParent() != groupsPathGroup) {
                LDAPObject ldapParentGroup = loadLDAPGroupByName(highestGroupToSync.getParent().getName());
                LDAPUtils.addMember(ldapProvider, MembershipType.DN, config.getMembershipLdapAttribute(), getMembershipUserLdapAttribute(), ldapParentGroup, ldapGroup);
            }
        } else {
            // No care about group inheritance. Let's just sync current group
            logger.debugf("Will sync group '%s' from DB to LDAP", groupName);
            processKeycloakGroupSyncToLDAP(kcGroup, new HashMap<>(), new HashSet<>(), new SynchronizationResult());
            ldapGroup = loadLDAPGroupByName(groupName);
        }
    }

    String membershipUserLdapAttrName = getMembershipUserLdapAttribute();

    LDAPUtils.addMember(ldapProvider, config.getMembershipTypeLdapAttribute(), config.getMembershipLdapAttribute(), membershipUserLdapAttrName, ldapGroup, ldapUser);
}