Search in sources :

Example 31 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class LDAPIdentityStore method add.

@Override
public void add(LDAPObject ldapObject) {
    // id will be assigned by the ldap server
    if (ldapObject.getUuid() != null) {
        throw new ModelException("Can't add object with already assigned uuid");
    }
    String entryDN = ldapObject.getDn().toString();
    BasicAttributes ldapAttributes = extractAttributesForSaving(ldapObject, true);
    this.operationManager.createSubContext(entryDN, ldapAttributes);
    ldapObject.setUuid(getEntryIdentifier(ldapObject));
    if (logger.isDebugEnabled()) {
        logger.debugf("Type with identifier [%s] and dn [%s] successfully added to LDAP store.", ldapObject.getUuid(), entryDN);
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) ModelException(org.keycloak.models.ModelException)

Example 32 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class LDAPQuery method getResultList.

public List<LDAPObject> getResultList() {
    // Apply mappers now
    LDAPMappersComparator ldapMappersComparator = new LDAPMappersComparator(ldapFedProvider.getLdapIdentityStore().getConfig());
    Collections.sort(mappers, ldapMappersComparator.sortAsc());
    for (ComponentModel mapperModel : mappers) {
        LDAPStorageMapper fedMapper = ldapFedProvider.getMapperManager().getMapper(mapperModel);
        fedMapper.beforeLDAPQuery(this);
    }
    List<LDAPObject> result = new ArrayList<LDAPObject>();
    try {
        for (LDAPObject ldapObject : ldapFedProvider.getLdapIdentityStore().fetchQueryResults(this)) {
            result.add(ldapObject);
        }
    } catch (Exception e) {
        throw new ModelException("LDAP Query failed", e);
    }
    return result;
}
Also used : LDAPStorageMapper(org.keycloak.storage.ldap.mappers.LDAPStorageMapper) LDAPMappersComparator(org.keycloak.storage.ldap.mappers.LDAPMappersComparator) ModelException(org.keycloak.models.ModelException) ComponentModel(org.keycloak.component.ComponentModel) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) NamingException(javax.naming.NamingException) ModelException(org.keycloak.models.ModelException) ModelDuplicateException(org.keycloak.models.ModelDuplicateException)

Example 33 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class GroupLDAPStorageMapper method syncDataFromFederationProviderToKeycloak.

// Sync from Ldap to KC
@Override
public SynchronizationResult syncDataFromFederationProviderToKeycloak(RealmModel realm) {
    SynchronizationResult syncResult = new SynchronizationResult() {

        @Override
        public String getStatus() {
            return String.format("%d imported groups, %d updated groups, %d removed groups", getAdded(), getUpdated(), getRemoved());
        }
    };
    logger.debugf("Syncing groups from LDAP into Keycloak DB. Mapper is [%s], LDAP provider is [%s]", mapperModel.getName(), ldapProvider.getModel().getName());
    // Get all LDAP groups
    List<LDAPObject> ldapGroups = getAllLDAPGroups(config.isPreserveGroupsInheritance());
    // Convert to internal format
    Map<String, LDAPObject> ldapGroupsMap = new HashMap<>();
    List<GroupTreeResolver.Group> ldapGroupsRep = new LinkedList<>();
    convertGroupsToInternalRep(ldapGroups, ldapGroupsMap, ldapGroupsRep);
    // Now we have list of LDAP groups. Let's form the tree (if needed)
    if (config.isPreserveGroupsInheritance()) {
        try {
            List<GroupTreeResolver.GroupTreeEntry> groupTrees = new GroupTreeResolver().resolveGroupTree(ldapGroupsRep, config.isIgnoreMissingGroups());
            updateKeycloakGroupTree(realm, groupTrees, ldapGroupsMap, syncResult);
        } catch (GroupTreeResolver.GroupTreeResolveException gre) {
            throw new ModelException("Couldn't resolve groups from LDAP. Fix LDAP or skip preserve inheritance. Details: " + gre.getMessage(), gre);
        }
    } else {
        syncFlatGroupStructure(realm, syncResult, ldapGroupsMap);
    }
    syncFromLDAPPerformedInThisTransaction = true;
    return syncResult;
}
Also used : ModelException(org.keycloak.models.ModelException) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult)

Example 34 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class LDAPOperationManager method createSubContext.

public void createSubContext(final String name, final Attributes attributes) {
    try {
        if (logger.isTraceEnabled()) {
            logger.tracef("Creating entry [%s] with attributes: [", name);
            NamingEnumeration<? extends Attribute> all = attributes.getAll();
            while (all.hasMore()) {
                Attribute attribute = all.next();
                String attrName = attribute.getID().toUpperCase();
                Object attrVal = attribute.get();
                if (attrName.contains("PASSWORD") || attrName.contains("UNICODEPWD")) {
                    attrVal = "********************";
                }
                logger.tracef("  %s = %s", attribute.getID(), attrVal);
            }
            logger.tracef("]");
        }
        execute(new LdapOperation<Void>() {

            @Override
            public Void execute(LdapContext context) throws NamingException {
                DirContext subcontext = context.createSubcontext(new LdapName(name), attributes);
                subcontext.close();
                return null;
            }

            @Override
            public String toString() {
                return new StringBuilder("LdapOperation: create\n").append(" dn: ").append(name).append("\n").append(" attributesSize: ").append(attributes.size()).toString();
            }
        });
    } catch (NamingException e) {
        throw new ModelException("Error creating subcontext [" + name + "]", e);
    }
}
Also used : ModelException(org.keycloak.models.ModelException) Attribute(javax.naming.directory.Attribute) DirContext(javax.naming.directory.DirContext) LdapName(javax.naming.ldap.LdapName) NamingException(javax.naming.NamingException) InitialLdapContext(javax.naming.ldap.InitialLdapContext) LdapContext(javax.naming.ldap.LdapContext)

Example 35 with ModelException

use of org.keycloak.models.ModelException in project keycloak by keycloak.

the class LDAPOperationManager method modifyAttributes.

/**
 * <p>
 * Modifies the given {@link Attribute} instances using the given DN. This method performs a REPLACE_ATTRIBUTE
 * operation.
 * </p>
 *
 * @param dn
 * @param attributes
 */
public void modifyAttributes(String dn, NamingEnumeration<Attribute> attributes) {
    try {
        List<ModificationItem> modItems = new ArrayList<ModificationItem>();
        while (attributes.hasMore()) {
            ModificationItem modItem = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attributes.next());
            modItems.add(modItem);
        }
        modifyAttributes(dn, modItems.toArray(new ModificationItem[] {}), null);
    } catch (NamingException ne) {
        throw new ModelException("Could not modify attributes on entry from DN [" + dn + "]", ne);
    }
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) ModelException(org.keycloak.models.ModelException) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException)

Aggregations

ModelException (org.keycloak.models.ModelException)74 RealmModel (org.keycloak.models.RealmModel)20 NamingException (javax.naming.NamingException)13 UserModel (org.keycloak.models.UserModel)13 ClientModel (org.keycloak.models.ClientModel)11 ComponentModel (org.keycloak.component.ComponentModel)10 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)10 IOException (java.io.IOException)9 Consumes (javax.ws.rs.Consumes)9 NotFoundException (javax.ws.rs.NotFoundException)8 BasicAttribute (javax.naming.directory.BasicAttribute)7 KeycloakSession (org.keycloak.models.KeycloakSession)7 RoleModel (org.keycloak.models.RoleModel)7 ErrorResponseException (org.keycloak.services.ErrorResponseException)7 ReadOnlyException (org.keycloak.storage.ReadOnlyException)7 POST (javax.ws.rs.POST)6 Path (javax.ws.rs.Path)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 AttributeInUseException (javax.naming.directory.AttributeInUseException)5