[dev] generic hook to add a user in turba
steve
mailling at gmx.co.uk
Thu Feb 5 13:54:34 PST 2004
Here is some files to patch to have a hook _horde_hook_signup_addextra that
is working with every Turba backend (DB, LDAP, ...), and not just with the
database
RCS file: /repository/horde/config/hooks.php.dist,v
retrieving revision 1.57
diff -w -b -r1.57 hooks.php.dist
449a450,462
> // Return a turba source to use when we add the extra information to the
turba backend
> // It is juste an example, since it can be usefull sometimes to add the
extra in
> // different sources, based on the extra (for example, different add
user form).
> if (!function_exists('_horde_hook_signup_getsource')) {
> function _horde_hook_signup_getsource($userID, $extra)
> {
>
> global $conf;
>
> return $conf['hooks']['turbasource'];
> }
> }
>
452,456c465
< // Here we connect to the database using the sql parameters configured in
$conf
< // and store the extra fields in turba_objects, using the $userId as the
key for
< // the object and values from the $extra array.
< // You could create your own sql syntax or code to store this in whichever
< // backend you require.
---
> // It is a generic hook to save extra data in any Turba source
458c467
< // example below. It also assumes that you are using an SQL backend.
---
> // Changed from the old implementation: the assignement of object_id
changed !!
462c471
< global $conf;
---
> global $registry;
464,465c473,476
< require_once 'DB.php';
< $_db = &DB::connect($conf['sql'], true);
---
> //We add it to the default source, later, we could load it from the $conf
> $source =
Horde::callHook('_horde_hook_signup_getsource', array($userID, $extra));
> if (empty($source))
> return true;
467,471c478,489
< $fields = array();
< $values = array();
< foreach ($extra as $field => $value) {
< $fields[] = 'object_' . String::lower($field);
< $values[] = $_db->quote(String::convertCharset($value,
NLS::getCharset(), $conf['sql']['charset']));
---
> if (!defined('TURBA_BASE')) {
> // Find the base file path of Turba.
> define('TURBA_BASE', HORDE_BASE . '/turba');
> }
> // Turba base library.
> require_once (TURBA_BASE . '/lib/Turba.php');
> require_once (TURBA_BASE . '/lib/Source.php');
>
> /* Load the source configuration */
> $path = $registry->getParam('configroot', 'turba');
> if (is_null($path)) {
> $path = $registry->getParam('fileroot', 'turba');
473,474d490
< $fields[] = 'object_id';
< $values[] = $_db->quote($userID);
476,478c492,510
< $query = 'INSERT INTO turba_objects ( owner_id, ' . implode(',
', $fields) . ')';
< $query .= ' VALUES ( \'admin\', ' . implode(', ', $values) . ')';
< $result = $_db->query($query);
---
> $cfgSources = array(); //The array is filled up in the
include
> require($path . '/config/sources.php');
>
> $driver = &Turba_Source::singleton($source,
$cfgSources[$source]);
> if (is_a($driver, 'PEAR_Error')) {
> $notification = &Notification::singleton();
> $notification->push(_("Failed to connect to the
specified directory."), 'horde.error');
> return false;
> }
> $newUser = array();
> $newUser['__owner'] = ''; //Maybe we should put
something else here?, $userID?
> $newUser['__type'] = 'Object';
> foreach ($extra as $field => $value) {
> if (!is_null($value)) {
> $newUser[$field] = $value;
> }
> }
> $key = $driver->addObject($newUser);
> return $key;
-----
I decided to specify in which turba backend we can find further information
in the table horde_users
RCS file: /repository/horde/scripts/db/auth.sql,v
retrieving revision 1.10
diff -w -b -r1.10 auth.sql
6c6,7
<
---
> turba_id VARCHAR(255) NULL,
> turba_source VARCHAR(255) NOT NULL default 'localsql',
I didn't make the alter statement, but to get the old behavior:
- turba_source must be :'localsql'
- turba_id must be = user_uid
--
Now, we have to change the way the signup is done.
RCS file: /repository/horde/lib/Signup.php,v
retrieving revision 1.28
diff -w -b -r1.28 Signup.php
96c96
< $added = Horde::callHook('_horde_hook_signup_addextra',
---
> $turba_id = Horde::callHook('_horde_hook_signup_addextra',
98,99c98,100
< if (!$added || is_a($added, 'PEAR_Error')) {
< Horde::logMessage($added, __FILE__, __LINE__,
PEAR_LOG_EMERG);
---
> if (!$turba_id || is_a($turba_id, 'PEAR_Error')) {
> Horde::logMessage($turba_id, __FILE__, __LINE__,
PEAR_LOG_EMERG);
> //I ma not nure we should do a fatal here?
101a103,108
> $turba_source =
Horde::callHook('_horde_hook_signup_getsource', array($info['user_name'],
$info['extra']));
>
> $success = $auth->updateUser($info['user_name'],
$info['user_name'], array('password' => $info['password']),
array('turba_id' => $turba_id, 'turba_source' => $turba_source));
> if (is_a($success, 'PEAR_Error')) {
> return $success;
> }
103a111
>
289a298,299
> $label = isset($field['label']) ? $field['label'] : null;
> $required = isset($field['required']) ?
$field['required'] : false;
291,293c301
< $this->addVariable($field['label'], 'extra[' .
$field_name . ']',
< $field['type'], $required, $readonly,
< $desc, $field_params);
---
> $this->addVariable($label, 'extra[' . $field_name . ']',
$field['type'], $required, $readonly, $desc, $field_params);
----
To conclude, we need to add a new parameter in the configuration (default
turba backend)
RCS file: /repository/horde/config/conf.php.dist,v
retrieving revision 1.85
diff -w -b -r1.85 conf.php.dist
565a566,570
> // Parameters defined when we call the hook _horde_hook_signup_addextra
> // Specify the turba source to use when we add the extra paremeters
> // If false, the extra informations are not added
> $conf['hooks']['turbasource'] = 'localsql';
>
More information about the dev
mailing list