Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/UserObserver.php
Show First 20 Lines • Show All 355 Lines • ▼ Show 20 Lines | class UserObserver | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function retrieving(User $user) | public function retrieving(User $user) | ||||
{ | { | ||||
// TODO \App\Jobs\User\ReadJob::dispatch($user->id); | // TODO \App\Jobs\User\ReadJob::dispatch($user->id); | ||||
} | } | ||||
/** | /** | ||||
* Handle the "updating" event. | * Handle the "updated" event. | ||||
* | * | ||||
* @param User $user The user that is being updated. | * @param User $user The user that is being updated. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function updating(User $user) | public function updated(User $user) | ||||
{ | { | ||||
\App\Jobs\User\UpdateJob::dispatch($user->id); | \App\Jobs\User\UpdateJob::dispatch($user->id); | ||||
$oldStatus = $user->getOriginal('status'); | |||||
$newStatus = $user->status; | |||||
if (($oldStatus & User::STATUS_DEGRADED) !== ($newStatus & User::STATUS_DEGRADED)) { | |||||
$wallets = []; | |||||
$isDegraded = $user->isDegraded(); | |||||
// Charge all entitlements as if they were being deleted, | |||||
// but don't delete them. Just debit the wallet and update | |||||
// entitlements' updated_at timestamp. On un-degrade we still | |||||
// update updated_at, but with no debit (the cost is 0 on a degraded account). | |||||
foreach ($user->wallets as $wallet) { | |||||
$wallet->updateEntitlements($isDegraded); | |||||
// Remember time of the degradation for sending periodic reminders | |||||
// and reset it on un-degradation | |||||
$val = $isDegraded ? \Carbon\Carbon::now()->toDateTimeString() : null; | |||||
$wallet->setSetting('degraded_last_reminder', $val); | |||||
$wallets[] = $wallet->id; | |||||
} | |||||
// (Un-)degrade users by invoking an update job. | |||||
// LDAP backend will read the wallet owner's degraded status and | |||||
// set LDAP attributes accordingly. | |||||
// We do not change their status as their wallets have its own state | |||||
\App\Entitlement::whereIn('wallet_id', $wallets) | |||||
->where('entitleable_id', '!=', $user->id) | |||||
->where('entitleable_type', User::class) | |||||
->pluck('entitleable_id') | |||||
->unique() | |||||
->each(function ($user_id) { | |||||
\App\Jobs\User\UpdateJob::dispatch($user_id); | |||||
}); | |||||
} | |||||
} | } | ||||
} | } |