source: app/Http/Middleware/HasInvitationToken.php

Last change on this file was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 2.1 KB
Line 
1<?php
2
3namespace App\Http\Middleware;
4
5use App\Models\Artist;
6use App\Models\ManagerInvite;
7use Closure;
8use Illuminate\Http\Request;
9use Illuminate\Support\Facades\Hash;
10
11class 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}
Note: See TracBrowser for help on using the repository browser.