Roundcube Community Forum

 

Automatic Identities

Started by maks, September 30, 2024, 07:10:56 AM

Previous topic - Next topic

maks

Is it possible to create user's idenitity based on ldap attribute? I would like the identity field to be automatically filled with attributes from ldap for example; E-mail from externalMail, name from cn or FullName field.

JohnDoh

Have a look at the new_user_identity plugin that ships with Roundcube.
Roundcube Plugins: Contextmenu, SpamAssassin Prefs, and more...

maks

I managed to solve this problem by creating my own plugin:
cat ldap_auto_fill.php
<?php

class ldap_auto_fill extends rcube_plugin
{
    public function 
init()
    {
        
$this->add_hook('login_after', array($this'fill_identity_from_ldap'));
    }

    public function 
fill_identity_from_ldap($args)
    {
        
// Pobierz nazwę użytkownika z sesji Roundcube
        
$rcmail rcmail::get_instance();
        
$user $rcmail->user->get_username();
        
$identity $rcmail->user->get_identity();

        
// Pobierz dane LDAP
        
$ldap_data $this->get_ldap_data($user);
        if (
$ldap_data) {
            
// Zaktualizuj tożsamość użytkownika w Roundcube
            
$identity['name'] = $ldap_data['cn'];
            
$identity['email'] = $ldap_data['externalMail'];
            
$rcmail->user->update_identity($identity['identity_id'], $identity);
        }
    }

    private function 
get_ldap_data($username)
    {
        
// Uzyskanie dostępu do globalnej konfiguracji Roundcube
        
$rcmail rcmail::get_instance();
        
$ldap_config $rcmail->config->get('ldap_public'); // Pobranie konfiguracji LDAP

        // Wybierz odpowiednią konfigurację LDAP (np. 'ispan_addressbook')
        
$ldap_settings $ldap_config['ispan_addressbook'];

        
// Użyj ustawień z głównego pliku config.inc.php
        
$ldap_host $ldap_settings['hosts'][0];
        
$ldap_base_dn $ldap_settings['base_dn'];
        
$ldap_bind_dn $ldap_settings['bind_dn'];
        
$ldap_bind_pass $ldap_settings['bind_pass'];
        
$ldap_search_filter "(uid=$username)";

        
// Połączenie z LDAP
        
$ldap_conn ldap_connect($ldap_host);
        
ldap_set_option($ldap_connLDAP_OPT_PROTOCOL_VERSION3);

        if (
$ldap_conn && ldap_bind($ldap_conn$ldap_bind_dn$ldap_bind_pass)) {
            
// Wyszukiwanie użytkownika w LDAP
            
$search_result ldap_search($ldap_conn$ldap_base_dn$ldap_search_filter, ['cn''externalMail']);

            if (
$search_result) {
                
$entries ldap_get_entries($ldap_conn$search_result);
                if (
$entries['count'] > 0) {
                    return [
                        
'cn' => $entries[0]['cn'][0],
                        
'externalMail' => $entries[0]['externalmail'][0]
                    ];
                }
            }
        }

        
ldap_close($ldap_conn);

        
// Zwróć null, jeśli nie udało się połączyć lub znaleźć użytkownika
        
return null;
    }
}