File: Group.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (370 lines) | stat: -rw-r--r-- 11,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/*
 * $Horde: horde/lib/Group.php,v 1.6.2.11 2003/01/17 10:22:14 jan Exp $
 *
 * Copyright 1999-2003 Stephane Huther <shuther@bigfoot.com>
 * Copyright 2001-2003 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

require_once HORDE_BASE . '/lib/Category.php';

/**
 * The Group:: class provides the Horde groups system.
 *
 * @author  Stephane Huther <shuther@bigfoot.com>
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.6.2.11 $
 * @since   Horde 2.1
 * @package horde.group
 */
class Group {

    /**
     * Pointer to a category instance to manage the different groups.
     * @var object Category $groups
     */
    var $groups;

    /**
     * Constructor
     */
    function Group()
    {
        global $conf;
        $this->groups = &Category::singleton($conf['category']['driver'],
                                             array_merge($conf['category']['params'],
                                                         array('group' => 'horde.groups')));
    }

    /**
     * Attempts to return a reference to a concrete Group instance.
     * It will only create a new instance if no Group instance
     * currently exists.
     *
     * This method must be invoked as: $var = &Group::singleton()
     *
     * @return object Group  The concrete Group reference, or false on an
     *                       error.
     */
    function &singleton()
    {
        static $group;

        if (!isset($group)) {
            $group = new Group();
        }

        return $group;
    }

    /**
     * Return a new group object.
     *
     * @param string $name The group's name.
     *
     * @return object CategoryObject_Group A new group object.
     */
    function &newGroup($name)
    {
        if (empty($name)) {
            return PEAR::raiseError('Group names must be non-empty');
        }
        $group = &new CategoryObject_Group($name);
        $group->setGroupOb($this);
        return $group;
    }

    /**
     * Return a CategoryObject_Group object corresponding to the named
     * group, with the users and other data retrieved appropriately.
     *
     * @param string $name The name of the group to retrieve.
     */
    function &getGroup($name)
    {
        $group = $this->groups->getCategory($name, 'CategoryObject_Group');
        if (!PEAR::isError($group)) {
            $group->setGroupOb($this);
        }
        return $group;
    }

    /**
     * Add a group to the groups system. The group must first be
     * created with Group::newGroup(), and have any initial users
     * added to it, before this function is called.
     *
     * @param object CategoryObject_Group $group The new group object.
     * @param optional string $parent            The name of the parent group
     *                                           (defaults to the category root)
     */
    function addGroup($group, $parent = '-1')
    {
        if (!((get_class($group) == 'categoryobject_group') ||
              is_subclass_of($group, 'CategoryObject_Group'))) {
            return PEAR::raiseError('Groups must be CategoryObject_Group objects or extend that class.');
        }
        return $this->groups->addCategory($group, $parent);
    }

    /**
     * Store updated data - users, etc. - of a group to the backend
     * system.
     *
     * @param object CategoryObject_Group $group   The group to update.
     */
    function updateGroup($group)
    {
        if (!((get_class($group) == 'categoryobject_group') ||
              is_subclass_of($group, 'CategoryObject_Group'))) {
            return PEAR::raiseError('Groups must be CategoryObject_Group objects or extend that class.');
        }
        return $this->groups->updateCategoryData($group);
    }

    /**
     * Change the name of a group without changing its contents or
     * where it is in the groups hierarchy.
     *
     * @param object CategoryObject_Group $group   The group to rename.
     * @param string                      $newName The group's new name.
     */
    function renameGroup($group, $newName)
    {
        if (!((get_class($group) == 'categoryobject_group') ||
              is_subclass_of($group, 'CategoryObject_Group'))) {
            return PEAR::raiseError('Groups must be CategoryObject_Group objects or extend that class.');
        }
        $newGroup = $group;
        $newGroup->name = $newName;
        return $this->groups->renameCategory($group, $newGroup);
    }

    /**
     * Remove a group from the groups system permanently.
     *
     * @param object CategoryObject_Group $group  The group to remove.
     * @param string                      $parent (optional) The group's immediate parent.
     */
    function removeGroup($group, $parent = '0')
    {
        if (!((get_class($group) == 'categoryobject_group') ||
              is_subclass_of($group, 'CategoryObject_Group'))) {
            return PEAR::raiseError('Groups must be CategoryObject_Group objects or extend that class.');
        }
        if ($this->groups->getNumberOfChildren($group) != 0) {
            return PEAR::raiseError('Cannot remove: children exist');
        }

        return $this->groups->removeCategory($group, $parent);
    }

    /**
     * Move a group around in the groups hierarchy.
     *
     * @param object CategoryObject_Group $group      The group to move.
     * @param string                      $old_parent The group's old parent.
     * @param string                      $new_parent The new parent.
     */
    function moveGroup($group, $old_parent, $new_parent)
    {
        if (!((get_class($group) == 'categoryobject_group') ||
              is_subclass_of($group, 'CategoryObject_Group'))) {
            return PEAR::raiseError('Groups must be CategoryObject_Group objects or extend that class.');
        }
        return $this->groups->moveCategory($group, $old_parent, $new_parent);
    }

    /**
     * Get a list of the parents of a child group.
     *
     * @param string $group The name of the child group.
     *
     * @return array
     */
    function getGroupParents($group)
    {
        return $this->groups->getParents($group);
    }

    /**
     * Get a list of every user that is a part of this group ONLY.
     *
     * @param string $group The name of the group.
     *
     * @return array The user list.
     * @access public
     */
    function listUsers($group)
    {
        $groupOb = &$this->getGroup($group);
        if (!isset($groupOb->data['users']) ||
            !is_array($groupOb->data['users'])) {
            return array();
        }

        return array_keys($groupOb->data['users']);
    }

    /**
     * Get a list of every user that is part of the specified group
     * and any of its subgroups.
     *
     * @param string $group The name of the parent group.
     *
     * @return array The complete user list.
     * @access public
     */
    function listAllUsers($group)
    {
        // Get a list of every group that is a sub-group of $group.
        $groups = $this->groups->export(CATEGORY_FORMAT_FLAT, $group);
        $groups = array_keys($groups);
        $users = array();
        foreach ($groups as $group) {
            $users = array_merge($users, $this->listUsers($group));
        }
        return array_values(array_flip(array_flip($users)));
    }

    /**
     * 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 = array();

        $groups = $this->groups->export(CATEGORY_FORMAT_FLAT);
        array_shift($groups);

        foreach ($groups as $group => $junk) {
            if ($this->userIsInGroup($user, $group)) {
                $memberships[] = $group;
            }
        }

        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 ($subgroups) {
            $users = $this->listAllUsers($group);
        } else {
            $users = $this->listUsers($group);
        }
        return in_array($user, $users);
    }

}

/**
 * Extension of the CategoryObject class for storing Group information
 * in the Categories driver. If you want to store specialized Group
 * information, you should extend this class instead of extending
 * CategoryObject directly.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.6.2.11 $
 * @since   Horde 2.1
 * @package horde.group
 */
class CategoryObject_Group extends CategoryObject {

    /** The Group object which this group came from - needed for
        updating data in the backend to make changes stick, etc.
        @var object Group $groupOb */
    var $groupOb;

    /**
     * The CategoryObject_Group constructor. Just makes sure to call
     * the parent constructor so that the group's name is set
     * properly.
     *
     * @param string $name The name of the group.
     */
    function CategoryObject_Group($name)
    {
        parent::CategoryObject($name);
    }

    /**
     * Associates a Group object with this group.
     *
     * @param object Group $groupOb The Group object.
     */
    function setGroupOb(&$groupOb)
    {
        $this->groupOb = &$groupOb;
    }

    /**
     * Adds a user to this group, and makes sure that the backend is
     * updated as well.
     *
     * @param string $username The user to add.
     */
    function addUser($username)
    {
        $this->data['users'][$username] = 1;
        if ($this->groupOb->groups->exists($this->getName())) {
            $this->groupOb->updateGroup($this);
        }
    }

    /**
     * Removes a user from this group, and makes sure that the backend
     * is updated as well.
     *
     * @param string $username The user to remove.
     */
    function removeUser($username)
    {
        unset($this->data['users'][$username]);
        $this->groupOb->updateGroup($this);
    }

    /**
     * Get a list of every user that is a part of this group
     * (and only this group)
     *
     * @return array The user list
     * @access public
     */
    function listUsers()
    {
        return $this->groupOb->listUsers($this->name);
    }

    /**
     * Get a list of every user that is a part of this group and
     * any of it's subgroups
     *
     * @return array The complete user list
     * @access public
     */
    function listAllUsers()
    {
        return $this->groupOb->listAllUsers($this->name);
    }

}
?>