forked from laravel/jetstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJetstream.php
490 lines (432 loc) · 11.3 KB
/
Jetstream.php
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
namespace Laravel\Jetstream;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Laravel\Jetstream\Contracts\AddsTeamMembers;
use Laravel\Jetstream\Contracts\CreatesTeams;
use Laravel\Jetstream\Contracts\DeletesTeams;
use Laravel\Jetstream\Contracts\DeletesUsers;
use Laravel\Jetstream\Contracts\InvitesTeamMembers;
use Laravel\Jetstream\Contracts\RemovesTeamMembers;
use Laravel\Jetstream\Contracts\UpdatesTeamNames;
class Jetstream
{
/**
* Indicates if Jetstream routes will be registered.
*
* @var bool
*/
public static $registersRoutes = true;
/**
* The roles that are available to assign to users.
*
* @var array
*/
public static $roles = [];
/**
* The permissions that exist within the application.
*
* @var array
*/
public static $permissions = [];
/**
* The default permissions that should be available to new entities.
*
* @var array
*/
public static $defaultPermissions = [];
/**
* The user model that should be used by Jetstream.
*
* @var string
*/
public static $userModel = 'App\\Models\\User';
/**
* The team model that should be used by Jetstream.
*
* @var string
*/
public static $teamModel = 'App\\Models\\Team';
/**
* The membership model that should be used by Jetstream.
*
* @var string
*/
public static $membershipModel = 'App\\Models\\Membership';
/**
* The team invitation model that should be used by Jetstream.
*
* @var string
*/
public static $teamInvitationModel = 'App\\Models\\TeamInvitation';
/**
* The Inertia manager instance.
*
* @var \Laravel\Jetstream\InertiaManager
*/
public static $inertiaManager;
/**
* Determine if Jetstream has registered roles.
*
* @return bool
*/
public static function hasRoles()
{
return count(static::$roles) > 0;
}
/**
* Find the role with the given key.
*
* @param string $key
* @return \Laravel\Jetstream\Role
*/
public static function findRole(string $key)
{
return static::$roles[$key] ?? null;
}
/**
* Define a role.
*
* @param string $key
* @param string $name
* @param array $permissions
* @return \Laravel\Jetstream\Role
*/
public static function role(string $key, string $name, array $permissions)
{
static::$permissions = collect(array_merge(static::$permissions, $permissions))
->unique()
->sort()
->values()
->all();
return tap(new Role($key, $name, $permissions), function ($role) use ($key) {
static::$roles[$key] = $role;
});
}
/**
* Determine if any permissions have been registered with Jetstream.
*
* @return bool
*/
public static function hasPermissions()
{
return count(static::$permissions) > 0;
}
/**
* Define the available API token permissions.
*
* @param array $permissions
* @return static
*/
public static function permissions(array $permissions)
{
static::$permissions = $permissions;
return new static;
}
/**
* Define the default permissions that should be available to new API tokens.
*
* @param array $permissions
* @return static
*/
public static function defaultApiTokenPermissions(array $permissions)
{
static::$defaultPermissions = $permissions;
return new static;
}
/**
* Return the permissions in the given list that are actually defined permissions for the application.
*
* @param array $permissions
* @return array
*/
public static function validPermissions(array $permissions)
{
return array_values(array_intersect($permissions, static::$permissions));
}
/**
* Determine if Jetstream is managing profile photos.
*
* @return bool
*/
public static function managesProfilePhotos()
{
return Features::managesProfilePhotos();
}
/**
* Determine if Jetstream is supporting API features.
*
* @return bool
*/
public static function hasApiFeatures()
{
return Features::hasApiFeatures();
}
/**
* Determine if Jetstream is supporting team features.
*
* @return bool
*/
public static function hasTeamFeatures()
{
return Features::hasTeamFeatures();
}
/**
* Determine if a given user model utilizes the "HasTeams" trait.
*
* @param \Illuminate\Database\Eloquent\Model
* @return bool
*/
public static function userHasTeamFeatures($user)
{
return (array_key_exists(HasTeams::class, class_uses_recursive($user)) ||
method_exists($user, 'currentTeam')) &&
static::hasTeamFeatures();
}
/**
* Determine if the application is using the terms confirmation feature.
*
* @return bool
*/
public static function hasTermsAndPrivacyPolicyFeature()
{
return Features::hasTermsAndPrivacyPolicyFeature();
}
/**
* Determine if the application is using any account deletion features.
*
* @return bool
*/
public static function hasAccountDeletionFeatures()
{
return Features::hasAccountDeletionFeatures();
}
/**
* Find a user instance by the given ID.
*
* @param int $id
* @return mixed
*/
public static function findUserByIdOrFail($id)
{
return static::newUserModel()->where('id', $id)->firstOrFail();
}
/**
* Find a user instance by the given email address or fail.
*
* @param string $email
* @return mixed
*/
public static function findUserByEmailOrFail(string $email)
{
return static::newUserModel()->where('email', $email)->firstOrFail();
}
/**
* Get the name of the user model used by the application.
*
* @return string
*/
public static function userModel()
{
return static::$userModel;
}
/**
* Get a new instance of the user model.
*
* @return mixed
*/
public static function newUserModel()
{
$model = static::userModel();
return new $model;
}
/**
* Specify the user model that should be used by Jetstream.
*
* @param string $model
* @return static
*/
public static function useUserModel(string $model)
{
static::$userModel = $model;
return new static;
}
/**
* Get the name of the team model used by the application.
*
* @return string
*/
public static function teamModel()
{
return static::$teamModel;
}
/**
* Get a new instance of the team model.
*
* @return mixed
*/
public static function newTeamModel()
{
$model = static::teamModel();
return new $model;
}
/**
* Specify the team model that should be used by Jetstream.
*
* @param string $model
* @return static
*/
public static function useTeamModel(string $model)
{
static::$teamModel = $model;
return new static;
}
/**
* Get the name of the membership model used by the application.
*
* @return string
*/
public static function membershipModel()
{
return static::$membershipModel;
}
/**
* Specify the membership model that should be used by Jetstream.
*
* @param string $model
* @return static
*/
public static function useMembershipModel(string $model)
{
static::$membershipModel = $model;
return new static;
}
/**
* Get the name of the team invitation model used by the application.
*
* @return string
*/
public static function teamInvitationModel()
{
return static::$teamInvitationModel;
}
/**
* Specify the team invitation model that should be used by Jetstream.
*
* @param string $model
* @return static
*/
public static function useTeamInvitationModel(string $model)
{
static::$teamInvitationModel = $model;
return new static;
}
/**
* Register a class / callback that should be used to create teams.
*
* @param string $class
* @return void
*/
public static function createTeamsUsing(string $class)
{
return app()->singleton(CreatesTeams::class, $class);
}
/**
* Register a class / callback that should be used to update team names.
*
* @param string $class
* @return void
*/
public static function updateTeamNamesUsing(string $class)
{
return app()->singleton(UpdatesTeamNames::class, $class);
}
/**
* Register a class / callback that should be used to add team members.
*
* @param string $class
* @return void
*/
public static function addTeamMembersUsing(string $class)
{
return app()->singleton(AddsTeamMembers::class, $class);
}
/**
* Register a class / callback that should be used to add team members.
*
* @param string $class
* @return void
*/
public static function inviteTeamMembersUsing(string $class)
{
return app()->singleton(InvitesTeamMembers::class, $class);
}
/**
* Register a class / callback that should be used to remove team members.
*
* @param string $class
* @return void
*/
public static function removeTeamMembersUsing(string $class)
{
return app()->singleton(RemovesTeamMembers::class, $class);
}
/**
* Register a class / callback that should be used to delete teams.
*
* @param string $class
* @return void
*/
public static function deleteTeamsUsing(string $class)
{
return app()->singleton(DeletesTeams::class, $class);
}
/**
* Register a class / callback that should be used to delete users.
*
* @param string $class
* @return void
*/
public static function deleteUsersUsing(string $class)
{
return app()->singleton(DeletesUsers::class, $class);
}
/**
* Manage Jetstream's Inertia settings.
*
* @return \Laravel\Jetstream\InertiaManager
*/
public static function inertia()
{
if (is_null(static::$inertiaManager)) {
static::$inertiaManager = new InertiaManager;
}
return static::$inertiaManager;
}
/**
* Find the path to a localized Markdown resource.
*
* @param string $name
* @return string|null
*/
public static function localizedMarkdownPath($name)
{
$localName = preg_replace('#(\.md)$#i', '.'.app()->getLocale().'$1', $name);
return Arr::first([
resource_path('markdown/'.$localName),
resource_path('markdown/'.$name),
], function ($path) {
return file_exists($path);
});
}
/**
* Configure Jetstream to not register its routes.
*
* @return static
*/
public static function ignoreRoutes()
{
static::$registersRoutes = false;
return new static;
}
}