GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c31bff...117582 )
by Caspar
10:25
created

ActivateController::activationRequired()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
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 $name_gen 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
    public function __construct()
30
    {
31
        $this->middleware('auth');
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public static function getUserHomeRoute()
38
    {
39
        return self::$userHomeRoute;
40
    }
41
42
    public static function getAdminHomeRoute()
43
    {
44
        return self::$adminHomeRoute;
45
    }
46
47
    public static function getActivationView()
48
    {
49
        return self::$activationView;
50
    }
51
52
    public static function getActivationRoute()
53
    {
54
        return self::$activationRoute;
55
    }
56
57
    public static function activeRedirect($user, $currentRoute)
58
    {
59
        if ($user->activated) {
60
            Log::info('Activated user attempted to visit '.$currentRoute.'. ', [$user]);
61
62
            if ($user->isAdmin()) {
63
                return redirect()->route(self::getAdminHomeRoute())->with('status', 'info')->with('message', trans('auth.alreadyActivated'));
64
            }
65
66
            return redirect()->route(self::getUserHomeRoute())->with('status', 'info')->with('message', trans('auth.alreadyActivated'));
67
        }
68
69
        return false;
70
    }
71
72
    public function initial()
73
    {
74
        $user = Auth::user();
75
        $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...
76
        $currentRoute = Route::currentRouteName();
77
78
        $rCheck = $this->activeRedirect($user, $currentRoute);
79
        if ($rCheck) {
80
            return $rCheck;
81
        }
82
83
        $data = [
84
            'name_gen' => $user->name_gen,
0 ignored issues
show
Bug introduced by
Accessing name_gen on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
85
            'date'     => $lastActivation->created_at->format('m/d/Y'),
86
        ];
87
88
        return view($this->getActivationView())->with($data);
89
    }
90
91
    public function activationRequired()
92
    {
93
        $user = Auth::user();
94
        $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...
95
        $currentRoute = Route::currentRouteName();
96
97
        $rCheck = $this->activeRedirect($user, $currentRoute);
98
        if ($rCheck) {
99
            return $rCheck;
100
        }
101
102
        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...
103
            $activationsCount = Activation::where('user_id', $user->id)->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))->count();
104
105
            if ($activationsCount > config('settings.timePeriod')) {
106
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
107
108
                $data = [
109
                    'name_gen' => $user->name_gen,
0 ignored issues
show
Bug introduced by
Accessing name_gen on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
110
                    'hours'    => config('settings.timePeriod'),
111
                ];
112
113
                return view('auth.exceeded')->with($data);
114
            }
115
        }
116
117
        Log::info('Registered attempted to navigate while unactivate. '.$currentRoute.'. ', [$user]);
118
119
        $data = [
120
            'name_gen' => $user->name_gen,
121
            'date'     => $lastActivation->created_at->format('m/d/Y'),
122
        ];
123
124
        return view($this->getActivationView())->with($data);
125
    }
126
127
    public function activate($token)
