Skip to content

Commit

Permalink
Merge pull request #59 from leepeuker/add-flag-to-disable-core-user-a…
Browse files Browse the repository at this point in the history
…ccount-changes

Add flag to disable core user account changes
  • Loading branch information
leepeuker authored Jul 18, 2022
2 parents a977f74 + 3496bda commit 75e5b38
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddCoreAccountChangesDisabledToUser extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
ALTER TABLE user DROP COLUMN core_account_changes_disabled;
SQL
);
}

public function up() : void
{
$this->execute(
<<<SQL
ALTER TABLE user ADD COLUMN core_account_changes_disabled TINYINT UNSIGNED DEFAULT 0 AFTER trakt_client_id;
SQL
);
}
}
16 changes: 16 additions & 0 deletions src/Application/User/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public function fetchDateFormatId(int $userId) : int
return $dateFormat;
}

public function fetchUser(int $userId) : Entity
{
$user = $this->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);
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/Application/User/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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;
Expand All @@ -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;
}
}
13 changes: 13 additions & 0 deletions src/Application/User/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions src/Command/UserUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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.");

Expand Down
11 changes: 7 additions & 4 deletions src/HttpController/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() ?? '-',
Expand Down
6 changes: 5 additions & 1 deletion templates/page/settings.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@
<form action="/user/password" method="post" style="padding-top: 0.3rem">
<div class="input-group input-group-sm mb-3">
<input type="password" class="form-control" name="currentPassword" placeholder="Current password" required
{% if coreAccountChangesDisabled == true %}disabled{% endif %}
style="margin-left: 10%;margin-right: 10%;text-align: center;">
</div>

<div class="input-group input-group-sm mb-3">
<input type="password" class="form-control" name="newPassword" placeholder="New password" required minlength="8"
{% if coreAccountChangesDisabled == true %}disabled{% endif %}
style="margin-left: 10%;margin-right: 10%;text-align: center;">
</div>

<div class="input-group input-group-sm mb-3">
<input type="password" class="form-control" name="newPasswordRepeat" placeholder="Repeat password" required minlength="8"
{% if coreAccountChangesDisabled == true %}disabled{% endif %}
style="margin-left: 10%;margin-right: 10%;text-align: center;">
</div>

Expand Down Expand Up @@ -201,7 +204,8 @@
{% endif %}
</div>
<div style="margin-bottom: 1rem">
<a class="btn btn-danger btn-sm" href="/user/delete-account" onclick="confirm('Are you sure you want to delete your account with all your data?')">Delete
<a class="btn btn-danger btn-sm {% if coreAccountChangesDisabled == true %}disabled{% endif %}" href="/user/delete-account"
onclick="confirm('Are you sure you want to delete your account with all your data?')">Delete
account</a>
</div>

Expand Down

0 comments on commit 75e5b38

Please sign in to comment.