public Map getConnections()

in extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/connection/ConnectionService.java [163:293]


    public Map<String, Connection> getConnections(LDAPAuthenticatedUser user)
            throws GuacamoleException {

        ConnectedLDAPConfiguration ldapConfig = user.getLDAPConfiguration();

        // Do not return any connections if base DN is not specified
        Dn configurationBaseDN = ldapConfig.getConfigurationBaseDN();
        if (configurationBaseDN == null)
            return Collections.<String, Connection>emptyMap();

        try {

            // Get the search filter for finding connections accessible by the
            // current user
            ExprNode connectionSearchFilter = getConnectionSearchFilter(user);

            // Find all Guacamole connections for the given user by
            // looking for direct membership in the guacConfigGroup
            // and possibly any groups the user is a member of that are
            // referred to in the seeAlso attribute of the guacConfigGroup.
            List<Entry> results = queryService.search(ldapConfig, ldapConfig.getLDAPConnection(),
                    configurationBaseDN, connectionSearchFilter, 0, GUAC_CONFIG_LDAP_ATTRIBUTES);

            // Return a map of all readable connections
            return queryService.asMap(results, (entry) -> {

                // Get common name (CN)
                Attribute cn = entry.get(LDAP_ATTRIBUTE_NAME_ID);
                
                if (cn == null) {
                    logger.warn("{} is missing a {}.",
                            CONNECTION_LDAP_OBJECT_CLASS, LDAP_ATTRIBUTE_NAME_ID);
                    return null;
                }
                
                String cnName;
                
                try {
                    cnName = cn.getString();
                }
                catch (LdapInvalidAttributeValueException e) {
                    logger.error("Invalid value for {} attribute: {}",
                            LDAP_ATTRIBUTE_NAME_ID, e.getMessage());
                    logger.debug("LDAP exception while getting CN attribute.", e);
                    return null;
                }

                // Get associated protocol
                Attribute protocol = entry.get(LDAP_ATTRIBUTE_NAME_PROTOCOL);
                if (protocol == null) {
                    logger.warn("{} \"{}\" is missing the "
                              + "required \"{}\" attribute.",
                            CONNECTION_LDAP_OBJECT_CLASS,
                            cnName, LDAP_ATTRIBUTE_NAME_PROTOCOL);
                    return null;
                }

                // Set protocol
                GuacamoleConfiguration config = new GuacamoleConfiguration();
                try {
                    config.setProtocol(protocol.getString());
                }
                catch (LdapInvalidAttributeValueException e) {
                    logger.error("Invalid value of the protocol entry: {}", e.getMessage());
                    logger.debug("LDAP exception when getting protocol value.", e);
                    return null;
                }
                
                // Get proxy configuration, if any
                GuacamoleProxyConfiguration proxyConfig;
                try {
                    proxyConfig = getProxyConfiguration(entry);
                }
                catch (GuacamoleException e) {
                    logger.error("Failed to retrieve proxy configuration.", e.getMessage());
                    logger.debug("Guacamole Exception when retrieving proxy configuration.", e);
                    return null;
                }

                // Get parameters, if any
                Attribute parameterAttribute = entry.get(LDAP_ATTRIBUTE_NAME_PARAMETER);
                if (parameterAttribute != null) {

                    // For each parameter
                    while (parameterAttribute.size() > 0) {
                        String parameter;
                        try {
                            parameter = parameterAttribute.getString();
                        }
                        catch (LdapInvalidAttributeValueException e) {
                            logger.warn("Parameter value not valid for {}: {}", cnName, e.getMessage());
                            logger.debug("LDAP exception when getting parameter value.", e);
                            return null;
                        }
                        parameterAttribute.remove(parameter);

                        // Parse parameter
                        int equals = parameter.indexOf('=');
                        if (equals != -1) {

                            // Parse name
                            String name = parameter.substring(0, equals);
                            String value = parameter.substring(equals+1);

                            config.setParameter(name, value);

                        }

                    }

                }

                // Store connection using cn for both identifier and name
                Connection connection = new SimpleConnection(cnName, cnName, proxyConfig, config, true);
                connection.setParentIdentifier(LDAPAuthenticationProvider.ROOT_CONNECTION_GROUP);

                // Inject LDAP-specific tokens only if LDAP handled user
                // authentication
                if (user instanceof LDAPAuthenticatedUser)
                    connection = new TokenInjectingConnection(connection, user.getTokens());

                return connection;

            });

        }
        catch (LdapException e) {
            throw new GuacamoleServerException("Error while querying for connections.", e);
        }

    }