Multi Adapter

This adapter lets you authenticate off more than one adapter. Authentication will take place on each adapter in the order they where added until 1) authentication returns true, or 2) there are no more adapters.

Basic example

 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
<?php
use Vespula\Auth\Auth;
use Vespula\Auth\Session\Session;
use Vespula\Auth\Adapter\Text;
use Vespula\Auth\Adapter\Sql;
use Vespula\Auth\Adapter\Multi;

$passwordfile = '/path/to/.htpasswd';
$textAdapter = new Text($passwordfile);

$textAdapter->setUserdata([
    'juser'=>[
        'fullname'=>'Joe User',
        'email'=>'juser@example.com'
    ]
]);

$dsn = 'mysql:dbname=mydatabase;host=localhost';
$pdo = new \PDO($dsn, 'dbuser', '********');

// $cols array must have a 'username' and 'password' element. You can use an alias if needed. See below.
// This data (except username and password) will populate the `getUserdata()` array
$cols = [
    'username',
    'password',
    'fullname',
    'email'
];
$from = 'user';
$where = 'active=1'; // optional

$sqlAdapter = new Sql($pdo, $from, $cols, $where);

$session = new Session();

$multiAdapter = new Multi();
$multiAdapter->addAdapter($textAdapter);
$multiAdapter->addAdapter($sqlAdapter);

$auth = new Auth($multiAdapter, $session);

$credentials = [
    'username'=>'juser',
    'password'=>'******'
];

// authenticate against the text adapter first, then the sql adapter.
$auth->login($credentials);

if ($auth->isValid()) {
    //...
}

Other methods

If a user is valid, you can get the adapter for that user by userid.

1
2
3
4
5
6
<?php
$multiAdpater = $auth->getAdapter();

$userAdapter = $multiAdapter->getUserAdapter('juser');

// Now, you can access the public methods of the adapter for that user, if needed.