mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 17:56:20 +00:00
#41: DB: Storage quota and current usage, accessible from /user
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=3
|
||||
db.version=4
|
||||
@@ -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);
|
||||
|
||||
// Change the password
|
||||
if (StringUtils.isNotBlank(password)) {
|
||||
// Change the 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();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=3
|
||||
db.version=4
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=3
|
||||
db.version=4
|
||||
@@ -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,11 +68,25 @@ 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()
|
||||
@@ -79,7 +94,8 @@ public class TestUserResource extends BaseJerseyTest {
|
||||
.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()
|
||||
|
||||
Reference in New Issue
Block a user