Skip to content
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

Add optional enviornment variable to set default login credentials #409

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The `Web UI` column is set to yes if an environment variable can alternatively b
| `ENABLE_REGISTRATION` | `0` | Enables public user registration | |
| `MIN_RUNTIME_IN_SECONDS_FOR_JOB_PROCESSING` | `15` | Minimum time between background jobs processing | |
| `TIMEZONE` | `"Europe/Berlin"` | Supported timezones [here](https://www.php.net/manual/en/timezones.php) | |
| `DEFAULT_LOGIN_EMAIL` | - | Email address to always autofill on login page | |
| `DEFAULT_LOGIN_PASSWORD` | - | Password to always autofill on login page | |

### Database

Expand Down
2 changes: 2 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public static function createLandingPageController(ContainerInterface $container
$container->get(UserApi::class),
$container->get(SessionWrapper::class),
$config->getAsBool('ENABLE_REGISTRATION', false),
$config->getAsStringNullable('DEFAULT_LOGIN_EMAIL'),
$config->getAsStringNullable('DEFAULT_LOGIN_PASSWORD'),
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/HttpController/LandingPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function __construct(
private readonly UserApi $userApi,
private readonly SessionWrapper $sessionWrapper,
private readonly bool $registrationEnabled,
private readonly ?string $defaultEmail,
private readonly ?string $defaultPassword,
) {
}

Expand All @@ -42,7 +44,9 @@ public function render() : Response
$this->twig->render('page/login.html.twig', [
'failedLogin' => $failedLogin,
'deletedAccount' => $deletedAccount,
'registrationEnabled' => $this->registrationEnabled
'registrationEnabled' => $this->registrationEnabled,
'defaultEmail' => $this->defaultEmail,
'defaultPassword' => $this->defaultPassword,
]),
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/ValueObject/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public function getAsString(string $key, ?string $fallbackValue = null) : string
}
}

public function getAsStringNullable(string $key, ?string $fallbackValue = null) : ?string
{
try {
return $this->getAsString($key, $fallbackValue);
} catch (ConfigKeyNotSetException) {
return null;
}
}

/** @throws ConfigKeyNotSetException */
private function get(string $key) : mixed
{
Expand Down
4 changes: 2 additions & 2 deletions templates/page/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
<form action="/login" method="post" enctype="multipart/form-data">
<h1 id="header" class="text-{{ theme == 'dark' ? 'light' : 'dark' }}" style="margin-bottom: 1rem">Movary</h1>
<div class="form-floating">
<input type="email" name="email" class="form-control text-{{ theme == 'dark' ? 'light' : 'dark' }}" id="floatingInput" placeholder="[email protected]" required>
<input type="email" name="email" value="{{ defaultEmail }}" class="form-control text-{{ theme == 'dark' ? 'light' : 'dark' }}" id="floatingInput" placeholder="[email protected]" required>
<label for="floatingInput">Email address</label>
</div>
<div class="form-floating">
<input type="password" name="password" class="form-control text-{{ theme == 'dark' ? 'light' : 'dark' }}" id="floatingPassword" placeholder="Password" required>
<input type="password" name="password" value="{{ defaultPassword }}" class="form-control text-{{ theme == 'dark' ? 'light' : 'dark' }}" id="floatingPassword" placeholder="Password" required>
<label for="floatingPassword">Password</label>
</div>

Expand Down