1: <?php
2: /**
3: * The Horde_Auth_Composite class provides a way to combine two separate
4: * drivers for admin vs. authentication purposes.
5: *
6: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you did
9: * not receive this file, http://www.horde.org/licenses/lgpl21
10: *
11: * @author Chuck Hagenbuch <chuck@horde.org>
12: * @category Horde
13: * @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
14: * @package Auth
15: */
16: class Horde_Auth_Composite extends Horde_Auth_Base
17: {
18: /**
19: * Constructor.
20: *
21: * @param array $params Required parameters:
22: * <pre>
23: * 'admin_driver' - (Horde_Auth_Base) The admin driver.
24: * 'auth_driver' - (Horde_Auth_Base) The auth driver.
25: * </pre>
26: *
27: * @throws InvalidArgumentException
28: */
29: public function __construct(array $params = array())
30: {
31: foreach (array('admin_driver', 'auth_driver') as $val) {
32: if (!isset($params[$val])) {
33: throw new InvalidArgumentException('Missing ' . $val . ' parameter.');
34: }
35: }
36:
37: parent::__construct($params);
38: }
39:
40: /**
41: * Find out if a set of login credentials are valid.
42: *
43: * @param string $userId The userId to check.
44: * @param array $credentials The credentials to use.
45: *
46: * @throws Horde_Auth_Exception
47: */
48: protected function _authenticate($userId, $credentials)
49: {
50: if (!$this->_params['auth_driver']->authenticate($userId, $credentials)) {
51: throw new Horde_Auth_Exception($this->_params['auth_driver']->getError(true), $this->_params['auth_driver']->getError());
52: }
53: }
54:
55: /**
56: * Query the current Auth object to find out if it supports the given
57: * capability.
58: *
59: * @param string $capability The capability to test for.
60: *
61: * @return boolean Whether or not the capability is supported.
62: */
63: public function hasCapability($capability)
64: {
65: try {
66: return $this->_params['admin_driver']->hasCapability($capability);
67: } catch (Horde_Auth_Exception $e) {
68: return false;
69: }
70: }
71:
72: /**
73: * Automatic authentication: Find out if the client matches an allowed IP
74: * block.
75: *
76: * @return boolean Whether or not the client is allowed.
77: */
78: public function transparent()
79: {
80: try {
81: return $this->_params['auth_driver']->transparent();
82: } catch (Horde_Auth_Exception $e) {
83: return false;
84: }
85: }
86:
87: /**
88: * Add a set of authentication credentials.
89: *
90: * @param string $userId The userId to add.
91: * @param array $credentials The credentials to use.
92: *
93: * @throws Horde_Auth_Exception
94: */
95: public function addUser($userId, $credentials)
96: {
97: $this->_params['admin_driver']->addUser($userId, $credentials);
98: }
99:
100: /**
101: * Update a set of authentication credentials.
102: *
103: * @param string $oldID The old userId.
104: * @param string $newID The new userId.
105: * @param array $credentials The new credentials
106: *
107: * @throws Horde_Auth_Exception
108: */
109: public function updateUser($oldID, $newID, $credentials)
110: {
111: $this->_params['admin_driver']->updateUser($oldID, $newID, $credentials);
112: }
113:
114: /**
115: * Reset a user's password. Used for example when the user does not
116: * remember the existing password.
117: *
118: * @param string $userId The user id for which to reset the password.
119: *
120: * @return string The new password on success.
121: * @throws Horde_Auth_Exception
122: */
123: public function resetPassword($userId)
124: {
125: return $this->_params['admin_driver']->resetPassword($userId);
126: }
127:
128: /**
129: * Delete a set of authentication credentials.
130: *
131: * @param string $userId The userId to delete.
132: *
133: * @throws Horde_Auth_Exception
134: */
135: public function removeUser($userId)
136: {
137: $this->_params['admin_driver']->removeUser($userId);
138: }
139:
140: /**
141: * List all users in the system.
142: *
143: * @return array The array of userIds.
144: * @throws Horde_Auth_Exception
145: */
146: public function listUsers($sort = false)
147: {
148: return $this->_params['admin_driver']->listUsers($sort);
149: }
150:
151: /**
152: * Checks if a userId exists in the system.
153: *
154: * @param string $userId User ID to check
155: *
156: * @return boolean Whether or not the userId already exists.
157: */
158: public function exists($userId)
159: {
160: try {
161: return $this->_params['admin_driver']->exists($userId);
162: } catch (Horde_Auth_Exception $e) {
163: return false;
164: }
165: }
166:
167: }
168: