SQL1Compat   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
eloc 29
c 4
b 0
f 0
dl 0
loc 62
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 54 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\sqlauth\Auth\Source;
6
7
use SimpleSAML\Logger;
8
9
/**
10
 * @deprecated Use the SQL2 class and the new SQL2 configuration format instead.
11
 *
12
 * @package SimpleSAMLphp
13
 */
14
15
class SQL1Compat extends SQL2
16
{
17
    /**
18
     * Constructor for this authentication source.
19
     *
20
     * @param array $info  Information about this authentication source.
21
     * @param array $config  Configuration.
22
     */
23
    public function __construct(array $info, array $config)
24
    {
25
        Logger::warning(
26
            'The sqlauth:SQL and sqlauth:SQL1Compat authentication sources are deprecated. ' .
27
            'Please migrate to sqlauth:SQL2 with the new configuration format.',
28
        );
29
30
        /* Transform SQL (version 1) config to SQL2 config
31
         * Version 1 supported only one database, but multiple queries. The first query was defined
32
         * to be the "authentication query", all subsequent queries were "attribute queries".
33
         */
34
        $v2config = [
35
            'sqlauth:SQL2',
36
            'databases' => [
37
                'default' => [
38
                    'dsn' => $config['dsn'],
39
                    'username' => $config['username'],
40
                    'password' => $config['password'],
41
                ],
42
            ],
43
44
            'auth_queries' => [
45
                'default' => [
46
                    'database' => 'default',
47
                    'query' => is_array($config['query']) ? $config['query'][0] : $config['query'],
48
                ],
49
            ],
50
        ];
51
52
        if (array_key_exists('username_regex', $config)) {
53
            $v2config['auth_queries']['default']['username_regex'] = $config['username_regex'];
54
        }
55
56
        $numQueries = is_array($config['query']) ? count($config['query']) : 0;
57
        if ($numQueries > 1) {
58
            $v2config['attr_queries'] = [];
59
            for ($i = 1; $i < $numQueries; $i++) {
60
                $v2config['attr_queries']['query' . $i] = [
61
                    'database' => 'default',
62
                    'query' => $config['query'][$i],
63
                ];
64
            }
65
        }
66
67
        // Copy other config keys that are not specific to SQL1 (eg. core:login_links)
68
        foreach (array_keys($config) as $key) {
69
            if (in_array($key, ['dsn', 'username', 'password', 'query', 'username_regex'])) {
70
                continue;
71
            }
72
73
            $v2config[$key] = $config[$key];
74
        }
75
76
        parent::__construct($info, $v2config);
77
    }
78
}
79