Skip to content

Artisan command for "refetching" user avatars for existing users #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions app/Console/Commands/ResetAvatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace BookStack\Console\Commands;

use BookStack\Auth\User;
use Illuminate\Console\Command;
use BookStack\Auth\UserRepo;

class ResetAvatar extends Command
{
/**
* The name and signature of the console command.
*
*/
protected $signature = 'bookstack:reset-avatar
{--id= : Numeric ID of the user to reset MFA for}
{--email= : Email address of the user to reset MFA for}
';

/**
* The console command description.
*
*/

protected $description = 'Reset & Fetch avatar for given user';

// protected $user;
protected $userRepo;

/**
* Create a new command instance.
*
*/

public function __construct(User $user, UserRepo $userRepo)
{
$this->user = $user;
$this->userRepo = $userRepo;
parent::__construct();
}



/**
* Execute the console command.
*
*/
public function handle()
{
$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;

$user = User::query()
->where($field, '=', $value)
->first();

if (!$user) {
$this->error("A user where {$field}={$value} could not be found.");

return 1;
}

$this->info("This will delete and re-fetch 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) {

$this->userRepo->downloadAndAssignUserAvatar($user);

$this->info('User avatar have been reset.');

return 0;
}

return 1;
}
}