Skip to content

Commit

Permalink
Hide map by author
Browse files Browse the repository at this point in the history
Fixes #213
  • Loading branch information
1-alex98 committed May 10, 2018
1 parent 8b2c06c commit 944ae1d
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 4 deletions.
91 changes: 91 additions & 0 deletions src/inttest/java/com/faforever/api/data/MapVersionElideTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.faforever.api.data;

import com.faforever.api.AbstractIntegrationTest;
import org.junit.Test;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepDefaultUser.sql")
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:sql/prepMapVersion.sql")
@Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:sql/cleanMapVersion.sql")
public class MapVersionElideTest extends AbstractIntegrationTest {

private static final String MAP_VERSION_HIDE_FALSE_ID_1 = "{\n" +
" \"data\": {\n" +
" \"type\": \"mapVersion\",\n" +
" \"id\": \"1\",\n" +
" \"attributes\": {\n" +
" \t\"hidden\": false\n" +
" }\n" +
" } \n" +
"}";
private static final String MAP_VERSION_HIDE_TRUE_ID_1 = "{\n" +
" \"data\": {\n" +
" \"type\": \"mapVersion\",\n" +
" \"id\": \"1\",\n" +
" \"attributes\": {\n" +
" \t\"hidden\": true\n" +
" }\n" +
" } \n" +
"}";
private static final String MAP_VERSION_RANKED_FALSE_ID_1 = "{\n" +
" \"data\": {\n" +
" \"type\": \"mapVersion\",\n" +
" \"id\": \"1\",\n" +
" \"attributes\": {\n" +
" \t\"ranked\": false\n" +
" }\n" +
" } \n" +
"}";
private static final String MAP_VERSION_RANKED_TRUE_ID_1 = "{\n" +
" \"data\": {\n" +
" \"type\": \"mapVersion\",\n" +
" \"id\": \"1\",\n" +
" \"attributes\": {\n" +
" \t\"ranked\": true\n" +
" }\n" +
" } \n" +
"}";


@WithUserDetails(AUTH_USER)
@Test
public void testUpdateHideToTrueDoesWork() throws Exception {
mockMvc.perform(
patch("/data/mapVersion/1")
.content(MAP_VERSION_HIDE_TRUE_ID_1))
.andExpect(status().isNoContent());
}

@WithUserDetails(AUTH_USER)
@Test
public void testUpdateHideToFalseDoesNotWork() throws Exception {
mockMvc.perform(
patch("/data/mapVersion/1")
.content(MAP_VERSION_HIDE_FALSE_ID_1))
.andExpect(status().isForbidden());
}


@WithUserDetails(AUTH_USER)
@Test
public void testUpdateRankedToFalseDoesWork() throws Exception {
mockMvc.perform(
patch("/data/mapVersion/1")
.content(MAP_VERSION_RANKED_FALSE_ID_1))
.andExpect(status().isNoContent());
}

@WithUserDetails(AUTH_USER)
@Test
public void testUpdateRankedToTrueDoesNotWork() throws Exception {
mockMvc.perform(
patch("/data/mapVersion/1")
.content(MAP_VERSION_RANKED_TRUE_ID_1))
.andExpect(status().isForbidden());
}
}
2 changes: 2 additions & 0 deletions src/inttest/resources/sql/cleanMapVersion.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DELETE FROM map_version;
DELETE FROM map;
6 changes: 6 additions & 0 deletions src/inttest/resources/sql/prepMapVersion.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DELETE FROM map_version;
DELETE FROM map;

INSERT INTO map (id, display_name, map_type, battle_type, author) VALUES (1, 'display name', 'mtype', 'btype', 1);
INSERT INTO map_version (id, description, max_players, width, height, version, filename, map_id, hidden, ranked)
VALUES (1, 'des', 2, 2, 2, 1, 'map/ghb.zip', 1, 0, 1);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faforever.api.config.elide;

