1<?php
2/**
3 * Representation of a group.
4 *
5 * PHP version 5
6 *
7 * @category Kolab
8 * @package  Kolab_Server
9 * @author   Gunnar Wrobel <wrobel@pardus.de>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @link     http://pear.horde.org/index.php?package=Kolab_Server
12 */
13
14/**
15 * This class provides methods to deal with groups.
16 *
17 * Copyright 2008-2016 Horde LLC (http://www.horde.org/)
18 *
19 * See the enclosed file COPYING for license information (LGPL). If you
20 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21 *
22 * @category Kolab
23 * @package  Kolab_Server
24 * @author   Gunnar Wrobel <wrobel@pardus.de>
25 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
26 * @link     http://pear.horde.org/index.php?package=Kolab_Server
27 */
28class Horde_Kolab_Server_Object_Groupofnames extends Horde_Kolab_Server_Object_Top
29{
30    /** The specific object class of this object type */
31    const OBJECTCLASS_GROUPOFNAMES = 'groupOfNames';
32
33    /** Define attributes specific to this object type */
34
35    /** The common name */
36    const ATTRIBUTE_CN     = 'cn';
37
38    /** The members of this group */
39    const ATTRIBUTE_MEMBER = 'member';
40
41    /**
42     * A structure to initialize the attribute structure for this class.
43     *
44     * @var array
45     */
46    public static $init_attributes = array(
47        'required' => array(
48            self::ATTRIBUTE_CN,
49            self::ATTRIBUTE_MEMBER,
50        ),
51        'defined' => array(
52            self::ATTRIBUTE_CN,
53            self::ATTRIBUTE_MEMBER,
54        ),
55        'object_classes' => array(
56            self::OBJECTCLASS_GROUPOFNAMES,
57        ),
58    );
59
60    /**
61     * Return the filter string to retrieve this object type.
62     *
63     * @static
64     *
65     * @return string The filter to retrieve this object type from the server
66     *                database.
67     */
68    public static function getFilter()
69    {
70        return '(' . self::ATTRIBUTE_OC . '=' . self::OBJECTCLASS_GROUPOFNAMES . ')';
71    }
72
73    /**
74     * Generates an ID for the given information.
75     *
76     * @param array &$info The data of the object.
77     *
78     * @static
79     *
80     * @return string|PEAR_Error The ID.
81     */
82    public function generateId(array &$info)
83    {
84        $id = $info[self::ATTRIBUTE_CN];
85        if (is_array($id)) {
86            $id = $id[0];
87        }
88        return trim(self::ATTRIBUTE_CN . '=' . $id, " \t\n\r\0\x0B,");
89    }
90
91    /**
92     * Retrieve the member list for this group.
93     *
94     * @return array|PEAR_Error The list of members in this group.
95     */
96    public function getMembers()
97    {
98        return $this->get(self::ATTRIBUTE_MEMBER, false);
99    }
100
101    /**
102     * Add a member to this group.
103     *
104     * @param string $member The UID of the member to add.
105     *
106     * @return array|PEAR_Error True if successful.
107     */
108    public function addMember($member)
109    {
110        if (!in_array($member, $this->getMembers())) {
111            $this->_cache[self::ATTRIBUTE_MEMBER][] = $member;
112        } else {
113            throw new Horde_Kolab_Server_Exception("The UID %s is already a member of the group %s!",
114                                                   $member, $this->_uid);
115        }
116        return $this->save($this->_cache);
117    }
118
119    /**
120     * Delete a member from this group.
121     *
122     * @param string $member The UID of the member to delete.
123     *
124     * @return array|PEAR_Error True if successful.
125     */
126    public function deleteMember($member)
127    {
128        $members = $this->getMembers();
129        if (in_array($member, $members)) {
130            //@todo: As the member attribute is required we may not remove the last member
131            $this->_cache[self::ATTRIBUTE_MEMBER] =
132                array_diff($this->_cache[self::ATTRIBUTE_MEMBER],
133                           array($member));
134        } else {
135            throw new Horde_Kolab_Server_Exception("The UID %s is no member of the group %s!",
136                                                   $member, $this->_uid);
137
138        }
139        return $this->save($this->_cache);
140    }
141
142    /**
143     * Is the specified UID member of this group?
144     *
145     * @param string $member The UID of the member to check.
146     *
147     * @return boolean|PEAR_Error True if the UID is a member of the group,
148     *                            false otherwise.
149     */
150    public function isMember($member)
151    {
152        $members = $this->getMembers();
153        if (!is_array($members)) {
154            return $member == $members;
155        }
156        if (!in_array($member, $members)) {
157            return false;
158        } else {
159            return true;
160        }
161    }
162}
163