-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathUser.php
More file actions
547 lines (454 loc) · 14.6 KB
/
User.php
File metadata and controls
547 lines (454 loc) · 14.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Enums\PriceTier;
use App\Enums\Subscription;
use App\Enums\TeamUserStatus;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasName;
use Filament\Panel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements FilamentUser, HasName
{
use Billable, HasApiTokens, HasFactory, Notifiable;
protected $guarded = [];
protected $hidden = [
'password',
'remember_token',
'github_token',
];
public function getFilamentName(): string
{
return $this->attributes['display_name'] ?? $this->name ?? $this->email;
}
public function canAccessPanel(Panel $panel): bool
{
return $this->isAdmin();
}
public function isAdmin(): bool
{
return in_array($this->email, config('filament.users'), true);
}
/**
* @return HasOne<Team>
*/
public function ownedTeam(): HasOne
{
return $this->hasOne(Team::class);
}
/**
* Get the team owner if this user is an active team member.
*/
public function getTeamOwner(): ?self
{
$membership = $this->activeTeamMembership();
if (! $membership) {
return null;
}
return $membership->team->owner;
}
/**
* @return HasMany<License>
*/
public function licenses(): HasMany
{
return $this->hasMany(License::class);
}
/**
* @return HasMany<WallOfLoveSubmission>
*/
public function wallOfLoveSubmissions(): HasMany
{
return $this->hasMany(WallOfLoveSubmission::class);
}
/**
* @return HasMany<Plugin>
*/
public function plugins(): HasMany
{
return $this->hasMany(Plugin::class);
}
/**
* @return HasMany<PluginLicense>
*/
public function pluginLicenses(): HasMany
{
return $this->hasMany(PluginLicense::class);
}
/**
* @return HasMany<ProductLicense>
*/
public function productLicenses(): HasMany
{
return $this->hasMany(ProductLicense::class);
}
/**
* Check if user has a license for a specific product.
*/
public function hasProductLicense(Product $product): bool
{
if ($this->productLicenses()->forProduct($product)->exists()) {
return true;
}
return $this->hasProductAccessViaTeam($product);
}
/**
* @return HasOne<DeveloperAccount>
*/
public function developerAccount(): HasOne
{
return $this->hasOne(DeveloperAccount::class);
}
/**
* @return HasMany<TeamUser>
*/
public function teamMemberships(): HasMany
{
return $this->hasMany(TeamUser::class);
}
public function isUltraTeamMember(): bool
{
// Team owners count as members
if ($this->ownedTeam && ! $this->ownedTeam->is_suspended) {
return true;
}
return TeamUser::query()
->where('user_id', $this->id)
->where('status', TeamUserStatus::Active)
->whereHas('team', fn ($query) => $query->where('is_suspended', false))
->exists();
}
public function activeTeamMembership(): ?TeamUser
{
return TeamUser::query()
->where('user_id', $this->id)
->where('status', TeamUserStatus::Active)
->whereHas('team', fn ($query) => $query->where('is_suspended', false))
->with('team')
->first();
}
/**
* @return \Illuminate\Database\Eloquent\Collection<int, TeamUser>
*/
public function activeTeamMemberships(): \Illuminate\Database\Eloquent\Collection
{
return TeamUser::query()
->where('user_id', $this->id)
->where('status', TeamUserStatus::Active)
->whereHas('team', fn ($query) => $query->where('is_suspended', false))
->with('team')
->get();
}
public function hasProductAccessViaTeam(Product $product): bool
{
$membership = $this->activeTeamMembership();
if (! $membership) {
return false;
}
// Check the owner's direct product licenses only (not via team) to avoid recursion
return $membership->team->owner->productLicenses()
->forProduct($product)
->exists();
}
public function hasActiveMaxLicense(): bool
{
return $this->licenses()
->where('policy_name', 'max')
->where('is_suspended', false)
->whereActive()
->exists();
}
public function hasActiveMaxSubLicense(): bool
{
return SubLicense::query()
->where('assigned_email', $this->email)
->where('is_suspended', false)
->whereActive()
->whereHas('parentLicense', function ($query): void {
$query->where('policy_name', 'max')
->where('is_suspended', false)
->whereActive();
})
->exists();
}
public function hasMaxAccess(): bool
{
return $this->hasActiveMaxLicense() || $this->hasActiveMaxSubLicense();
}
/**
* Check if the user's subscription is a comped (free) subscription.
* Covers both legacy comped (is_comped flag) and comped Ultra price.
*/
public function hasCompedSubscription(): bool
{
$subscription = $this->subscription();
if (! $subscription || ! $subscription->active()) {
return false;
}
if ($subscription->is_comped) {
return true;
}
$compedPriceId = config('subscriptions.plans.max.stripe_price_id_comped');
return $compedPriceId && $this->subscribedToPrice($compedPriceId);
}
public function hasActiveUltraSubscription(): bool
{
$subscription = $this->subscription();
if (! $subscription) {
return false;
}
// Comped Ultra subs use a dedicated price — always grant Ultra access
$compedUltraPriceId = config('subscriptions.plans.max.stripe_price_id_comped');
if ($compedUltraPriceId && $this->subscribedToPrice($compedUltraPriceId)) {
return true;
}
// Legacy comped Max subs should not get Ultra access
if ($subscription->is_comped) {
return false;
}
return $this->subscribedToPrice(array_filter([
config('subscriptions.plans.max.stripe_price_id'),
config('subscriptions.plans.max.stripe_price_id_monthly'),
config('subscriptions.plans.max.stripe_price_id_eap'),
config('subscriptions.plans.max.stripe_price_id_discounted'),
]));
}
/**
* Check if the user has Ultra access (paying or comped Ultra),
* qualifying them for Ultra benefits like Teams and free plugins.
*/
public function hasUltraAccess(): bool
{
$subscription = $this->subscription();
if (! $subscription || ! $subscription->active()) {
return false;
}
// Comped Ultra subs always get full access
$compedUltraPriceId = config('subscriptions.plans.max.stripe_price_id_comped');
if ($compedUltraPriceId && $this->subscribedToPrice($compedUltraPriceId)) {
return true;
}
$planPriceId = $subscription->stripe_price;
if (! $planPriceId) {
foreach ($subscription->items as $item) {
if (! Subscription::isExtraSeatPrice($item->stripe_price)) {
$planPriceId = $item->stripe_price;
break;
}
}
}
if (! $planPriceId) {
return false;
}
try {
if (Subscription::fromStripePriceId($planPriceId) !== Subscription::Max) {
return false;
}
} catch (\RuntimeException) {
return false;
}
// Legacy comped Max subs don't get Ultra access
return ! $subscription->is_comped;
}
/**
* Check if user was an Early Access Program customer.
* EAP customers purchased before June 1, 2025.
*/
public function isEapCustomer(): bool
{
return $this->licenses()
->where('created_at', '<', '2025-06-01 00:00:00')
->exists();
}
/**
* Get all price tiers the user is eligible for.
* Always includes 'regular', plus any special tiers based on their status.
*
* @return array<PriceTier>
*/
public function getEligiblePriceTiers(): array
{
$tiers = [PriceTier::Regular];
if ($this->subscribed()) {
$tiers[] = PriceTier::Subscriber;
}
if ($this->isEapCustomer()) {
$tiers[] = PriceTier::Eap;
}
return $tiers;
}
public function hasDiscordConnected(): bool
{
return ! empty($this->discord_id);
}
public function hasActualLicense(): bool
{
return $this->licenses()->exists();
}
protected function displayName(): Attribute
{
return Attribute::make(get: function () {
return $this->attributes['display_name'] ?? $this->name ?? 'Unknown';
});
}
protected function firstName(): Attribute
{
return Attribute::make(get: function () {
if (empty($this->name)) {
return null;
}
$nameParts = explode(' ', $this->name, 2);
return $nameParts[0];
});
}
protected function lastName(): Attribute
{
return Attribute::make(get: function () {
if (empty($this->name)) {
return null;
}
$nameParts = explode(' ', $this->name, 2);
return $nameParts[1] ?? null;
});
}
public function findStripeCustomerRecords(): Collection
{
$search = static::stripe()->customers->search([
'query' => 'email:"'.$this->email.'"',
]);
return collect($search->data);
}
public function getPluginLicenseKey(): string
{
if (! $this->plugin_license_key) {
$this->plugin_license_key = bin2hex(random_bytes(32));
$this->save();
}
return $this->plugin_license_key;
}
public function regeneratePluginLicenseKey(): string
{
$this->plugin_license_key = bin2hex(random_bytes(32));
$this->save();
return $this->plugin_license_key;
}
public function hasPluginAccess(Plugin $plugin): bool
{
if ($plugin->isFree()) {
return true;
}
// Authors always have access to their own plugins
if ($plugin->user_id === $this->id) {
return true;
}
if ($this->pluginLicenses()->forPlugin($plugin)->active()->exists()) {
return true;
}
// Ultra team members get access to all official (first-party) plugins
if ($plugin->isOfficial() && $this->isUltraTeamMember()) {
return true;
}
// Team members get access to plugins the team owner has purchased
$teamOwner = $this->getTeamOwner();
if ($teamOwner && $teamOwner->pluginLicenses()->forPlugin($plugin)->active()->exists()) {
return true;
}
return false;
}
public function getGitHubToken(): ?string
{
if (! $this->github_token) {
return null;
}
try {
return decrypt($this->github_token);
} catch (\Exception) {
return null;
}
}
public function hasGitHubToken(): bool
{
return $this->getGitHubToken() !== null;
}
/**
* Plugin names that are available for free to eligible subscribers.
*/
public const FREE_PLUGINS_OFFER = [
'nativephp/mobile-biometrics',
'nativephp/mobile-geolocation',
'nativephp/mobile-firebase',
'nativephp/mobile-secure-storage',
'nativephp/mobile-scanner',
];
/**
* Check if user is eligible for the free plugins offer.
* Eligible if they purchased or renewed since Nov 1st 2025.
*/
public function isEligibleForFreePluginsOffer(): bool
{
$cutoffDate = '2025-11-01 00:00:00';
// Check for licenses created since the cutoff (new purchases)
$hasRecentLicense = $this->licenses()
->where('created_at', '>=', $cutoffDate)
->exists();
if ($hasRecentLicense) {
return true;
}
// Check for active subscription renewals (subscription updated since cutoff)
// This catches renewals where the subscription was updated
$hasRecentRenewal = $this->subscriptions()
->where('stripe_status', 'active')
->where('updated_at', '>=', $cutoffDate)
->exists();
return $hasRecentRenewal;
}
/**
* Check if user has already claimed all free plugins.
*/
public function hasClaimedFreePlugins(): bool
{
$freePluginIds = Plugin::query()
->whereIn('name', self::FREE_PLUGINS_OFFER)
->pluck('id');
if ($freePluginIds->isEmpty()) {
return false;
}
// Check if user has licenses for all the free plugins
$claimedCount = $this->pluginLicenses()
->whereIn('plugin_id', $freePluginIds)
->count();
return $claimedCount >= $freePluginIds->count();
}
/**
* Check if user should see the free plugins offer banner.
* Offer expires on 31st May 2026.
*/
public function shouldSeeFreePluginsOffer(): bool
{
$offerExpiresAt = '2026-05-31 23:59:59';
if (now()->gt($offerExpiresAt)) {
return false;
}
return $this->isEligibleForFreePluginsOffer() && ! $this->hasClaimedFreePlugins();
}
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'receives_notification_emails' => 'boolean',
'receives_new_plugin_notifications' => 'boolean',
'mobile_repo_access_granted_at' => 'datetime',
'claude_plugins_repo_access_granted_at' => 'datetime',
'discord_role_granted_at' => 'datetime',
];
}
}