1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LinkRestrictedAccess\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute; |
8
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
use JsonFieldCast\Casts\SimpleJsonField; |
14
|
|
|
use LinkRestrictedAccess\Database\Factories\RestrictedLinkFactory; |
15
|
|
|
use LinkRestrictedAccess\RestrictedAccess; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @property \JsonFieldCast\Json\SimpleJsonField $access_configuration |
19
|
|
|
* @property \JsonFieldCast\Json\SimpleJsonField $meta |
20
|
|
|
*/ |
21
|
|
|
class RestrictedLink extends Model |
22
|
|
|
{ |
23
|
|
|
use HasFactory; |
24
|
|
|
|
25
|
|
|
protected $guarded = []; |
26
|
|
|
|
27
|
|
|
protected $casts = [ |
28
|
|
|
'use_pin' => 'datetime', |
29
|
|
|
'access_configuration' => SimpleJsonField::class, |
30
|
|
|
'meta' => SimpleJsonField::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
protected $accessConfigsMap = [ |
34
|
|
|
'checkName' => 'use_name', |
35
|
|
|
'checkEmail' => 'use_email', |
36
|
|
|
]; |
37
|
|
|
|
38
|
14 |
|
public function getTable(): string |
39
|
|
|
{ |
40
|
14 |
|
return config('restricted-access.tables.links'); |
41
|
|
|
} |
42
|
|
|
|
43
|
15 |
|
protected static function boot(): void |
44
|
|
|
{ |
45
|
15 |
|
parent::boot(); |
46
|
|
|
|
47
|
15 |
|
static::saving(function ($model) { |
48
|
14 |
|
if (!$model->uuid) { |
49
|
14 |
|
$model->uuid = (string)Str::uuid(); |
50
|
|
|
} |
51
|
15 |
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
public function linkable(): MorphTo |
55
|
|
|
{ |
56
|
3 |
|
return $this->morphTo('linkable'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
7 |
|
public function openActions(): HasMany |
61
|
|
|
{ |
62
|
7 |
|
return $this->hasMany(RestrictedAccess::linkOpenActionModel(), 'link_id', 'id'); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
public function checkPin(): Attribute |
66
|
|
|
{ |
67
|
6 |
|
return Attribute::make( |
68
|
6 |
|
fn () => (bool)$this->use_pin, |
|
|
|
|
69
|
6 |
|
fn ($value) => [ |
70
|
6 |
|
'use_pin' => $value ? $this->use_pin ?? Carbon::now() : null, |
|
|
|
|
71
|
6 |
|
], |
72
|
6 |
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
protected function getAccessConfig(string $key): Attribute |
76
|
|
|
{ |
77
|
5 |
|
$field = $this->accessConfigsMap[$key]??Str::snake($key); |
78
|
|
|
|
79
|
5 |
|
return Attribute::make( |
80
|
5 |
|
fn () => (bool)$this->access_configuration->getDateAttribute($field), |
81
|
5 |
|
function ($value) use ($field) { |
82
|
2 |
|
if ($value) { |
83
|
2 |
|
$this->access_configuration->setDate($field, $this->access_configuration->getDateAttribute($field, Carbon::now())); |
84
|
|
|
} else { |
85
|
1 |
|
$this->access_configuration->removeAttribute($field); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return []; |
89
|
5 |
|
}, |
90
|
5 |
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
public function checkName(): Attribute |
94
|
|
|
{ |
95
|
5 |
|
return $this->getAccessConfig(__FUNCTION__); |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
public function checkEmail(): Attribute |
99
|
|
|
{ |
100
|
4 |
|
return $this->getAccessConfig(__FUNCTION__); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
public function needVerification(): bool |
104
|
|
|
{ |
105
|
1 |
|
return $this->check_pin || |
|
|
|
|
106
|
1 |
|
$this->check_email || |
|
|
|
|
107
|
1 |
|
$this->check_name; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function link(): string |
111
|
|
|
{ |
112
|
1 |
|
return $this->linkable->restrictedLink($this); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
public function cookieName(): string |
116
|
|
|
{ |
117
|
4 |
|
return "ral_{$this->uuid}"; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function openActionFromCookie(\Illuminate\Http\Request $request): ?RestrictedLinkOpenAction |
121
|
|
|
{ |
122
|
1 |
|
$openUuid = $request->cookie($this->cookieName()); |
123
|
1 |
|
if ($openUuid) { |
124
|
1 |
|
return RestrictedAccess::linkOpenActionModel()::query()->where('uuid', $openUuid)->first(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function verifiedOpenActionFromCookie(\Illuminate\Http\Request $request): ?RestrictedLinkOpenAction |
131
|
|
|
{ |
132
|
1 |
|
$open = $this->openActionFromCookie($request); |
133
|
1 |
|
if($open?->verified($request)) { |
134
|
1 |
|
return $open; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
public static function scopeByKey(Builder $query, string $uuid, Model|int|null $relatedModel = null): Builder |
141
|
|
|
{ |
142
|
1 |
|
$query->where('uuid', $uuid); |
143
|
|
|
|
144
|
1 |
|
if ($relatedModel) { |
145
|
1 |
|
if ($relatedModel instanceof Model) { |
146
|
1 |
|
$relatedModel = $relatedModel->getKey(); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$query->whereHas('linkable', fn (Builder $q) => $q->whereKey($relatedModel)); |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
return $query; |
153
|
|
|
} |
154
|
|
|
|
155
|
14 |
|
protected static function newFactory(): RestrictedLinkFactory |
156
|
|
|
{ |
157
|
14 |
|
return RestrictedLinkFactory::new(); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.