Completed
Push — master ( e2b0c5...f862ce )
by Sam
08:22
created

Authenticator   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 3 1
A get_login_form() 0 3 1
A get_cms_login_form() 0 3 1
A supports_cms() 0 4 1
A register() 0 4 1
B register_authenticator() 0 22 5
A unregister() 0 4 1
A unregister_authenticator() 0 8 3
A is_registered() 0 4 1
A get_authenticators() 0 10 2
A set_default_authenticator() 0 4 1
A get_default_authenticator() 0 4 1
A on_register() 0 4 1
A on_unregister() 0 4 1
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use SilverStripe\Core\Object;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\Form;
8
9
/**
10
 * Abstract base class for an authentication method
11
 *
12
 * This class is used as a base class for the different authentication
13
 * methods like {@link MemberAuthenticator} or {@link OpenIDAuthenticator}.
14
 *
15
 * @author Markus Lanthaler <[email protected]>
16
 */
17
abstract class Authenticator extends Object
18
{
19
20
    /**
21
     * This variable holds all authenticators that should be used
22
     *
23
     * @var array
24
     */
25
    private static $authenticators = array(MemberAuthenticator::class);
26
27
    /**
28
     * Used to influence the order of authenticators on the login-screen
29
     * (default shows first).
30
     *
31
     * @var string
32
     */
33
    private static $default_authenticator = MemberAuthenticator::class;
34
35
36
    /**
37
     * Method to authenticate an user
38
     *
39
     * @param array $RAW_data Raw data to authenticate the user
40
     * @param Form $form Optional: If passed, better error messages can be
41
     *                             produced by using
42
     *                             {@link Form::sessionMessage()}
43
     * @return bool|Member Returns FALSE if authentication fails, otherwise
44
     *                     the member object
45
     */
46
    public static function authenticate($RAW_data, Form $form = null)
47
    {
48
    }
49
50
    /**
51
     * Method that creates the login form for this authentication method
52
     *
53
     * @param Controller $controller The parent controller, necessary to create the
54
     *                   appropriate form action tag
55
     * @return Form Returns the login form to use with this authentication
56
     *              method
57
     */
58
    public static function get_login_form(Controller $controller)
59
    {
60
    }
61
62
    /**
63
     * Method that creates the re-authentication form for the in-CMS view
64
     *
65
     * @param Controller $controller
66
     */
67
    public static function get_cms_login_form(Controller $controller)
68
    {
69
    }
70
71
    /**
72
     * Determine if this authenticator supports in-cms reauthentication
73
     *
74
     * @return bool
75
     */
76
    public static function supports_cms()
77
    {
78
        return false;
79
    }
80
81
82
    public static function register($authenticator)
83
    {
84
        self::register_authenticator($authenticator);
85
    }
86
87
88
    /**
89
     * Register a new authenticator
90
     *
91
     * The new authenticator has to exist and to be derived from the
92
     * {@link Authenticator}.
93
     * Every authenticator can be registered only once.
94
     *
95
     * @param string $authenticator Name of the authenticator class to
96
     *                              register
97
     * @return bool Returns TRUE on success, FALSE otherwise.
98
     */
99
    public static function register_authenticator($authenticator)
100
    {
101
        $authenticator = trim($authenticator);
102
103
        if (class_exists($authenticator) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
104
            return false;
105
        }
106
107
        if (is_subclass_of($authenticator, self::class) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
108
            return false;
109
        }
110
111
        if (in_array($authenticator, self::$authenticators) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
112
            if (call_user_func(array($authenticator, 'on_register')) === true) {
113
                array_push(self::$authenticators, $authenticator);
114
            } else {
115
                return false;
116
            }
117
        }
118
119
        return true;
120
    }
121
122
    public static function unregister($authenticator)
123
    {
124
        self::unregister_authenticator($authenticator);
125
    }
126
127
    /**
128
     * Remove a previously registered authenticator
129
     *
130
     * @param string $authenticator Name of the authenticator class to register
131
     * @return bool Returns TRUE on success, FALSE otherwise.
132
     */
133
    public static function unregister_authenticator($authenticator)
134
    {
135
        if (call_user_func(array($authenticator, 'on_unregister')) === true) {
136
            if (in_array($authenticator, self::$authenticators)) {
137
                unset(self::$authenticators[array_search($authenticator, self::$authenticators)]);
138
            }
139
        }
140
    }
141
142
143
    /**
144
     * Check if a given authenticator is registered
145
     *
146
     * @param string $authenticator Name of the authenticator class to check
147
     * @return bool Returns TRUE if the authenticator is registered, FALSE
148
     *              otherwise.
149
     */
150
    public static function is_registered($authenticator)
151
    {
152
        return in_array($authenticator, self::$authenticators);
153
    }
154
155
156
    /**
157
     * Get all registered authenticators
158
     *
159
     * @return array Returns an array with the class names of all registered
160
     *               authenticators.
161
     */
162
    public static function get_authenticators()
163
    {
164
        // put default authenticator first (mainly for tab-order on loginform)
165
        if ($key = array_search(self::$default_authenticator, self::$authenticators)) {
166
            unset(self::$authenticators[$key]);
167
            array_unshift(self::$authenticators, self::$default_authenticator);
168
        }
169
170
        return self::$authenticators;
171
    }
172
173
    /**
174
     * Set a default authenticator (shows first in tabs)
175
     *
176
     * @param string
177
     */
178
    public static function set_default_authenticator($authenticator)
179
    {
180
        self::$default_authenticator = $authenticator;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public static function get_default_authenticator()
187
    {
188
        return self::$default_authenticator;
189
    }
190
191
192
    /**
193
     * Callback function that is called when the authenticator is registered
194
     *
195
     * Use this method for initialization of a newly registered authenticator.
196
     * Just overload this method and it will be called when the authenticator
197
     * is registered.
198
     * <b>If the method returns FALSE, the authenticator won't be
199
     * registered!</b>
200
     *
201
     * @return bool Returns TRUE on success, FALSE otherwise.
202
     */
203
    protected static function on_register()
204
    {
205
        return true;
206
    }
207
208
    /**
209
     * Callback function that is called when an authenticator is removed.
210
     *
211
     * @return bool
212
     */
213
    protected static function on_unregister()
214
    {
215
        return true;
216
    }
217
}
218