Completed
Push — authenticator-refactor ( 16f104...61b037 )
by Simon
06:52
created

LogoutHandler::doLogOut()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 1
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security\MemberAuthenticator;
4
5
6
use SilverStripe\Control\Cookie;
7
use SilverStripe\Control\RequestHandler;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Security\Member;
10
use SilverStripe\Security\RememberLoginHash;
11
use SilverStripe\Security\Security;
12
13
/**
14
 * Class LogoutHandler handles logging out Members from their session and/or cookie.
15
 * The logout process destroys all traces of the member on the server (not the actual computer user
16
 * at the other end of the line, don't worry)
17
 *
18
 * @package SilverStripe\Security\MemberAuthenticator
19
 */
20
class LogoutHandler extends RequestHandler
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $url_handlers = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
        '' => 'logout'
27
    ];
28
29
    /**
30
     * @var array
31
     */
32
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
33
        'logout'
34
    ];
35
36
37
    /**
38
     * Log out form handler method
39
     *
40
     * This method is called when the user clicks on "logout" on the form
41
     * created when the parameter <i>$checkCurrentUser</i> of the
42
     * {@link __construct constructor} was set to TRUE and the user was
43
     * currently logged in.
44
     *
45
     * @return bool|Member
46
     */
47
    public function logout()
48
    {
49
        $member = Security::getCurrentUser();
50
51
        return $this->doLogOut($member);
52
    }
53
54
    /**
55
     *
56
     * @param Member $member
57
     * @return bool|Member Return a member if something goes wrong
58
     */
59
    public function doLogOut($member)
60
    {
61
        if ($member instanceof Member) {
62
            Session::clear('loggedInAs');
63
            if (Member::config()->get('login_marker_cookie')) {
64
                Cookie::set(Member::config()->get('login_marker_cookie'), null, 0);
65
            }
66
67
            Session::destroy();
68
69
            // Clears any potential previous hashes for this member
70
            RememberLoginHash::clear($member, Cookie::get('alc_device'));
71
72
            Cookie::set('alc_enc', null); // // Clear the Remember Me cookie
73
            Cookie::force_expiry('alc_enc');
74
            Cookie::set('alc_device', null);
75
            Cookie::force_expiry('alc_device');
76
77
            // Switch back to live in order to avoid infinite loops when
78
            // redirecting to the login screen (if this login screen is versioned)
79
            Session::clear('readingMode');
80
81
            // Remove the member from Security, for Security reasons
82
            Security::setCurrentUser(null);
83
        }
84
85
        return true;
86
    }
87
88
89
}