|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BookStack\Console\Commands; |
| 6 | + |
| 7 | +use BookStack\Users\Models\User; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use BookStack\Uploads\UserAvatars; |
| 10 | +use Illuminate\Database\Eloquent\Collection; |
| 11 | + |
| 12 | +final class RefreshAvatarCommand extends Command |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'bookstack:refresh-avatar |
| 20 | + {--id= : Numeric ID of the user to refresh avatar for} |
| 21 | + {--email= : Email address of the user to refresh avatar for} |
| 22 | + {--users-without-avatars : Refresh avatars for users that currently have no avatar} |
| 23 | + {--a|all : Refresh all user avatars} |
| 24 | + {--f|force : Actually run the update for --users-without-avatars, Defaults to a dry-run}'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The console command description. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $description = 'Refresh avatar for given user or users'; |
| 32 | + |
| 33 | + public function handle(UserAvatars $userAvatar): int |
| 34 | + { |
| 35 | + $dryRun = !$this->option('force'); |
| 36 | + |
| 37 | + if ($this->option('users-without-avatars')) { |
| 38 | + return $this->handleUpdateWithoutAvatars($userAvatar, $dryRun); |
| 39 | + } |
| 40 | + |
| 41 | + if ($this->option('all')) { |
| 42 | + return $this->handleUpdateAllAvatars($userAvatar, $dryRun); |
| 43 | + } |
| 44 | + |
| 45 | + return $this->handleSingleUserUpdate($userAvatar); |
| 46 | + } |
| 47 | + |
| 48 | + private function handleUpdateWithoutAvatars(UserAvatars $userAvatar, bool $dryRun): int |
| 49 | + { |
| 50 | + $users = User::query()->where('image_id', '=', 0)->get(); |
| 51 | + $this->info(count($users) . ' user(s) found without avatars.'); |
| 52 | + |
| 53 | + if (!$dryRun) { |
| 54 | + $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars of users that do not have one?'); |
| 55 | + if (!$proceed) { |
| 56 | + return self::SUCCESS; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return $this->processUsers($users, $userAvatar, $dryRun); |
| 61 | + } |
| 62 | + |
| 63 | + private function handleUpdateAllAvatars(UserAvatars $userAvatar, bool $dryRun): int |
| 64 | + { |
| 65 | + $users = User::query()->get(); |
| 66 | + $this->info(count($users) . ' user(s) found.'); |
| 67 | + |
| 68 | + if (!$dryRun) { |
| 69 | + $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars for ALL USERS?'); |
| 70 | + if (!$proceed) { |
| 71 | + return self::SUCCESS; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return $this->processUsers($users, $userAvatar, $dryRun); |
| 76 | + } |
| 77 | + |
| 78 | + private function processUsers(Collection $users, UserAvatars $userAvatar, bool $dryRun): int |
| 79 | + { |
| 80 | + $exitCode = self::SUCCESS; |
| 81 | + foreach ($users as $user) { |
| 82 | + $this->getOutput()->write("ID {$user->id} - ", false); |
| 83 | + |
| 84 | + if ($dryRun) { |
| 85 | + $this->warn('Not updated'); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + if ($this->fetchAvatar($userAvatar, $user)) { |
| 90 | + $this->info('Updated'); |
| 91 | + } else { |
| 92 | + $this->error('Not updated'); |
| 93 | + $exitCode = self::FAILURE; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + $this->getOutput()->newLine(); |
| 98 | + if ($dryRun) { |
| 99 | + $this->comment('Dry run, no avatars have been updated'); |
| 100 | + $this->comment('Run with -f or --force to perform the update'); |
| 101 | + } |
| 102 | + |
| 103 | + return $exitCode; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + private function handleSingleUserUpdate(UserAvatars $userAvatar): int |
| 108 | + { |
| 109 | + $id = $this->option('id'); |
| 110 | + $email = $this->option('email'); |
| 111 | + if (!$id && !$email) { |
| 112 | + $this->error('Either a --id=<number> or --email=<email> option must be provided.'); |
| 113 | + $this->error('Run with `--help` to more options'); |
| 114 | + |
| 115 | + return self::FAILURE; |
| 116 | + } |
| 117 | + |
| 118 | + $field = $id ? 'id' : 'email'; |
| 119 | + $value = $id ?: $email; |
| 120 | + |
| 121 | + $user = User::query() |
| 122 | + ->where($field, '=', $value) |
| 123 | + ->first(); |
| 124 | + |
| 125 | + if (!$user) { |
| 126 | + $this->error("A user where {$field}={$value} could not be found."); |
| 127 | + |
| 128 | + return self::FAILURE; |
| 129 | + } |
| 130 | + |
| 131 | + $this->info("This will refresh the avatar for user: \n- ID: {$user->id}\n- Name: {$user->name}\n- Email: {$user->email}\n"); |
| 132 | + $confirm = $this->confirm('Are you sure you want to proceed?'); |
| 133 | + if ($confirm) { |
| 134 | + if ($this->fetchAvatar($userAvatar, $user)) { |
| 135 | + $this->info('User avatar has been updated.'); |
| 136 | + return self::SUCCESS; |
| 137 | + } |
| 138 | + |
| 139 | + $this->info('Could not update avatar please review logs.'); |
| 140 | + } |
| 141 | + |
| 142 | + return self::FAILURE; |
| 143 | + } |
| 144 | + |
| 145 | + private function fetchAvatar(UserAvatars $userAvatar, User $user): bool |
| 146 | + { |
| 147 | + $oldId = $user->avatar->id ?? 0; |
| 148 | + |
| 149 | + $userAvatar->fetchAndAssignToUser($user); |
| 150 | + |
| 151 | + $user->refresh(); |
| 152 | + $newId = $user->avatar->id ?? $oldId; |
| 153 | + return $oldId !== $newId; |
| 154 | + } |
| 155 | +} |
0 commit comments