Completed
Push — master ( 20efb0...a2cc06 )
by Hamish
29s
created

Authenticator   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 194
rs 10
wmc 22
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 2 1
A get_login_form() 0 2 1
A get_cms_login_form() 0 2 1
A supports_cms() 0 3 1
A get_name() 0 2 1
A register() 0 3 1
B register_authenticator() 0 19 5
A unregister() 0 3 1
A unregister_authenticator() 0 7 3
A is_registered() 0 3 1
A get_authenticators() 0 9 2
A set_default_authenticator() 0 5 1
A get_default_authenticator() 0 3 1
A on_register() 0 3 1
A on_unregister() 0 3 1
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use Object;
6
use Form;
7
use Controller;
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
 * @package framework
17
 * @subpackage security
18
 */
19
abstract class Authenticator extends Object {
20
21
	/**
22
	 * This variable holds all authenticators that should be used
23
	 *
24
	 * @var array
25
	 */
26
	private static $authenticators = array('SilverStripe\\Security\\MemberAuthenticator');
27
28
	/**
29
	 * Used to influence the order of authenticators on the login-screen
30
	 * (default shows first).
31
	 *
32
	 * @var string
33
	 */
34
	private static $default_authenticator = 'SilverStripe\\Security\\MemberAuthenticator';
35
36
37
	/**
38
	 * Method to authenticate an user
39
	 *
40
	 * @param array $RAW_data Raw data to authenticate the user
41
	 * @param Form $form Optional: If passed, better error messages can be
42
	 *                             produced by using
43
	 *                             {@link Form::sessionMessage()}
44
	 * @return bool|Member Returns FALSE if authentication fails, otherwise
45
	 *                     the member object
46
	 */
47
	public static function authenticate($RAW_data, Form $form = null) {
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
	 * Method that creates the re-authentication form for the in-CMS view
63
	 *
64
	 * @param Controller $controller
65
	 */
66
	public static function get_cms_login_form(Controller $controller) {
67
	}
68
69
	/**
70
	 * Determine if this authenticator supports in-cms reauthentication
71
	 *
72
	 * @return bool
73
	 */
74
	public static function supports_cms() {
75
		return false;
76
	}
77
78
79
	/**
80
	 * Get the name of the authentication method
81
	 *
82
	 * @return string Returns the name of the authentication method.
83
	 */
84
	public static function get_name() {
85
	}
86
87
	public static function register($authenticator) {
88
		self::register_authenticator($authenticator);
89
	}
90
91
92
	/**
93
	 * Register a new authenticator
94
	 *
95
	 * The new authenticator has to exist and to be derived from the
96
	 * {@link Authenticator}.
97
	 * Every authenticator can be registered only once.
98
	 *
99
	 * @param string $authenticator Name of the authenticator class to
100
	 *                              register
101
	 * @return bool Returns TRUE on success, FALSE otherwise.
102
	 */
103
	public static function register_authenticator($authenticator) {
104
		$authenticator = trim($authenticator);
105
106
		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...
107
			return false;
108
109
		if(is_subclass_of($authenticator, 'SilverStripe\\Security\\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...
110
			return false;
111
112
		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...
113
			if(call_user_func(array($authenticator, 'on_register')) === true) {
114
				array_push(self::$authenticators, $authenticator);
115
			} else {
116
				return false;
117
			}
118
		}
119
120
		return true;
121
	}
122
123
	public static function unregister($authenticator) {
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
		if(call_user_func(array($authenticator, 'on_unregister')) === true) {
135
			if(in_array($authenticator, self::$authenticators)) {
136
				unset(self::$authenticators[array_search($authenticator, self::$authenticators)]);
137
			}
138
		}
139
	}
140
141
142
	/**
143
	 * Check if a given authenticator is registered
144
	 *
145
	 * @param string $authenticator Name of the authenticator class to check
146
	 * @return bool Returns TRUE if the authenticator is registered, FALSE
147
	 *              otherwise.
148
	 */
149
	public static function is_registered($authenticator) {
150
		return in_array($authenticator, self::$authenticators);
151
	}
152
153
154
	/**
155
	 * Get all registered authenticators
156
	 *
157
	 * @return array Returns an array with the class names of all registered
158
	 *               authenticators.
159
	 */
160
	public static function get_authenticators() {
161
		// put default authenticator first (mainly for tab-order on loginform)
162
		if($key = array_search(self::$default_authenticator,self::$authenticators)) {
163
			unset(self::$authenticators[$key]);
164
			array_unshift(self::$authenticators, self::$default_authenticator);
165
		}
166
167
		return self::$authenticators;
168
	}
169
170
	/**
171
	 * Set a default authenticator (shows first in tabs)
172
	 *
173
	 * @param string
174
	 */
175
	public static function set_default_authenticator($authenticator) {
176
		self::$default_authenticator = $authenticator;
177
178
179
	}
180
181
	/**
182
	 * @return string
183
	 */
184
	public static function get_default_authenticator() {
185
		return self::$default_authenticator;
186
	}
187
188
189
	/**
190
	 * Callback function that is called when the authenticator is registered
191
	 *
192
	 * Use this method for initialization of a newly registered authenticator.
193
	 * Just overload this method and it will be called when the authenticator
194
	 * is registered.
195
	 * <b>If the method returns FALSE, the authenticator won't be
196
	 * registered!</b>
197
	 *
198
	 * @return bool Returns TRUE on success, FALSE otherwise.
199
	 */
200
	protected static function on_register() {
201
		return true;
202
	}
203
204
	/**
205
	 * Callback function that is called when an authenticator is removed.
206
	 *
207
	 * @return bool
208
	 */
209
	protected static function on_unregister() {
210
		return true;
211
	}
212
}
213
214