Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/UserObserver.php
Show First 20 Lines • Show All 293 Lines • ▼ Show 20 Lines | class UserObserver | ||||
* Handle the user "restored" event. | * Handle the user "restored" event. | ||||
* | * | ||||
* @param \App\User $user The user | * @param \App\User $user The user | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function restored(User $user) | public function restored(User $user) | ||||
{ | { | ||||
$wallets = $user->wallets()->pluck('id')->all(); | |||||
// Restore user entitlements | // Restore user entitlements | ||||
// We'll restore only these that were deleted last. So, first we get | \App\Entitlement::restoreEntitlementsFor($user); | ||||
// the maximum deleted_at timestamp and then use it to select | |||||
// entitlements for restore | |||||
$deleted_at = \App\Entitlement::withTrashed() | |||||
->where('entitleable_id', $user->id) | |||||
->where('entitleable_type', User::class) | |||||
->max('deleted_at'); | |||||
if ($deleted_at) { | |||||
$threshold = (new \Carbon\Carbon($deleted_at))->subMinute(); | |||||
// We need at least the user domain so it can be created in ldap. | // We need at least the user domain so it can be created in ldap. | ||||
// FIXME: What if the domain is owned by someone else? | // FIXME: What if the domain is owned by someone else? | ||||
$domain = $user->domain(); | $domain = $user->domain(); | ||||
if ($domain->trashed() && !$domain->isPublic()) { | if ($domain->trashed() && !$domain->isPublic()) { | ||||
// Note: Domain entitlements will be restored by the DomainObserver | // Note: Domain entitlements will be restored by the DomainObserver | ||||
$domain->restore(); | $domain->restore(); | ||||
} | } | ||||
// Restore user entitlements | |||||
\App\Entitlement::withTrashed() | |||||
->where('entitleable_id', $user->id) | |||||
->where('entitleable_type', User::class) | |||||
->where('deleted_at', '>=', $threshold) | |||||
->update(['updated_at' => now(), 'deleted_at' => null]); | |||||
// Note: We're assuming that cost of entitlements was correct | |||||
// on user deletion, so we don't have to re-calculate it again. | |||||
} | |||||
// FIXME: Should we reset user aliases? or re-validate them in any way? | // FIXME: Should we reset user aliases? or re-validate them in any way? | ||||
// Create user record in LDAP, then run the verification process | // Create user record in LDAP, then run the verification process | ||||
$chain = [ | $chain = [ | ||||
new \App\Jobs\User\VerifyJob($user->id), | new \App\Jobs\User\VerifyJob($user->id), | ||||
]; | ]; | ||||
\App\Jobs\User\CreateJob::withChain($chain)->dispatch($user->id); | \App\Jobs\User\CreateJob::withChain($chain)->dispatch($user->id); | ||||
Show All 28 Lines |