Skip to content

Commit

Permalink
Check permission to view map description
Browse files Browse the repository at this point in the history
  • Loading branch information
Seltsamsel committed May 4, 2019
1 parent 345d5e3 commit 37d81e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
12 changes: 9 additions & 3 deletions controller/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,18 @@ public function all_players(): JsonResponse
public function rmatches(): JsonResponse
{
return $this->respond(function () {
return zone_util::matches()->get_all_rmatches();
return zone_util::matches()->get_all_rmatches(acl::has_permission($this->auth, self::is_activated($this->get_user_id()), acl::u_zone_view_maps));
});
}

public function pmatches(int $page = 0): JsonResponse
{
return $this->respond(function ($args) {
return [
'items' => zone_util::matches()->get_pmatches($args['page']),
'items' => zone_util::matches()->get_pmatches(
$args['page'],
acl::has_permission($this->auth, self::is_activated($this->get_user_id()), acl::u_zone_view_maps)
),
'total_pages' => zone_util::matches()->get_pmatches_total_pages()
];
}, [], [
Expand All @@ -456,7 +459,10 @@ public function pmatches(int $page = 0): JsonResponse
public function match(int $match_id): JsonResponse
{
return $this->respond(function ($args) {
return zone_util::matches()->get_match($args['match_id']);
$match = zone_util::matches()->get_match(
$args['match_id'],
acl::has_permission($this->auth, self::is_activated($this->get_user_id()), acl::u_zone_view_maps)
);
}, [], [
'match_id' => $match_id,
]);
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/view/components/Match.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<template v-if="match.map">
<div v-t="'NCZONE_MATCH_MAP'"></div>
<div @click="openMapDescriptionOverlay(match.map)">{{ match.map.name }}</div>
<div @click="openMapDescription(match.map)">{{ match.map.name }}</div>
</template>

<template v-if="haveGlobalCivs">
Expand Down Expand Up @@ -117,6 +117,11 @@ export default {
addPair () {
this.openAddPairPreviewOverlay(this.match.id)
},
openMapDescription (map) {
if (this.canViewMaps) {
this.openMapDescriptionOverlay(map)
}
},
...mapActions([
'postMatchResult',
'openAddPairPreviewOverlay',
Expand Down Expand Up @@ -159,7 +164,8 @@ export default {
'timer',
'canModPost',
'canAddPairMod',
'canAddPairUser'
'canAddPairUser',
'canViewMaps'
])
},
data () {
Expand Down
23 changes: 14 additions & 9 deletions zone/entity/match.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ class match implements \JsonSerializable
private $players;
private $forum_topic_id;

public static function create_by_row_finished(array $row): match
public static function create_by_row_finished(array $row, bool $with_description = True): match
{
return self::_create_by_row($row, true);
return self::_create_by_row($row, true, $with_description);
}

public static function create_by_row_unfinished(array $row): match
public static function create_by_row_unfinished(array $row, bool $with_description = True): match
{
return self::_create_by_row($row, false);
return self::_create_by_row($row, false, $with_description);
}

private static function _create_by_row(array $row, $finished): match
private static function _create_by_row(array $row, bool $finished, bool $with_description = True): match
{
$match_id = (int)$row['match_id'];
$map_id = (int)$row['map_id'];
Expand All @@ -55,12 +55,17 @@ private static function _create_by_row(array $row, $finished): match
'id' => (int)$row['draw_user_id'],
'username' => $row['draw_username'],
];
$entity->map = empty($map_id) ? null : [

$map_array = [
'id' => $map_id,
'name' => $row['map_name'],
'description' => $row['map_description'],
'image' => $map_image_url
'name' => $row['map_name']
];
if ($with_description) {
$map_array['description'] = $row['map_description'];
$map_array['image'] = $map_image_url;
}

$entity->map = empty($map_id) ? null : $map_array;
$entity->civs = array_merge(['both' => zone_util::matches()->get_match_civs($match_id, $map_id), 'banned' => zone_util::matches()->get_banned_civs($match_id, $map_id)], zone_util::matches()->get_team_civs($team1_id, $team2_id, $map_id));
$entity->bets = zone_util::bets()->get_bets($team1_id, $team2_id, $finished ? true : false);
$entity->players = zone_util::players()->get_match_players($match_id, $team1_id, $team2_id, $map_id);
Expand Down
24 changes: 18 additions & 6 deletions zone/matches.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,27 +659,39 @@ private function load_match_rows(
return $this->db->get_rows($sql, $limit, $offset);
}

public function get_match(int $match_id): entity\match
public function get_match(int $match_id, bool $with_description = True): entity\match
{
$rows = $this->load_match_rows(["t.match_id = {$match_id}"]);
if (empty($rows)) {
return null;
}
return entity\match::create_by_row_finished($rows[0]);
return entity\match::create_by_row_finished($rows[0], $with_description);
}

public function get_all_rmatches(): array
public function get_all_rmatches(bool $with_description = True): array
{
$rows = $this->load_match_rows(['t.post_time = 0'], ['t.draw_time DESC']);
return \array_map([match::class, 'create_by_row_unfinished'], $rows);
return \array_map(
function(array $row) use ($with_description): entity\match
{
return entity\match::create_by_row_finished($row, $with_description);
},
$rows
);
}

public function get_pmatches(int $page = 0): array
public function get_pmatches(int $page = 0, bool $with_description = True): array
{
$limit = (int)config::get(config::pmatches_page_size);
$offset = $page * $limit;
$rows = $this->load_match_rows(['t.post_time > 0'], ['t.post_time DESC'], $offset, $limit);
return \array_map([entity\match::class, 'create_by_row_finished'], $rows);
return \array_map(
function(array $row) use ($with_description): entity\match
{
return entity\match::create_by_row_finished($row, $with_description);
},
$rows
);
}

public function get_pmatches_total_pages(): int
Expand Down

0 comments on commit 37d81e1

Please sign in to comment.