Merge pull request #94 from nextcloud/enh/3/disable-login-if-nextcloud-is-running

disable login if Nextcloud is running
This commit is contained in:
Simon L 2022-01-14 12:09:14 +01:00 committed by GitHub
commit 94e56757dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 10 deletions

View file

@ -89,9 +89,13 @@ $app->get('/containers', function ($request, $response, $args) use ($container)
'last_backup_time' => $configurationManager->GetLastBackupTime(), 'last_backup_time' => $configurationManager->GetLastBackupTime(),
]); ]);
})->setName('profile'); })->setName('profile');
$app->get('/login', function ($request, $response, $args) { $app->get('/login', function ($request, $response, $args) use ($container) {
$view = Twig::fromRequest($request); $view = Twig::fromRequest($request);
return $view->render($response, 'login.twig'); /** @var \AIO\Docker\DockerActionManager $dockerActionManger */
$dockerActionManger = $container->get(\AIO\Docker\DockerActionManager::class);
return $view->render($response, 'login.twig', [
'is_login_allowed' => $dockerActionManger->isLoginAllowed(),
]);
}); });
$app->get('/setup', function ($request, $response, $args) use ($container) { $app->get('/setup', function ($request, $response, $args) use ($container) {
$view = Twig::fromRequest($request); $view = Twig::fromRequest($request);

View file

@ -12,12 +12,17 @@ use Psr\Http\Message\ServerRequestInterface as Request;
class LoginController class LoginController
{ {
private AuthManager $authManager; private AuthManager $authManager;
private DockerActionManager $dockerActionManager;
public function __construct(AuthManager $authManager) { public function __construct(AuthManager $authManager, DockerActionManager $dockerActionManager) {
$this->authManager = $authManager; $this->authManager = $authManager;
$this->dockerActionManager = $dockerActionManager;
} }
public function TryLogin(Request $request, Response $response, $args) : Response { public function TryLogin(Request $request, Response $response, $args) : Response {
if (!$this->dockerActionManager->isLoginAllowed()) {
return $response->withHeader('Location', '/')->withStatus(302);
}
$password = $request->getParsedBody()['password']; $password = $request->getParsedBody()['password'];
if($this->authManager->CheckCredentials($password)) { if($this->authManager->CheckCredentials($password)) {
$this->authManager->SetAuthState(true); $this->authManager->SetAuthState(true);

View file

@ -517,4 +517,13 @@ class DockerActionManager
return -1; return -1;
} }
} }
public function isLoginAllowed() : bool {
$id = 'nextcloud-aio-apache';
$apacheContainer = $this->containerDefinitionFetcher->GetContainerById($id);
if ($this->GetContainerStartingState($apacheContainer) instanceof RunningState) {
return false;
}
return true;
}
} }

View file

@ -5,13 +5,18 @@
<div class="login"> <div class="login">
<img src="/img/logo-blue.svg" style="margin-left: auto;margin-right: auto;display: block;"> <img src="/img/logo-blue.svg" style="margin-left: auto;margin-right: auto;display: block;">
<h1>Nextcloud AIO Login</h1> <h1>Nextcloud AIO Login</h1>
<p>Log in using your Nextcloud AIO password. If you don't have it, you can also use the automatic login from your Nextcloud.</p> {% if is_login_allowed == true %}
<form method="POST" action="/api/auth/login"> <p>Log in using your Nextcloud AIO password.</p>
<input type="text" name="password" placeholder="Password" /> <form method="POST" action="/api/auth/login">
<input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}"> <input type="text" name="password" placeholder="Password" />
<input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}"> <input type="hidden" name="{{csrf.keys.name}}" value="{{csrf.name}}">
<input type="submit" class="button" value="Login" /> <input type="hidden" name="{{csrf.keys.value}}" value="{{csrf.value}}">
</form> <input type="submit" class="button" value="Login" />
</form>
{% else %}
<p>The login is blocked since Nextcloud is running. Please use the automatic login from your Nextcloud.<br><br>
You can unblock the login by running 'sudo docker stop nextcloud-aio-apache'.</p>
{% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}