#41: DB: Storage quota and current usage, accessible from /user

This commit is contained in:
jendib 2015-11-24 00:30:01 +01:00
parent dd671795e6
commit 1cae964c09
12 changed files with 107 additions and 96 deletions

View File

@ -101,6 +101,8 @@ public class UserDao {
// Update the user
userFromDb.setEmail(user.getEmail());
userFromDb.setStorageQuota(user.getStorageQuota());
userFromDb.setStorageCurrent(user.getStorageCurrent());
// Create audit log
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE);

View File

@ -54,6 +54,18 @@ public class User implements Loggable {
@Column(name = "USE_EMAIL_C", nullable = false, length = 100)
private String email;
/**
* Storage quota.
*/
@Column(name = "USE_STORAGEQUOTA_N", nullable = false)
private Long storageQuota;
/**
* Storage current usage.
*/
@Column(name = "USE_STORAGECURRENT_N", nullable = false)
private Long storageCurrent;
/**
* Creation date.
*/
@ -66,149 +78,87 @@ public class User implements Loggable {
@Column(name = "USE_DELETEDATE_D")
private Date deleteDate;
/**
* Getter of id.
*
* @return id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter of roleId.
*
* @return roleId
*/
public String getRoleId() {
return roleId;
}
/**
* Setter of roleId.
*
* @param roleId roleId
*/
public void setRoleId(String roleId) {
this.roleId = roleId;
}
/**
* Getter of username.
*
* @return username
*/
public String getUsername() {
return username;
}
/**
* Setter of username.
*
* @param username username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Getter of password.
*
* @return password
*/
public String getPassword() {
return password;
}
/**
* Setter of password.
*
* @param password password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* Getter of email.
*
* @return email
*/
public String getEmail() {
return email;
}
/**
* Setter of email.
*
* @param email email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Getter of createDate.
*
* @return createDate
*/
public Date getCreateDate() {
return createDate;
}
/**
* Setter of createDate.
*
* @param createDate createDate
*/
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/**
* Getter of deleteDate.
*
* @return deleteDate
*/
@Override
public Date getDeleteDate() {
return deleteDate;
}
/**
* Setter of deleteDate.
*
* @param deleteDate deleteDate
*/
public void setDeleteDate(Date deleteDate) {
this.deleteDate = deleteDate;
}
/**
* Getter de privateKey.
* @return privateKey
*/
public String getPrivateKey() {
return privateKey;
}
/**
* Setter de privateKey.
* @param privateKey privateKey
*/
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public Long getStorageQuota() {
return storageQuota;
}
public void setStorageQuota(Long storageQuota) {
this.storageQuota = storageQuota;
}
public Long getStorageCurrent() {
return storageCurrent;
}
public void setStorageCurrent(Long storageCurrent) {
this.storageCurrent = storageCurrent;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)

View File

@ -1 +1 @@
db.version=3
db.version=4

View File

@ -0,0 +1,3 @@
alter table T_USER add column USE_STORAGEQUOTA_N bigint not null default 10000000000;
alter table T_USER add column USE_STORAGECURRENT_N bigint not null default 0;
update T_CONFIG set CFG_VALUE_C = '4' where CFG_ID_C = 'DB_VERSION';

View File

@ -20,6 +20,8 @@ public class TestJpa extends BaseTransactionalTest {
user.setUsername("username");
user.setEmail("toto@docs.com");
user.setRoleId("admin");
user.setStorageCurrent(0l);
user.setStorageQuota(10l);
user.setPrivateKey("AwesomePrivateKey");
String id = userDao.create(user);

View File

@ -159,6 +159,22 @@ public class ValidationUtil {
}
}
/**
* Checks if the string is a number.
*
* @param s String to validate
* @param name Name of the parameter
* @return Parsed number
* @throws ClientException
*/
public static Long validateLong(String s, String name) throws ClientException {
try {
return Long.valueOf(s);
} catch (NumberFormatException e) {
throw new ClientException("ValidationError", MessageFormat.format("{0} is not a number", name));
}
}
/**
* Validates and parses a date.
*

View File

@ -40,7 +40,7 @@ public class ClientUtil {
form.param("username", username);
form.param("email", username + "@docs.com");
form.param("password", "12345678");
form.param("time_zone", "Asia/Tokyo");
form.param("storage_quota", "1000000"); // 1MB quota
resource.path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.put(Entity.form(form), JsonObject.class);

View File

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

View File

@ -64,7 +64,8 @@ public class UserResource extends BaseResource {
public Response register(
@FormParam("username") String username,
@FormParam("password") String password,
@FormParam("email") String email) {
@FormParam("email") String email,
@FormParam("storage_quota") String storageQuotaStr) {
if (!authenticate()) {
throw new ForbiddenClientException();
@ -76,6 +77,7 @@ public class UserResource extends BaseResource {
ValidationUtil.validateAlphanumeric(username, "username");
password = ValidationUtil.validateLength(password, "password", 8, 50);
email = ValidationUtil.validateLength(email, "email", 3, 50);
Long storageQuota = ValidationUtil.validateLong(storageQuotaStr, "storage_quota");
ValidationUtil.validateEmail(email, "email");
// Create the user
@ -84,6 +86,8 @@ public class UserResource extends BaseResource {
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
user.setStorageQuota(storageQuota);
user.setStorageCurrent(0l);
try {
user.setPrivateKey(EncryptionUtil.generatePrivateKey());
} catch (NoSuchAlgorithmException e) {
@ -119,7 +123,8 @@ public class UserResource extends BaseResource {
@POST
public Response update(
@FormParam("password") String password,
@FormParam("email") String email) {
@FormParam("email") String email,
@FormParam("storage_quota") String storageQuotaStr) {
if (!authenticate()) {
throw new ForbiddenClientException();
@ -135,9 +140,13 @@ public class UserResource extends BaseResource {
if (email != null) {
user.setEmail(email);
}
if (StringUtils.isNotBlank(storageQuotaStr)) {
Long storageQuota = ValidationUtil.validateLong(storageQuotaStr, "storage_quota");
user.setStorageQuota(storageQuota);
}
user = userDao.update(user);
// Change the password
if (StringUtils.isNotBlank(password)) {
user.setPassword(password);
userDao.updatePassword(user);
@ -162,7 +171,8 @@ public class UserResource extends BaseResource {
public Response update(
@PathParam("username") String username,
@FormParam("password") String password,
@FormParam("email") String email) {
@FormParam("email") String email,
@FormParam("storage_quota") String storageQuotaStr) {
if (!authenticate()) {
throw new ForbiddenClientException();
@ -184,11 +194,14 @@ public class UserResource extends BaseResource {
if (email != null) {
user.setEmail(email);
}
if (StringUtils.isNotBlank(storageQuotaStr)) {
Long storageQuota = ValidationUtil.validateLong(storageQuotaStr, "storage_quota");
user.setStorageQuota(storageQuota);
}
user = userDao.update(user);
if (StringUtils.isNotBlank(password)) {
// Change the password
if (StringUtils.isNotBlank(password)) {
user.setPassword(password);
userDao.updatePassword(user);
}
@ -406,7 +419,9 @@ 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("email", user.getEmail())
.add("storage_quota", user.getStorageQuota())
.add("storage_current", user.getStorageCurrent());
JsonArrayBuilder baseFunctions = Json.createArrayBuilder();
for (String baseFunction : ((UserPrincipal) principal).getBaseFunctionSet()) {
baseFunctions.add(baseFunction);
@ -441,7 +456,9 @@ public class UserResource extends BaseResource {
JsonObjectBuilder response = Json.createObjectBuilder()
.add("username", user.getUsername())
.add("email", user.getEmail());
.add("email", user.getEmail())
.add("storage_quota", user.getStorageQuota())
.add("storage_current", user.getStorageCurrent());
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=3
db.version=4

View File

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

View File

@ -55,7 +55,8 @@ public class TestUserResource extends BaseJerseyTest {
.put(Entity.form(new Form()
.param("username", " bb ")
.param("email", "bob@docs.com")
.param("password", "12345678")));
.param("password", "12345678")
.param("storage_quota", "10")));
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
json = response.readEntity(JsonObject.class);
Assert.assertEquals("ValidationError", json.getString("type"));
@ -67,19 +68,34 @@ public class TestUserResource extends BaseJerseyTest {
.put(Entity.form(new Form()
.param("username", "bob-")
.param("email", "bob@docs.com")
.param("password", "12345678")));
.param("password", "12345678")
.param("storage_quota", "10")));
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
json = response.readEntity(JsonObject.class);
Assert.assertEquals("ValidationError", json.getString("type"));
Assert.assertTrue(json.getString("message"), json.getString("message").contains("alphanumeric"));
// Create a user KO (invalid quota)
response = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.put(Entity.form(new Form()
.param("username", "bob")
.param("email", "bob@docs.com")
.param("password", "12345678")
.param("storage_quota", "nope")));
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
json = response.readEntity(JsonObject.class);
Assert.assertEquals("ValidationError", json.getString("type"));
Assert.assertTrue(json.getString("message"), json.getString("message").contains("number"));
// Create a user KO (email format validation)
response = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.put(Entity.form(new Form()
.param("username", "bob")
.param("email", "bobdocs.com")
.param("password", "12345678")));
.param("password", "12345678")
.param("storage_quota", "10")));
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
json = response.readEntity(JsonObject.class);
Assert.assertEquals("ValidationError", json.getString("type"));
@ -89,7 +105,8 @@ public class TestUserResource extends BaseJerseyTest {
Form form = new Form()
.param("username", " bob ")
.param("email", " bob@docs.com ")
.param("password", " 12345678 ");
.param("password", " 12345678 ")
.param("storage_quota", "10");
json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.put(Entity.form(form), JsonObject.class);
@ -154,6 +171,8 @@ public class TestUserResource extends BaseJerseyTest {
.get(JsonObject.class);
Assert.assertEquals("alice@docs.com", json.getString("email"));
Assert.assertFalse(json.getBoolean("is_default_password"));
Assert.assertEquals(0l, json.getJsonNumber("storage_current").longValue());
Assert.assertEquals(1000000l, json.getJsonNumber("storage_quota").longValue());
// Check bob user information
json = target().path("/user").request()
@ -219,6 +238,8 @@ public class TestUserResource extends BaseJerseyTest {
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminAuthenticationToken)
.get(JsonObject.class);
Assert.assertTrue(json.getBoolean("is_default_password"));
Assert.assertEquals(0l, json.getJsonNumber("storage_current").longValue());
Assert.assertEquals(10000000000l, json.getJsonNumber("storage_quota").longValue());
// User admin updates his information
json = target().path("/user").request()