LoginAndRedirect   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 24
c 4
b 1
f 0
dl 0
loc 44
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 11 1
A isDev() 0 21 3
1
<?php
2
3
namespace Sunnysideup\TemplateOverview\Control;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\Util\IPUtils;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Environment;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Security\IdentityStore;
12
use SilverStripe\Security\MemberAuthenticator\CookieAuthenticationHandler;
13
use SilverStripe\Security\Security;
14
15
/**
16
 * Class \Sunnysideup\TemplateOverview\Control\LoginAndRedirect
17
 *
18
 */
19
class LoginAndRedirect extends Controller
20
{
21
    private static $allowed_actions = [
22
        'login' => '->isDev',
23
    ];
24
25
    private static $allowed_ips = [
26
        '127.0.0.1',
27
    ];
28
29
    public function login($request)
30
    {
31
        $url = $request->getVar('BackURL');
32
        $member = CheckAllTemplatesResponseController::get_test_user();
33
        Security::setCurrentUser($member);
34
        // Injector::inst()->get(IdentityStore::class)->logIn($member, true);
35
        Injector::inst()->get(CookieAuthenticationHandler::class)
36
            ->logIn($member, $persist = true)
37
        ;
38
        // die($url);
39
        return $this->redirect($url);
40
    }
41
42
    public function isDev()
43
    {
44
        if (Environment::getEnv('SS_ALLOW_SMOKE_TEST')) {
45
            $allowedIPs = Config::inst()->get(self::class, 'allowed_ips');
46
            if (IPUtils::checkIP($this->request->getIP(), $allowedIPs)) {
47
                return Director::isDev();
48
            }
49
50
            user_error(
51
                'Please include your ip address in LoginAndRedirect.allowed_ips: ' .
52
                    $this->request->getIP() . '.
53
                    Currently set are: ' . implode(', ', $allowedIPs),
54
                E_USER_ERROR
55
            );
56
57
            return;
58
        }
59
60
        user_error(
61
            'Please set SS_ALLOW_SMOKE_TEST in your environment variables to use this service.',
62
            E_USER_ERROR
63
        );
64
    }
65
}
66