~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Group/hooks.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * The Group_hooks:: class provides the Horde groups system with the
 
4
 * addition of adding support for hook functions to define if a user
 
5
 * is in a group.
 
6
 *
 
7
 * $Horde: framework/Group/Group/hooks.php,v 1.7.2.1 2005/01/03 12:19:01 jan Exp $
 
8
 *
 
9
 * Copyright 2003-2005 Jason Rust <jrust@rustyparts.com>
 
10
 *
 
11
 * See the enclosed file COPYING for license information (LGPL). If you
 
12
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
13
 *
 
14
 * @author  Jason Rust <jrust@rustyparts.com>
 
15
 * @since   Horde 3.0
 
16
 * @package Horde_Group
 
17
 */
 
18
class Group_hooks extends Group {
 
19
 
 
20
    /**
 
21
     * Constructor.
 
22
     */
 
23
    function Group_hooks()
 
24
    {
 
25
        parent::Group();
 
26
        require $GLOBALS['registry']->get('fileroot', 'horde') . '/config/hooks.php';
 
27
    }
 
28
 
 
29
    /**
 
30
     * Get a list of every group that $user is in.
 
31
     *
 
32
     * @param string $user  The user to get groups for.
 
33
     *
 
34
     * @return array  An array of all groups the user is in.
 
35
     */
 
36
    function getGroupMemberships($user)
 
37
    {
 
38
        $memberships = parent::getGroupMemberships($user);
 
39
        $funcs = get_defined_functions();
 
40
        foreach ($funcs['user'] as $funcName) {
 
41
            if (strpos($funcName, '_group_hook_') === 0) {
 
42
                $groupName = substr($funcName, 12);
 
43
                if (!in_array($groupName, $memberships) &&
 
44
                    $this->exists($groupName) &&
 
45
                    call_user_func($funcName, $user)) {
 
46
                    $memberships[] = $groupName;
 
47
                }
 
48
            }
 
49
        }
 
50
 
 
51
        return $memberships;
 
52
    }
 
53
 
 
54
    /**
 
55
     * Say if a user is a member of a group or not.
 
56
     *
 
57
     * @param          string  $user       The name of the user.
 
58
     * @param          string  $group      The name of the group.
 
59
     * @param optional boolean $subgroups  Return true if the user is in any subgroups
 
60
     *                                     of $group, also.
 
61
     *
 
62
     * @access public
 
63
     * @return boolean
 
64
     */
 
65
    function userIsInGroup($user, $group, $subgroups = false)
 
66
    {
 
67
        if ($this->hasHook($group)) {
 
68
            if (call_user_func($this->_getGroupHookName($group), $user)) {
 
69
                $inGroup = true;
 
70
            } else {
 
71
                $inGroup = false;
 
72
            }
 
73
        } else {
 
74
            $inGroup = false;
 
75
        }
 
76
 
 
77
        if ($inGroup || parent::userIsInGroup($user, $group, $subgroups)) {
 
78
            return true;
 
79
        } else {
 
80
            return false;
 
81
        }
 
82
    }
 
83
 
 
84
    /**
 
85
     * Determines if a group has a hook associated with it.
 
86
     *
 
87
     * @param string $name  The group name.
 
88
     *
 
89
     * @access public
 
90
     * @return boolean  True if the group has a hook, false otherwise
 
91
     */
 
92
    function hasHook($name)
 
93
    {
 
94
        return function_exists($this->_getGroupHookName($name));
 
95
    }
 
96
 
 
97
    /**
 
98
     * Returns the name of the hook function.
 
99
     *
 
100
     * @param string $name  The group name.
 
101
     *
 
102
     * @access public
 
103
     * @return string  The function name for the hook for this group
 
104
     */
 
105
    function _getGroupHookName($name)
 
106
    {
 
107
        return '_group_hook_' . str_replace(':', '__', $name);
 
108
    }
 
109
 
 
110
}