1 | <?php |
||
2 | |||
3 | namespace LinkRestrictedAccess\Models; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
||
7 | use Illuminate\Database\Eloquent\Model; |
||
8 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
9 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
10 | use Illuminate\Http\Request; |
||
11 | use Illuminate\Support\Str; |
||
12 | use JsonFieldCast\Casts\SimpleJsonField; |
||
13 | use LinkRestrictedAccess\Database\Factories\RestrictedLinkOpenActionFactory; |
||
14 | |||
15 | /** |
||
16 | * @property \JsonFieldCast\Json\SimpleJsonField $verification_result |
||
17 | * @property \JsonFieldCast\Json\SimpleJsonField $meta |
||
18 | */ |
||
19 | class RestrictedLinkOpenAction extends Model |
||
20 | { |
||
21 | use HasFactory; |
||
22 | |||
23 | protected $guarded = []; |
||
24 | |||
25 | protected $casts = [ |
||
26 | 'viewed_at' => 'datetime', |
||
27 | 'verification_result' => SimpleJsonField::class, |
||
28 | 'meta' => SimpleJsonField::class, |
||
29 | ]; |
||
30 | |||
31 | 7 | public function getTable(): string |
|
32 | { |
||
33 | 7 | return config('restricted-access.tables.opens'); |
|
34 | } |
||
35 | |||
36 | 7 | protected static function boot(): void |
|
37 | { |
||
38 | 7 | parent::boot(); |
|
39 | |||
40 | 7 | static::saving(function ($model) { |
|
41 | 7 | if (!$model->uuid) { |
|
42 | 7 | $model->uuid = (string)Str::uuid(); |
|
43 | } |
||
44 | 7 | }); |
|
45 | } |
||
46 | |||
47 | 1 | public function link(): BelongsTo |
|
48 | { |
||
49 | 1 | return $this->belongsTo(\LinkRestrictedAccess\RestrictedAccess::restrictedLinkModel(), 'link_id', 'id'); |
|
50 | } |
||
51 | |||
52 | 1 | public function viewer(): MorphTo |
|
53 | { |
||
54 | 1 | return $this->morphTo('viewer'); |
|
55 | } |
||
56 | |||
57 | 3 | public function accessExpired(): bool |
|
58 | { |
||
59 | 3 | if ($this->viewed_at) { |
|
0 ignored issues
–
show
|
|||
60 | 3 | return Carbon::now()->subHours(24)->greaterThan($this->viewed_at); |
|
61 | } |
||
62 | |||
63 | 1 | return true; |
|
64 | } |
||
65 | |||
66 | 5 | protected function makeBrowserFingerPrint(Request $request): string |
|
67 | { |
||
68 | 5 | return md5($request->ip() . '_' . $request->userAgent()); |
|
69 | } |
||
70 | |||
71 | 5 | public function fillBrowserFingerPrint(Request $request) |
|
72 | { |
||
73 | 5 | return $this->fill([ |
|
74 | 5 | 'browser_fingerprint' => $this->makeBrowserFingerPrint($request), |
|
75 | 5 | ]); |
|
76 | } |
||
77 | |||
78 | 2 | public function checkBrowserFingerPrint(Request $request): bool |
|
79 | { |
||
80 | 2 | return $this->browser_fingerprint == $this->makeBrowserFingerPrint($request); |
|
0 ignored issues
–
show
|
|||
81 | } |
||
82 | |||
83 | 2 | public function verified(Request $request): bool |
|
84 | { |
||
85 | 2 | return $this->checkBrowserFingerPrint($request) && !$this->accessExpired(); |
|
86 | } |
||
87 | |||
88 | 4 | protected static function newFactory(): RestrictedLinkOpenActionFactory |
|
89 | { |
||
90 | 4 | return RestrictedLinkOpenActionFactory::new(); |
|
91 | } |
||
92 | } |
||
93 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.