1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

Avatar Commend: Simplified and updated during review

During review of #4560.

- Simplified command to share as much log as possible across different
  run options.
- Extracted out user handling to share with MFA command.
- Added specific handling for disabled avatar fetching.
- Added mention of avatar endpoint, to make it clear where these avatars
  are coming from (Protect against user expectation of LDAP avatar sync).
- Simplified a range of the testing.
- Tweaked wording and code formatting.
This commit is contained in:
Dan Brown
2023-09-19 15:53:01 +01:00
parent ca98155373
commit 4b4d8ba2a1
5 changed files with 187 additions and 270 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace BookStack\Console\Commands;
use BookStack\Users\Models\User;
use Exception;
use Illuminate\Console\Command;
/**
* @mixin Command
*/
trait HandlesSingleUser
{
/**
* Fetch a user provided to this command.
* Expects the command to accept 'id' and 'email' options.
* @throws Exception
*/
private function fetchProvidedUser(): User
{
$id = $this->option('id');
$email = $this->option('email');
if (!$id && !$email) {
throw new Exception("Either a --id=<number> or --email=<email> option must be provided.\nRun this command with `--help` to show more options.");
}
$field = $id ? 'id' : 'email';
$value = $id ?: $email;
$user = User::query()
->where($field, '=', $value)
->first();
if (!$user) {
throw new Exception("A user where {$field}={$value} could not be found.");
}
return $user;
}
}

View File

