1: <?php
2: /**
3: * This interface represents a user from the user database.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_FreeBusy
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_FreeBusy
12: */
13:
14: /**
15: * This interface represents a user from the user database.
16: *
17: * Copyright 2010 Kolab Systems AG
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you did not
20: * receive this file, see
21: * http://www.horde.org/licenses/lgpl21.
22: *
23: * @category Kolab
24: * @package Kolab_FreeBusy
25: * @author Gunnar Wrobel <wrobel@pardus.de>
26: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
27: * @link http://pear.horde.org/index.php?package=Kolab_FreeBusy
28: */
29: abstract class Horde_Kolab_FreeBusy_UserDb_User_Kolab
30: {
31: /**
32: * The connection to the database.
33: *
34: * @var Horde_Kolab_Server_Composite
35: */
36: private $_db;
37:
38: /**
39: * The user representation.
40: *
41: * @var ???
42: */
43: private $_user;
44:
45: /**
46: * The user ID in the db.
47: *
48: * @var string
49: */
50: private $_guid;
51:
52: /**
53: * The representation of the server configuration.
54: *
55: * @var ???
56: */
57: private $_server;
58:
59: /**
60: * Constructor
61: *
62: * @param Horde_Kolab_Server_Composite $db The connection to the server.
63: */
64: public function __construct(Horde_Kolab_Server_Composite $db)
65: {
66: $this->_db = $db;
67: }
68:
69: //@todo
70: protected function getServer()
71: {
72: if ($this->_server === null) {
73: $this->_server = $this->_validate(
74: $this->_db->fetch(
75: sprintf('k=kolab,%s', $this->_db->getBaseUid()),
76: KOLAB_OBJECT_SERVER
77: )
78: );
79: }
80: return $this->_server;
81: }
82:
83: protected function getUserDbUser()
84: {
85: if ($this->_user === null) {
86: $this->_user = $this->fetchUserDbUser();
87: }
88: return $this->_user;
89: }
90:
91: abstract protected function fetchUserDbUser();
92:
93: /**
94: * Fetch the specified user from the user database.
95: *
96: * @param string $user The user ID.
97: *
98: * @return Horde_Kolab_Server_Object_Kolab_User The user object.
99: */
100: protected function fetchUser($user)
101: {
102: return $this->_fetch(
103: $this->_db->search->searchGuidForUidOrMail($user), $user
104: );
105: }
106:
107: /**
108: * Fetch the specified owner from the user database.
109: *
110: * @param string $owner The owner ID.
111: *
112: * @return Horde_Kolab_Server_Object_Kolab_User The user object.
113: */
114: protected function fetchOwner($owner)
115: {
116: return $this->_fetch(
117: $this->_db->search->searchGuidForUidOrMailOrAlias(
118: $owner
119: ),
120: $owner
121: );
122: }
123:
124: /**
125: * Fetch the specified primary ID from the user database.
126: *
127: * @param string $mail The primary mail address.
128: *
129: * @return Horde_Kolab_Server_Object_Kolab_User The user object.
130: */
131: protected function fetchUserByPrimaryId($mail)
132: {
133: return $this->_fetch(
134: $this->_db->search->searchGuidForMail($mail), $mail
135: );
136: }
137:
138: /**
139: * Fetch the specified global UID from the user database.
140: *
141: * @param string $guid The global UID.
142: * @param string $search The search ID.
143: *
144: * @return Horde_Kolab_Server_Object_Kolab_User The user object.
145: */
146: private function _fetch($guid, $search)
147: {
148: try {
149: if ($guid === false) {
150: throw new Horde_Kolab_FreeBusy_Exception(sprintf('Unknown user "%s"!', $search));
151: }
152: $this->_guid = $guid;
153: return $this->_db->objects->fetch(
154: $this->_guid,
155: 'Horde_Kolab_Server_Object_Kolab_User'
156: );
157: } catch (Horde_Kolab_Server_Exception $e) {
158: throw new Horde_Kolab_FreeBusy_Exception($e);
159: }
160: }
161:
162: /**
163: * Finds out if the provided password is valid for this user.
164: *
165: * @param string $pass The password.
166: *
167: * @return boolean Whether or not the password was correct.
168: */
169: protected function authenticate($pass)
170: {
171: try {
172: $this->_db->server->connectGuid($this->_guid, $pass);
173: } catch (Horde_Kolab_Server_Exception_Bindfailed $e) {
174: return false;
175: }
176: return true;
177: }
178:
179: /**
180: * Return the primary id of the user accessing the system.
181: *
182: * @return string The primary id.
183: */
184: public function getGuid()
185: {
186: $this->getUserDbUser();
187: return $this->_guid;
188: }
189:
190: /**
191: * Return the primary id of the user accessing the system.
192: *
193: * @return string The primary id.
194: */
195: public function getPrimaryId()
196: {
197: return $this->getMail();
198: }
199:
200: /**
201: * Return the mail address of the resource owner.
202: *
203: * @return string The mail address.
204: */
205: public function getMail()
206: {
207: return $this->_validate($this->getUserDbUser()->getSingle('mail'));
208: }
209:
210: /**
211: * Return the primary domain of the user accessing the system.
212: *
213: * @return string The primary domain.
214: */
215: public function getDomain()
216: {
217: $mail = $this->getMail();
218: $idx = strpos($mail, '@');
219: if ($idx !== false) {
220: return substr($mail, $idx + 1);
221: } else {
222: return '';
223: }
224: }
225:
226: /**
227: * Return the name of the resource owner.
228: *
229: * @return string The name of the owner.
230: */
231: public function getName()
232: {
233: return $this->_validate($this->getUserDbUser()->getSingle('cn'));
234: }
235:
236: /**
237: * Indicates the correct remote server for the resource owner.
238: *
239: * @param string $type The requested resource type.
240: *
241: * @return string The server name.
242: */
243: //@todo
244: public function getRemoteServer($type = '')
245: {
246: return $this->_validate($this->getUserDbUser()->getServer('freebusy'));
247: }
248:
249: /**
250: * Return how many days into the past the free/busy data should be
251: * calculated for this owner.
252: *
253: * @return int The number of days.
254: */
255: //@todo
256: public function getFreeBusyPast()
257: {
258: return $this->_validate($this->getServer()->get(KOLAB_ATTR_FBPAST));
259: }
260:
261: /**
262: * Return how many days into the future the free/busy data should be
263: * calculated for this owner.
264: *
265: * @return int The number of days.
266: */
267: //@todo
268: public function getFreeBusyFuture()
269: {
270: return $this->_validate($this->getUserDbUser()->get(KOLAB_ATTR_FBFUTURE));
271: }
272:
273: /**
274: * Return the groups this user is member of.
275: *
276: * @return array The groups for this user.
277: */
278: public function getGroups()
279: {
280: return $this->_validate($this->getUserDbUser()->getGroupAddresses());
281: }
282:
283: protected function _validate($result)
284: {
285: if (is_a($result, 'PEAR_Error')) {
286: throw new Horde_Kolab_FreeBusy_Exception($result->getMessage(), $result->getCode());
287: }
288: return $result;
289: }
290: }
291: