Closes #30: Delete locale & theme concept

This commit is contained in:
jendib 2015-09-07 23:49:12 +02:00
parent 0fe51d355c
commit a95dcf488d
25 changed files with 43 additions and 488 deletions

View File

@ -10,21 +10,11 @@ import com.google.common.collect.Lists;
* @author jtremeaux
*/
public class Constants {
/**
* Default locale.
*/
public static final String DEFAULT_LOCALE_ID = "en";
/**
* Default timezone ID.
*/
public static final String DEFAULT_TIMEZONE_ID = "Europe/London";
/**
* Default theme ID.
*/
public static final String DEFAULT_THEME_ID = "default.less";
/**
* Administrator's default password ("admin").
*/

View File

@ -1,43 +0,0 @@
package com.sismics.docs.core.dao.jpa;
import com.sismics.docs.core.model.jpa.Locale;
import com.sismics.util.context.ThreadLocalContext;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.List;
/**
* Locale DAO.
*
* @author jtremeaux
*/
public class LocaleDao {
/**
* Gets a locale by its ID.
*
* @param id Locale ID
* @return Locale
*/
public Locale getById(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
try {
return em.find(Locale.class, id);
} catch (NoResultException e) {
return null;
}
}
/**
* Returns the list of all locales.
*
* @return List of locales
*/
@SuppressWarnings("unchecked")
public List<Locale> findAll() {
EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select l from Locale l order by l.id");
return q.getResultList();
}
}

View File

@ -16,7 +16,6 @@ import org.mindrot.jbcrypt.BCrypt;
import com.google.common.base.Joiner;
import com.sismics.docs.core.constant.AuditLogType;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.jpa.criteria.UserCriteria;
import com.sismics.docs.core.dao.jpa.dto.UserDto;
import com.sismics.docs.core.model.jpa.User;
@ -78,7 +77,6 @@ public class UserDao {
// Create the user
user.setCreateDate(new Date());
user.setPassword(hashPassword(user.getPassword()));
user.setTheme(Constants.DEFAULT_THEME_ID);
em.persist(user);
// Create audit log
@ -102,10 +100,7 @@ public class UserDao {
User userFromDb = (User) q.getSingleResult();
// Update the user
userFromDb.setLocaleId(user.getLocaleId());
userFromDb.setEmail(user.getEmail());
userFromDb.setTheme(user.getTheme());
userFromDb.setFirstConnection(user.isFirstConnection());
// Create audit log
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE);
@ -231,7 +226,7 @@ public class UserDao {
Map<String, Object> parameterMap = new HashMap<String, Object>();
List<String> criteriaList = new ArrayList<String>();
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3, u.USE_IDLOCALE_C as c4");
StringBuilder sb = new StringBuilder("select u.USE_ID_C as c0, u.USE_USERNAME_C as c1, u.USE_EMAIL_C as c2, u.USE_CREATEDATE_D as c3");
sb.append(" from T_USER u ");
// Add search criterias
@ -260,7 +255,6 @@ public class UserDao {
userDto.setUsername((String) o[i++]);
userDto.setEmail((String) o[i++]);
userDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
userDto.setLocaleId((String) o[i++]);
userDtoList.add(userDto);
}
paginatedList.setResultList(userDtoList);

View File

@ -11,11 +11,6 @@ public class UserDto {
*/
private String id;
/**
* Locale ID.
*/
private String localeId;
/**
* Username.
*/
@ -49,24 +44,6 @@ public class UserDto {
this.id = id;
}
/**
* Getter of localeId.
*
* @return localeId
*/
public String getLocaleId() {
return localeId;
}
/**
* Setter of localeId.
*
* @param localeId localeId
*/
public void setLocaleId(String localeId) {
this.localeId = localeId;
}
/**
* Getter of username.
*

View File

@ -1,49 +0,0 @@
package com.sismics.docs.core.model.jpa;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.google.common.base.MoreObjects;
/**
* Locale entity.
*
* @author jtremeaux
*/
@Entity
@Table(name = "T_LOCALE")
public class Locale {
/**
* Locale ID (ex: fr_FR).
*/
@Id
@Column(name = "LOC_ID_C", length = 10)
private String id;
/**
* Getter of id.
*
* @return id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.toString();
}
}

View File

@ -27,12 +27,6 @@ public class User implements Loggable {
@Column(name = "USE_ID_C", length = 36)
private String id;
/**
* Locale ID.
*/
@Column(name = "USE_IDLOCALE_C", nullable = false, length = 10)
private String localeId;
/**
* Role ID.
*/
@ -63,18 +57,6 @@ public class User implements Loggable {
@Column(name = "USE_EMAIL_C", nullable = false, length = 100)
private String email;
/**
* Theme.
*/
@Column(name = "USE_THEME_C", nullable = false, length = 100)
private String theme;
/**
* True if the user hasn't dismissed the first connection screen.
*/
@Column(name = "USE_FIRSTCONNECTION_B", nullable = false)
private boolean firstConnection;
/**
* Creation date.
*/
@ -105,24 +87,6 @@ public class User implements Loggable {
this.id = id;
}
/**
* Getter of localeId.
*
* @return localeId
*/
public String getLocaleId() {
return localeId;
}
/**
* Setter of localeId.
*
* @param localeId localeId
*/
public void setLocaleId(String localeId) {
this.localeId = localeId;
}
/**
* Getter of roleId.
*
@ -195,42 +159,6 @@ public class User implements Loggable {
this.email = email;
}
/**
* Getter of theme.
*
* @return theme
*/
public String getTheme() {
return theme;
}
/**
* Setter of theme.
*
* @param theme theme
*/
public void setTheme(String theme) {
this.theme = theme;
}
/**
* Getter of firstConnection.
*
* @return firstConnection
*/
public boolean isFirstConnection() {
return firstConnection;
}
/**
* Setter of firstConnection.
*
* @param firstConnection firstConnection
*/
public void setFirstConnection(boolean firstConnection) {
this.firstConnection = firstConnection;
}
/**
* Getter of createDate.
*

View File

@ -1,62 +0,0 @@
package com.sismics.util;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.dao.jpa.LocaleDao;
import org.apache.commons.lang.StringUtils;
import java.util.Locale;
/**
* Locale utilities.
*
* @author jtremeaux
*/
public class LocaleUtil {
/**
* Returns the locale from its language / country code (ex: fr_FR).
*
* @param localeCode Locale code
* @return Locate instance
*/
public static final Locale getLocale(String localeCode) {
String[] localeCodeArray = localeCode.split("_");
String language = localeCodeArray[0];
String country = "";
String variant = "";
if (localeCodeArray.length >= 2) {
country = localeCodeArray[1];
}
if (localeCodeArray.length >= 3) {
variant = localeCodeArray[2];
}
return new Locale(language, country, variant);
}
/**
* Extracts the ID of the locale from the HTTP Accept-Language header.
*
* @param acceptLanguageHeader header
* @return Locale ID
*/
public static String getLocaleIdFromAcceptLanguage(String acceptLanguageHeader) {
String localeId = null;
if (StringUtils.isNotBlank(acceptLanguageHeader)) {
acceptLanguageHeader = acceptLanguageHeader.replaceAll("-", "_");
localeId = acceptLanguageHeader.split(",")[0];
}
if (StringUtils.isNotBlank(localeId)) {
LocaleDao localeDao = new LocaleDao();
com.sismics.docs.core.model.jpa.Locale locale = localeDao.getById(localeId);
if (locale != null) {
localeId = locale.getId();
} else {
// The client provided an unknown locale
localeId = Constants.DEFAULT_LOCALE_ID;
}
}
if (StringUtils.isBlank(localeId)) {
localeId = Constants.DEFAULT_LOCALE_ID;
}
return localeId;
}
}

View File

@ -1,43 +0,0 @@
package com.sismics.util;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* Message utilities.
*
* @author jtremeaux
*/
public class MessageUtil {
/**
* Returns a message formated in the specified locale.
* Returns **key** if no message is set for this key.
*
* @param locale Locale
* @param key Message key
* @param args Arguments of the message
* @return Formated message
*/
public static String getMessage(Locale locale, String key, Object... args) {
ResourceBundle resources = ResourceBundle.getBundle("messages", locale);
String message = null;
try {
message = resources.getString(key);
} catch (MissingResourceException e) {
message = "**" + key + "**";
}
return MessageFormat.format(message, args);
}
/**
* Returns the resource bundle containing messages for the locale.
*
* @param locale Locale to use
* @return Resource bundle
*/
public static ResourceBundle getMessage(Locale locale) {
return ResourceBundle.getBundle("messages", locale);
}
}

View File

@ -9,7 +9,6 @@
<class>com.sismics.docs.core.model.jpa.AuthenticationToken</class>
<class>com.sismics.docs.core.model.jpa.BaseFunction</class>
<class>com.sismics.docs.core.model.jpa.Config</class>
<class>com.sismics.docs.core.model.jpa.Locale</class>
<class>com.sismics.docs.core.model.jpa.User</class>
<class>com.sismics.docs.core.model.jpa.RoleBaseFunction</class>
<class>com.sismics.docs.core.model.jpa.Document</class>

View File

@ -1 +1 @@
db.version=0
db.version=1

View File

@ -0,0 +1,5 @@
alter table T_USER drop column USE_IDLOCALE_C;
alter table T_USER drop column USE_THEME_C;
alter table T_USER drop column USE_FIRSTCONNECTION_B;
drop table T_LOCALE;
update T_CONFIG set CFG_VALUE_C = '1' where CFG_ID_C = 'DB_VERSION';

View File

@ -19,7 +19,6 @@ public class TestJpa extends BaseTransactionalTest {
User user = new User();
user.setUsername("username");
user.setEmail("toto@docs.com");
user.setLocaleId("fr");
user.setRoleId("admin");
user.setPrivateKey("AwesomePrivateKey");
String id = userDao.create(user);

View File

@ -2,8 +2,6 @@ package com.sismics.security;
import org.joda.time.DateTimeZone;
import java.util.Locale;
/**
* Anonymous principal.
*
@ -12,11 +10,6 @@ import java.util.Locale;
public class AnonymousPrincipal implements IPrincipal {
public static final String ANONYMOUS = "anonymous";
/**
* User locale.
*/
private Locale locale;
/**
* User timezone.
*/
@ -44,20 +37,6 @@ public class AnonymousPrincipal implements IPrincipal {
return true;
}
@Override
public Locale getLocale() {
return locale;
}
/**
* Setter of locale.
*
* @param locale locale
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
@Override
public DateTimeZone getDateTimeZone() {
return dateTimeZone;

View File

@ -1,9 +1,8 @@
package com.sismics.security;
import org.joda.time.DateTimeZone;
import java.security.Principal;
import java.util.Locale;
import org.joda.time.DateTimeZone;
/**
* Interface of principals.
@ -25,13 +24,6 @@ public interface IPrincipal extends Principal {
*/
public String getId();
/**
* Returns the locale of the principal.
*
* @return Locale of the principal
*/
public Locale getLocale();
/**
* Returns the timezone of the principal.
*

View File

@ -1,10 +1,9 @@
package com.sismics.security;
import org.joda.time.DateTimeZone;
import java.util.Locale;
import java.util.Set;
import org.joda.time.DateTimeZone;
/**
* Authenticated users principal.
*
@ -21,11 +20,6 @@ public class UserPrincipal implements IPrincipal {
*/
private String name;
/**
* Locale of the principal.
*/
private Locale locale;
/**
* Timezone of the principal.
*/
@ -85,20 +79,6 @@ public class UserPrincipal implements IPrincipal {
this.name = name;
}
@Override
public Locale getLocale() {
return locale;
}
/**
* Setter of locale.
*
* @param locale locale
*/
public void setLocale(Locale locale) {
this.locale = locale;
}
@Override
public DateTimeZone getDateTimeZone() {
return dateTimeZone;

View File

@ -1,26 +1,31 @@
package com.sismics.util.filter;
import com.sismics.docs.core.constant.Constants;
import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.TransactionUtil;
import com.sismics.util.EnvironmentUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.jpa.EMF;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Locale;
import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.TransactionUtil;
import com.sismics.util.EnvironmentUtil;
import com.sismics.util.context.ThreadLocalContext;
import com.sismics.util.jpa.EMF;
/**
* Filter used to process a couple things in the request context.
@ -35,9 +40,6 @@ public class RequestContextFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// Force the locale in order to not depend on the execution environment
Locale.setDefault(new Locale(Constants.DEFAULT_LOCALE_ID));
// Initialize the app directory
if (!filterConfig.getServletContext().getServerInfo().startsWith("Grizzly")) {
EnvironmentUtil.setWebappContext(true);

View File

@ -3,7 +3,6 @@ package com.sismics.util.filter;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import javax.servlet.Filter;
@ -28,7 +27,6 @@ import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.util.TransactionUtil;
import com.sismics.security.AnonymousPrincipal;
import com.sismics.security.UserPrincipal;
import com.sismics.util.LocaleUtil;
/**
* This filter is used to authenticate the user having an active session via an authentication token stored in database.
@ -155,10 +153,6 @@ public class TokenBasedSecurityFilter implements Filter {
private void injectAuthenticatedUser(HttpServletRequest request, User user) {
UserPrincipal userPrincipal = new UserPrincipal(user.getId(), user.getUsername());
// Add locale
Locale locale = LocaleUtil.getLocale(user.getLocaleId());
userPrincipal.setLocale(locale);
// Add base functions
RoleBaseFunctionDao userBaseFuction = new RoleBaseFunctionDao();
Set<String> baseFunctionSet = userBaseFuction.findByRoleId(user.getRoleId());
@ -174,7 +168,6 @@ public class TokenBasedSecurityFilter implements Filter {
*/
private void injectAnonymousUser(HttpServletRequest request) {
AnonymousPrincipal anonymousPrincipal = new AnonymousPrincipal();
anonymousPrincipal.setLocale(request.getLocale());
anonymousPrincipal.setDateTimeZone(DateTimeZone.forID(Constants.DEFAULT_TIMEZONE_ID));
request.setAttribute(PRINCIPAL_ATTRIBUTE, anonymousPrincipal);

View File

@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=0
db.version=1

View File

@ -1,41 +0,0 @@
package com.sismics.docs.rest.resource;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import com.sismics.docs.core.dao.jpa.LocaleDao;
import com.sismics.docs.core.model.jpa.Locale;
/**
* Locale REST resources.
*
* @author jtremeaux
*/
@Path("/locale")
public class LocaleResource extends BaseResource {
/**
* Returns the list of all locales.
*
* @return Response
*/
@GET
public Response list() {
LocaleDao localeDao = new LocaleDao();
List<Locale> localeList = localeDao.findAll();
JsonArrayBuilder items = Json.createArrayBuilder();
for (Locale locale : localeList) {
items.add(Json.createObjectBuilder()
.add("id", locale.getId()));
}
JsonObjectBuilder response = Json.createObjectBuilder()
.add("locales", items);
return Response.ok().entity(response.build()).build();
}
}

View File

@ -42,7 +42,6 @@ import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.ValidationUtil;
import com.sismics.security.UserPrincipal;
import com.sismics.util.LocaleUtil;
import com.sismics.util.filter.TokenBasedSecurityFilter;
/**
@ -91,8 +90,6 @@ public class UserResource extends BaseResource {
}
user.setCreateDate(new Date());
user.setLocaleId(LocaleUtil.getLocaleIdFromAcceptLanguage(request.getHeader("Accept-Language")));
// Create the user
UserDao userDao = new UserDao();
try {
@ -116,14 +113,12 @@ public class UserResource extends BaseResource {
*
* @param password Password
* @param email E-Mail
* @param firstConnection True if the user hasn't acknowledged the first connection wizard yet.
* @return Response
*/
@POST
public Response update(
@FormParam("password") String password,
@FormParam("email") String email,
@FormParam("first_connection") Boolean firstConnection) {
@FormParam("email") String email) {
if (!authenticate()) {
throw new ForbiddenClientException();
@ -139,9 +134,6 @@ public class UserResource extends BaseResource {
if (email != null) {
user.setEmail(email);
}
if (firstConnection != null && hasBaseFunction(BaseFunction.ADMIN)) {
user.setFirstConnection(firstConnection);
}
user = userDao.update(user);
@ -232,7 +224,7 @@ public class UserResource extends BaseResource {
}
/**
* This resource is used to authenticate the user and create a user ession.
* This resource is used to authenticate the user and create a user session.
* The "session" is only used to identify the user, no other data is stored in the session.
*
* @param username Username
@ -402,9 +394,6 @@ public class UserResource extends BaseResource {
if (!authenticate()) {
response.add("anonymous", true);
String localeId = LocaleUtil.getLocaleIdFromAcceptLanguage(request.getHeader("Accept-Language"));
response.add("locale", localeId);
// Check if admin has the default password
UserDao userDao = new UserDao();
User adminUser = userDao.getById("admin");
@ -416,9 +405,7 @@ public class UserResource extends BaseResource {
UserDao userDao = new UserDao();
User user = userDao.getById(principal.getId());
response.add("username", user.getUsername())
.add("email", user.getEmail())
.add("locale", user.getLocaleId())
.add("first_connection", user.isFirstConnection());
.add("email", user.getEmail());
JsonArrayBuilder baseFunctions = Json.createArrayBuilder();
for (String baseFunction : ((UserPrincipal) principal).getBaseFunctionSet()) {
baseFunctions.add(baseFunction);
@ -453,8 +440,7 @@ public class UserResource extends BaseResource {
JsonObjectBuilder response = Json.createObjectBuilder()
.add("username", user.getUsername())
.add("email", user.getEmail())
.add("locale", user.getLocaleId());
.add("email", user.getEmail());
return Response.ok().entity(response.build()).build();
}

View File

@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=0
db.version=1

View File

@ -1,3 +1,3 @@
api.current_version=${project.version}
api.min_version=1.0
db.version=0
db.version=1

View File

@ -1,27 +0,0 @@
package com.sismics.docs.rest;
import javax.json.JsonArray;
import javax.json.JsonObject;
import org.junit.Assert;
import org.junit.Test;
/**
* Test the locale resource.
*
* @author jtremeaux
*/
public class TestLocaleResource extends BaseJerseyTest {
/**
* Test the locale resource.
*
* @throws JSONException
*/
@Test
public void testLocaleResource() {
JsonObject json = target().path("/locale").request().get(JsonObject.class);
JsonArray locale = json.getJsonArray("locales");
Assert.assertTrue(locale.size() > 0);
}
}

View File

@ -51,10 +51,8 @@ public class TestSecurity extends BaseJerseyTest {
// User testsecurity changes his email OK
json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, testSecurityAuthenticationToken)
.post(Entity.form(
new Form()
.param("email", "testsecurity2@docs.com")
.param("locale", "en")), JsonObject.class);
.post(Entity.form(new Form()
.param("email", "testsecurity2@docs.com")), JsonObject.class);
Assert.assertEquals("ok", json.getString("status"));
// User testsecurity logs out

View File

@ -154,7 +154,6 @@ public class TestUserResource extends BaseJerseyTest {
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, aliceAuthToken)
.get(JsonObject.class);
Assert.assertEquals("alice@docs.com", json.getString("email"));
Assert.assertFalse(json.getBoolean("first_connection"));
Assert.assertFalse(json.getBoolean("is_default_password"));
// Check bob user information
@ -220,21 +219,20 @@ public class TestUserResource extends BaseJerseyTest {
JsonObject json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.get(JsonObject.class);
Assert.assertTrue(json.getBoolean("first_connection"));
Assert.assertTrue(json.getBoolean("is_default_password"));
// User admin updates his information
json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.post(Entity.form(new Form()
.param("first_connection", "false")), JsonObject.class);
.param("email", "newadminemail@docs.com")), JsonObject.class);
Assert.assertEquals("ok", json.getString("status"));
// Check admin information update
json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.get(JsonObject.class);
Assert.assertFalse(json.getBoolean("first_connection"));
Assert.assertEquals("newadminemail@docs.com", json.getString("email"));
// User admin update admin_user1 information
json = target().path("/user").request()