|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Security; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Control\RequestHandler; |
|
7
|
|
|
use SilverStripe\Control\Session; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Forms\Form; |
|
10
|
|
|
use SilverStripe\Forms\FormAction; |
|
11
|
|
|
use SilverStripe\Forms\HiddenField; |
|
12
|
|
|
use SilverStripe\Forms\Validator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Log out form to display to users who arrive at 'Security/logout' without a |
|
16
|
|
|
* CSRF token. It's preferable to link to {@link Security::logout_url()} |
|
17
|
|
|
* directly - we only use a form so that we can preserve the "BackURL" if set |
|
18
|
|
|
*/ |
|
19
|
|
|
class LogoutForm extends Form |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
RequestHandler $controller = null, |
|
26
|
|
|
$name = self::DEFAULT_NAME, |
|
27
|
|
|
FieldList $fields = null, |
|
28
|
|
|
FieldList $actions = null, |
|
29
|
|
|
Validator $validator = null |
|
30
|
|
|
) { |
|
31
|
|
|
$this->setController($controller); |
|
32
|
|
|
|
|
33
|
|
|
if (!$fields) { |
|
34
|
|
|
$fields = $this->getFormFields(); |
|
35
|
|
|
} |
|
36
|
|
|
if (!$actions) { |
|
37
|
|
|
$actions = $this->getFormActions(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct($controller, $name, $fields, $actions); |
|
41
|
|
|
|
|
42
|
|
|
$this->setFormAction(Security::logout_url()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Build the FieldList for the logout form |
|
47
|
|
|
* |
|
48
|
|
|
* @return FieldList |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getFormFields() |
|
51
|
|
|
{ |
|
52
|
|
|
$fields = FieldList::create(); |
|
53
|
|
|
|
|
54
|
|
|
$controller = $this->getController(); |
|
55
|
|
|
$backURL = $controller->getBackURL() |
|
56
|
|
|
?: $controller->getReturnReferer(); |
|
57
|
|
|
|
|
58
|
|
|
// Protect against infinite redirection back to the logout URL after logging out |
|
59
|
|
|
if (!$backURL || Director::makeRelative($backURL) === $controller->getRequest()->getURL()) { |
|
|
|
|
|
|
60
|
|
|
$backURL = Director::baseURL(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$fields->push(HiddenField::create('BackURL', 'BackURL', $backURL)); |
|
64
|
|
|
|
|
65
|
|
|
return $fields; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Build default logout form action FieldList |
|
70
|
|
|
* |
|
71
|
|
|
* @return FieldList |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getFormActions() |
|
74
|
|
|
{ |
|
75
|
|
|
$actions = FieldList::create( |
|
76
|
|
|
FormAction::create('doLogout', _t('SilverStripe\\Security\\Member.BUTTONLOGOUT', "Log out")) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
return $actions; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: