ActivateController::exceeded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 23
rs 9.7666
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Activation;
7
use App\Models\Profile;
8
use App\Models\User;
9
use App\Traits\ActivationTrait;
10
use App\Traits\CaptureIpTrait;
11
use Auth;
12
use Carbon\Carbon;
13
use Illuminate\Support\Facades\Log;
14
use Illuminate\Support\Facades\Route;
15
use jeremykenedy\LaravelRoles\Models\Role;
16
17
class ActivateController extends Controller
18
{
19
    use ActivationTrait;
0 ignored issues
show
Bug introduced by
The trait App\Traits\ActivationTrait requires the property $email which is not provided by App\Http\Controllers\Auth\ActivateController.
Loading history...
20
21
    private static $userHomeRoute = 'public.home';
22
    private static $adminHomeRoute = 'public.home';
23
    private static $activationView = 'auth.activation';
24
    private static $activationRoute = 'activation-required';
25
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        $this->middleware('auth');
34
    }
35
36
    /**
37
     * Gets the user home route.
38
     *
39
     * @return string
40
     */
41
    public static function getUserHomeRoute()
42
    {
43
        return self::$userHomeRoute;
44
    }
45
46
    /**
47
     * Gets the admin home route.
48
     *
49
     * @return string
50
     */
51
    public static function getAdminHomeRoute()
52
    {
53
        return self::$adminHomeRoute;
54
    }
55
56
    /**
57
     * Gets the activation view.
58
     *
59
     * @return string
60
     */
61
    public static function getActivationView()
62
    {
63
        return self::$activationView;
64
    }
65
66
    /**
67
     * Gets the activation route.
68
     *
69
     * @return string
70
     */
71
    public static function getActivationRoute()
72
    {
73
        return self::$activationRoute;
74
    }
75
76
    /**
77
     * Redirect the user after activation with admin logic.
78
     *
79
     * @param $user             The user
80
     * @param currentRoute      The current route
81
     *
82
     * @return Redirect
83
     */
84
    public static function activeRedirect($user, $currentRoute)
85
    {
86
        if ($user->activated) {
87
            Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
88
89
            $message = trans('auth.regThanks');
90
            if (config('settings.activation')) {
91
                $message = trans('auth.alreadyActivated');
92
            }
93
94
            if ($user->isAdmin()) {
95
                return redirect()->route(self::getAdminHomeRoute())
96
                ->with('status', 'info')
97
                ->with('message', $message);
98
            }
99
100
            return redirect()->route(self::getUserHomeRoute())
101
                ->with('status', 'info')
102
                ->with('message', $message);
103
        }
104
105
        return false;
106
    }
107
108
    /**
109
     * Initial Activation View.
110
     *
111
     * @return Redirect
112
     */
113
    public function initial()
114
    {
115
        $user = Auth::user();
116
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
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...
117
        $currentRoute = Route::currentRouteName();
118
119
        $rCheck = $this->activeRedirect($user, $currentRoute);
0 ignored issues
show
Bug introduced by
It seems like $currentRoute can also be of type string; however, parameter $currentRoute of App\Http\Controllers\Aut...oller::activeRedirect() does only seem to accept App\Http\Controllers\Auth\The, maybe add an additional type check? ( Ignorable by Annotation )

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

119
        $rCheck = $this->activeRedirect($user, /** @scrutinizer ignore-type */ $currentRoute);
Loading history...
120
        if ($rCheck) {
0 ignored issues
show
introduced by
$rCheck is of type App\Http\Controllers\Auth\Redirect, thus it always evaluated to true.
Loading history...
121
            return $rCheck;
122
        }
123
124
        $data = [
125
            'email' => $user->email,
126
            'date'  => $lastActivation->created_at->format('m/d/Y'),
127
        ];
128
129
        return view($this->getActivationView())->with($data);
130
    }
131
132
    /**
133
     * Check if actication is required.
134
     *
135
     * @return View
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Auth\View was not found. Did you mean View? If so, make sure to prefix the type with \.
Loading history...
136
     */
137
    public function activationRequired()
138
    {
139
        $user = Auth::user();
140
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
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...
141
        $currentRoute = Route::currentRouteName();
142
143
        $rCheck = $this->activeRedirect($user, $currentRoute);
0 ignored issues
show
Bug introduced by
It seems like $currentRoute can also be of type string; however, parameter $currentRoute of App\Http\Controllers\Aut...oller::activeRedirect() does only seem to accept App\Http\Controllers\Auth\The, maybe add an additional type check? ( Ignorable by Annotation )

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

143
        $rCheck = $this->activeRedirect($user, /** @scrutinizer ignore-type */ $currentRoute);
Loading history...
144
        if ($rCheck) {
0 ignored issues
show
introduced by
$rCheck is of type App\Http\Controllers\Auth\Redirect, thus it always evaluated to true.
Loading history...
145
            return $rCheck;
146
        }
147
148
        if ($user->activated === false) {
149
            $activationsCount = Activation::where('user_id', $user->id)
150
                ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
151
                ->count();
152
153
            if ($activationsCount > config('settings.maxAttempts')) {
154
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
155
156
                $data = [
157
                    'email' => $user->email,
158
                    'hours' => config('settings.timePeriod'),
159
                ];
160
161
                return view('auth.exceeded')->with($data);
162
            }
163
        }
164
165
        Log::info('Registered attempted to navigate while unactivate. '.$currentRoute.'. ', [$user]);
166
167
        $data = [
168
            'email' => $user->email,
169
            'date'  => $lastActivation ? $lastActivation->created_at->format('m/d/Y') : null, //
170
        ];
171
172
        return view($this->getActivationView())->with($data);
173
    }
