Skip to content

Commit

Permalink
Bugfix/changes by rtx2070 (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
bratkartoffel authored Jan 28, 2024
2 parents 46b547d + 4f063cd commit 643baff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 85 deletions.
118 changes: 35 additions & 83 deletions include/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1160,89 +1160,41 @@ function createHelpLink(int $module, int $category): string

function getCurrentPage(): string
{
$p = array_key_exists('p', $_GET) ? $_GET['p'] : 'index';
if (isLoggedIn()) {
switch ($p) {
case 'admin':
case 'admin_benutzer':
case 'admin_benutzer_bearbeiten':
case 'admin_benutzer_importieren':
case 'admin_gruppe':
case 'admin_gruppe_bearbeiten':
case 'admin_test':
case 'admin_markt':
case 'admin_vertrag':
case 'admin_vertrag_einstellen':
case 'admin_vertrag_bearbeiten':
case 'admin_markt_einstellen':
case 'admin_markt_bearbeiten':
case 'admin_log_bank':
case 'admin_log_bioladen':
case 'admin_log_gruppenkasse':
case 'admin_log_login':
case 'admin_log_mafia':
case 'admin_log_marktplatz':
case 'admin_log_nachrichten':
case 'admin_log_vertraege':
if (!isAdmin()) {
redirectTo('/?p=index', 101, __LINE__);
}
$page = $p;
break;
case 'bank':
case 'bioladen':
case 'buero':
case 'forschungszentrum':
case 'gebaeude':
case 'marktplatz_liste':
case 'marktplatz_verkaufen':
case 'plantage':
case 'vertraege_liste':
case 'vertraege_neu':
case 'mafia':
case 'statistik':
case 'gruppe':
case 'gruppe_einstellungen':
case 'gruppe_mitgliederverwaltung':
case 'gruppe_diplomatie':
case 'gruppe_kasse':
case 'gruppe_logbuch':
case 'gruppe_krieg_details':
case 'rangliste':
case 'rangliste_spezial':
case 'index':
case 'impressum':
case 'regeln':
case 'einstellungen':
case 'nachrichten_lesen':
case 'nachrichten_liste':
case 'nachrichten_schreiben':
case 'notizblock':
case 'hilfe':
case 'profil':
case 'special':
$page = $p;
break;
default:
$page = 'index';
break;
}
} else {
switch ($p) {
case 'anmelden':
case 'registrieren':
case 'index':
case 'passwort_vergessen':
case 'regeln':
case 'impressum':
$page = $p;
break;
default:
$page = 'index';
break;
}
}
return $page;
$adminSeiten = [
'admin', 'admin_benutzer', 'admin_benutzer_bearbeiten', 'admin_benutzer_importieren',
'admin_gruppe', 'admin_gruppe_bearbeiten', 'admin_test', 'admin_markt',
'admin_vertrag', 'admin_vertrag_einstellen', 'admin_vertrag_bearbeiten',
'admin_markt_einstellen', 'admin_markt_bearbeiten', 'admin_log_bank',
'admin_log_bioladen', 'admin_log_gruppenkasse', 'admin_log_login',
'admin_log_mafia', 'admin_log_marktplatz', 'admin_log_nachrichten',
'admin_log_vertraege'
];

$userSeiten = [
'bank', 'bioladen', 'buero', 'forschungszentrum', 'gebaeude', 'marktplatz_liste',
'marktplatz_verkaufen', 'plantage', 'vertraege_liste', 'vertraege_neu', 'mafia',
'statistik', 'gruppe', 'gruppe_einstellungen', 'gruppe_mitgliederverwaltung',
'gruppe_diplomatie', 'gruppe_kasse', 'gruppe_logbuch', 'gruppe_krieg_details',
'rangliste', 'rangliste_spezial', 'index', 'impressum', 'regeln', 'einstellungen',
'nachrichten_lesen', 'nachrichten_liste', 'nachrichten_schreiben', 'notizblock',
'hilfe', 'profil', 'special'
];

$gastSeiten = ['anmelden', 'registrieren', 'index', 'passwort_vergessen', 'regeln', 'impressum'];

$aktuelleSeite = array_key_exists('p', $_GET) ? $_GET['p'] : 'index';
$istEingeloggt = isLoggedIn();
$istAdmin = isAdmin();

if ($istAdmin && in_array($aktuelleSeite, $adminSeiten)) {
return $aktuelleSeite;
} elseif ($istEingeloggt && in_array($aktuelleSeite, $userSeiten)) {
return $aktuelleSeite;
} elseif (!$istEingeloggt && in_array($aktuelleSeite, $gastSeiten)) {
return $aktuelleSeite;
}
redirectTo('/?p=index', 101, __LINE__);
return 'index';
}

function buildingRequirementsMet(int $building_id, array $player): bool
Expand Down
36 changes: 36 additions & 0 deletions tests/src/test/java/eu/fraho/blm2/st/GeneralTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
*/
package eu.fraho.blm2.st;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;

public class GeneralTests extends AbstractTest {
private final int userIdUser = getNextUserId();

@ParameterizedTest
@ValueSource(strings = {
".git/HEAD",
Expand All @@ -34,4 +46,28 @@ void testSensitiveFilesInacessible(String path) throws IOException, InterruptedE
HttpResponse<String> response = simpleHttpGet("%s/%s".formatted(AbstractTest.BASE_URL, path));
Assertions.assertEquals(4, response.statusCode() / 100);
}

@ParameterizedTest
@MethodSource("getAdminPages")
void testRegularUserMayNotAccessAdmin(String page) {
resetPlayer(userIdUser, getClass().getSimpleName());
login("test" + userIdUser);

WebDriver driver = getDriver();
driver.get("%s/?p=%s".formatted(AbstractTest.BASE_URL, page));
assertElementPresent(By.id("meldung_101"));
}

static List<String> getAdminPages() throws IOException {
try (Stream<Path> files = Files.list(Path.of("../pages/"))) {
List<String> pages = files.map(Path::getFileName)
.map(Path::toString)
.filter(s -> s.endsWith(".inc.php"))
.filter(s -> s.startsWith("admin"))
.map(s -> s.substring(0, s.length() - 8))
.toList();
MatcherAssert.assertThat(pages.size(), Matchers.greaterThan(10));
return pages;
}
}
}
2 changes: 0 additions & 2 deletions tests/src/test/resources/Dockerfile-php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ RUN apt-get update \
# clean apt cache
&& rm -rf /var/lib/apt/lists/*

COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

HEALTHCHECK --interval=60s --timeout=2s \
CMD curl --silent --fail http://localhost/ || exit 1
1 change: 1 addition & 0 deletions tests/src/test/resources/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
- ./config.ini:/var/www/html/blm2/config/config.ini
- ./test-reset-player.php:/var/www/html/blm2/actions/test-reset-player.php
- ./test-run-cron.php:/var/www/html/blm2/actions/test-run-cron.php
# - ./xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini

inbucket:
image: inbucket/inbucket:3.0.4
Expand Down

0 comments on commit 643baff

Please sign in to comment.