Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

ChiefValidateInvite::handle()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 5
nop 2
dl 0
loc 20
ccs 9
cts 9
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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