Compare commits

..

No commits in common. "212b32c4ab8d25a302ba3d8603edcc06c5c00637" and "525d7404096daf8668988bc64d5ee4d963c2b77d" have entirely different histories.

14 changed files with 70 additions and 106 deletions

View File

@ -10,7 +10,7 @@
}
],
"require": {
"esoko/soko-web": "0.3",
"esoko/soko-web": "0.1",
"fzaninotto/faker": "^1.9"
},
"require-dev": {

8
composer.lock generated
View File

@ -4,15 +4,15 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0f82e530dedc414cfc0b04daf3c2085c",
"content-hash": "4c91d823e927375028a2b3c9d974a8bd",
"packages": [
{
"name": "esoko/soko-web",
"version": "v0.3",
"version": "v0.1",
"source": {
"type": "git",
"url": "https://git.esoko.eu/esoko/soko-web.git",
"reference": "014a5480967c03c00dda5ee34c7eaf4be224b96e"
"reference": "a17a88e9d44e42cef3d996fc66b4dd1134b85def"
},
"require": {
"phpmailer/phpmailer": "^6.8",
@ -33,7 +33,7 @@
"GNU GPL 3.0"
],
"description": "Lightweight web framework",
"time": "2023-04-16T14:54:22+00:00"
"time": "2023-04-07T17:32:15+00:00"
},
{
"name": "fzaninotto/faker",

View File

@ -1,13 +1,5 @@
<?php
use SokoWeb\Interfaces\Response\IRedirect;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Response\Redirect;
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
require '../web.php';
$method = strtolower($_SERVER['REQUEST_METHOD']);
@ -27,40 +19,35 @@ if ($match !== null) {
$handler = $route->getHandler();
$controller = new $handler[0](Container::$request);
if (
$controller instanceof IAuthenticationRequired &&
$controller->isAuthenticationRequired() &&
Container::$request->user() === null
) {
Container::$request->session()->set('redirect_after_login', substr($_SERVER['REQUEST_URI'], strlen('/')));
$response = new Redirect(Container::$routeCollection->getRoute('login')->generateLink(), IRedirect::TEMPORARY);
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
return;
if ($controller instanceof SokoWeb\Interfaces\Authorization\ISecured) {
$authorized = $controller->authorize();
} else {
$authorized = true;
}
if ($method === 'post' && Container::$request->post('anti_csrf_token') !== Container::$request->session()->get('anti_csrf_token')) {
$content = new JsonContent(['error' => 'no_valid_anti_csrf_token']);
$content = new SokoWeb\Response\JsonContent(['error' => 'no_valid_anti_csrf_token']);
header('Content-Type: text/html; charset=UTF-8', true, 403);
$content->render();
return;
}
if (
!($controller instanceof ISecured) ||
$controller->authorize()
) {
if ($authorized) {
$response = call_user_func([$controller, $handler[1]]);
if ($response instanceof IContent) {
if ($response instanceof SokoWeb\Interfaces\Response\IContent) {
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
$response->render();
return;
} elseif ($response instanceof IRedirect) {
} elseif ($response instanceof SokoWeb\Interfaces\Response\IRedirect) {
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
return;
}
}
}
$content = new HtmlContent('error/404');
$content = new SokoWeb\Response\HtmlContent('error/404');
header('Content-Type: text/html; charset=UTF-8', true, 404);
$content->render();

View File

@ -141,7 +141,9 @@
if (document.getElementById('challengeButton')) {
document.getElementById('challengeButton').onclick = function () {
MapGuesser.showModal('challenge');
document.getElementById('createNewChallengeButton').href = '/challenge/new/' + this.dataset.mapId;
document.getElementById('playMode').style.visibility = 'hidden';
var timeLimit = document.getElementById('timeLimit').value;
document.getElementById('timeLimitLabel').innerText = 'Time limit of ' + Util.printTimeForHuman(timeLimit);
};

View File

@ -2,7 +2,7 @@
use DateTime;
use Faker\Factory;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
@ -21,7 +21,7 @@ use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Repository\UserInChallengeRepository;
use SokoWeb\Response\Redirect;
class GameController implements IAuthenticationRequired
class GameController implements ISecured
{
const NUMBER_OF_ROUNDS = 5;
@ -53,9 +53,9 @@ class GameController implements IAuthenticationRequired
$this->userInChallengeRepository = new UserInChallengeRepository();
}
public function isAuthenticationRequired(): bool
public function authorize(): bool
{
return empty($_ENV['ENABLE_GAME_FOR_GUESTS']);
return !empty($_ENV['ENABLE_GAME_FOR_GUESTS']) || $this->request->user() !== null;
}
public function getGame(): IContent

View File

@ -1,7 +1,7 @@
<?php namespace MapGuesser\Controller;
use DateTime;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest;
use MapGuesser\Util\Geo\Position;
use SokoWeb\Response\JsonContent;
@ -15,14 +15,17 @@ use MapGuesser\PersistentData\Model\Place;
use MapGuesser\PersistentData\Model\PlaceInChallenge;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\Model\UserPlayedPlace;
use MapGuesser\Repository\ChallengeRepository;
use MapGuesser\Repository\GuessRepository;
use MapGuesser\Repository\MapRepository;
use MapGuesser\Repository\MultiRoomRepository;
use MapGuesser\Repository\PlaceInChallengeRepository;
use MapGuesser\Repository\PlaceRepository;
use MapGuesser\Repository\UserInChallengeRepository;
use MapGuesser\Repository\UserPlayedPlaceRepository;
use MapGuesser\Repository\UserRepository;
class GameFlowController implements IAuthenticationRequired
class GameFlowController implements ISecured
{
const NUMBER_OF_ROUNDS = 5;
const MAX_SCORE = 1000;
@ -58,9 +61,9 @@ class GameFlowController implements IAuthenticationRequired
$this->guessRepository = new GuessRepository();
}
public function isAuthenticationRequired(): bool
public function authorize(): bool
{
return empty($_ENV['ENABLE_GAME_FOR_GUESTS']);
return !empty($_ENV['ENABLE_GAME_FOR_GUESTS']) || $this->request->user() !== null;
}
public function initialData(): IContent

View File

@ -36,8 +36,6 @@ class LoginController
private UserPlayedPlaceRepository $userPlayedPlaceRepository;
private string $redirectUrl;
public function __construct(IRequest $request)
{
$this->request = $request;
@ -46,19 +44,15 @@ class LoginController
$this->userConfirmationRepository = new UserConfirmationRepository();
$this->userPasswordResetterRepository = new UserPasswordResetterRepository();
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
$this->redirectUrl = $this->request->session()->has('redirect_after_login') ?
$this->request->session()->get('redirect_after_login') :
\Container::$routeCollection->getRoute('index')->generateLink();
}
public function getLoginForm()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
return new HtmlContent('login/login', ['redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/login');
}
public function getGoogleLoginRedirect(): IRedirect
@ -82,8 +76,7 @@ class LoginController
public function getSignupForm()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
if ($this->request->session()->has('tmp_user_data')) {
@ -105,8 +98,7 @@ class LoginController
public function getSignupWithGoogleForm()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
if (!$this->request->session()->has('google_user_data')) {
@ -117,14 +109,13 @@ class LoginController
$user = $this->userRepository->getByEmail($userData['email']);
return new HtmlContent('login/google_signup', ['found' => $user !== null, 'email' => $userData['email'], 'redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/google_signup', ['found' => $user !== null, 'email' => $userData['email']]);
}
public function getRequestPasswordResetForm()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
return new HtmlContent('login/password_reset_request', ['email' => $this->request->query('email')]);
@ -138,8 +129,7 @@ class LoginController
public function getResetPasswordForm()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
$token = $this->request->query('token');
@ -151,13 +141,12 @@ class LoginController
$user = $this->userRepository->getById($resetter->getUserId());
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail()]);
}
public function login(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -209,15 +198,13 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
public function loginWithGoogle()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
if ($this->request->query('state') !== $this->request->session()->get('oauth_state')) {
@ -255,8 +242,7 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
public function logout(): IRedirect
@ -269,8 +255,7 @@ class LoginController
public function signup(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new JsonContent(['redirect' => ['target' => '/' . \Container::$routeCollection->getRoute('home')->generateLink()]]);
}
$user = $this->userRepository->getByEmail($this->request->post('email'));
@ -289,8 +274,7 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
$data = ['redirect' => ['target' => '/' . $this->redirectUrl]];
$data = ['redirect' => ['target' => '/' . \Container::$routeCollection->getRoute('index')->generateLink()]];
} else {
$data = [
'error' => [
@ -371,7 +355,6 @@ class LoginController
public function signupWithGoogle(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -401,7 +384,6 @@ class LoginController
$this->request->session()->delete('google_user_data');
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -422,8 +404,7 @@ class LoginController
public function activate()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
$confirmation = $this->userConfirmationRepository->getByToken(substr($this->request->query('token'), 0, 32));
@ -445,15 +426,13 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
public function cancel()
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
return new Redirect(\Container::$routeCollection->getRoute('index')->generateLink(), IRedirect::TEMPORARY);
}
$confirmation = $this->userConfirmationRepository->getByToken(substr($this->request->query('token'), 0, 32));
@ -482,10 +461,9 @@ class LoginController
public function requestPasswordReset(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . $this->redirectUrl
'target' => '/' . \Container::$routeCollection->getRoute('home')->generateLink()
]
]);
}
@ -560,10 +538,9 @@ class LoginController
public function resetPassword(): IContent
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . $this->redirectUrl
'target' => '/' . \Container::$routeCollection->getRoute('home')->generateLink()
]
]);
}
@ -604,7 +581,6 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
return new JsonContent(['success' => true]);
}
@ -665,9 +641,4 @@ class LoginController
]);
$mail->send();
}
private function deleteRedirectUrl(): void
{
$this->request->session()->delete('redirect_after_login');
}
}

