Completed
Pull Request — master (#6927)
by Damian
11:37 queued 03:26
created

Authenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Forms\Form;
10
11
/**
12
 * Abstract base class for an authentication method
13
 *
14
 * This class is used as a base class for the different authentication
15
 * methods like {@link MemberAuthenticator} or {@link OpenIDAuthenticator}.
16
 *
17
 * @author Markus Lanthaler <[email protected]>
18
 */
19
abstract class Authenticator
20
{
21
    use Injectable;
22
    use Configurable;
23
    use Extensible;
24
25
    public function __construct()
26
    {
27
        $this->constructExtensions();
28
    }
29
30
    /**
31
     * This variable holds all authenticators that should be used
32
     *
33
     * @var array
34
     */
35
    private static $authenticators = [];
36
37
    /**
38
     * Used to influence the order of authenticators on the login-screen
39
     * (default shows first).
40
     *
41
     * @var string
42
     */
43
    private static $default_authenticator = MemberAuthenticator::class;
44
45
46
    /**
47
     * Method to authenticate an user
48
     *
49
     * @param array $RAW_data Raw data to authenticate the user
50
     * @param Form $form Optional: If passed, better error messages can be
51
     *                             produced by using
52
     *                             {@link Form::sessionMessage()}
53
     * @return bool|Member Returns FALSE if authentication fails, otherwise
54
     *                     the member object
55
     */
56
    public static function authenticate($RAW_data, Form $form = null)
57
    {
58
    }
59
60
    /**
61
     * Method that creates the login form for this authentication method
62
     *
63
     * @param Controller $controller The parent controller, necessary to create the
64
     *                   appropriate form action tag
65
     * @return Form Returns the login form to use with this authentication
66
     *              method
67
     */
68
    public static function get_login_form(Controller $controller)
69
    {
70
    }
71
72
    /**
73
     * Method that creates the re-authentication form for the in-CMS view
74
     *
75
     * @param Controller $controller
76
     */
77
    public static function get_cms_login_form(Controller $controller)
78
    {
79
    }
80
81
    /**
82
     * Determine if this authenticator supports in-cms reauthentication
83
     *
84
     * @return bool
85
     */
86
    public static function supports_cms()
87
    {
88
        return false;
89
    }
90
91
    /**
92
     * Check if a given authenticator is registered
93
     *
94
     * @param string $authenticator Name of the authenticator class to check
95
     * @return bool Returns TRUE if the authenticator is registered, FALSE
96
     *              otherwise.
97
     */
98
    public static function is_registered($authenticator)
99
    {
100
        $authenticators = self::config()->get('authenticators');
101
        if (count($authenticators) === 0) {
102
            $authenticators = [self::config()->get('default_authenticator')];
103
        }
104
105
        return in_array($authenticator, $authenticators, true);
106
    }
107
108
109
    /**
110
     * Get all registered authenticators
111
     *
112
     * @return array Returns an array with the class names of all registered
113
     *               authenticators.
114
     */
115
    public static function get_authenticators()
116
    {
117
        $authenticators = self::config()->get('authenticators');
118
        $default = self::config()->get('default_authenticator');
119
120
        if (count($authenticators) === 0) {
121
            $authenticators = [$default];
122
        }
123
        // put default authenticator first (mainly for tab-order on loginform)
124
        // But only if there's no other authenticator
125
        if (($key = array_search($default, $authenticators, true)) && count($authenticators) > 1) {
126
            unset($authenticators[$key]);
127
            array_unshift($authenticators, $default);
128
        }
129
130
        return $authenticators;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public static function get_default_authenticator()
137
    {
138
        return self::config()->get('default_authenticator');
139
    }
140
}
141