• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 12489544920

25 Dec 2024 04:53AM UTC coverage: 78.401% (+0.02%) from 78.385%
12489544920

Pull #168

github

web-flow
Merge a36445178 into 338a94856
Pull Request #168: #140 global export mode not affect for email testing

2 of 2 new or added lines in 1 file covered. (100.0%)

1020 of 1301 relevant lines covered (78.4%)

11.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Middleware/SecurityMiddleware.php
1
<?php
2

3
namespace RonasIT\Support\Middleware;
4

5
use Closure;
6
use Illuminate\Cache\Repository;
7
use Symfony\Component\HttpFoundation\Response;
8

9
class SecurityMiddleware
10
{
11
    protected $cache;
12

13
    public const MAINTENANCE_MODE_KEY = 'maintenance_activated';
14
    public const MAINTENANCE_MODE_HEADER = 'maintenance';
15

16
    public function __construct(Repository $cache)
17
    {
18
        $this->cache = $cache;
×
19
    }
20

21
    public function handle($request, Closure $next)
22
    {
23
        if ($this->needToEnable($request)) {
×
24
            $this->cache->forever(self::MAINTENANCE_MODE_KEY, true);
×
25
        }
26

27
        if ($this->needToDisable($request)) {
×
28
            $this->cache->forget(self::MAINTENANCE_MODE_KEY);
×
29
        }
30

31
        if ($this->cache->get(self::MAINTENANCE_MODE_KEY)) {
×
32
            return $this->getFailResponse();
×
33
        }
34

35
        return $next($request);
×
36
    }
37

38
    protected function needToEnable($request): bool
39
    {
40
        return ($request->header(self::MAINTENANCE_MODE_HEADER) === 'activate') && ($request->header('App-Key') === config('app.key'));
×
41
    }
42

43
    protected function needToDisable($request): bool
44
    {
45
        return ($request->header(self::MAINTENANCE_MODE_HEADER) === 'deactivate') && ($request->header('App-Key') === config('app.key'));
×
46
    }
47

48
    //To hide the reason from attackers
49
    protected function getFailResponse()
50
    {
51
        $code = array_rand($this->codeVariations());
×
52

53
        return response(view("errors.{$code}")->render(), $code);
×
54
    }
55

56
    protected function codeVariations(): array
57
    {
58
        return [
×
59
            Response::HTTP_INTERNAL_SERVER_ERROR,
×
60
            Response::HTTP_NOT_IMPLEMENTED,
×
61
            Response::HTTP_BAD_GATEWAY,
×
62
            Response::HTTP_SERVICE_UNAVAILABLE,
×
63
            Response::HTTP_GATEWAY_TIMEOUT,
×
64
            Response::HTTP_VERSION_NOT_SUPPORTED,
×
65
            Response::HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL,
×
66
            Response::HTTP_INSUFFICIENT_STORAGE,
×
67
            Response::HTTP_LOOP_DETECTED,
×
68
            Response::HTTP_NOT_EXTENDED,
×
69
            Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED,
×
70
        ];
×
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc