Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/UserObserver.php
Show First 20 Lines • Show All 249 Lines • ▼ Show 20 Lines | public function forceDeleting(User $user) | ||||
->delete(); | ->delete(); | ||||
Transaction::where('object_type', Wallet::class) | Transaction::where('object_type', Wallet::class) | ||||
->whereIn('object_id', $wallets) | ->whereIn('object_id', $wallets) | ||||
->delete(); | ->delete(); | ||||
} | } | ||||
/** | /** | ||||
* Handle the user "restoring" event. | |||||
* | |||||
* @param \App\User $user The user | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function restoring(User $user) | |||||
{ | |||||
// Make sure it's not DELETED/LDAP_READY/IMAP_READY/SUSPENDED anymore | |||||
if ($user->isDeleted()) { | |||||
$user->status ^= User::STATUS_DELETED; | |||||
} | |||||
if ($user->isLdapReady()) { | |||||
$user->status ^= User::STATUS_LDAP_READY; | |||||
} | |||||
if ($user->isImapReady()) { | |||||
$user->status ^= User::STATUS_IMAP_READY; | |||||
} | |||||
if ($user->isSuspended()) { | |||||
$user->status ^= User::STATUS_SUSPENDED; | |||||
} | |||||
$user->status |= User::STATUS_ACTIVE; | |||||
// Note: $user->save() is invoked between 'restoring' and 'restored' events | |||||
} | |||||
/** | |||||
* Handle the user "restored" event. | |||||
* | |||||
* @param \App\User $user The user | |||||
* | |||||
* @return void | |||||
vanmeeuwen: Here too, please avoid touching `updated_at`. | |||||
*/ | |||||
public function restored(User $user) | |||||
{ | |||||
$wallets = $user->wallets()->pluck('id')->all(); | |||||
// Restore user entitlements | |||||
// We'll restore only these that were deleted last. So, first we get | |||||
// 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. | |||||
// FIXME: What if the domain is owned by someone else? | |||||
$domain = $user->domain(); | |||||
if ($domain->trashed() && !$domain->isPublic()) { | |||||
// Note: Domain entitlements will be restored by the DomainObserver | |||||
$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? | |||||
// Create user record in LDAP, then run the verification process | |||||
$chain = [ | |||||
new \App\Jobs\User\VerifyJob($user->id), | |||||
]; | |||||
\App\Jobs\User\CreateJob::withChain($chain)->dispatch($user->id); | |||||
} | |||||
/** | |||||
* Handle the "retrieving" event. | * Handle the "retrieving" event. | ||||
* | * | ||||
* @param User $user The user that is being retrieved. | * @param User $user The user that is being retrieved. | ||||
* | * | ||||
* @todo This is useful for audit. | * @todo This is useful for audit. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
Show All 17 Lines |
Here too, please avoid touching updated_at.