5 * Populates a new user's default identity from LDAP on their first visit.
7 * This plugin requires that a working public_ldap directory be configured.
10 * @author Kris Steinhoff
12 * Example configuration:
14 * // The id of the address book to use to automatically set a new
15 * // user's full name in their new identity. (This should be an
16 * // string, which refers to the $rcmail_config['ldap_public'] array.)
17 * $rcmail_config['new_user_identity_addressbook'] = 'People';
19 * // When automatically setting a new users's full name in their
20 * // new identity, match the user's login name against this field.
21 * $rcmail_config['new_user_identity_match'] = 'uid';
23 * // Use this field (from fieldmap configuration) to fill alias col of
24 * // the new user record.
25 * $rcmail_config['new_user_identity_alias'] = 'alias';
27 class new_user_identity extends rcube_plugin
29 public $task = 'login';
35 $this->add_hook('user_create', array($this, 'lookup_user_name'));
38 function lookup_user_name($args)
40 $rcmail = rcmail::get_instance();
42 if ($this->init_ldap($args['host'])) {
43 $results = $this->ldap->search('*', $args['user'], TRUE);
44 if (count($results->records) == 1) {
45 $args['user_name'] = $results->records[0]['name'];
46 if (!$args['user_email'] && strpos($results->records[0]['email'], '@')) {
47 $args['user_email'] = rcube_idn_to_ascii($results->records[0]['email']);
49 if (($alias_col = $rcmail->config->get('new_user_identity_alias')) && $results->records[0][$alias_col]) {
50 $args['alias'] = $results->records[0][$alias_col];
57 private function init_ldap($host)
60 return $this->ldap->ready;
62 $rcmail = rcmail::get_instance();
64 $addressbook = $rcmail->config->get('new_user_identity_addressbook');
65 $ldap_config = (array)$rcmail->config->get('ldap_public');
66 $match = $rcmail->config->get('new_user_identity_match');
68 if (empty($addressbook) || empty($match) || empty($ldap_config[$addressbook])) {
72 $this->ldap = new new_user_identity_ldap_backend(
73 $ldap_config[$addressbook],
74 $rcmail->config->get('ldap_debug'),
75 $rcmail->config->mail_domain($host),
78 return $this->ldap->ready;
82 class new_user_identity_ldap_backend extends rcube_ldap
84 function __construct($p, $debug, $mail_domain, $search)
86 parent::__construct($p, $debug, $mail_domain);
87 $this->prop['search_fields'] = (array)$search;