Shield   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 122
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldDenyAccess() 0 5 4
A requiresLogin() 0 8 6
A isActive() 0 7 3
A currentUrlIsIgnored() 0 15 3
A clientIpIsWhitelisted() 0 7 1
A hasIpWhitelist() 0 4 1
A getIpWhitelist() 0 10 2
A allowWhitelistedIpsOnly() 0 4 1
A whitelistRequiresLogin() 0 4 1
1
<?php
2
3
namespace CodeZero\StageFront;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\Request;
7
use Illuminate\Support\Facades\Session;
8
9
class Shield
10
{
11
    /**
12
     * Check if StageFront should deny access to the user
13
     * with a 403 Forbidden HTTP status.
14
     *
15
     * @return bool
16
     */
17 16
    public function shouldDenyAccess()
18
    {
19 16
        return $this->isActive()
20 16
            && ($this->hasIpWhitelist() && ! $this->clientIpIsWhitelisted() && $this->allowWhitelistedIpsOnly());
21
    }
22
23
    /**
24
     * Check if StageFront requires the user to log in.
25
     *
26
     * @return bool
27
     */
28 16
    public function requiresLogin()
29
    {
30 16
        return $this->isActive()
31 6
            && ( ! $this->hasIpWhitelist()
32 6
                || ($this->clientIpIsWhitelisted() && $this->whitelistRequiresLogin())
33 16
                || ( ! $this->clientIpIsWhitelisted() && ! $this->allowWhitelistedIpsOnly())
34
            );
35
    }
36
37
    /**
38
     * Check if StageFront is active.
39
     * Once a user logs in, the shield is considered inactive.
40
     *
41
     * @return bool
42
     */
43 16
    protected function isActive()
44
    {
45 16
        $enabled = Config::get('stagefront.enabled', false);
46 16
        $unlocked = Session::get('stagefront.unlocked', false);
47
48 16
        return $enabled && ! $unlocked && ! $this->currentUrlIsIgnored();
49
    }
50
51
    /**
52
     * Check if the current URL should be ignored.
53
     *
54
     * @return bool
55
     */
56 15
    protected function currentUrlIsIgnored()
57
    {
58 15
        $ignoredUrls = Config::get('stagefront.ignore_urls', []);
59 15
        $ignoredUrls[] = Config::get('stagefront.url');
60
61 15
        foreach ($ignoredUrls as $url) {
62 15
            $url = trim($url, '/');
63
64 15
            if (Request::is($url)) {
65 11
                return true;
66
            }
67
        }
68
69 7
        return false;
70
    }
71
72
    /**
73
     * Check if the client IP is whitelisted.
74
     *
75
     * @return bool
76
     */
77 7
    protected function clientIpIsWhitelisted()
78
    {
79 7
        $clientIp = Request::ip();
80 7
        $ips = array_map('trim', $this->getIpWhitelist());
81
82 7
        return in_array($clientIp, $ips);
83
    }
84
85
    /**
86
     * Check if a IP whitelist is configured.
87
     *
88
     * @return bool
89
     */
90 7
    protected function hasIpWhitelist()
91
    {
92 7
        return ! empty($this->getIpWhitelist());
93
    }
94
95
    /**
96
     * Get the IP whitelist from the config file.
97
     *
98
     * @return array
99
     */
100 7
    protected function getIpWhitelist()
101
    {
102 7
        $whitelist = Config::get('stagefront.ip_whitelist', []);
103
104 7
        if (is_array($whitelist)) {
105 1
            return $whitelist;
106
        }
107
108 6
        return explode(',', $whitelist);
109
    }
110
111
    /**
112
     * Get the option to grant access to whitelisted IP's only.
113
     *
114
     * @return bool
115
     */
116 7
    protected function allowWhitelistedIpsOnly()
117
    {
118 7
        return Config::get('stagefront.ip_whitelist_only', false);
119
    }
120
121
    /**
122
     * Get the option to require users with whitelisted IP's to login.
123
     *
124
     * @return bool
125
     */
126 4
    public function whitelistRequiresLogin()
127
    {
128 4
        return Config::get('stagefront.ip_whitelist_require_login', false);
129
    }
130
}
131