128
    {
129
        $user = Auth::user();
130
        $currentRoute = Route::currentRouteName();
131
        $ipAddress = new CaptureIpTrait();
132
        $role = Role::where('slug', '=', 'user')->first();
133
        $profile = new Profile();
134
135
        $rCheck = $this->activeRedirect($user, $currentRoute);
136
        if ($rCheck) {
137
            return $rCheck;
138
        }
139
140
        $activation = Activation::where('token', $token)->get()->where('user_id', $user->id)->first();
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
142
        if (empty($activation)) {
143
            Log::info('Registered user attempted to activate with an invalid token: '.$currentRoute.'. ', [$user]);
144
145
            return redirect()->route(self::getActivationRoute())->with('status', 'danger')->with('message', trans('auth.invalidToken'));
146
        }
147
148
        $user->activated = true;
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...
149
        $user->detachAllRoles();
0 ignored issues
show
Bug introduced by
The method detachAllRoles() 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

149
        $user->/** @scrutinizer ignore-call */ 
150
               detachAllRoles();
Loading history...
150
        $user->attachRole($role);
0 ignored issues
show
Bug introduced by
The method attachRole() 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

150
        $user->/** @scrutinizer ignore-call */ 
151
               attachRole($role);
Loading history...
151
        $user->signup_confirmation_ip_address = $ipAddress->getClientIp();
0 ignored issues
show
Bug introduced by
Accessing signup_confirmation_ip_address on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
152
        $user->profile()->save($profile);
0 ignored issues
show
Bug introduced by
The method profile() 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

152
        $user->/** @scrutinizer ignore-call */ 
153
               profile()->save($profile);
Loading history...
153
        $user->save();
154
155
        $allActivations = Activation::where('user_id', $user->id)->get();
156
        foreach ($allActivations as $anActivation) {
157
            $anActivation->delete();
158
        }
159
160
        Log::info('Registered user successfully activated. '.$currentRoute.'. ', [$user]);
161
162
        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

162
        if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
163
            return redirect()->route(self::getAdminHomeRoute())->with('status', 'success')->with('message', trans('auth.successActivated'));
164
        }
165
166
        return redirect()->route(self::getUserHomeRoute())->with('status', 'success')->with('message', trans('auth.successActivated'));
167
    }
168
169
    public function resend()
170
    {
171
        $user = Auth::user();
172
        $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...
Unused Code introduced by
The assignment to $lastActivation is dead and can be removed.
Loading history...
173
        $currentRoute = Route::currentRouteName();
174
175
        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...
176
            $activationsCount = Activation::where('user_id', $user->id)->where('created_at', '>=', Carbon::now()->subHours(config('settings.timePeriod')))->count();
177
178
            if ($activationsCount >= config('settings.maxAttempts')) {
179
                Log::info('Exceded max resends in last '.config('settings.timePeriod').' hours. '.$currentRoute.'. ', [$user]);
180
181
                $data = [
182
                    'name_gen' => $user->name_gen,
0 ignored issues
show
Bug introduced by
Accessing name_gen on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
183
                    'hours'    => config('settings.timePeriod'),
184
                ];
185
186
                return view('auth.exceeded')->with($data);
187
            }
188
189
            $sendEmail = $this->initiateEmailActivation($user);
0 ignored issues
show
Unused Code introduced by
The assignment to $sendEmail is dead and can be removed.
Loading history...
190
191
            Log::info('Activation resent to registered user. '.$currentRoute.'. ', [$user]);
192
193
            return redirect()->route(self::getActivationRoute())->with('status', 'success')->with('message', trans('auth.activationSent'));
194
        }
195
196
        Log::info('Activated user attempte to navigate to '.$currentRoute.'. ', [$user]);
197
198
        return $this->activeRedirect($user, $currentRoute)->with('status', 'info')->with('message', trans('auth.alreadyActivated'));
199
    }
200
201
    public function exceeded()
202
    {
203
        $user = Auth::user();
204
        $currentRoute = Route::currentRouteName();
205
        $timePeriod = config('settings.timePeriod');
206
        $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...
207
        $activationsCount = Activation::where('user_id', $user->id)->where('created_at', '>=', Carbon::now()->subHours($timePeriod))->count();
208
209
        if ($activationsCount >= config('settings.maxAttempts')) {
210
            Log::info('Locked non-activated user attempted to visit '.$currentRoute.'. ', [$user]);
211
212
            $data = [
213
                'hours'    => config('settings.timePeriod'),
214
                'name_gen' => $user->name_gen,
0 ignored issues
show
Bug introduced by
Accessing name_gen on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
215
                'lastDate' => $lastActivation->created_at->format('m/d/Y'),
216
            ];
217
218
            return view('auth.exceeded')->with($data);
219
        }
220
221
        return $this->activeRedirect($user, $currentRoute)->with('status', 'info')->with('message', trans('auth.alreadyActivated'));
222
    }
223
}
224