From 3496bda6c247257af1057a93f6f0a9b4f1654caf Mon Sep 17 00:00:00 2001 From: Lee Peuker Date: Mon, 18 Jul 2022 19:17:23 +0200 Subject: [PATCH] Add flag to disable core user account changes --- Makefile | 4 +-- ...43_AddCoreAccountChangesDisabledToUser.php | 24 ++++++++++++++++ src/Application/User/Api.php | 16 +++++++++++ src/Application/User/Entity.php | 28 +++++++++++++++++++ src/Application/User/Repository.php | 13 +++++++++ src/Command/UserUpdate.php | 7 +++++ src/HttpController/SettingsController.php | 11 +++++--- templates/page/settings.html.twig | 6 +++- 8 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 db/migrations/20220718170243_AddCoreAccountChangesDisabledToUser.php diff --git a/Makefile b/Makefile index 0b1b5bd8..ab8b3961 100644 --- a/Makefile +++ b/Makefile @@ -65,10 +65,10 @@ db_migration_create: app_sync_all: app_sync_trakt app_sync_tmdb app_database_migrate: - make exec_app_cmd CMD="php bin/console.php database:migration --migrate" + make exec_app_cmd CMD="php bin/console.php database:migration:migrate " app_database_rollback: - make exec_app_cmd CMD="php bin/console.php database:migration --rollback" + make exec_app_cmd CMD="php bin/console.php database:migration:rollback" app_user_create_test: make exec_app_cmd CMD="php bin/console.php user:create a@a a" diff --git a/db/migrations/20220718170243_AddCoreAccountChangesDisabledToUser.php b/db/migrations/20220718170243_AddCoreAccountChangesDisabledToUser.php new file mode 100644 index 00000000..d4594d4e --- /dev/null +++ b/db/migrations/20220718170243_AddCoreAccountChangesDisabledToUser.php @@ -0,0 +1,24 @@ +execute( + <<execute( + <<repository->findUserById($userId); + + if ($user === null) { + throw new \RuntimeException('User does not exist with id : ' . $userId); + } + + return $user; + } + public function findPlexWebhookId(int $userId) : ?string { return $this->repository->findPlexWebhookId($userId); @@ -84,6 +95,11 @@ public function regeneratePlexWebhookId(int $userId) : string return $plexWebhookId; } + public function updateCoreAccountChangesDisabled(int $userId, bool $updateCoreAccountChangesDisabled) : void + { + $this->repository->updateCoreAccountChangesDisabled($userId, $updateCoreAccountChangesDisabled); + } + public function updateDateFormatId(int $userId, int $dateFormat) : void { $this->repository->updateDateFormatId($userId, $dateFormat); diff --git a/src/Application/User/Entity.php b/src/Application/User/Entity.php index 354e5e5e..e003943d 100644 --- a/src/Application/User/Entity.php +++ b/src/Application/User/Entity.php @@ -7,7 +7,11 @@ class Entity private function __construct( private readonly int $id, private readonly string $passwordHash, + private readonly bool $areCoreAccountChangesDisabled, private readonly ?string $plexWebhookUuid, + private readonly ?string $dateFormat, + private readonly ?string $TraktUserName, + private readonly ?string $TraktClientId, ) { } @@ -16,10 +20,24 @@ public static function createFromArray(array $data) : self return new self( (int)$data['id'], $data['password'], + (bool)$data['core_account_changes_disabled'], $data['plex_webhook_uuid'], + $data['date_format'], + $data['trakt_user_name'], + $data['trakt_client_id'], ); } + public function areCoreAccountChangesDisabled() : bool + { + return $this->areCoreAccountChangesDisabled; + } + + public function getDateFormat() : ?string + { + return $this->dateFormat; + } + public function getId() : int { return $this->id; @@ -34,4 +52,14 @@ public function getPlexWebhookId() : ?string { return $this->plexWebhookUuid; } + + public function getTraktClientId() : ?string + { + return $this->TraktClientId; + } + + public function getTraktUserName() : ?string + { + return $this->TraktUserName; + } } diff --git a/src/Application/User/Repository.php b/src/Application/User/Repository.php index 59a17028..6c9516ce 100644 --- a/src/Application/User/Repository.php +++ b/src/Application/User/Repository.php @@ -167,6 +167,19 @@ public function setPlexWebhookId(int $userId, ?string $plexWebhookId) : void ); } + public function updateCoreAccountChangesDisabled(int $userId, bool $coreAccountChangesDisabled) : void + { + $this->dbConnection->update( + 'user', + [ + 'core_account_changes_disabled' => $coreAccountChangesDisabled === true ? 1 : 0, + ], + [ + 'id' => $userId, + ] + ); + } + public function updateDateFormatId(int $userId, int $dateFormat) : void { $this->dbConnection->update( diff --git a/src/Command/UserUpdate.php b/src/Command/UserUpdate.php index 9c8b4a73..f3d2f60d 100644 --- a/src/Command/UserUpdate.php +++ b/src/Command/UserUpdate.php @@ -27,6 +27,7 @@ protected function configure() : void ->addArgument('userId', InputArgument::REQUIRED, 'ID of user') ->addOption('email', [], InputOption::VALUE_OPTIONAL, 'New email') ->addOption('password', [], InputOption::VALUE_OPTIONAL, 'New password') + ->addOption('coreAccountChangesDisabled', [], InputOption::VALUE_OPTIONAL, 'Set core account changes disabled status') ->addOption('traktUserName', [], InputOption::VALUE_OPTIONAL, 'New trakt user name') ->addOption('traktClientId', [], InputOption::VALUE_OPTIONAL, 'New trakt client id'); } @@ -60,6 +61,12 @@ protected function execute(InputInterface $input, OutputInterface $output) : int $this->userApi->updateTraktClientId($userId, $traktClientId); } + + $coreAccountChangesDisabled = $input->getOption('coreAccountChangesDisabled'); + if ($coreAccountChangesDisabled !== null) { + + $this->userApi->updateCoreAccountChangesDisabled($userId, (bool)$coreAccountChangesDisabled); + } } catch (User\Exception\PasswordTooShort $t) { $this->generateOutput($output, "Error: Password must be at least {$t->getMinLength()} characters long."); diff --git a/src/HttpController/SettingsController.php b/src/HttpController/SettingsController.php index 2c7a8a41..7ac5a1d7 100644 --- a/src/HttpController/SettingsController.php +++ b/src/HttpController/SettingsController.php @@ -112,13 +112,16 @@ public function render() : Response $_SESSION['dateFormatUpdated'], ); + $user = $this->userApi->fetchUser($userId); + return Response::create( StatusCode::createOk(), $this->twig->render('page/settings.html.twig', [ + 'coreAccountChangesDisabled' => $user->areCoreAccountChangesDisabled(), 'dateFormats' => DateFormat::getFormats(), - 'dateFormatSelected' => $this->userApi->fetchDateFormatId($userId), + 'dateFormatSelected' => $user->getDateFormat(), 'dateFormatUpdated' => $dateFormatUpdated, - 'plexWebhookUrl' => $this->userApi->findPlexWebhookId($userId) ?? '-', + 'plexWebhookUrl' => $user->getPlexWebhookId() ?? '-', 'passwordErrorNotEqual' => $passwordErrorNotEqual, 'passwordErrorMinLength' => $passwordErrorMinLength, 'passwordErrorCurrentInvalid' => $passwordErrorCurrentInvalid, @@ -129,8 +132,8 @@ public function render() : Response 'importHistoryError' => $importHistoryError, 'deletedUserHistory' => $deletedUserHistory, 'deletedUserRatings' => $deletedUserRatings, - 'traktClientId' => $this->userApi->findTraktClientId($userId), - 'traktUserName' => $this->userApi->findTraktUserName($userId), + 'traktClientId' => $user->getTraktClientId(), + 'traktUserName' => $user->getTraktUserName(), 'applicationVersion' => $this->applicationVersion ?? '-', 'lastSyncTrakt' => $this->syncLogRepository->findLastTraktSync() ?? '-', 'lastSyncTmdb' => $this->syncLogRepository->findLastTmdbSync() ?? '-', diff --git a/templates/page/settings.html.twig b/templates/page/settings.html.twig index 56bce092..61f281f3 100644 --- a/templates/page/settings.html.twig +++ b/templates/page/settings.html.twig @@ -46,16 +46,19 @@
@@ -201,7 +204,8 @@ {% endif %}
- Delete + Delete account