[dev] Group hooks patch

Jason Rust jrust at rustyparts.com
Mon Jan 5 09:48:46 PST 2004


Finally getting around to finishing up the hooks driver for Groups.
Attached is a patch which cleans up a couple bugs in the driver, adds an
example hook to hooks.php.dist, adds a group driver option to
conf.php.dist, and patches Group::singleton() to check for that driver.

-Jason
-------------- next part --------------
Index: config/conf.php.dist
===================================================================
RCS file: /repository/horde/config/conf.php.dist,v
retrieving revision 1.74
diff -u -r1.74 conf.php.dist
--- config/conf.php.dist	4 Jan 2004 15:58:15 -0000	1.74
+++ config/conf.php.dist	5 Jan 2004 17:34:54 -0000
@@ -302,6 +302,20 @@
 
 
 /**
+ ** Groups Settings
+ **/
+
+// What backend should we use for Horde Groups?
+// Backends:
+//   'category' -- Use Horde Categories to store information about the
+//                 groups.
+//   'hooks'    -- This extends the categories backend to allow hooks
+//                 to extend a group to dynamically include members
+//                 into a group.  See hooks.php.dist for an example.
+$conf['group']['driver'] = 'category';
+
+
+/**
  ** Cache System Settings
  **/
 
Index: config/hooks.php.dist
===================================================================
RCS file: /repository/horde/config/hooks.php.dist,v
retrieving revision 1.55
diff -u -r1.55 hooks.php.dist
--- config/hooks.php.dist	26 Nov 2003 15:28:40 -0000	1.55
+++ config/hooks.php.dist	5 Jan 2004 17:34:55 -0000
@@ -676,3 +676,29 @@
         $template->set('date', strftime('%a, %e %b %Y'));
     }
 }
+
+// This is an example of a group hook.  To use it you must set the group
+// driver to hooks in conf.php.  Then you must create a IT_department
+// group (because that is how we know what hook to call).  You can add
+// users to the group as normal, and in addition this function will be
+// called to dynamically include users in the group.  In this example we
+// will look up whether or not this user is part of the IT department
+// using an external database.
+if (!function_exists('_group_hook_IT_department')) {
+    function _group_hook_IT_department($userName)
+    {
+        global $conf;
+
+        $dept = 'IT';
+        include_once 'DB.php';
+        $_db = &DB::connect($conf['sql'], true);
+        $query = sprintf('SELECT COUNT(*) FROM departments WHERE user_name=%s AND department=%s',
+                $_db->quote($userName), $_db->quote($dept));
+        $result = $_db->getOne($userName);
+        if (!is_a($result, 'PEAR_Error') && $result > 0) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+}
Index: lib/Group/hooks.php
===================================================================
RCS file: /repository/horde/lib/Group/hooks.php,v
retrieving revision 1.3
diff -u -r1.3 hooks.php
--- lib/Group/hooks.php	1 Jan 2004 15:16:05 -0000	1.3
+++ lib/Group/hooks.php	5 Jan 2004 17:34:55 -0000
@@ -43,7 +43,7 @@
                 $groupName = substr($funcName, 12);
                 if (!in_array($groupName, $memberships) &&
                     $this->exists($groupName) &&
-                    call_user_func($this->_getGroupHookName($funcName), $user)) {
+                    call_user_func($funcName, $user)) {
                     $memberships[] = $groupName;
                 }
             }
@@ -71,6 +71,8 @@
             } else {
                 $inGroup = false;
             }
+        } else {
+            $inGroup = false;
         }
 
         if ($inGroup || parent::userIsInGroup($user, $group, $subgroups)) {
Index: lib/Group.php
===================================================================
RCS file: /repository/horde/lib/Group.php,v
retrieving revision 1.56
diff -u -r1.56 Group.php
--- lib/Group.php	1 Jan 2004 15:15:51 -0000	1.56
+++ lib/Group.php	5 Jan 2004 17:34:59 -0000
@@ -408,6 +408,8 @@
             $auth = &Auth::singleton($conf['auth']['driver']);
             if ($auth->hasCapability('groups')) {
                 $group = &Group::factory($auth->getDriver(), $auth);
+            } elseif (!empty($conf['group']['driver']) && $conf['group']['driver'] != 'category') {
+                $group = &Group::factory($conf['group']['driver']);
             } else {
                 $group = new Group();
             }


More information about the dev mailing list