View File

@ -2,7 +2,6 @@
use DateTime;
use SokoWeb\Interfaces\Authentication\IUser;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Interfaces\Response\IContent;
@ -23,7 +22,7 @@ use SokoWeb\Response\JsonContent;
use MapGuesser\Util\Geo\Bounds;
use MapGuesser\Util\Panorama\Pov;
class MapAdminController implements IAuthenticationRequired, ISecured
class MapAdminController implements ISecured
{
private static string $unnamedMapName = '[unnamed map]';
@ -58,14 +57,11 @@ class MapAdminController implements IAuthenticationRequired, ISecured
$this->userInChallengeRepository = new UserInChallengeRepository();
}
public function isAuthenticationRequired(): bool
{
return true;
}
public function authorize(): bool
{
return $this->request->user()->hasPermission(IUser::PERMISSION_ADMIN);
$user = $this->request->user();
return $user !== null && $user->hasPermission(IUser::PERMISSION_ADMIN);
}
public function getMapEditor(): IContent

View File

@ -2,7 +2,7 @@
use DateTime;
use SokoWeb\Http\Request;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Response\IRedirect;
@ -19,7 +19,7 @@ use SokoWeb\Response\JsonContent;
use SokoWeb\Response\Redirect;
use SokoWeb\Util\JwtParser;
class UserController implements IAuthenticationRequired
class UserController implements ISecured
{
private IRequest $request;
@ -46,9 +46,11 @@ class UserController implements IAuthenticationRequired
$this->guessRepository = new GuessRepository();
}
public function isAuthenticationRequired(): bool
public function authorize(): bool
{
return true;
$user = $this->request->user();
return $user !== null;
}
public function getAccount(): IContent

