File: Composite.php

package info (click to toggle)
php-horde-passwd 5.0.7-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 3,412 kB
  • sloc: php: 2,017; xml: 1,136; javascript: 29; makefile: 18; sh: 3
file content (89 lines) | stat: -rw-r--r-- 2,563 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
 * Copyright 2003-2017 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.horde.org/licenses/gpl.
 *
 * @category  Horde
 * @copyright 2003-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   Passwd
 */

/**
 * The composite class chains other drivers together to change and a user's
 * password stored on various backends.
 *
 * @author    Max Kalika <max@horde.org>
 * @category  Horde
 * @copyright 2003-2017 Horde LLC
 * @license   http://www.horde.org/licenses/gpl GPL
 * @package   Passwd
 */
class Passwd_Driver_Composite extends Passwd_Driver
{
    /**
     * Hash of instantiated drivers.
     *
     * @var array
     */
    protected $_drivers = null;

    /**
     * @param array $params  Driver parameters:
     *   - drivers: (array) Array of Passwd_Driver objects.
     *
     * @throws Passwd_Exception
     */
    public function __construct(array $params = array())
    {
        if (!isset($params['drivers']) || !is_array($params['drivers'])) {
            throw new Passwd_Exception(_("Required 'drivers' is misconfigured in Composite configuration."));
        }

        parent::__construct($params);
    }

    /**
     * Instantiate configured drivers.
     */
    protected function _loadDrivers()
    {
        if (!is_null($this->_drivers)) {
            return;
        }

        $driver = $GLOBALS['injector']->getInstance('Passwd_Factory_Driver');

        foreach ($this->_params['drivers'] as $key => $val) {
            if (!isset($this->_drivers[$key])) {
                try {
                    $res = $driver->create($key, array_merge($val, array(
                        'is_subdriver' => true
                    )));
                } catch (Passwd_Exception $e) {
                    throw new Passwd_Exception(sprintf(_("%s: unable to load sub driver: %s"), $key, $e->getMessage()));
                }

                $this->_drivers[$key] = $res;
            }
        }
    }

    /**
     */
    protected function _changePassword($user, $oldpass, $newpass)
    {
        $this->_loadDrivers();

        foreach ($this->_drivers as $key => $driver) {
            try {
                $driver->changePassword($user, $oldpass,  $newpass);
            } catch (Passwd_Exception $e) {
                throw new Passwd_Exception(sprintf(_("Failure in changing password for %s: %s"), $this->_params['drivers'][$key]['name'], $e->getMessage()));
            }
        }
    }

}