1: <?php
2: /**
3: * Representation of a Kolab user 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 for Kolab.
16: *
17: * Copyright 2008-2012 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: */
28: class Horde_Kolab_Server_Object_Kolabgroupofnames extends Horde_Kolab_Server_Object_Groupofnames
29: {
30: /** Define attributes specific to this object type */
31:
32: /** The visibility of the group */
33: const ATTRIBUTE_VISIBILITY = 'visible';
34:
35: /** The ou subtree of the group */
36: const ATTRIBUTE_OU = 'ou';
37:
38: /** The members of this group */
39: const ATTRIBUTE_MEMBER = 'member';
40:
41: /** The mail address of this group */
42: const ATTRIBUTE_MAIL = 'mail';
43:
44: /** The specific object class of this object type */
45: const OBJECTCLASS_KOLABGROUPOFNAMES = 'kolabGroupOfNames';
46:
47: /**
48: * A structure to initialize the attribute structure for this class.
49: *
50: * @var array
51: */
52: static public $init_attributes = array(
53: 'defined' => array(
54: self::ATTRIBUTE_VISIBILITY,
55: self::ATTRIBUTE_MAIL,
56: ),
57: 'derived' => array(
58: self::ATTRIBUTE_VISIBILITY => array(),
59: ),
60: 'object_classes' => array(
61: self::OBJECTCLASS_KOLABGROUPOFNAMES,
62: ),
63: );
64:
65: /**
66: * Return the filter string to retrieve this object type.
67: *
68: * @return string The filter to retrieve this object type from the server
69: * database.
70: */
71: public static function getFilter()
72: {
73: $criteria = array('AND' => array(array('field' => self::ATTRIBUTE_OC,
74: 'op' => '=',
75: 'test' => self::OBJECTCLASS_KOLABGROUPOFNAMES),
76: ),
77: );
78: return $criteria;
79: }
80:
81: /**
82: * Derive an attribute value.
83: *
84: * @param string $attr The attribute to derive.
85: *
86: * @return mixed The value of the attribute.
87: */
88: protected function derive($attr)
89: {
90: switch ($attr) {
91: case self::ATTRIBUTE_VISIBILITY:
92: //@todo This needs structural knowledge and should be in a
93: //structural class.
94: return strpos($this->uid, 'cn=internal') === false;
95: default:
96: return parent::derive($attr);
97: }
98: }
99:
100: /**
101: * Generates an ID for the given information.
102: *
103: * @param array $info The data of the object.
104: *
105: * @static
106: *
107: * @return string|PEAR_Error The ID.
108: */
109: public function generateId(array &$info)
110: {
111: if ($this->exists()) {
112: if (!isset($info[self::ATTRIBUTE_MAIL])
113: && !isset($info[self::ATTRIBUTE_CN])) {
114: return false;
115: }
116: if (!isset($info[self::ATTRIBUTE_MAIL])) {
117: $info[self::ATTRIBUTE_MAIL] = $this->get(self::ATTRIBUTE_MAIL);
118: }
119: if (!isset($info[self::ATTRIBUTE_CN])) {
120: $info[self::ATTRIBUTE_CN] = $this->get(self::ATTRIBUTE_CN);
121: }
122: }
123:
124: if (isset($info[self::ATTRIBUTE_MAIL])) {
125: $id = $info[self::ATTRIBUTE_MAIL];
126: } else {
127: $id = $info[self::ATTRIBUTE_CN];
128: }
129: if (is_array($id)) {
130: $id = $id[0];
131: }
132: return self::ATTRIBUTE_CN . '=' . $this->server->structure->quoteForUid(trim($id, " \t\n\r\0\x0B,"));
133: }
134:
135: /**
136: * Distill the server side object information to save.
137: *
138: * @param array $info The information about the object.
139: *
140: * @return NULL.
141: *
142: * @throws Horde_Kolab_Server_Exception If the given information contains errors.
143: */
144: public function prepareObjectInformation(array &$info)
145: {
146: if (!$this->exists()) {
147: if (!isset($info[self::ATTRIBUTE_CN])) {
148: if (!isset($info[self::ATTRIBUTE_MAIL])) {
149: throw new Horde_Kolab_Server_Exception('Either the mail address or the common name has to be specified for a group object!');
150: } else {
151: $info[self::ATTRIBUTE_CN] = $info[self::ATTRIBUTE_MAIL];
152: }
153: }
154: }
155: }
156:
157: /**
158: * Returns the set of search operations supported by this object type.
159: *
160: * @return array An array of supported search operations.
161: */
162: static public function getSearchOperations()
163: {
164: $searches = array(
165: /* 'gidForMail', */
166: /* 'memberOfGroupAddress', */
167: /* 'getGroupAddresses', */
168: );
169: return $searches;
170: }
171:
172: /**
173: * Identify the GID for the first group found with the given mail.
174: *
175: * @param string $mail Search for groups with this mail address.
176: * @param int $restrict A Horde_Kolab_Server::RESULT_* result restriction.
177: *
178: * @return mixed The GID or false if there was no result.
179: *
180: * @throws Horde_Kolab_Server_Exception
181: */
182: static public function gidForMail($server, $mail,
183: $restrict = 0)
184: {
185: $criteria = array('AND' => array(array('field' => self::ATTRIBUTE_MAIL,
186: 'op' => '=',
187: 'test' => $mail),
188: ),
189: );
190: return self::gidForSearch($server, $criteria, $restrict);
191: }
192:
193: /**
194: * Is the given UID member of the group with the given mail address?
195: *
196: * @param string $uid UID of the user.
197: * @param string $mail Search the group with this mail address.
198: *
199: * @return boolean True in case the user is in the group, false otherwise.
200: *
201: * @throws Horde_Kolab_Server_Exception
202: */
203: static public function memberOfGroupAddress($server, $uid, $mail)
204: {
205: $criteria = array('AND' =>
206: array(
207: array('field' => self::ATTRIBUTE_MAIL,
208: 'op' => '=',
209: 'test' => $mail),
210: array('field' => self::ATTRIBUTE_MEMBER,
211: 'op' => '=',
212: 'test' => $uid),
213: ),
214: );
215:
216: $result = self::gidForSearch($server, $criteria,
217: self::RESULT_SINGLE);
218: return !empty($result);
219: }
220:
221:
222: /**
223: * Get the mail addresses for the group of this object.
224: *
225: * @param string $uid The UID of the object to fetch.
226: *
227: * @return array An array of mail addresses.
228: *
229: * @throws Horde_Kolab_Server_Exception
230: */
231: static public function getGroupAddresses($server, $uid)
232: {
233: $criteria = array('AND' =>
234: array(
235: array('field' => self::ATTRIBUTE_OC,
236: 'op' => '=',
237: 'test' => self::OBJECTCLASS_GROUPOFNAMES),
238: array('field' => self::ATTRIBUTE_MEMBER,
239: 'op' => '=',
240: 'test' => $uid),
241: ),
242: );
243:
244: $data = self::attrsForSearch($server, $criteria, array(self::ATTRIBUTE_MAIL),
245: self::RESULT_MANY);
246:
247: if (empty($data)) {
248: return array();
249: }
250:
251: $mails = array();
252: foreach ($data as $element) {
253: if (isset($element[self::ATTRIBUTE_MAIL])) {
254: if (is_array($element[self::ATTRIBUTE_MAIL])) {
255: $mails = array_merge($mails, $element[self::ATTRIBUTE_MAIL]);
256: } else {
257: $mails[] = $element[self::ATTRIBUTE_MAIL];
258: }
259: }
260: }
261: return $mails;
262: }
263: }
264: