Repository navigation
Expand file tree
/
Copy pathPrepareUserModel.php
More file actions
171 lines (138 loc) · 5.71 KB
/
Copy pathPrepareUserModel.php
File metadata and controls
171 lines (138 loc) · 5.71 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleAuth\Install;
use NyonCode\WireCore\Foundation\Setup\ClassSource;
use NyonCode\WireCore\Foundation\Setup\Contracts\SetupConsole;
use NyonCode\WireCore\Foundation\Setup\Contracts\SetupStep;
use NyonCode\WireCore\Foundation\Setup\SetupOutcome;
use NyonCode\WireCore\Foundation\Setup\SetupState;
/**
* Give the user model what the Fortify features switched on need.
*
* `fortify:install` publishes a config with two-factor and passkeys on, and the
* migrations for both — and leaves the model alone. So an application set up by
* `wire:install` offered "Sign in with a passkey" on its sign-in screen and had
* no way to register one: the profile's passkey card and two-factor card only
* appear for a model that can hold them, and Fortify's own passkey routes
* resolve a `PasskeyUser`. Found on a clean install, where neither worked.
*
* What is added follows the published config, not the defaults: a feature the
* application switched off (the question {@see ConfigureFortify} asks) gets
* nothing. And only where the package behind the feature is installed —
* `laravel/passkeys` arrives with Fortify's passkeys, but a trait that does not
* exist would take the whole application down on its next request.
*
* Runs after {@see ConfigureFortify} (40), whose answers it reads, and before the
* migrations (100).
*/
final readonly class PrepareUserModel implements SetupStep
{
/**
* Feature => what the user model needs for it: traits, then interfaces.
*
* @var array<string, array{0: list<class-string>, 1: list<class-string>}>
*/
public const NEEDS = [
'twoFactorAuthentication' => [['Laravel\\Fortify\\TwoFactorAuthenticatable'], []],
'passkeys' => [['Laravel\\Passkeys\\PasskeyAuthenticatable'], ['Laravel\\Passkeys\\Contracts\\PasskeyUser']],
];
/** How each feature is named to a person reading the installer. */
private const LABELS = [
'twoFactorAuthentication' => 'two-factor',
'passkeys' => 'passkeys',
];
public function label(): string
{
return 'User model';
}
public function state(): SetupState
{
$model = ClassSource::userModel();
if ($model === null || ! is_file(config_path('fortify.php'))) {
return SetupState::Blocked;
}
return $this->missing($model) === [] ? SetupState::Done : SetupState::Pending;
}
public function summary(): string
{
$model = ClassSource::userModel();
if ($model === null) {
return 'no app/Models/User.php to prepare';
}
if (! is_file(config_path('fortify.php'))) {
return 'needs Fortify set up first — there is no config/fortify.php';
}
$missing = $this->missing($model);
return $missing === []
? 'the user model can hold what the sign-in features store'
: 'let the user model hold '.implode(' and ', array_map(static fn (string $feature): string => self::LABELS[$feature], array_keys($missing))).', or those features are switched on and do nothing';
}
public function apply(SetupConsole $console): SetupOutcome
{
$model = ClassSource::userModel();
if ($model === null) {
return SetupOutcome::Skipped;
}
$missing = $this->missing($model);
foreach ($missing as [$traits, $interfaces]) {
foreach ($traits as $trait) {
$model->addTrait($trait);
}
foreach ($interfaces as $interface) {
$model->addInterface($interface);
}
}
if (! $model->save()) {
$console->warn('Could not write '.$model->path().' — add these to the class yourself: '.$this->names($missing).'.');
return SetupOutcome::Failed;
}
if ($missing !== []) {
$console->note('Added to the user model: '.$this->names($missing).'.');
}
return SetupOutcome::Applied;
}
public function package(): string
{
return 'nyoncode/wire-module-auth';
}
public function sort(): int
{
return 45;
}
/**
* The switched-on features whose needs the model does not meet yet.
*
* @return array<string, array{0: list<class-string>, 1: list<class-string>}>
*/
private function missing(ClassSource $model): array
{
$states = (new FortifyFeatures((string) file_get_contents(config_path('fortify.php'))))
->states(array_keys(self::NEEDS));
$missing = [];
foreach (self::NEEDS as $feature => [$traits, $interfaces]) {
// Switched on, installed, and not there yet — a trait whose package
// is missing would take the application down on its next request.
if (($states[$feature] ?? false) === true && self::installed([...$traits, ...$interfaces]) && $model->needs($traits, $interfaces)) {
$missing[$feature] = [$traits, $interfaces];
}
}
return $missing;
}
/** @param list<class-string> $classes */
private static function installed(array $classes): bool
{
return array_filter($classes, static fn (string $class): bool => ! trait_exists($class) && ! interface_exists($class)) === [];
}
/** @param array<string, array{0: list<class-string>, 1: list<class-string>}> $missing */
private function names(array $missing): string
{
$classes = [];
foreach ($missing as [$traits, $interfaces]) {
foreach ([...$traits, ...$interfaces] as $class) {
$parts = explode('\\', $class);
$classes[] = (string) end($parts);
}
}
return implode(', ', $classes);
}
}