3 declare(strict_types=1);
5 namespace BookStack\Console\Commands;
7 use BookStack\Users\Models\User;
8 use Illuminate\Console\Command;
9 use BookStack\Uploads\UserAvatars;
10 use Illuminate\Database\Eloquent\Collection;
12 final class RefreshAvatarCommand extends Command
15 * The name and signature of the console command.
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}';
27 * The console command description.
31 protected $description = 'Refresh avatar for given user or users';
33 public function handle(UserAvatars $userAvatar): int
35 $dryRun = !$this->option('force');
37 if ($this->option('users-without-avatars')) {
38 return $this->handleUpdateWithoutAvatars($userAvatar, $dryRun);
41 if ($this->option('all')) {
42 return $this->handleUpdateAllAvatars($userAvatar, $dryRun);
45 return $this->handleSingleUserUpdate($userAvatar);
48 private function handleUpdateWithoutAvatars(UserAvatars $userAvatar, bool $dryRun): int
50 $users = User::query()->where('image_id', '=', 0)->get();
51 $this->info(count($users) . ' user(s) found without avatars.');
54 $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars of users that do not have one?');
60 return $this->processUsers($users, $userAvatar, $dryRun);
63 private function handleUpdateAllAvatars(UserAvatars $userAvatar, bool $dryRun): int
65 $users = User::query()->get();
66 $this->info(count($users) . ' user(s) found.');
69 $proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars for ALL USERS?');
75 return $this->processUsers($users, $userAvatar, $dryRun);
78 private function processUsers(Collection $users, UserAvatars $userAvatar, bool $dryRun): int
80 $exitCode = self::SUCCESS;
81 foreach ($users as $user) {
82 $this->getOutput()->write("ID {$user->id} - ", false);
85 $this->warn('Not updated');
89 if ($this->fetchAvatar($userAvatar, $user)) {
90 $this->info('Updated');
92 $this->error('Not updated');
93 $exitCode = self::FAILURE;
97 $this->getOutput()->newLine();
99 $this->comment('Dry run, no avatars have been updated');
100 $this->comment('Run with -f or --force to perform the update');
107 private function handleSingleUserUpdate(UserAvatars $userAvatar): int
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');
115 return self::FAILURE;
118 $field = $id ? 'id' : 'email';
119 $value = $id ?: $email;
121 $user = User::query()
122 ->where($field, '=', $value)
126 $this->error("A user where {$field}={$value} could not be found.");
128 return self::FAILURE;
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?');
134 if ($this->fetchAvatar($userAvatar, $user)) {
135 $this->info('User avatar has been updated.');
136 return self::SUCCESS;
139 $this->info('Could not update avatar please review logs.');
142 return self::FAILURE;
145 private function fetchAvatar(UserAvatars $userAvatar, User $user): bool
147 $oldId = $user->avatar->id ?? 0;
149 $userAvatar->fetchAndAssignToUser($user);
152 $newId = $user->avatar->id ?? $oldId;
153 return $oldId !== $newId;