[dev] Deny permissions

Jason Rust jrust at rustyparts.com
Thu May 29 12:00:15 PDT 2003


> Hrm. The biggest problem I see with this is that it requires iterating
> through every single group to check for hooks. That loses the benefit of
> the category updates that I've been committing. Any chance of an
> implementation that doesn't require that?

Ok, attached is a new version that uses get_defined_functions() to
figure out if a hook is present.  That way it only does one look up per
hook function.  Only problem is that the exists() method is case
sensitive right now, so hook name of _group_hook_authUsers will not work
since PHP passes the function name back as _group_hook_authusers.  Is it
a problem to make the exists method case insensitive?

> Also, it should probably be Group_hooks, not Group_Hooks.

Changed.

-Jason
-------------- next part --------------
<?php
/**
 * The Group_Hooks:: class provides the Horde groups system with the
 * addition of adding support for hook functions to define if a user
 * is in a group.
 *
 * $Horde: horde/lib/Group.php,v 1.40 2003/05/25 14:34:23 chuck Exp $
 *
 * Copyright 2003 Jason Rust <jrust at rustyparts.com>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Jason Rust <jrust at rustyparts.com>
 * @version $Revision: 1.40 $
 * @since   Horde 3.0
 * @package horde.group
 */
class Group_hooks extends Group {

    /**
     * Constructor
     */
    function Group_hooks()
    {
        $this->Group();
        global $registry;
        require $registry->getParam('fileroot', 'horde') . '/config/hooks.php';
    }

    /**
     * Get a list of every group that $user is in.
     *
     * @param string $user  The user to get groups for.
     *
     * @return array  An array of all groups the user is in.
     */
    function getGroupMemberships($user)
    {
        $memberships = parent::getGroupMemberships($user);
        $funcs = get_defined_functions();
        foreach ($funcs['user'] as $funcName) {
            if (strpos($funcName, '_group_hook_') === 0) {
                $groupName = substr($funcName, 12);
                if (!in_array($groupName, $memberships) &&
                    $this->exists($groupName) &&
                    call_user_func($this->_getGroupHookName($funcName), $user)) {
                    $memberships[] = $groupName;
                }
            }
        }

        return $memberships;
    }

    /**
     * Say if a user is a member of a group or not.
     *
     * @param          string  $user       The name of the user.
     * @param          string  $group      The name of the group.
     * @param optional boolean $subgroups  Return true if the user is in any subgroups
     *                                     of $group, also.
     *
     * @return boolean
     * @access public
     */
    function userIsInGroup($user, $group, $subgroups = false)
    {
        if ($this->groupHasHook($group)) {
            if (call_user_func($this->_getGroupHookName($group), $user)) {
                $inGroup = true;
            } else {
                $inGroup = false;
            }
        }

        if ($inGroup || parent::userIsInGroup($user, $group, $subgroups)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Determines if a group has a hook associated with it. 
     *
     * @param string $name The group name
     *
     * @access public
     * @return bool True if the group has a hook, false otherwise 
     */
    function groupHasHook($name)
    {
        return function_exists($this->_getGroupHookName($name));
    }

    /**
     * Returns the name of the hook function.
     *
     * @param string $name The group name
     *
     * @access public
     * @return string The function name for the hook for this group
     */
    function _getGroupHookName($name)
    {
        return '_group_hook_' . $name;
    }

}


More information about the dev mailing list