Completed
Push — master ( 3c190c...6d5f13 )
by Robbie
08:21
created

SAMLSecurityExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
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
 * Class SAMLSecurityExtension
4
 *
5
 * Extensions to the {@link Security} controller to support {@link SAMLAuthenticator}
6
 */
7
class SAMLSecurityExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * Will redirect the user directly to the IdP login endpoint if:
11
     *
12
     * 1) the 'SAMLAuthenticator' is the default authenticator
13
     * 2) there isn't a GET param showloginform set to 1
14
     * 3) the member is not currently logged in
15
     * 4) there are no form messages (errors or notices)
16
     *
17
     * @return void
18
     */
19
    public function onBeforeSecurityLogin()
20
    {
21
        if (Authenticator::get_default_authenticator() != 'SAMLAuthenticator') {
22
            return;
23
        }
24
25
        // by going to the URL Security/login?showloginform=1 we bypass the auto sign on
26
        if ($this->owner->request->getVar('showloginform') == 1) {
27
            return;
28
        }
29
30
        // if member is already logged in, don't auto-sign-on, this is most likely because
31
        // of unsufficient permissions.
32
        $member = Member::currentUser();
33
        if ($member && $member->exists()) {
34
            return;
35
        }
36
37
        // if there are form messages, don't auto-sign-on, this is most likely because of
38
        // login errors / failures or other notices.
39
        if (Session::get('FormInfo')) {
40
            // since FormInfo can be a "nulled" array, we have to check
41
            foreach (Session::get('FormInfo') as $form => $info) {
42
                foreach ($info as $name => $value) {
43
                    if ($value !== null) {
44
                        return;
45
                    }
46
                }
47
            }
48
        }
49
50
        $backURL = Session::get('BackURL');
51
        if ($this->owner->request->getVar('BackURL')) {
52
            $backURL = $this->owner->request->getVar('BackURL');
53
        }
54
55
        $authenticator = Injector::inst()->create('SAMLAuthenticator');
56
        $authenticator->authenticate(["BackURL" => $backURL]);
57
    }
58
}
59