jeremykenedy /
laravel-auth
| 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
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 |