You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

new_user_identity.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * New user identity
  4. *
  5. * Populates a new user's default identity from LDAP on their first visit.
  6. *
  7. * This plugin requires that a working public_ldap directory be configured.
  8. *
  9. * @author Kris Steinhoff
  10. * @license GNU GPLv3+
  11. */
  12. class new_user_identity extends rcube_plugin
  13. {
  14. public $task = 'login';
  15. private $rc;
  16. private $ldap;
  17. function init()
  18. {
  19. $this->rc = rcmail::get_instance();
  20. $this->add_hook('user_create', array($this, 'lookup_user_name'));
  21. $this->add_hook('login_after', array($this, 'login_after'));
  22. }
  23. function lookup_user_name($args)
  24. {
  25. if ($this->init_ldap($args['host'])) {
  26. $results = $this->ldap->search('*', $args['user'], true);
  27. if (count($results->records) == 1) {
  28. $user_name = is_array($results->records[0]['name']) ? $results->records[0]['name'][0] : $results->records[0]['name'];
  29. $user_email = is_array($results->records[0]['email']) ? $results->records[0]['email'][0] : $results->records[0]['email'];
  30. $args['user_name'] = $user_name;
  31. $args['email_list'] = array();
  32. if (!$args['user_email'] && strpos($user_email, '@')) {
  33. $args['user_email'] = rcube_utils::idn_to_ascii($user_email);
  34. }
  35. foreach (array_keys($results[0]) as $key) {
  36. if (!preg_match('/^email($|:)/', $key)) {
  37. continue;
  38. }
  39. foreach ((array) $results->records[0][$key] as $alias) {
  40. if (strpos($alias, '@')) {
  41. $args['email_list'][] = rcube_utils::idn_to_ascii($alias);
  42. }
  43. }
  44. }
  45. }
  46. }
  47. return $args;
  48. }
  49. function login_after($args)
  50. {
  51. $this->load_config();
  52. if ($this->ldap || !$this->rc->config->get('new_user_identity_onlogin')) {
  53. return $args;
  54. }
  55. $identities = $this->rc->user->list_emails();
  56. $ldap_entry = $this->lookup_user_name(array(
  57. 'user' => $this->rc->user->data['username'],
  58. 'host' => $this->rc->user->data['mail_host'],
  59. ));
  60. foreach ((array) $ldap_entry['email_list'] as $email) {
  61. foreach ($identities as $identity) {
  62. if ($identity['email'] == $email) {
  63. continue 2;
  64. }
  65. }
  66. $plugin = $this->rc->plugins->exec_hook('identity_create', array(
  67. 'login' => true,
  68. 'record' => array(
  69. 'user_id' => $this->rc->user->ID,
  70. 'standard' => 0,
  71. 'email' => $email,
  72. 'name' => $ldap_entry['user_name']
  73. ),
  74. ));
  75. if (!$plugin['abort'] && $plugin['record']['email']) {
  76. $this->rc->user->insert_identity($plugin['record']);
  77. }
  78. }
  79. return $args;
  80. }
  81. private function init_ldap($host)
  82. {
  83. if ($this->ldap) {
  84. return $this->ldap->ready;
  85. }
  86. $this->load_config();
  87. $addressbook = $this->rc->config->get('new_user_identity_addressbook');
  88. $ldap_config = (array)$this->rc->config->get('ldap_public');
  89. $match = $this->rc->config->get('new_user_identity_match');
  90. if (empty($addressbook) || empty($match) || empty($ldap_config[$addressbook])) {
  91. return false;
  92. }
  93. $this->ldap = new new_user_identity_ldap_backend(
  94. $ldap_config[$addressbook],
  95. $this->rc->config->get('ldap_debug'),
  96. $this->rc->config->mail_domain($host),
  97. $match);
  98. return $this->ldap->ready;
  99. }
  100. }
  101. class new_user_identity_ldap_backend extends rcube_ldap
  102. {
  103. function __construct($p, $debug, $mail_domain, $search)
  104. {
  105. parent::__construct($p, $debug, $mail_domain);
  106. $this->prop['search_fields'] = (array)$search;
  107. }
  108. }