174
175
    /**
176
     * Activate a valid user with a token.
177
     *
178
     * @param string $token The token
179
     *
180
     * @return Redirect
181
     */
182
    public function activate($token)
183
    {
184
        $user = Auth::user();
185
        $currentRoute = Route::currentRouteName();
186
        $ipAddress = new CaptureIpTrait();
187
        $role = Role::where('slug', '=', 'user')->first();
188
189
        $rCheck = $this->activeRedirect($user, $currentRoute);
0 ignored issues
show
Bug introduced by
It seems like $currentRoute can also be of type string; however, parameter $currentRoute of App\Http\Controllers\Aut...oller::activeRedirect() does only seem to accept App\Http\Controllers\Auth\The, maybe add an additional type check? ( Ignorable by Annotation )

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

189
        $rCheck = $this->activeRedirect($user, /** @scrutinizer ignore-type */ $currentRoute);
Loading history...
190
        if ($rCheck) {
0 ignored issues
show
introduced by
$rCheck is of type App\Http\Controllers\Auth\Redirect, thus it always evaluated to true.
Loading history...
191
            return $rCheck;
192
        }
193
194
        $activation = Activation::where('token', $token)->get()
195
            ->where('user_id', $user->id)
196
            ->first();
197
198
        if (empty($activation)) {
199
            Log::info('Registered user attempted to activate with an invalid token: '.$currentRoute.'. ', [$user]);
200
201
            return redirect()->route(self::getActivationRoute())
202
                ->with('status', 'danger')
203
                ->with('message', trans('auth.invalidToken'));
204
        }
205
206
        $user->activated = true;
207
        $user->detachAllRoles();
208
        $user->attachRole($role);
209
        $user->signup_confirmation_ip_address = $ipAddress->getClientIp();
210
        $user->save();
211
212
        $allActivations = Activation::where('user_id', $user->id)->get();
213
        foreach ($allActivations as $anActivation) {
214
            $anActivation->delete();
215
        }
216
217
        Log::info('Registered user successfully activated. '.$currentRoute.'. ', [$user]);
218
219
        if ($user->isAdmin()) {
220
            return redirect()->route(self::getAdminHomeRoute())
221
            ->with('status', 'success')
222
            ->with('message', trans('auth.successActivated'));
223
        }
224
225
        return redirect()->route(self::getUserHomeRoute())
226
            ->with('status', 'success')
227
            ->with('message', trans('auth.successActivated'));
228
    }
229
230
    /**
231
     * Resend Activation.
232
     *
233
     * @return Redirect
234
     */
235
    public function resend()
236
    {
237
        $user = Auth::user();
238
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Unused Code introduced by
The assignment to $lastActivation is dead and can be removed.
Loading history...
Bug introduced by
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...
239
        $currentRoute = Route::currentRouteName();
240
241
        if ($user->activated === false) {
0 ignored issues
show
Bug introduced by
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...
242
            $activationsCount = Activation::where('user_id', $user->id)
243
                ->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))
244
                ->count();
245
246
            if ($activationsCount >= config('settings.maxAttempts')) {
247
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
248
249
                $data = [
250
                    'email' => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
251
                    'hours' => config('settings.timePeriod'),
252
                ];
253
254
                return view('auth.exceeded')->with($data);
255
            }
256
257
            $sendEmail = $this->initiateEmailActivation($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Http\Controllers\Aut...itiateEmailActivation() does only seem to accept App\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

257
            $sendEmail = $this->initiateEmailActivation(/** @scrutinizer ignore-type */ $user);
Loading history...
Unused Code introduced by
The assignment to $sendEmail is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $sendEmail is correct as $this->initiateEmailActivation($user) targeting App\Http\Controllers\Aut...itiateEmailActivation() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
258
259
            Log::info('Activation resent to registered user. '.$currentRoute.'. ', [$user]);
260
261
            return redirect()->route(self::getActivationRoute())
262
                ->with('status', 'success')
263
                ->with('message', trans('auth.activationSent'));
264
        }
265
266
        Log::info('Activated user attempte to navigate to '.$currentRoute.'. ', [$user]);
267
268
        return $this->activeRedirect($user, $currentRoute)
0 ignored issues
show
Bug introduced by
It seems like $currentRoute can also be of type string; however, parameter $currentRoute of App\Http\Controllers\Aut...oller::activeRedirect() does only seem to accept App\Http\Controllers\Auth\The, maybe add an additional type check? ( Ignorable by Annotation )

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

268
        return $this->activeRedirect($user, /** @scrutinizer ignore-type */ $currentRoute)
Loading history...
269
            ->with('status', 'info')
270
            ->with('message', trans('auth.alreadyActivated'));
271
    }
272
273
    /**
274
     * Check if use is already activated.
275
     *
276
     * @return Redirect
277
     */
278
    public function exceeded()
279
    {
280
        $user = Auth::user();
281
        $currentRoute = Route::currentRouteName();
282
        $timePeriod = config('settings.timePeriod');
283
        $lastActivation = Activation::where('user_id', $user->id)->get()->last();
0 ignored issues
show
Bug introduced by
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...
284
        $activationsCount = Activation::where('user_id', $user->id)
285
            ->where('created_at', '>=', Carbon::now()->subHours($timePeriod))
286
            ->count();
287
288
        if ($activationsCount >= config('settings.maxAttempts')) {
289
            Log::info('Locked non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
290
291
            $data = [
292
                'hours'    => config('settings.timePeriod'),
293
                'email'    => $user->email,
0 ignored issues
show
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
294
                'lastDate' => $lastActivation->created_at->format('m/d/Y'),
295
            ];
296
297
            return view('auth.exceeded')->with($data);
298
        }
299
300
        return redirect()->route(self::getActivationRoute());
301
    }
302
}
303