1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 17:56:20 +00:00

#79: POST /theme/color to change the main color

This commit is contained in:
jendib
2016-04-18 00:00:46 +02:00
parent 55cdca0c7d
commit 4e768e9103
11 changed files with 138 additions and 36 deletions

View File

@@ -57,9 +57,8 @@ public abstract class BaseResource {
* Checks if the user has a base function. Throw an exception if the check fails.
*
* @param baseFunction Base function to check
* @throws JSONException
*/
protected void checkBaseFunction(BaseFunction baseFunction) {
void checkBaseFunction(BaseFunction baseFunction) {
if (!hasBaseFunction(baseFunction)) {
throw new ForbiddenClientException();
}
@@ -70,9 +69,8 @@ public abstract class BaseResource {
*
* @param baseFunction Base function to check
* @return True if the user has the base function
* @throws JSONException
*/
protected boolean hasBaseFunction(BaseFunction baseFunction) {
boolean hasBaseFunction(BaseFunction baseFunction) {
if (principal == null || !(principal instanceof UserPrincipal)) {
return false;
}
@@ -86,7 +84,7 @@ public abstract class BaseResource {
* @param shareId Share ID (optional)
* @return List of ACL target ID
*/
protected List<String> getTargetIdList(String shareId) {
List<String> getTargetIdList(String shareId) {
List<String> targetIdList = Lists.newArrayList(principal.getGroupIdSet());
if (principal.getId() != null) {
targetIdList.add(principal.getId());

View File

@@ -64,7 +64,6 @@ public class TagResource extends BaseResource {
* Returns stats on tags.
*
* @return Response
* @throws JSONException
*/
@GET
@Path("/stats")

View File

@@ -1,12 +1,21 @@
package com.sismics.docs.rest.resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.json.*;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import com.google.common.base.Strings;
import com.sismics.docs.core.constant.ConfigType;
import com.sismics.docs.core.dao.jpa.ConfigDao;
import com.sismics.docs.core.model.jpa.Config;
import com.sismics.docs.rest.constant.BaseFunction;
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.css.Selector;
import java.io.StringReader;
import java.util.Map;
/**
* Theme REST resources.
*
@@ -23,8 +32,75 @@ public class ThemeResource extends BaseResource {
@Path("/stylesheet")
@Produces("text/css")
public Response stylesheet() {
JsonObject themeConfig = getThemeConfig();
// Build the stylesheet
StringBuilder sb = new StringBuilder();
sb.append(new Selector("body"));
sb.append(new Selector(".navbar")
.rule("background-color", themeConfig.getString("color", "inherit")));
return Response.ok().entity(sb.toString()).build();
}
@POST
@Path("/color")
public Response color(@FormParam("color") String color) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
checkBaseFunction(BaseFunction.ADMIN);
// Validate input data
ValidationUtil.validateHexColor(color, "color", true);
// Update the JSON
JsonObjectBuilder json = getMutableThemeConfig();
if (Strings.isNullOrEmpty(color)) {
json.add("color", JsonValue.NULL);
} else {
json.add("color", color);
}
// Persist the new configuration
ConfigDao configDao = new ConfigDao();
configDao.update(ConfigType.THEME, json.build().toString());
// Always return OK
JsonObjectBuilder response = Json.createObjectBuilder()
.add("status", "ok");
return Response.ok().entity(response.build()).build();
}
/**
* Returns the theme configuration object.
*
* @return Theme configuration
*/
private JsonObject getThemeConfig() {
ConfigDao configDao = new ConfigDao();
Config themeConfig = configDao.getById(ConfigType.THEME);
if (themeConfig == null) {
return Json.createObjectBuilder().build();
}
try (JsonReader reader = Json.createReader(new StringReader(themeConfig.getValue()))) {
return reader.readObject();
}
}
/**
* Returns a mutable theme configuration.
*
* @return Json builder
*/
private JsonObjectBuilder getMutableThemeConfig() {
JsonObject themeConfig = getThemeConfig();
JsonObjectBuilder json = Json.createObjectBuilder();
for (Map.Entry<String, JsonValue> entry : themeConfig.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
return json;
}
}

View File

@@ -1,7 +1,13 @@
package com.sismics.docs.rest;
import com.sismics.util.filter.TokenBasedSecurityFilter;
import org.junit.Assert;
import org.junit.Test;
import javax.json.JsonObject;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
/**
* Test the theme resource.
*
@@ -13,9 +19,23 @@ public class TestThemeResource extends BaseJerseyTest {
*/
@Test
public void testThemeResource() {
// Login admin
String adminToken = clientUtil.login("admin", "admin", false);
// Get the stylesheet anonymously
String stylesheet = target().path("/theme/stylesheet").request()
.get(String.class);
System.out.println(stylesheet);
Assert.assertTrue(stylesheet.contains("background-color: inherit;"));
// Update the main color as admin
target().path("/theme/color").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.post(Entity.form(new Form()
.param("color", "#ff0000")), JsonObject.class);
// Get the stylesheet anonymously
stylesheet = target().path("/theme/stylesheet").request()
.get(String.class);
Assert.assertTrue(stylesheet.contains("background-color: #ff0000;"));
}
}