CheckIsUserActivated   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 38
c 3
b 1
f 0
dl 0
loc 71
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 61 13
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Models\Activation;
6
use Auth;
7
use Carbon\Carbon;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Log;
11
use Illuminate\Support\Facades\Route;
12
13
class CheckIsUserActivated
14
{
15
    /**
16
     * Handle an incoming request.
17
     *
18
     * @param \Illuminate\Http\Request $request
19
     * @param \Closure                 $next
20
     *
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next)
24
    {
25
        if (config('settings.activation')) {
26
            $user = Auth::user();
27
            $currentRoute = Route::currentRouteName();
28
            $routesAllowed = [
29
                'activation-required',
30
                'activate/{token}',
31
                'activate',
32
                'activation',
33
                'exceeded',
34
                'authenticated.activate',
35
                'authenticated.activation-resend',
36
                'social/redirect/{provider}',
37
                'social/handle/{provider}',
38
                'logout',
39
                'welcome',
40
            ];
41
42
            if (! in_array($currentRoute, $routesAllowed)) {
43
                if ($user && $user->activated != 1) {
44
                    Log::info('Non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
45
46
                    return redirect()->route('activation-required')
47
                        ->with([
48
                            'message' => 'Activation is required. ',
49
                            'status'  => 'danger',
50
                        ]);
51
                }
52
            }
53
54
            if ($user && $user->activated != 1) {
55
                $activationsCount = Activation::where('user_id', $user->id)
56
                    ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
57
                    ->count();
58
59
                if ($activationsCount >= config('settings.maxAttempts')) {
60
                    return redirect()->route('exceeded');
61
                }
62
            }
63
64
            if (in_array($currentRoute, $routesAllowed)) {
65
                if ($user && $user->activated == 1) {
66
                    // Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
67
68
                    if ($user->isAdmin()) {
0 ignored issues
show
Bug introduced by
The method isAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                    if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
69
                        return redirect('home');
70
                    }
71
72
                    return redirect('home');
73
                }
74
75
                if (! $user) {
76
                    Log::info('Non registered visit to '.$currentRoute.'. ');
77
78
                    return redirect()->route('welcome');
79
                }
80
            }
81
        }
82
83
        return $next($request);
84
    }
85
}
86