import com.faforever.api.config.ApplicationProfile;
import com.faforever.api.data.checks.BooleanChange;
import com.faforever.api.data.checks.IsAuthenticated;
import com.faforever.api.data.checks.IsClanMembershipDeletable;
import com.faforever.api.data.checks.IsEntityOwner;
Expand Down Expand Up @@ -96,7 +97,8 @@ public EntityDictionary entityDictionary() {
checks.put(HasBanRead.EXPRESSION, HasBanRead.Inline.class);
checks.put(HasBanUpdate.EXPRESSION, HasBanUpdate.Inline.class);
checks.put(HasLadder1v1Update.EXPRESSION, HasLadder1v1Update.Inline.class);

checks.put(BooleanChange.TO_FALSE_EXPRESSION, BooleanChange.ToFalse.class);
checks.put(BooleanChange.TO_TRUE_EXPRESSION, BooleanChange.ToTrue.class);
return new EntityDictionary(checks);
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/faforever/api/data/checks/BooleanChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.faforever.api.data.checks;

import com.yahoo.elide.security.ChangeSpec;
import com.yahoo.elide.security.RequestScope;
import com.yahoo.elide.security.checks.OperationCheck;

import java.util.Optional;

public class BooleanChange {

public static final String TO_FALSE_EXPRESSION = "boolean changed to false";
public static final String TO_TRUE_EXPRESSION = "boolean changed to true";


public static class ToFalse extends OperationCheck<Object> {

@Override
public boolean ok(Object entity, RequestScope requestScope, Optional<ChangeSpec> optionalChangeSpec) {
if (!optionalChangeSpec.isPresent()) {
return true;
}
ChangeSpec changeSpec = optionalChangeSpec.get();
if (!(changeSpec.getModified() instanceof Boolean)) {
throw new IllegalStateException("This Expression can only be applied to boolean fields");
}
return !((Boolean) changeSpec.getModified());
}

}

public static class ToTrue extends OperationCheck<Object> {

@Override
public boolean ok(Object entity, RequestScope requestScope, Optional<ChangeSpec> optionalChangeSpec) {
if (!optionalChangeSpec.isPresent()) {
return true;
}
ChangeSpec changeSpec = optionalChangeSpec.get();
if (!(changeSpec.getModified() instanceof Boolean)) {
throw new IllegalStateException("This Expression can only be applied to boolean fields");
}
return ((Boolean) changeSpec.getModified());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Inline extends InlineCheck<OwnableEntity> {
public boolean ok(OwnableEntity entity, RequestScope requestScope, Optional<ChangeSpec> changeSpec) {
Object opaqueUser = requestScope.getUser().getOpaqueUser();
return opaqueUser instanceof FafUserDetails
&& entity.getEntityOwner() != null
&& entity.getEntityOwner().getId() == ((FafUserDetails) opaqueUser).getId();
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/faforever/api/data/domain/MapVersion.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.BooleanChange;
import com.faforever.api.data.checks.IsEntityOwner;
import com.faforever.api.data.checks.permission.IsModerator;
import com.faforever.api.data.listeners.MapVersionEnricher;
import com.yahoo.elide.annotation.Audit;
Expand Down Expand Up @@ -28,7 +30,7 @@
@EntityListeners(MapVersionEnricher.class)
@Table(name = "map_version")
@Include(rootLevel = true, type = MapVersion.TYPE_NAME)
public class MapVersion extends AbstractEntity {
public class MapVersion extends AbstractEntity implements OwnableEntity {

public static final String TYPE_NAME = "mapVersion";

Expand Down Expand Up @@ -85,14 +87,14 @@ public String getFilename() {
return filename;
}

@UpdatePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION + " or (" + IsEntityOwner.EXPRESSION + " and " + BooleanChange.TO_FALSE_EXPRESSION + ")")
@Audit(action = Action.UPDATE, logStatement = "Updated map version `{0}` attribute ranked to: {1}", logExpressions = {"${mapVersion.id}", "${mapVersion.ranked}"})
@Column(name = "ranked")
public boolean isRanked() {
return ranked;
}

@UpdatePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION + " or (" + IsEntityOwner.EXPRESSION + " and " + BooleanChange.TO_TRUE_EXPRESSION + ")")
@Audit(action = Action.UPDATE, logStatement = "Updated map version `{0}` attribute hidden to: {1}", logExpressions = {"${mapVersion.id}", "${mapVersion.hidden}"})
@Column(name = "hidden")
public boolean isHidden() {
Expand Down Expand Up @@ -147,4 +149,10 @@ public List<MapVersionReview> getReviews() {
public MapVersionReviewsSummary getReviewsSummary() {
return reviewsSummary;
}

@Transient
@Override
public Login getEntityOwner() {
return map.getAuthor();
}
}

0 comments on commit 944ae1d

Please sign in to comment.