@@ -1,16 +1,16 @@
<?php
declare(strict_types=1);
namespace BookStack\Console\Commands;
use BookStack\Users\Models\User;
use Exception;
use Illuminate\Console\Command;
use BookStack\Uploads\UserAvatars;
use Illuminate\Database\Eloquent\Collection;
final class RefreshAvatarCommand extends Command
class RefreshAvatarCommand extends Command
{
use HandlesSingleUser;
/**
* The name and signature of the console command.
*
@@ -20,128 +20,89 @@ final class RefreshAvatarCommand extends Command
{--id= : Numeric ID of the user to refresh avatar for}
{--email= : Email address of the user to refresh avatar for}
{--users-without-avatars : Refresh avatars for users that currently have no avatar}
{--a|all : Refresh all user avatars}
{--f|force : Actually run the update for --users-without-avatars, Defaults to a dry-run}';
{--a|all : Refresh avatars for all users}
{--f|force : Actually run the update, Defaults to a dry-run}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh avatar for given user or users';
protected $description = 'Refresh avatar for the given user(s)';
public function handle(UserAvatars $userAvatar): int
{
$dryRun = !$this->option('force');
if (!$userAvatar->avatarFetchEnabled()) {
$this->error("Avatar fetching is disabled on this instance.");
return self::FAILURE;
}
if ($this->option('users-without-avatars')) {
return $this->handleUpdateWithoutAvatars($userAvatar, $dryRun);
return $this->processUsers(User::query()->whereDoesntHave('avatar')->get()->all(), $userAvatar);
}
if ($this->option('all')) {
return $this->handleUpdateAllAvatars($userAvatar, $dryRun);
return $this->processUsers(User::query()->get()->all(), $userAvatar);
}
return $this->handleSingleUserUpdate($userAvatar);
try {
$user = $this->fetchProvidedUser();
return $this->processUsers([$user], $userAvatar);
} catch (Exception $exception) {
$this->error($exception->getMessage());
return self::FAILURE;
}
}
private function handleUpdateWithoutAvatars(UserAvatars $userAvatar, bool $dryRun): int
/**
* @param User[] $users
*/
private function processUsers(array $users, UserAvatars $userAvatar): int
{
$users = User::query()->where('image_id', '=', 0)->get();
$this->info(count($users) . ' user(s) found without avatars.');
$dryRun = !$this->option('force');
$this->info(count($users) . " user(s) found to update avatars for.");
if (count($users) === 0) {
return self::SUCCESS;
}
if (!$dryRun) {
$proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars of users that do not have one?');
$fetchHost = parse_url($userAvatar->getAvatarUrl(), PHP_URL_HOST);
$this->warn("This will destroy any existing avatar images these users have, and attempt to fetch new avatar images from {$fetchHost}.");
$proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to proceed?');
if (!$proceed) {
return self::SUCCESS;
}
}
return $this->processUsers($users, $userAvatar, $dryRun);
}
$this->info("");
private function handleUpdateAllAvatars(UserAvatars $userAvatar, bool $dryRun): int
{
$users = User::query()->get();
$this->info(count($users) . ' user(s) found.');
if (!$dryRun) {
$proceed = !$this->input->isInteractive() || $this->confirm('Are you sure you want to refresh avatars for ALL USERS?');
if (!$proceed) {
return self::SUCCESS;
}
}
return $this->processUsers($users, $userAvatar, $dryRun);
}
private function processUsers(Collection $users, UserAvatars $userAvatar, bool $dryRun): int
{
$exitCode = self::SUCCESS;
foreach ($users as $user) {
$this->getOutput()->write("ID {$user->id} - ", false);
$linePrefix = "[ID: {$user->id}] $user->email -";
if ($dryRun) {
$this->warn('Not updated');
$this->warn("{$linePrefix} Not updated");
continue;
}
if ($this->fetchAvatar($userAvatar, $user)) {
$this->info('Updated');
$this->info("{$linePrefix} Updated");
} else {
$this->error('Not updated');
$this->error("{$linePrefix} Not updated");
$exitCode = self::FAILURE;
}
}
$this->getOutput()->newLine();
if ($dryRun) {
$this->comment('Dry run, no avatars have been updated');
$this->comment('Run with -f or --force to perform the update');
$this->comment("");
$this->comment("Dry run, no avatars were updated.");
$this->comment('Run with -f or --force to perform the update.');
}
return $exitCode;
}
private function handleSingleUserUpdate(UserAvatars $userAvatar): int
{
$id = $this->option('id');
$email = $this->option('email');
if (!$id && !$email) {
$this->error('Either a --id=<number> or --email=<email> option must be provided.');
$this->error('Run with `--help` to more options');
return self::FAILURE;
}
$field = $id ? 'id' : 'email';
$value = $id ?: $email;
$user = User::query()
->where($field, '=', $value)
->first();
if (!$user) {
$this->error("A user where {$field}={$value} could not be found.");
return self::FAILURE;
}
$this->info("This will refresh the avatar for user: \n- ID: {$user->id}\n- Name: {$user->name}\n- Email: {$user->email}\n");
$confirm = $this->confirm('Are you sure you want to proceed?');
if ($confirm) {
if ($this->fetchAvatar($userAvatar, $user)) {
$this->info('User avatar has been updated.');
return self::SUCCESS;
}
$this->info('Could not update avatar please review logs.');
}
return self::FAILURE;
}
private function fetchAvatar(UserAvatars $userAvatar, User $user): bool
{
$oldId = $user->avatar->id ?? 0;

View File

@@ -2,11 +2,13 @@
namespace BookStack\Console\Commands;
use BookStack\Users\Models\User;
use Exception;
use Illuminate\Console\Command;
class ResetMfaCommand extends Command
{
use HandlesSingleUser;
/**
* The name and signature of the console command.
*
@@ -29,25 +31,10 @@ class ResetMfaCommand extends Command
*/
public function handle(): int
{
$id = $this->option('id');
$email = $this->option('email');
if (!$id && !$email) {
$this->error('Either a --id=<number> or --email=<email> option must be provided.');
return 1;
}
$field = $id ? 'id' : 'email';
$value = $id ?: $email;
/** @var User $user */
$user = User::query()
->where($field, '=', $value)
->first();
if (!$user) {
$this->error("A user where {$field}={$value} could not be found.");
try {
$user = $this->fetchProvidedUser();
} catch (Exception $exception) {
$this->error($exception->getMessage());
return 1;
}

View File

@@ -127,7 +127,7 @@ class UserAvatars
/**
* Check if fetching external avatars is enabled.
*/
protected function avatarFetchEnabled(): bool
public function avatarFetchEnabled(): bool
{
$fetchUrl = $this->getAvatarUrl();
@@ -137,7 +137,7 @@ class UserAvatars
/**
* Get the URL to fetch avatars from.
*/
protected function getAvatarUrl(): string
public function getAvatarUrl(): string
{
$configOption = config('services.avatar_url');
if ($configOption === false) {