Created
August 28, 2014 06:32
-
-
Save flocke/fc4cb3b3d952dae0df49 to your computer and use it in GitHub Desktop.
Use PostgreSQL to set the identity for new users in RoundCube.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* New user identity | |
* | |
* Populates a new user's default identity from LDAP on their first visit. | |
* | |
* This plugin requires that a working public_ldap directory be configured. | |
* | |
* @version @package_version@ | |
* @author Kris Steinhoff | |
* @license GNU GPLv3+ | |
* | |
* Example configuration: | |
* | |
* // The id of the address book to use to automatically set a new | |
* // user's full name in their new identity. (This should be an | |
* // string, which refers to the $config['ldap_public'] array.) | |
* $config['new_user_identity_addressbook'] = 'People'; | |
* | |
* // When automatically setting a new users's full name in their | |
* // new identity, match the user's login name against this field. | |
* $config['new_user_identity_match'] = 'uid'; | |
*/ | |
class new_user_identity_pgsql extends rcube_plugin | |
{ | |
public $task = 'login'; | |
private $db; | |
function init() | |
{ | |
$this->add_hook('user_create', array($this, 'lookup_user_name')); | |
} | |
function lookup_user_name($args) | |
{ | |
$rcmail = rcmail::get_instance(); | |
$db_host = $rcmail->config->get('new_user_identity_pgsql_host'); | |
$db_user = $rcmail->config->get('new_user_identity_pgsql_user'); | |
$db_pass = $rcmail->config->get('new_user_identity_pgsql_pass'); | |
$db_name = $rcmail->config->get('new_user_identity_pgsql_database'); | |
$db_query = str_replace ( '%u', $args['user'], $rcmail->config->get('new_user_identity_pgsql_query') ); | |
if ( $dbconn = pg_connect("host=$db_host user=$db_user password=$db_pass dbname=$db_name") ) { | |
$result = pg_query ( $db_query ); | |
if ( $result ) { | |
if ( pg_num_rows ( $result ) == 1 ) { | |
$user_email = pg_fetch_result ( $result, 0, 0 ); | |
if (!$args['user_email'] && strpos($user_email, '@')) { | |
$args['user_email'] = rcube_utils::idn_to_ascii($user_email); | |
} | |
$args['user_name'] = pg_fetch_result ( $result, 0, 1 ); | |
} | |
pg_free_result ( $result ); | |
} | |
pg_close ( $dbconn ); | |
return $args; | |
} | |
} | |
} | |
class new_user_identity_pgsql_backend | |
{ | |
function __construct($p, $debug, $mail_domain, $search) | |
{ | |
parent::__construct($p, $debug, $mail_domain); | |
$this->prop['search_fields'] = (array)$search; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment