Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

ChiefValidateInvite   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 6
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Middleware;
4
5
use Closure;
6
use Thinktomorrow\Chief\Users\Invites\Invitation;
7
use Thinktomorrow\Chief\Users\Invites\InvitationState;
8
9
class ChiefValidateInvite
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @param  \Closure  $next
16
     * @return mixed
17
     */
18
    public function handle($request, Closure $next)
19
    {
20
        // Verifies a valid signature and still outside expiration period
21
        if (! $request->hasValidSignature()) {
22
            return redirect()->route('invite.expired');
23
        }
24
25
        if (! $invitation = Invitation::findByToken($request->token)) {
26
            return redirect()->route('invite.expired');
27
        }
28
29
        if (in_array($invitation->state(), [InvitationState::ACCEPTED, InvitationState::REVOKED])) {
30
31
            // We allow the user to pass if the invitee is already logged in. Otherwise the invite link cannot be reused.
32
            if (! auth()->guard('chief')->check() || ! auth()->guard('chief')->user()->is($invitation->invitee)) {
33
                return redirect()->route('invite.expired');
34
            }
35
        }
36
37
        return $next($request);
38
    }
39
}
40