dev-think-one /
laravel-restricted-access
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LinkRestrictedAccess\Http\Controllers; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Http\Request; |
||
| 7 | use Illuminate\Support\Facades\Cookie; |
||
| 8 | use Illuminate\Support\Facades\Redirect; |
||
| 9 | use Illuminate\Support\Facades\Response; |
||
| 10 | use Illuminate\Validation\Rule; |
||
| 11 | use Illuminate\Validation\ValidationException; |
||
| 12 | use LinkRestrictedAccess\Models\RestrictedLink; |
||
| 13 | use LinkRestrictedAccess\Models\RestrictedLinkOpenAction; |
||
| 14 | |||
| 15 | class OpenDocumentController extends Controller |
||
| 16 | { |
||
| 17 | 3 | public function __invoke(Request $request) |
|
| 18 | { |
||
| 19 | /** @var RestrictedLink $restrictedLink */ |
||
| 20 | 3 | $restrictedLink = \LinkRestrictedAccess\RestrictedAccess::restrictedLinkModel()::query() |
|
| 21 | 3 | ->where('uuid', $request->route('uuid')) |
|
| 22 | 3 | ->firstOrFail(); |
|
| 23 | |||
| 24 | 3 | $request->validate([ |
|
| 25 | 3 | 'pin' => [ |
|
| 26 | 3 | 'nullable', |
|
| 27 | 3 | Rule::requiredIf($restrictedLink->checkPin), |
|
|
0 ignored issues
–
show
|
|||
| 28 | 3 | 'string', |
|
| 29 | 3 | 'max:100', |
|
| 30 | 3 | ], |
|
| 31 | 3 | 'name' => [ |
|
| 32 | 3 | 'nullable', |
|
| 33 | 3 | Rule::requiredIf($restrictedLink->checkName), |
|
|
0 ignored issues
–
show
|
|||
| 34 | 3 | 'string', |
|
| 35 | 3 | 'max:255', |
|
| 36 | 3 | ], |
|
| 37 | 3 | 'email' => [ |
|
| 38 | 3 | 'nullable', |
|
| 39 | 3 | Rule::requiredIf($restrictedLink->checkEmail), |
|
|
0 ignored issues
–
show
|
|||
| 40 | 3 | 'email', |
|
| 41 | 3 | 'max:255', |
|
| 42 | 3 | ], |
|
| 43 | 3 | ]); |
|
| 44 | |||
| 45 | if ( |
||
| 46 | 3 | $restrictedLink->checkPin && |
|
| 47 | ( |
||
| 48 | 3 | !$restrictedLink->pin || |
|
|
0 ignored issues
–
show
|
|||
| 49 | 3 | $restrictedLink->pin != $request->input('pin') |
|
| 50 | ) |
||
| 51 | |||
| 52 | ) { |
||
| 53 | 1 | throw ValidationException::withMessages([ |
|
| 54 | 1 | 'pin' => __('Incorrect pin'), |
|
| 55 | 1 | ]); |
|
| 56 | } |
||
| 57 | |||
| 58 | /** @var RestrictedLinkOpenAction $openAction */ |
||
| 59 | 3 | $openAction = $restrictedLink->openActions()->make([ |
|
| 60 | 3 | 'viewed_at' => Carbon::now(), |
|
| 61 | 3 | ])->fillBrowserFingerPrint($request); |
|
| 62 | |||
| 63 | |||
| 64 | 3 | if ($user = $request->user()) { |
|
| 65 | 1 | $openAction->viewer()->associate($user); |
|
| 66 | } |
||
| 67 | |||
| 68 | 3 | if ($restrictedLink->checkPin) { |
|
| 69 | 3 | $openAction->verification_result->setAttribute('pin_checked', true); |
|
| 70 | } |
||
| 71 | |||
| 72 | 3 | if ($restrictedLink->checkName) { |
|
| 73 | 3 | $openAction->verification_result->setAttribute('name', $request->input('name')); |
|
| 74 | } |
||
| 75 | |||
| 76 | 3 | if ($restrictedLink->checkEmail) { |
|
| 77 | 3 | $openAction->verification_result->setAttribute('email', $request->input('email')); |
|
| 78 | } |
||
| 79 | |||
| 80 | 3 | $openAction->save(); |
|
| 81 | |||
| 82 | 3 | $message = __('Access allowed.'); |
|
| 83 | |||
| 84 | 3 | $cookie = Cookie::make($restrictedLink->cookieName(), $openAction->uuid, 60 * 24 /* 24 hours */); |
|
| 85 | |||
| 86 | 3 | if ($request->expectsJson()) { |
|
| 87 | 2 | return Response::json([ |
|
| 88 | 2 | 'message' => $message, |
|
| 89 | 2 | 'data' => [ |
|
| 90 | 2 | 'success' => true, |
|
| 91 | 2 | ], |
|
| 92 | 2 | ])->withCookie($cookie); |
|
| 93 | } |
||
| 94 | |||
| 95 | 1 | return Redirect::back() |
|
| 96 | 1 | ->with(['message' => $message]) |
|
| 97 | 1 | ->withCookie($cookie); |
|
| 98 | } |
||
| 99 | } |
||
| 100 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.