1: <?php
2: /**
3: * The composite class chains other drivers together to change and a user's
4: * password stored on various backends.
5: *
6: * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.php.
10: *
11: * @author Max Kalika <max@horde.org>
12: * @package Passwd
13: */
14: class Passwd_Driver_Composite extends Passwd_Driver
15: {
16: /**
17: * Hash of instantiated drivers.
18: *
19: * @var array
20: */
21: protected $_drivers = array();
22:
23: /**
24: * State of the loaded drivers.
25: *
26: * @var boolean
27: */
28: protected $_loaded = false;
29:
30: /**
31: * Constructor.
32: *
33: * @param array $params A hash containing chained drivers and their
34: * parameters.
35: *
36: * @throws Passwd_Exception
37: */
38: public function __construct($params = array())
39: {
40: if (!isset($params['drivers']) || !is_array($params['drivers'])) {
41: throw new Passwd_Exception(_("Required 'drivers' is misconfigured in Composite configuration."));
42: }
43:
44: parent::__construct($params);
45: }
46:
47: /**
48: * Instantiate configured drivers.
49: */
50: protected function _loadDrivers()
51: {
52: if ($this->_loaded) {
53: return;
54: }
55:
56: foreach ($this->_params['drivers'] as $key => $settings) {
57: if (isset($this->_drivers[$key])) {
58: continue;
59: }
60: $settings['is_subdriver'] = true;
61: try {
62: $res = $GLOBALS['injector']
63: ->getInstance('Passwd_Factory_Driver')
64: ->create($key, $settings);
65: } catch (Passwd_Exception $e) {
66: throw new Passwd_Exception(
67: sprintf(_("%s: unable to load sub driver: %s"),
68: $key, $e->getMessage()));
69: }
70:
71: $this->_drivers[$key] = $res;
72: }
73:
74: $this->_loaded = true;
75: }
76:
77: /**
78: * Changes the user's password.
79: *
80: * @param string $username The user for which to change the password.
81: * @param string $old_password The old (current) user password.
82: * @param string $new_password The new user password to set.
83: *
84: * @throws Passwd_Exception
85: */
86: public function changePassword($username, $old_password, $new_password)
87: {
88: $this->_loadDrivers();
89:
90: foreach ($this->_drivers as $key => $driver) {
91: if (isset($driver->_params['be_username'])) {
92: $user = $driver->_params['be_username'];
93: } else {
94: $user = $username;
95: }
96: try {
97: $driver->changePassword($user, $old_password, $new_password);
98: } catch (Passwd_Exception $e) {
99: throw new Passwd_Exception(
100: sprintf(_("Failure in changing password for %s: %s"),
101: $this->_params['drivers'][$key]['name'],
102: $e->getMessage()));
103: }
104: }
105: }
106: }
107: