Changeset View
Changeset View
Standalone View
Standalone View
src/app/Observers/UserObserver.php
<?php | <?php | ||||
namespace App\Observers; | namespace App\Observers; | ||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Group; | |||||
use App\Transaction; | use App\Transaction; | ||||
use App\User; | use App\User; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
class UserObserver | class UserObserver | ||||
{ | { | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | public function deleting(User $user) | ||||
$entitlement->delete(); | $entitlement->delete(); | ||||
} | } | ||||
// Remove owned users/domains | // Remove owned users/domains | ||||
$wallets = $user->wallets()->pluck('id')->all(); | $wallets = $user->wallets()->pluck('id')->all(); | ||||
$assignments = Entitlement::whereIn('wallet_id', $wallets)->get(); | $assignments = Entitlement::whereIn('wallet_id', $wallets)->get(); | ||||
$users = []; | $users = []; | ||||
$domains = []; | $domains = []; | ||||
$groups = []; | |||||
$entitlements = []; | $entitlements = []; | ||||
foreach ($assignments as $entitlement) { | foreach ($assignments as $entitlement) { | ||||
if ($entitlement->entitleable_type == Domain::class) { | if ($entitlement->entitleable_type == Domain::class) { | ||||
$domains[] = $entitlement->entitleable_id; | $domains[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == User::class && $entitlement->entitleable_id != $user->id) { | } elseif ($entitlement->entitleable_type == User::class && $entitlement->entitleable_id != $user->id) { | ||||
$users[] = $entitlement->entitleable_id; | $users[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == Group::class) { | |||||
$groups[] = $entitlement->entitleable_id; | |||||
} else { | } else { | ||||
$entitlements[] = $entitlement->id; | $entitlements[] = $entitlement; | ||||
} | } | ||||
} | } | ||||
$users = array_unique($users); | |||||
$domains = array_unique($domains); | |||||
// Domains/users/entitlements need to be deleted one by one to make sure | // Domains/users/entitlements need to be deleted one by one to make sure | ||||
// events are fired and observers can do the proper cleanup. | // events are fired and observers can do the proper cleanup. | ||||
if (!empty($users)) { | if (!empty($users)) { | ||||
foreach (User::whereIn('id', $users)->get() as $_user) { | foreach (User::whereIn('id', array_unique($users))->get() as $_user) { | ||||
$_user->delete(); | $_user->delete(); | ||||
} | } | ||||
} | } | ||||
if (!empty($domains)) { | if (!empty($domains)) { | ||||
foreach (Domain::whereIn('id', $domains)->get() as $_domain) { | foreach (Domain::whereIn('id', array_unique($domains))->get() as $_domain) { | ||||
$_domain->delete(); | $_domain->delete(); | ||||
} | } | ||||
} | } | ||||
if (!empty($entitlements)) { | if (!empty($groups)) { | ||||
Entitlement::whereIn('id', $entitlements)->delete(); | foreach (Group::whereIn('id', array_unique($groups))->get() as $_group) { | ||||
$_group->delete(); | |||||
} | |||||
} | |||||
foreach ($entitlements as $entitlement) { | |||||
$entitlement->delete(); | |||||
} | } | ||||
// FIXME: What do we do with user wallets? | // FIXME: What do we do with user wallets? | ||||
\App\Jobs\User\DeleteJob::dispatch($user->id); | \App\Jobs\User\DeleteJob::dispatch($user->id); | ||||
} | } | ||||
/** | /** | ||||
* Handle the "deleting" event on forceDelete() call. | * Handle the "deleting" event on forceDelete() call. | ||||
* | * | ||||
* @param User $user The user that is being deleted. | * @param User $user The user that is being deleted. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function forceDeleting(User $user) | public function forceDeleting(User $user) | ||||
{ | { | ||||
// TODO: We assume that at this moment all belongings are already soft-deleted. | // TODO: We assume that at this moment all belongings are already soft-deleted. | ||||
// Remove owned users/domains | // Remove owned users/domains | ||||
$wallets = $user->wallets()->pluck('id')->all(); | $wallets = $user->wallets()->pluck('id')->all(); | ||||
$assignments = Entitlement::withTrashed()->whereIn('wallet_id', $wallets)->get(); | $assignments = Entitlement::withTrashed()->whereIn('wallet_id', $wallets)->get(); | ||||
$entitlements = []; | $entitlements = []; | ||||
$domains = []; | $domains = []; | ||||
$groups = []; | |||||
$users = []; | $users = []; | ||||
foreach ($assignments as $entitlement) { | foreach ($assignments as $entitlement) { | ||||
$entitlements[] = $entitlement->id; | $entitlements[] = $entitlement->id; | ||||
if ($entitlement->entitleable_type == Domain::class) { | if ($entitlement->entitleable_type == Domain::class) { | ||||
$domains[] = $entitlement->entitleable_id; | $domains[] = $entitlement->entitleable_id; | ||||
} elseif ( | } elseif ( | ||||
$entitlement->entitleable_type == User::class | $entitlement->entitleable_type == User::class | ||||
&& $entitlement->entitleable_id != $user->id | && $entitlement->entitleable_id != $user->id | ||||
) { | ) { | ||||
$users[] = $entitlement->entitleable_id; | $users[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == Group::class) { | |||||
$groups[] = $entitlement->entitleable_id; | |||||
} | } | ||||
} | } | ||||
$users = array_unique($users); | |||||
$domains = array_unique($domains); | |||||
// Remove the user "direct" entitlements explicitely, if they belong to another | // Remove the user "direct" entitlements explicitely, if they belong to another | ||||
// user's wallet they will not be removed by the wallets foreign key cascade | // user's wallet they will not be removed by the wallets foreign key cascade | ||||
Entitlement::withTrashed() | Entitlement::withTrashed() | ||||
->where('entitleable_id', $user->id) | ->where('entitleable_id', $user->id) | ||||
->where('entitleable_type', User::class) | ->where('entitleable_type', User::class) | ||||
->forceDelete(); | ->forceDelete(); | ||||
// Users need to be deleted one by one to make sure observers can do the proper cleanup. | // Users need to be deleted one by one to make sure observers can do the proper cleanup. | ||||
if (!empty($users)) { | if (!empty($users)) { | ||||
foreach (User::withTrashed()->whereIn('id', $users)->get() as $_user) { | foreach (User::withTrashed()->whereIn('id', array_unique($users))->get() as $_user) { | ||||
$_user->forceDelete(); | $_user->forceDelete(); | ||||
} | } | ||||
} | } | ||||
// Domains can be just removed | // Domains can be just removed | ||||
if (!empty($domains)) { | if (!empty($domains)) { | ||||
Domain::withTrashed()->whereIn('id', $domains)->forceDelete(); | Domain::withTrashed()->whereIn('id', array_unique($domains))->forceDelete(); | ||||
} | |||||
// Groups can be just removed | |||||
if (!empty($groups)) { | |||||
Group::withTrashed()->whereIn('id', array_unique($groups))->forceDelete(); | |||||
} | } | ||||
// Remove transactions, they also have no foreign key constraint | // Remove transactions, they also have no foreign key constraint | ||||
Transaction::where('object_type', Entitlement::class) | Transaction::where('object_type', Entitlement::class) | ||||
->whereIn('object_id', $entitlements) | ->whereIn('object_id', $entitlements) | ||||
->delete(); | ->delete(); | ||||
Transaction::where('object_type', Wallet::class) | Transaction::where('object_type', Wallet::class) | ||||
Show All 30 Lines |