Repository navigation
Expand file tree
/
Copy pathWireModuleUsersServiceProvider.php
More file actions
341 lines (304 loc) · 13.6 KB
/
Copy pathWireModuleUsersServiceProvider.php
File metadata and controls
341 lines (304 loc) · 13.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleUsers;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;
use NyonCode\LaravelPackageToolkit\Commands\InstallCommand;
use NyonCode\LaravelPackageToolkit\Packager;
use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\WireCore\Core\Plugin\PluginManager;
use NyonCode\WireCore\Core\Tenancy\Events\TenantEntered;
use NyonCode\WireCore\Core\Tenancy\Events\TenantLeft;
use NyonCode\WireCore\Foundation\Setup\SetupRegistry;
use NyonCode\WireCore\Foundation\View\PageChrome;
use NyonCode\WireModuleUsers\Console\WireAssignRoleCommand;
use NyonCode\WireModuleUsers\Console\WireRevokeRoleCommand;
use NyonCode\WireModuleUsers\Console\WireUserCommand;
use NyonCode\WireModuleUsers\Http\Middleware\SetCurrentTeam;
use NyonCode\WireModuleUsers\Install\CreateFirstAdministrator;
use NyonCode\WireModuleUsers\Install\EnableRoles;
use NyonCode\WireModuleUsers\Install\EnableTeams;
use NyonCode\WireModuleUsers\Support\Avatars;
use NyonCode\WireModuleUsers\Support\EmailVerification;
use NyonCode\WireModuleUsers\Support\Impersonation;
use NyonCode\WireModuleUsers\Support\Permissions;
use NyonCode\WireModuleUsers\Support\Roles;
use NyonCode\WireModuleUsers\Support\Teams;
use NyonCode\WireModuleUsers\Support\TwoFactor;
/**
* A module that arrives as a package.
*
* The reference implementation of the path ADR 0029 describes: an application
* installs this and gets a users area — it edits no config, lists no class, and
* the module registers itself.
*
* **`resolving`, in the register phase.** The callback runs while the container
* builds the manager, so the module is in the list before `PluginManager::boot()`
* and before the core provider spreads declarations into the registries.
* Registering any later is refused outright, because a module that arrives then
* looks installed and does nothing. The `has()` guard keeps a provider that
* boots twice — tests do — idempotent.
*/
class WireModuleUsersServiceProvider extends PackageServiceProvider
{
/**
* @throws \Exception
*/
public function configure(Packager $packager): void
{
$packager
->name('WireModuleUsers')
->hasShortName('wire-module-users')
->registeredPackage(function (): void {
$this->app->resolving(PluginManager::class, function (PluginManager $manager): void {
if (! $manager->has('users')) {
$manager->register(new UsersModule);
}
});
// In here rather than in a second `registeredPackage()`: the
// toolkit's lifecycle hooks hold one closure each and assign
// rather than append, so a second call would drop the module
// registration above without a word.
SetupRegistry::instance()->register(
EnableRoles::class,
EnableTeams::class,
CreateFirstAdministrator::class,
);
})
->hasConfig()
->hasViews()
->hasTranslations()
->bootedPackage(function (): void {
$this->bootTeams();
$this->bootImpersonation();
$this->bootUserMenu();
$this->bootEmailVerification();
})
// `wire:user` rather than `wire-module-users:user`: it is typed by
// hand and lives beside `wire:install`, the same way core ships
// `make:wire-dashboard` rather than `wire-core:make-dashboard`.
->hasCommands([WireUserCommand::class, WireAssignRoleCommand::class, WireRevokeRoleCommand::class])
->hasInstallCommand(function (InstallCommand $command): void {
$command
->publishConfig()
->afterInstallation(fn (InstallCommand $installer) => $this->reportEnvironment($installer));
})
->hasAbout();
}
/**
* Keep a verified flag tied to the address it was granted for.
*
* Two Eloquent events rather than a form hook, for the reason
* {@see EmailVerification} gives: three screens in this module write the
* address, an application may add a fourth, and `Form::afterSave()` holds
* one closure — a rule installed there is a rule the next hook removes.
*
* `updating` clears the flag in the same statement that writes the address,
* so the row is never briefly a verified stranger. `updated` asks for the
* new address to be proven, and only then: a notification sent before the
* write would advertise an address that a failed save left unwritten.
*
* Registered on the configured model rather than globally — this is a rule
* about *this* application's user, and a listener on `Model` would clear a
* column on anything that happened to have one.
*/
protected function bootEmailVerification(): void
{
if (! EmailVerification::resetsOnChange()) {
return;
}
$model = config('wire-module-users.model');
if (! is_string($model) || ! is_subclass_of($model, Model::class)) {
return;
}
$model::updating(static function (Model $user): void {
EmailVerification::forget($user);
});
$model::updated(static function (Model $user): void {
if ($user->wasChanged(EmailVerification::column())) {
EmailVerification::requestProof($user);
}
});
}
/**
* Everything teams need, and only where this application has them.
*
* Two registrations, and they are not the same kind of thing. The
* middleware is correctness: with `permission.teams` on, every permission
* read is scoped by whatever team the registrar was last told about, so a
* request that never tells it sees the previous one — which in a worker is
* somebody else's. The switcher is a view, and it reaches the shell's top
* bar through the registry rather than by editing a layout this package does
* not own.
*
* Both are skipped outright where teams are off, because a middleware on
* every web request and a component in every top bar are not free.
*/
/**
* The way back from an impersonation, in the top bar — only where the
* application turned impersonation on (Support\Impersonation).
*/
protected function bootImpersonation(): void
{
if (! Impersonation::enabled()) {
return;
}
$this->app->make(PageChrome::class)->add(
'wire-module-users::impersonation-banner',
PageChrome::TOPBAR,
);
}
protected function bootTeams(): void
{
// A team belongs to a company (ADR 0040 §6), and the company is entered
// after the `web` group has run: follow it when it changes. Registered
// whether or not teams are on at boot — followTenant() asks at the time.
Event::listen([TenantEntered::class, TenantLeft::class], static fn () => Teams::followTenant());
if (! Teams::enabled()) {
return;
}
// Pushed onto the group rather than aliased for a route to opt into:
// the team scopes authorization, and a page that forgot the alias would
// authorize against the wrong one while looking correct.
$this->app->make(Kernel::class)->appendMiddlewareToGroup('web', SetCurrentTeam::class);
$this->app->make(PageChrome::class)->add(
'wire-module-users::team-switcher',
PageChrome::TOPBAR,
);
}
/**
* Put the link to the profile page in the user's own menu.
*
* This module owns the page, so this module contributes the link. The shell
* draws a menu and can name no module's views; before the `USER_MENU` region
* existed, the only place for this link was a layout slot every application
* wrote by hand — reaching into this package's translations from a file this
* package cannot see.
*
* Registered whether or not the shell has booted, because provider order is
* composer's discovery order: a check for the shell here would answer false
* for one that boots afterwards, and the entry would be missing from a menu
* that exists. Whether the page is routed at all is the view's question, and
* it is asked at render because zones give it several answers.
*
* The sort keeps it above the auth module's way out. Neither package can see
* the other, and "Sign out" first reads as a bug.
*/
protected function bootUserMenu(): void
{
if (! config('wire-module-users.profile.menu_item', true)) {
return;
}
$this->app->make(PageChrome::class)->add(
'wire-module-users::profile-menu-item',
PageChrome::USER_MENU,
sort: 10,
);
}
/**
* Say what this installation actually got.
*
* Roles are the half that depends on the application: the module works
* without them, and an installer that stayed silent about it would leave
* someone looking for a screen that was never going to appear.
*/
protected function reportEnvironment(InstallCommand $command): void
{
$command->comment(' ✅ Users are registered as the `users` module');
$this->reportPermissions($command);
foreach ($this->optionalFeatures() as $line) {
$command->comment($line);
}
if (Roles::enabled()) {
$command->comment(' ✅ Roles found — role management is part of this installation');
return;
}
// Named where it is actionable rather than as a nested aside: this is the
// installation that has no roles, and this is the line that gives it some.
$command->comment(' ↩︎ No roles: install nyoncode/laravel-permission-extended and use ITS HasRoles trait on your user model (not Spatie\'s)');
}
/**
* Everything optional this installation did or did not get.
*
* Reported rather than left to be discovered: an avatar upload that never
* appears because a column is missing, and a two-factor card that never
* appears because Fortify is not installed, are both indistinguishable from
* a broken package until somebody says so.
*
* @return array<int, string>
*/
protected function optionalFeatures(): array
{
return [
Avatars::enabled()
? ' ✅ Avatars: the `'.Avatars::column().'` column is there, so the profile has a photo'
: ' ↩︎ No avatars: add a nullable `'.Avatars::column().'` string column to your users table',
TwoFactor::enabled()
? ' ✅ Two-factor: Fortify is installed and its feature is on — the profile card drives it'
: ' ↩︎ No two-factor: install laravel/fortify and enable its twoFactorAuthentication feature',
Teams::enabled()
? ' ✅ Teams: permission.teams is on — the top bar gets a switcher'
: ' ↩︎ No teams: install nyoncode/laravel-permission-extended, set permission.teams to true, and point wire-module-users.teams.model at your team model',
];
}
/**
* Say which screens are guarded, and say it loudest when none are.
*
* These abilities ship with real defaults precisely so this is a non-event
* on a normal install. The line that matters is the other one: an
* installation that set them to null has opened the screen that assigns
* roles, and that decision should be visible at install time rather than
* discovered later.
*/
protected function reportPermissions(InstallCommand $command): void
{
$open = [];
foreach (['users', 'roles'] as $resource) {
foreach (['viewAny', 'view', 'create', 'update'] as $page) {
if (Permissions::for($resource, $page) === null) {
$open[] = "{$resource}.{$page}";
}
}
}
if ($open === []) {
$command->comment(' ✅ User and role screens require an ability — see wire-module-users.permissions');
return;
}
$command->comment(' ⚠️ Open to anyone the panel admits: '.implode(', ', $open));
$command->comment(' Name an ability in wire-module-users.permissions — these screens set passwords and assign roles');
}
/** How the `about` row puts it: guarded, partly guarded, or not at all. */
protected static function permissionSummary(): string
{
$named = 0;
$total = 0;
foreach (['users', 'roles'] as $resource) {
foreach (['viewAny', 'view', 'create', 'update'] as $page) {
$total++;
if (Permissions::for($resource, $page) !== null) {
$named++;
}
}
}
return match (true) {
$named === $total => 'all screens guarded',
$named === 0 => 'OPEN — no ability required',
default => "{$named} of {$total} screens guarded",
};
}
/**
* @return array<string, string>
*/
public function aboutData(): array
{
return [
'User model' => (string) config('wire-module-users.model'),
'Permissions' => self::permissionSummary(),
'Roles' => Roles::enabled() ? 'enabled' : 'off',
'Avatars' => Avatars::enabled() ? 'enabled' : 'off',
'Two-factor' => TwoFactor::enabled() ? 'enabled' : 'off',
'Teams' => Teams::enabled() ? 'enabled' : 'off',
];
}
}