2017-11-04 19:38:38 +00:00
|
|
|
<?php
|
2023-03-26 12:32:13 +00:00
|
|
|
declare(strict_types=1);
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
namespace App\Command;
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
use App\Entity\User;
|
|
|
|
use App\Exception\Api\ForbiddenException;
|
|
|
|
use App\Exception\Api\UserNotFoundException;
|
|
|
|
use App\Repository\UserRepository;
|
|
|
|
use App\Service\Api\UserApi;
|
2017-11-04 19:38:38 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2023-03-26 12:32:13 +00:00
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2017-11-05 01:42:08 +00:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2017-11-04 19:38:38 +00:00
|
|
|
use Symfony\Component\Console\Helper\ProgressBar;
|
2023-03-26 12:32:13 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2017-11-04 19:38:38 +00:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2023-03-26 12:32:13 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
#[AsCommand(name: 'app:privacy:update', description: 'Check removed users status and restore if user was deleted by error.')]
|
2017-11-05 01:42:08 +00:00
|
|
|
class UpdateUsersPrivacyCommand extends Command
|
2017-11-04 19:38:38 +00:00
|
|
|
{
|
2023-03-26 12:32:13 +00:00
|
|
|
public function __construct(
|
|
|
|
private readonly EntityManagerInterface $em,
|
2023-03-26 14:07:00 +00:00
|
|
|
private readonly LoggerInterface $logger,
|
|
|
|
private readonly UserRepository $userRepo,
|
|
|
|
private readonly UserApi $api,
|
|
|
|
private readonly int $pointApiDelay,
|
|
|
|
private readonly int $pointAppUserId,
|
2023-03-26 12:32:13 +00:00
|
|
|
) {
|
2017-11-04 19:38:38 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->addOption(
|
|
|
|
'all-users',
|
|
|
|
null,
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
'If set, command will check all users instead of service subscribers only'
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
2017-11-04 19:38:38 +00:00
|
|
|
{
|
2023-03-26 12:32:13 +00:00
|
|
|
$io = new SymfonyStyle($input, $output);
|
2017-11-04 19:38:38 +00:00
|
|
|
|
|
|
|
$this->logger->debug(static::class.' started.');
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
$progress = $io->createProgressBar();
|
|
|
|
$progress->setFormat(ProgressBar::FORMAT_DEBUG);
|
2017-11-04 19:38:38 +00:00
|
|
|
|
|
|
|
try {
|
2023-03-26 12:32:13 +00:00
|
|
|
$usersForUpdate = $this->getUsersForUpdate($input);
|
2017-11-04 19:38:38 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]);
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
return Command::FAILURE;
|
2017-11-04 19:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Processing users privacy.');
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
$progress->start(count($usersForUpdate));
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
foreach ($usersForUpdate as $user) {
|
2023-04-01 16:10:56 +00:00
|
|
|
\usleep($this->pointApiDelay);
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
$progress->advance();
|
2017-11-04 19:38:38 +00:00
|
|
|
$this->logger->info('Processing @'.$user->getLogin());
|
|
|
|
|
|
|
|
$this->updateUser($user);
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
$progress->finish();
|
2017-11-04 19:38:38 +00:00
|
|
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
$this->logger->debug('Finished');
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
return Command::SUCCESS;
|
2017-11-04 19:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function updateUser(User $user): void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$remoteUser = $this->api->getUserById($user->getId());
|
|
|
|
|
|
|
|
if ($remoteUser !== $user) {
|
2017-11-06 01:37:14 +00:00
|
|
|
$this->logger->error('Remote user is not equal with local.', [
|
|
|
|
'local_user_id' => $user->getId(),
|
|
|
|
'local_user_login' => $user->getLogin(),
|
|
|
|
'local_user_name' => $user->getName(),
|
|
|
|
'remote_user_id' => $remoteUser->getId(),
|
|
|
|
'remote_user_login' => $remoteUser->getLogin(),
|
|
|
|
'remote_user_name' => $remoteUser->getName(),
|
|
|
|
]);
|
2017-11-04 19:38:38 +00:00
|
|
|
}
|
|
|
|
} catch (UserNotFoundException $e) {
|
|
|
|
$this->logger->info('User not found. Marking as removed.', ['user_id' => $user->getId(), 'user_login' => $user->getLogin()]);
|
|
|
|
|
|
|
|
$user->markAsRemoved();
|
|
|
|
} catch (ForbiddenException $e) {
|
|
|
|
$this->logger->info('User profile access forbidden', ['user_id' => $user->getId(), 'user_login' => $user->getLogin()]);
|
|
|
|
|
|
|
|
$user->updatePrivacy(false, true);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error(
|
|
|
|
'Error while updating user privacy',
|
|
|
|
[
|
|
|
|
'user_login' => $user->getLogin(),
|
|
|
|
'user_id' => $user->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 12:32:13 +00:00
|
|
|
/** @return User[] */
|
|
|
|
private function getUsersForUpdate(InputInterface $input): array
|
2017-11-04 19:38:38 +00:00
|
|
|
{
|
2023-03-26 12:32:13 +00:00
|
|
|
if ($input->getOption('all-users')) {
|
2017-11-04 19:38:38 +00:00
|
|
|
return $this->userRepo->findBy(['removed' => false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var User $serviceUser */
|
|
|
|
try {
|
2023-03-26 14:07:00 +00:00
|
|
|
$serviceUser = $this->userRepo->findActiveUserWithSubscribers($this->pointAppUserId);
|
2017-11-04 19:38:38 +00:00
|
|
|
} catch (\Exception $e) {
|
2023-03-26 14:07:00 +00:00
|
|
|
$this->logger->error('Error while getting active user with subscribers', ['app_user_id' => $this->pointAppUserId]);
|
2017-11-04 19:38:38 +00:00
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$serviceUser) {
|
2019-01-18 23:07:00 +00:00
|
|
|
$this->logger->warning('Service user not found or marked as removed. Falling back to API.');
|
2017-11-04 19:38:38 +00:00
|
|
|
|
2019-01-18 23:07:00 +00:00
|
|
|
try {
|
2023-03-26 14:07:00 +00:00
|
|
|
$serviceUser = $this->api->getUserById($this->pointAppUserId);
|
2019-01-18 23:07:00 +00:00
|
|
|
} catch (UserNotFoundException $e) {
|
|
|
|
throw new \RuntimeException('Service user not found in the database and could not be retrieved from API.');
|
|
|
|
}
|
2017-11-04 19:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Getting service subscribers');
|
|
|
|
|
|
|
|
try {
|
2023-03-26 14:07:00 +00:00
|
|
|
return $this->api->getUserSubscribersById($this->pointAppUserId);
|
2017-11-04 19:38:38 +00:00
|
|
|
} catch (UserNotFoundException $e) {
|
|
|
|
$this->logger->critical('Service user deleted or API response is invalid');
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->warning(
|
|
|
|
'Error while getting service subscribers. Fallback to local list.',
|
|
|
|
[
|
|
|
|
'user_login' => $serviceUser->getLogin(),
|
|
|
|
'user_id' => $serviceUser->getId(),
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$localSubscribers = [];
|
|
|
|
|
|
|
|
foreach ($serviceUser->getSubscribers() as $subscription) {
|
|
|
|
$localSubscribers[] = $subscription->getSubscriber();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $localSubscribers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|