1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Middleware;
|
---|
4 |
|
---|
5 | use App\Models\Artist;
|
---|
6 | use App\Models\ManagerInvite;
|
---|
7 | use Closure;
|
---|
8 | use Illuminate\Http\Request;
|
---|
9 | use Illuminate\Support\Facades\Hash;
|
---|
10 |
|
---|
11 | class HasInvitationToken
|
---|
12 | {
|
---|
13 | /**
|
---|
14 | * Handle an incoming request.
|
---|
15 | *
|
---|
16 | * @param \Illuminate\Http\Request $request
|
---|
17 | * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
---|
18 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\never
|
---|
19 | */
|
---|
20 | public function handle(Request $request, Closure $next)
|
---|
21 | {
|
---|
22 | if ($request->isMethod('get')) {
|
---|
23 | if (!$request->hasValidSignature()) {
|
---|
24 | return abort(401);
|
---|
25 | }
|
---|
26 |
|
---|
27 | if (!$request->route('token')) {
|
---|
28 | return abort(404);
|
---|
29 | }
|
---|
30 |
|
---|
31 | $invitation_token = $request->route('token');
|
---|
32 |
|
---|
33 | $invitations = ManagerInvite::where('email', $request->email)->get();
|
---|
34 | foreach($invitations as $invitation) {
|
---|
35 | if (Hash::check($invitation_token, $invitation->invitation_token)) {
|
---|
36 | $new_invitation = ManagerInvite::where('invitation_token', $invitation->invitation_token)->firstOrFail();
|
---|
37 |
|
---|
38 | if (Artist::where('user_id', $new_invitation->artist_id)
|
---|
39 | ->whereNotNull('manager_id')
|
---|
40 | ->exists()) {
|
---|
41 | return redirect(route('login'))
|
---|
42 | ->with('error', 'An error occurred. Please try again!');
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (is_null($new_invitation)) {
|
---|
46 | return redirect(route('login'))
|
---|
47 | ->with('error', 'An error occurred. Please try again!');
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (!is_null($new_invitation->registered_at)) {
|
---|
51 | return redirect(route('login'))
|
---|
52 | ->with('error', 'The invitation link has already been used');
|
---|
53 | }
|
---|
54 |
|
---|
55 | $request->merge(['artist_id' => $new_invitation->artist_id]);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | return $next($request);
|
---|
61 | }
|
---|
62 | }
|
---|