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

SAMLSecurityExtension::onBeforeSecurityLogin()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 39
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 8
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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