|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backstage\Laravel\Users\Commands; |
| 4 | + |
| 5 | +use function Termwind\ask; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use function Laravel\Prompts\info; |
| 8 | + |
| 9 | +use function Laravel\Prompts\text; |
| 10 | +use Illuminate\Support\Collection; |
| 11 | +use Spatie\Permission\Models\Role; |
| 12 | +use function Laravel\Prompts\error; |
| 13 | +use function Laravel\Prompts\table; |
| 14 | +use function Laravel\Prompts\search; |
| 15 | +use function Laravel\Prompts\select; |
| 16 | +use Spatie\Permission\Traits\HasRoles; |
| 17 | +use function Laravel\Prompts\multisearch; |
| 18 | +use Spatie\Permission\Traits\HasPermissions; |
| 19 | +use Backstage\Laravel\Users\Eloquent\Models\User; |
| 20 | + |
| 21 | +class ListUsersCommand extends Command |
| 22 | +{ |
| 23 | + protected $signature = 'users:list {--edit}'; |
| 24 | + |
| 25 | + protected $description = 'List all users in the system.'; |
| 26 | + |
| 27 | + public function handle(): void |
| 28 | + { |
| 29 | + $users = User::all(); |
| 30 | + |
| 31 | + if ($users->isEmpty()) { |
| 32 | + error('No users found.'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + info('Found ' . $users->count() . ' user(s):'); |
| 37 | + |
| 38 | + $this->renderTable($users); |
| 39 | + |
| 40 | + if (!$this->option('edit')) { |
| 41 | + $editingUsers = $this->confirm('Do you want to edit any user?', false); |
| 42 | + |
| 43 | + if (!$editingUsers) { |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $userId = search( |
| 49 | + label: 'Search for the user that should receive the mail', |
| 50 | + options: fn(string $value) => strlen($value) > 0 |
| 51 | + ? User::whereLike('name', "%{$value}%")->pluck('name', 'id')->map(fn($name, $id) => $name . ' (ID: ' . $id . ')')->toArray() |
| 52 | + : [] |
| 53 | + ); |
| 54 | + |
| 55 | + /** |
| 56 | + * @var HasRoles $user |
| 57 | + */ |
| 58 | + $user = User::find($userId); |
| 59 | + |
| 60 | + if (!$user) { |
| 61 | + error('User not found!'); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + info('User selected:'); |
| 66 | + $this->renderTable(collect([$user])); |
| 67 | + |
| 68 | + $changableFields = ['name', 'email', 'roles']; |
| 69 | + |
| 70 | + $fieldToChange = $this->choice('Which field do you want to change?', $changableFields); |
| 71 | + |
| 72 | + switch ($fieldToChange) { |
| 73 | + case 'name': |
| 74 | + $newName = text('Enter the new name:', required: true); |
| 75 | + $user->name = $newName; |
| 76 | + break; |
| 77 | + |
| 78 | + case 'email': |
| 79 | + $newEmail = text('Enter the new email:', required: true); |
| 80 | + $user->email = $newEmail; |
| 81 | + break; |
| 82 | + |
| 83 | + case 'roles': |
| 84 | + $attachingRoles = $this->confirm('Do you want to attach roles to the user?', false); |
| 85 | + |
| 86 | + if (!$attachingRoles) { |
| 87 | + $detachingRoles = $this->confirm('Do you want to detach roles from the user?', false); |
| 88 | + |
| 89 | + $contaningRoles = $user->getRoleNames(); |
| 90 | + |
| 91 | + if ($contaningRoles->isEmpty()) { |
| 92 | + error('User has no roles to detach.'); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + $selectedRoles = multisearch( |
| 97 | + label: 'Select the roles to detach from the user', |
| 98 | + options: fn(string $value) => strlen($value) > 0 |
| 99 | + ? $contaningRoles->values()->all() |
| 100 | + : [], |
| 101 | + required: true, |
| 102 | + ); |
| 103 | + |
| 104 | + $collectedSeletedRoles = collect($selectedRoles); |
| 105 | + |
| 106 | + if ($collectedSeletedRoles->isEmpty()) { |
| 107 | + error('No roles selected.'); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + foreach ($collectedSeletedRoles->toArray() as $role) { |
| 112 | + $user->removeRole($role); |
| 113 | + } |
| 114 | + |
| 115 | + info('Roles detached from the user.'); |
| 116 | + |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + $roleClass = config('permission.models.role', Role::class); |
| 121 | + $allRoles = $roleClass::pluck('name'); |
| 122 | + $currentUserRoles = $user->getRoleNames(); |
| 123 | + |
| 124 | + $diffedRoles = $allRoles->diff($currentUserRoles); |
| 125 | + |
| 126 | + if ($diffedRoles->isEmpty()) { |
| 127 | + error('No roles available to assign to the user.'); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + $selectedRoles = multisearch( |
| 132 | + label: 'Select the roles to assign to the user', |
| 133 | + options: fn(string $value) => strlen($value) > 0 |
| 134 | + ? $diffedRoles->values()->all() |
| 135 | + : [], |
| 136 | + required: true, |
| 137 | + ); |
| 138 | + |
| 139 | + $collectedSeletedRoles = collect($selectedRoles); |
| 140 | + |
| 141 | + if ($collectedSeletedRoles->isEmpty()) { |
| 142 | + error('No roles selected.'); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + $user->syncRoles($collectedSeletedRoles->toArray()); |
| 147 | + } |
| 148 | + |
| 149 | + $user->save(); |
| 150 | + } |
| 151 | + |
| 152 | + protected function renderTable(Collection $users) |
| 153 | + { |
| 154 | + table([ |
| 155 | + 'ID', |
| 156 | + 'Name', |
| 157 | + 'Email', |
| 158 | + 'Verified', |
| 159 | + 'Role(s)', |
| 160 | + 'Created At' |
| 161 | + ], $users->map(fn(User $user) => [ |
| 162 | + $user->id, |
| 163 | + $user->name, |
| 164 | + $user->email, |
| 165 | + $user->hasVerifiedEmail() ? 'Yes' : 'No', |
| 166 | + $user->getRoleNames()->implode(', ') ?: 'No roles', |
| 167 | + $user->created_at->format('Y-m-d H:i:s') . ' (' . $user->created_at->diffForHumans() . ')', |
| 168 | + ])->toArray()); |
| 169 | + } |
| 170 | +} |
0 commit comments