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