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\Group; | ||||
use App\Resource; | use App\Resource; | ||||
use App\SharedFolder; | |||||
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 127 Lines • ▼ Show 20 Lines | public function deleting(User $user) | ||||
// 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 = []; | $groups = []; | ||||
$resources = []; | $resources = []; | ||||
$folders = []; | |||||
$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) { | } elseif ($entitlement->entitleable_type == Group::class) { | ||||
$groups[] = $entitlement->entitleable_id; | $groups[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == Resource::class) { | } elseif ($entitlement->entitleable_type == Resource::class) { | ||||
$resources[] = $entitlement->entitleable_id; | $resources[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == SharedFolder::class) { | |||||
$folders[] = $entitlement->entitleable_id; | |||||
} else { | } else { | ||||
$entitlements[] = $entitlement; | $entitlements[] = $entitlement; | ||||
} | } | ||||
} | } | ||||
// 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)) { | ||||
Show All 15 Lines | public function deleting(User $user) | ||||
} | } | ||||
if (!empty($resources)) { | if (!empty($resources)) { | ||||
foreach (Resource::whereIn('id', array_unique($resources))->get() as $_resource) { | foreach (Resource::whereIn('id', array_unique($resources))->get() as $_resource) { | ||||
$_resource->delete(); | $_resource->delete(); | ||||
} | } | ||||
} | } | ||||
if (!empty($folders)) { | |||||
foreach (SharedFolder::whereIn('id', array_unique($folders))->get() as $_folder) { | |||||
$_folder->delete(); | |||||
} | |||||
} | |||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
$entitlement->delete(); | $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); | ||||
Show All 15 Lines | public function forceDeleting(User $user) | ||||
// 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 = []; | $groups = []; | ||||
$resources = []; | $resources = []; | ||||
$folders = []; | |||||
$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) { | } elseif ($entitlement->entitleable_type == Group::class) { | ||||
$groups[] = $entitlement->entitleable_id; | $groups[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == Resource::class) { | } elseif ($entitlement->entitleable_type == Resource::class) { | ||||
$resources[] = $entitlement->entitleable_id; | $resources[] = $entitlement->entitleable_id; | ||||
} elseif ($entitlement->entitleable_type == SharedFolder::class) { | |||||
$folders[] = $entitlement->entitleable_id; | |||||
} | } | ||||
} | } | ||||
// 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) | ||||
Show All 16 Lines | public function forceDeleting(User $user) | ||||
Group::withTrashed()->whereIn('id', array_unique($groups))->forceDelete(); | Group::withTrashed()->whereIn('id', array_unique($groups))->forceDelete(); | ||||
} | } | ||||
// Resources can be just removed | // Resources can be just removed | ||||
if (!empty($resources)) { | if (!empty($resources)) { | ||||
Resource::withTrashed()->whereIn('id', array_unique($resources))->forceDelete(); | Resource::withTrashed()->whereIn('id', array_unique($resources))->forceDelete(); | ||||
} | } | ||||
// Shared folders can be just removed | |||||
if (!empty($folders)) { | |||||
SharedFolder::withTrashed()->whereIn('id', array_unique($folders))->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) | ||||
->whereIn('object_id', $wallets) | ->whereIn('object_id', $wallets) | ||||
->delete(); | ->delete(); | ||||
▲ Show 20 Lines • Show All 86 Lines • Show Last 20 Lines |