This repository was archived by the owner on Aug 22, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMagicLinkManager.php
More file actions
109 lines (87 loc) · 2.96 KB
/
MagicLinkManager.php
File metadata and controls
109 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Codedge\MagicLink;
use Codedge\MagicLink\Repositories\SettingsRepository;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Statamic\Contracts\Auth\User;
use Statamic\Facades\YAML;
final class MagicLinkManager
{
protected MagicLink $magicLink;
protected Filesystem $files;
protected SettingsRepository $settingsRepository;
protected string $path;
protected User $user;
public function __construct(Filesystem $files, SettingsRepository $settingsRepository)
{
$this->files = $files;
$this->settingsRepository = $settingsRepository;
$this->path = storage_path('statamic-magiclink/magic-links.yaml');
}
public function createForUser(User $user): self
{
$this->user = $user;
$this->magicLink = new MagicLink($user);
return $this;
}
public function redirectTo(string $redirect): self
{
$this->magicLink->setRedirectTo($redirect);
return $this;
}
public function generate(): MagicLink
{
$link = $this->magicLink->generate();
$payload[$this->user->email()] = [
'email' => $this->user->email(),
'expire_time' => $this->magicLink->getExpireTime()->timestamp,
'hash' => $link->getHash(),
'redirect_to' => $this->magicLink->getRedirectTo(),
];
$this->save(collect($payload));
return $link;
}
public function get(): Collection
{
if (! $this->files->exists($this->path)) {
return collect();
}
return collect(YAML::parse($this->files->get($this->path)));
}
public function save(Collection $content)
{
if (! $this->files->isDirectory($dir = dirname($this->path))) {
$this->files->makeDirectory($dir);
}
// Handle already existing entries and overwrite them with the new content
// Each user can only have one magic link!
$existing = $this->get();
$merged = $existing->merge($content);
return $this->files->put($this->path, YAML::dump($merged->all()));
}
/**
* Validate a given email address against the addresses set either in
* - ALLOWED_ADDRESSES
* - ALLOWED_DOMAINS.
*
* If no allowed address or domain is set, the given email is considered valid.
*/
public function validAddress(string $email): bool
{
$valid = false;
if ($this->settingsRepository->allowedAddresses()->count() !== 0) {
if (in_array($email, $this->settingsRepository->allowedAddresses()->toArray())) {
$valid = true;
}
}
if ($this->settingsRepository->allowedDomains()->count() !== 0) {
$parts = explode('@', $email);
if (in_array($parts[1], $this->settingsRepository->allowedDomains()->toArray())
) {
$valid = true;
}
}
return $valid;
}
}