View File

@ -5,7 +5,7 @@
@section(main)
<h2>Sign up</h2>
<div class="box">
<form id="googleSignupForm" action="/signup/google" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<form id="googleSignupForm" action="/signup/google" method="post" data-redirect-on-success="/">
<?php if ($found): ?>
<p class="justify">Please confirm that you link your account to your Google account.</p>
<?php else: ?>

View File

@ -3,7 +3,7 @@
@section(main)
<h2>Login</h2>
<div class="box">
<form id="loginForm" action="/login" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<form id="loginForm" action="/login" method="post" data-redirect-on-success="/">
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" required autofocus>
<input type="password" class="text big fullWidth marginTop" name="password" placeholder="Password" required minlength="6">
<p id="loginFormError" class="formError justify marginTop"></p>

View File

@ -4,7 +4,7 @@
<h2>Reset password</h2>
<div class="box">
<?php if ($success) : ?>
<form id="resetPasswordForm" action="/password/reset/<?= $token ?>" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<form id="resetPasswordForm" action="/password/reset/<?= $token ?>" method="post" data-redirect-on-success="/">
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" value="<?= $email ?>" disabled>
<input type="password" class="text big fullWidth marginTop" name="password" placeholder="Password" required minlength="6" autofocus>
<input type="password" class="text big fullWidth marginTop" name="password_confirm" placeholder="Password confirmation" required minlength="6">

View File

@ -96,6 +96,7 @@ TODO: condition!
</div>
</div>
<div class="buttonContainer">
<?php if (!empty($_ENV['ENABLE_GAME_FOR_GUESTS']) || Container::$request->user()): ?>
<?php if ($isAdmin): ?>
<button class="button fullWidth noRightRadius playButton" data-map-id="<?= $map['id'] ?>" data-map-name="<?= htmlspecialchars($map['name']) ?>" title="Play map '<?= $map['name'] ?>'">Play this map</button>
<a class="button yellow fullWidth noLeftRadius noRightRadius" href="/admin/mapEditor/<?= $map['id']; ?>" title="Edit map '<?= $map['name'] ?>'">Edit</a>
@ -103,6 +104,9 @@ TODO: condition!
<?php else: ?>
<button class="button fullWidth playButton" data-map-id="<?= $map['id'] ?>" data-map-name="<?= htmlspecialchars($map['name']) ?>" title="Play map '<?= $map['name'] ?>'">Play this map</button>
<?php endif; ?>
<?php else: ?>
<a href="/login" class="button fullWidth" title="Play map '<?= $map['name'] ?>'">Play this map</a>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>

View File

@ -110,7 +110,6 @@ Container::$request = new SokoWeb\Request\Request(
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'],
$_GET,
$_POST,
getallheaders(),
$_SESSION,
new MapGuesser\Repository\UserRepository()
);