Completed
Pull Request — master (#84)
by Robbie
08:08
created

SAMLSecurityExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D onBeforeSecurityLogin() 0 39 10
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Authenticators;
4
5
use SilverStripe\Control\Session;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Security\Authenticator;
9
use SilverStripe\Security\Member;
10
11
/**
12
 * Class SAMLSecurityExtension
13
 *
14
 * Extensions to the {@link Security} controller to support {@link SAMLAuthenticator}
15
 *
16
 * @package activedirectory
17
 */
18
class SAMLSecurityExtension extends Extension
19
{
20
    /**
21
     * Will redirect the user directly to the IdP login endpoint if:
22
     *
23
     * 1) the 'SAMLAuthenticator' is the default authenticator
24
     * 2) there isn't a GET param showloginform set to 1
25
     * 3) the member is not currently logged in
26
     * 4) there are no form messages (errors or notices)
27
     *
28
     * @return void
29
     */
30
    public function onBeforeSecurityLogin()
31
    {
32
        if (Authenticator::get_default_authenticator() != 'SilverStripe\\ActiveDirectory\\Authenticators\\SAMLAuthenticator') {
33
            return;
34
        }
35
36
        // by going to the URL Security/login?showloginform=1 we bypass the auto sign on
37
        if ($this->owner->request->getVar('showloginform') == 1) {
38
            return;
39
        }
40
41
        // if member is already logged in, don't auto-sign-on, this is most likely because
42
        // of unsufficient permissions.
43
        $member = Member::currentUser();
44
        if ($member && $member->exists()) {
45
            return;
46
        }
47
48
        // if there are form messages, don't auto-sign-on, this is most likely because of
49
        // login errors / failures or other notices.
50
        if (Session::get('FormInfo')) {
51
            // since FormInfo can be a "nulled" array, we have to check
52
            foreach (Session::get('FormInfo') as $form => $info) {
53
                foreach ($info as $name => $value) {
54
                    if ($value !== null) {
55
                        return;
56
                    }
57
                }
58
            }
59
        }
60
61
        $backURL = Session::get('BackURL');
62
        if ($this->owner->request->getVar('BackURL')) {
63
            $backURL = $this->owner->request->getVar('BackURL');
64
        }
65
66
        $authenticator = Injector::inst()->create('SilverStripe\\ActiveDirectory\\Authenticators\\SAMLAuthenticator');
67
        $authenticator->authenticate(['BackURL' => $backURL]);
68
    }
69
}
70