Commit 50179e03 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Merge branch '867-user-db-change' into 'master'

Resolve "user db change"

Closes #867

See merge request minerva/core!849
parents 45b2286e 653b397c
......@@ -2,15 +2,14 @@ package lcsb.mapviewer.model.user;
import java.awt.Color;
import java.io.Serializable;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import javax.persistence.*;
import lcsb.mapviewer.model.security.Privilege;
import org.hibernate.annotations.Cascade;
import lcsb.mapviewer.model.security.Privilege;
@Entity
public class User implements Serializable {
......@@ -20,8 +19,10 @@ public class User implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true, nullable = false)
private String login;
@Column(nullable = false)
private String cryptedPassword;
private String name;
......@@ -32,25 +33,29 @@ public class User implements Serializable {
/**
* User defined color overriding system
* {@link ConfigurationElementType#MIN_COLOR_VAL}. Used for coloring minimum values in overlays.
* {@link ConfigurationElementType#MIN_COLOR_VAL}. Used for coloring minimum
* values in overlays.
*/
private Color minColor;
/**
* User defined color overriding system
* {@link ConfigurationElementType#MAX_COLOR_VAL}. Used for coloring maximum values in overlays.
* {@link ConfigurationElementType#MAX_COLOR_VAL}. Used for coloring maximum
* values in overlays.
*/
private Color maxColor;
/**
* User defined color overriding system
* {@link ConfigurationElementType#NEUTRAL_COLOR_VAL}. Used for coloring neutral values (0) in overlays.
* {@link ConfigurationElementType#NEUTRAL_COLOR_VAL}. Used for coloring neutral
* values (0) in overlays.
*/
private Color neutralColor;
/**
* User defined color overriding system
* {@link ConfigurationElementType#SIMPLE_COLOR_VAL}. Used for coloring overlays without values and colors.
* {@link ConfigurationElementType#SIMPLE_COLOR_VAL}. Used for coloring overlays
* without values and colors.
*/
private Color simpleColor;
......@@ -67,11 +72,7 @@ public class User implements Serializable {
private Set<Calendar> termsOfUseConsentDates = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "user_privilege_map_table",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "privilege_id")
)
@JoinTable(name = "user_privilege_map_table", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "privilege_id"))
private Set<Privilege> privileges = new HashSet<>();
@OneToOne(cascade = CascadeType.ALL)
......
update user_table set crypted_password = '' where crypted_password is null;
ALTER TABLE user_table ALTER COLUMN crypted_password SET NOT NULL;
package lcsb.mapviewer.persist.dao.user;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.PropertyValueException;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.*;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.model.security.Privilege;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.AnnotatorParamDefinition;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.model.user.UserAnnotationSchema;
import lcsb.mapviewer.model.user.UserClassAnnotators;
import lcsb.mapviewer.model.user.UserClassRequiredAnnotations;
import lcsb.mapviewer.model.user.UserClassValidAnnotations;
import lcsb.mapviewer.model.user.UserGuiPreference;
import lcsb.mapviewer.model.user.*;
import lcsb.mapviewer.model.user.annotator.AnnotatorConfigParameter;
import lcsb.mapviewer.model.user.annotator.AnnotatorData;
import lcsb.mapviewer.persist.PersistTestFunctions;
......@@ -54,6 +45,7 @@ public class UserDaoTest extends PersistTestFunctions {
long counter = userDao.getCount();
user = new User();
user.setCryptedPassword("");
user.setLogin(testLogin);
user.addPrivilege(new Privilege(PrivilegeType.CAN_CREATE_OVERLAYS));
userDao.add(user);
......@@ -80,6 +72,52 @@ public class UserDaoTest extends PersistTestFunctions {
}
}
@Test(expected = PropertyValueException.class)
public void testTryUserWithNullPassword() {
try {
User user = new User();
user.setLogin(testLogin);
userDao.add(user);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test(expected = PropertyValueException.class)
public void testTryUserWithNullLogin() {
try {
User user = new User();
user.setCryptedPassword("ZX");
userDao.add(user);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test(expected = ConstraintViolationException.class)
public void testTryUserWithExistingLogin() {
try {
User user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
userDao.add(user);
User user2 = new User();
user2.setLogin(testLogin);
user2.setCryptedPassword("");
userDao.add(user2);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testAddDeleteAdd() {
try {
......@@ -90,6 +128,7 @@ public class UserDaoTest extends PersistTestFunctions {
user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
userDao.add(user);
long counter2 = userDao.getCount();
......@@ -101,6 +140,7 @@ public class UserDaoTest extends PersistTestFunctions {
assertNull(user2);
user2 = new User();
user2.setCryptedPassword("");
user2.setLogin(testLogin);
userDao.add(user2);
......@@ -172,6 +212,8 @@ public class UserDaoTest extends PersistTestFunctions {
public void testUserWithAnnotatorSchema() throws Exception {
try {
User user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
UserAnnotationSchema uas = new UserAnnotationSchema();
uas.setValidateMiriamTypes(true);
UserClassAnnotators ca = new UserClassAnnotators();
......@@ -213,6 +255,8 @@ public class UserDaoTest extends PersistTestFunctions {
public void testUserWithAnnotatorSchemaGuiPreferences() throws Exception {
try {
User user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
UserAnnotationSchema uas = new UserAnnotationSchema();
user.setAnnotationSchema(uas);
UserGuiPreference option = new UserGuiPreference();
......@@ -238,6 +282,8 @@ public class UserDaoTest extends PersistTestFunctions {
public void testUserWithAnnotatorSchemaRequiredAnnotations() throws Exception {
try {
User user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
UserAnnotationSchema uas = new UserAnnotationSchema();
user.setAnnotationSchema(uas);
uas.addClassRequiredAnnotations(
......@@ -261,6 +307,8 @@ public class UserDaoTest extends PersistTestFunctions {
public void testUserWithAnnotatorParams() throws Exception {
try {
User user = new User();
user.setLogin(testLogin);
user.setCryptedPassword("");
UserAnnotationSchema uas = new UserAnnotationSchema();
user.setAnnotationSchema(uas);
UserClassAnnotators classAnnotators = new UserClassAnnotators(Integer.class);
......
......@@ -94,58 +94,58 @@ public class UserRestImpl extends BaseRestImpl {
String column = string.toLowerCase();
Object value;
switch (column) {
case "id":
case "idobject":
value = user.getId();
break;
case "name":
value = user.getName();
break;
case "surname":
value = user.getSurname();
break;
case "login":
value = user.getLogin();
break;
case "email":
value = user.getEmail();
break;
case "mincolor":
value = user.getMinColor();
break;
case "maxcolor":
value = user.getMaxColor();
break;
case "neutralcolor":
value = user.getNeutralColor();
break;
case "simplecolor":
value = user.getSimpleColor();
break;
case "removed":
value = user.isRemoved();
break;
case "termsofuseconsent":
value = user.isTermsOfUseConsent();
break;
case "connectedtoldap":
value = user.isConnectedToLdap();
break;
case "ldapaccountavailable":
if (ldapAvailable == null) {
ldapAvailable = false;
}
value = ldapAvailable;
break;
case "privileges":
value = preparePrivileges(user);
break;
case "preferences":
value = preparePreferences(user);
break;
default:
value = "Unknown column";
break;
case "id":
case "idobject":
value = user.getId();
break;
case "name":
value = user.getName();
break;
case "surname":
value = user.getSurname();
break;
case "login":
value = user.getLogin();
break;
case "email":
value = user.getEmail();
break;
case "mincolor":
value = user.getMinColor();
break;
case "maxcolor":
value = user.getMaxColor();
break;
case "neutralcolor":
value = user.getNeutralColor();
break;
case "simplecolor":
value = user.getSimpleColor();
break;
case "removed":
value = user.isRemoved();
break;
case "termsofuseconsent":
value = user.isTermsOfUseConsent();
break;
case "connectedtoldap":
value = user.isConnectedToLdap();
break;
case "ldapaccountavailable":
if (ldapAvailable == null) {
ldapAvailable = false;
}
value = ldapAvailable;
break;
case "privileges":
value = preparePrivileges(user);
break;
case "preferences":
value = preparePreferences(user);
break;
default:
value = "Unknown column";
break;
}
result.put(string, value);
}
......@@ -412,7 +412,7 @@ public class UserRestImpl extends BaseRestImpl {
}
if (value instanceof MiriamType) {
return (MiriamType) value;
}
}
return MiriamType.valueOf((String) value);
}
......@@ -707,13 +707,16 @@ public class UserRestImpl extends BaseRestImpl {
throw new QueryException("login must match url");
}
} else if (key.equalsIgnoreCase("defaultPrivileges")) {
if (stringValue != null ) {
if (stringValue != null) {
defaultPrivileges = "true".equalsIgnoreCase(stringValue);
}
} else {
throw new QueryException("Unknown parameter: " + key);
}
}
if (user.getCryptedPassword() == null) {
throw new QueryException("password cannot be null");
}
getUserService().addUser(user);
if (defaultPrivileges) {
getUserService().grantDefaultPrivileges(user);
......
......@@ -63,6 +63,7 @@ public class UserRestImplTest extends RestTestFunctions {
try {
User user = new User();
user.setLogin(TEST_LOGIN);
user.setCryptedPassword("c");
userService.addUser(user);
userRestImpl.addUser(TEST_LOGIN, new LinkedMultiValueMap<>());
fail("Exception expected");
......@@ -77,6 +78,7 @@ public class UserRestImplTest extends RestTestFunctions {
public void testUpdatePreferences() throws Exception {
try {
User user = new User();
user.setCryptedPassword("");
user.setLogin(TEST_LOGIN);
userService.addUser(user);
......
......@@ -146,6 +146,7 @@ public class UserServiceTest extends ServiceTestFunctions {
long logCount = getInfos().size();
User user2 = new User();
user2.setLogin("login");
user2.setCryptedPassword("");
userService.addUser(user2);
assertNotNull(user2.getId());
long logCount2 = getInfos().size();
......
......@@ -300,7 +300,9 @@ public class UserControllerIntegrationTest extends ControllerIntegrationTest {
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("name", "FirstName"))));
new BasicNameValuePair("name", "FirstName"),
new BasicNameValuePair("password", "FirstName")
)));
RequestBuilder grantRequest = post("/users/" + testLogin)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
......@@ -313,6 +315,24 @@ public class UserControllerIntegrationTest extends ControllerIntegrationTest {
assertNotNull(userService.getUserByLogin(testLogin));
}
@Test
public void testAddUserWithoutPassword() throws Exception {
String testLogin = "xyz";
MockHttpSession session = createSession(TEST_ADMIN_LOGIN, TEST_ADMIN_PASSWORD);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("name", "FirstName")
)));
RequestBuilder grantRequest = post("/users/" + testLogin)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(grantRequest)
.andExpect(status().isBadRequest());
}
@Test
public void testGrantDefaultPrivilegesWhenAddingUser() throws Exception {
String testLogin = "xyz";
......@@ -324,6 +344,7 @@ public class UserControllerIntegrationTest extends ControllerIntegrationTest {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("name", "FirstName"),
new BasicNameValuePair("password", "FirstName"),
new BasicNameValuePair("defaultPrivileges", "true"))));
RequestBuilder grantRequest = post("/users/" + testLogin)
......@@ -358,6 +379,7 @@ public class UserControllerIntegrationTest extends ControllerIntegrationTest {
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("name", "FirstName"),
new BasicNameValuePair("password", "FirstName"),
new BasicNameValuePair("defaultPrivileges", "true"))));
RequestBuilder grantRequest = post("/users/" + testLogin)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment