[horde] Sharing user information between hooks

Jens Wahnes wahnes at uni-koeln.de
Mon Oct 26 14:43:47 UTC 2015


On Fri, Oct 02 2015, at 19:14:28 +0000, Michael J Rubinsky wrote:

> Quoting Jens Wahnes <wahnes at uni-koeln.de>:
>> To customize the user experience, we are using hooks in our Horde
>> installation.  Among others, we are using preauth-Hooks both within the
>> "horde" scope (.../config/hooks.php) and within the "imp" scope
>> (.../imp/config/hooks.php).  So far, these hooks work completely
>> independent from each other.  However, we would like to change that and
>> access information that has been generated by Horde's preauth hook from
>> within IMP's preauth hook.  What is the best way to share such
>> information between these hooks?  Should I add some key-value pair to
>> the user's session?  If so, how should I go about that?  Should I use
>> PHP's $_SESSION variable?  Or is there a preferred way to do that
>> through some interface the Horde framework provides?

> IIRC, depending on the context, it's possible for the preauth hooks to  
> be executed before the session is created, so you may or may not be able 
> to utilize the horde session within those hooks.

For what we are trying to achieve, storing things within the session
works quite well.  To me, it looks like all of Horde's hooks are
executed before any of IMP's hooks are executed (using LDAP
authentication, that is - might be different when using IMAP
authentication through IMP).  So we are off just fine as long as we
look into IMP preauth requests only if they have got a $userId set. 
Now we are just dependend on the fact that IMP's hooks are run after
Horde's, so I hope that things will stay that way for a while. :-)

> For the record, though, you should access the session using the
> $GLOBALS['session'] object, not the raw php $_SESSION variable.

Just in case anyone else is interested in doing something like this, I
got this working using statements like these:

$GLOBALS['session']->set('RRZK', 'foo', 'bar');

$foo = $GLOBALS['session']->get('RRZK', 'foo');

However, now that this is working, I'm already off to try something
else.

The reason to introduce code to write things into the session was to
save unnecessary LDAP requests that would otherwise have to be executed
in both Horde's hooks and IMP's hooks.  The bulk of LDAP requests we
see, however, seems to stem from the fact that a complete session is
set up (including hooks being run) whenever there is a request through
CalDAV or such.  And not only is a session created for the user
performing the CalDAV request, but also for all users that have granted
this user access to their calendars, plus all users that this user is
granting access to his or her calendars.  All in all, that's a lot of
sessions with lots of LDAP requests.  (Consider, for example, a group
of just 5 people granting calendar access to each other's personal
calendar.  Then you've got 5 LDAP requests per every CalDAV request. 
If everyone within the group is accessing all 5 calendars via CalDAV,
that's 5*5*5 = 125 LDAP requests for just 5 people.)

So I came to the idea to use memcache to cache the LDAP data, since we
make heavy use of memcache anyways.  I dug around the Horde API docs a
bit and came up with some code that looks like this (not exactly, but
close enough):


$memcache = $GLOBALS['injector']->getInstance('Horde_Core_Factory_HashTable')->create($GLOBALS['injector']);
$memcachekey = "rrzk_foo_$username";

if (!($foo = $memcache->get($memcachekey))) { 
    # some code here that sets $foo from LDAP
    $memcache->set($memcachekey, $foo, array('expire' => $expirytime, 'replace' => 1));
}


This does in fact work, but there is one problem: Calling the memcache
methods this way does not seem to honor the prefix for memcache keys
that can be set through $conf['hashtable']['params']['prefix'].  Since
other configuration options are actually picked up (e.g. the hostnames
set in $conf['hashtable']['params']['hostspec']), there must be some
error in the above code.  Can anyone spot it?  Do I have to call other
(more generic) methods to make use of the prefix for memcache keys?  Or
is prefixing the key for memcache left up to the function calling the
memcache set and get functions?  If so, how would I access the name of
the memcache prefix from within a hook?


Jens
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <http://lists.horde.org/archives/horde/attachments/20151026/906948b6/attachment.bin>


More information about the horde mailing list