Issues (152)

app/Http/Middleware/CheckIsUserActivated.php (3 issues)

Labels
Severity
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) {
0 ignored issues
show
Accessing activated on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
44
                    Log::info('Non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
45
46
                    return redirect()->route('activation-required')
47
                        ->with([
48
                            'notice' => 'Activation is required. ',
49
                        ]);
50
                }
51
            }
52
53
            if ($user && $user->activated != 1) {
54
                $activationsCount = Activation::where('user_id', $user->id)
0 ignored issues
show
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
                    ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
56
                    ->count();
57
58
                if ($activationsCount >= config('settings.maxAttempts')) {
59
                    return redirect()->route('exceeded');
60
                }
61
            }
62
63
            if (in_array($currentRoute, $routesAllowed)) {
64
                if ($user && $user->activated == 1) {
65
                    Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
66
67
                    if ($user->isAdmin()) {
0 ignored issues
show
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

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