[Fix] trim values in privilege handler when storing

This commit is contained in:
Robert von Burg 2021-03-17 15:53:31 +01:00
parent bd51435bd1
commit fcb0e1fdc0
4 changed files with 35 additions and 26 deletions

View File

@ -288,16 +288,16 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
boolean propertySelected; boolean propertySelected;
// userId // userId
userIdSelected = selUserId == null || selUserId.equals(user.getUserId()); userIdSelected = isEmpty(selUserId) || selUserId.equals(user.getUserId());
// username // username
usernameSelected = selUsername == null || selUsername.equals(user.getUsername()); usernameSelected = isEmpty(selUsername) || selUsername.equals(user.getUsername());
// firstname // firstname
firstNameSelected = selFirstName == null || selFirstName.equals(user.getFirstname()); firstNameSelected = isEmpty(selFirstName) || selFirstName.equals(user.getFirstname());
// lastname // lastname
lastNameSelected = selLastName == null || selLastName.equals(user.getLastname()); lastNameSelected = isEmpty(selLastName) || selLastName.equals(user.getLastname());
// user state // user state
userStateSelected = selUserState == null || selUserState.equals(user.getUserState()); userStateSelected = selUserState == null || selUserState.equals(user.getUserState());

View File

@ -15,9 +15,12 @@
*/ */
package li.strolch.privilege.model; package li.strolch.privilege.model;
import static li.strolch.utils.helper.StringHelper.trimOrEmpty;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import li.strolch.privilege.base.PrivilegeException; import li.strolch.privilege.base.PrivilegeException;
import li.strolch.privilege.handler.PrivilegeHandler; import li.strolch.privilege.handler.PrivilegeHandler;
@ -49,15 +52,15 @@ public class PrivilegeRep implements Serializable {
* the {@link PrivilegePolicy} configured to evaluate if the privilege is granted * the {@link PrivilegePolicy} configured to evaluate if the privilege is granted
* @param allAllowed * @param allAllowed
* a boolean defining if a {@link Role} with this {@link IPrivilege} has unrestricted access to a {@link * a boolean defining if a {@link Role} with this {@link IPrivilege} has unrestricted access to a {@link
* Restrictable} * Restrictable}
* @param denyList * @param denyList
* a list of deny rules for this {@link IPrivilege} * a list of deny rules for this {@link IPrivilege}
* @param allowList * @param allowList
* a list of allow rules for this {@link IPrivilege} * a list of allow rules for this {@link IPrivilege}
*/ */
public PrivilegeRep(String name, String policy, boolean allAllowed, Set<String> denyList, Set<String> allowList) { public PrivilegeRep(String name, String policy, boolean allAllowed, Set<String> denyList, Set<String> allowList) {
this.name = name; this.name = trimOrEmpty(name);
this.policy = policy; this.policy = trimOrEmpty(policy);
this.allAllowed = allAllowed; this.allAllowed = allAllowed;
this.denyList = denyList; this.denyList = denyList;
this.allowList = allowList; this.allowList = allowList;
@ -96,7 +99,7 @@ public class PrivilegeRep implements Serializable {
* the name to set * the name to set
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = trimOrEmpty(name);
} }
/** /**
@ -111,7 +114,7 @@ public class PrivilegeRep implements Serializable {
* the policy to set * the policy to set
*/ */
public void setPolicy(String policy) { public void setPolicy(String policy) {
this.policy = policy; this.policy = trimOrEmpty(policy);
} }
/** /**
@ -141,7 +144,7 @@ public class PrivilegeRep implements Serializable {
* the denyList to set * the denyList to set
*/ */
public void setDenyList(Set<String> denyList) { public void setDenyList(Set<String> denyList) {
this.denyList = denyList; this.denyList = denyList.stream().map(String::trim).collect(Collectors.toSet());
} }
/** /**
@ -156,7 +159,7 @@ public class PrivilegeRep implements Serializable {
* the allowList to set * the allowList to set
*/ */
public void setAllowList(Set<String> allowList) { public void setAllowList(Set<String> allowList) {
this.allowList = allowList; this.allowList = allowList.stream().map(String::trim).collect(Collectors.toSet());
} }
/** /**

View File

@ -15,6 +15,8 @@
*/ */
package li.strolch.privilege.model; package li.strolch.privilege.model;
import static li.strolch.utils.helper.StringHelper.trimOrEmpty;
import java.io.Serializable; import java.io.Serializable;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -45,7 +47,7 @@ public class RoleRep implements Serializable {
* the list of privileges granted to this role * the list of privileges granted to this role
*/ */
public RoleRep(String name, List<PrivilegeRep> privileges) { public RoleRep(String name, List<PrivilegeRep> privileges) {
this.name = name; this.name = trimOrEmpty(name);
this.privileges = privileges; this.privileges = privileges;
} }
@ -81,7 +83,7 @@ public class RoleRep implements Serializable {
* the name to set * the name to set
*/ */
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = trimOrEmpty(name);
} }
/** /**

View File

@ -16,10 +16,12 @@
package li.strolch.privilege.model; package li.strolch.privilege.model;
import static li.strolch.privilege.base.PrivilegeConstants.*; import static li.strolch.privilege.base.PrivilegeConstants.*;
import static li.strolch.utils.helper.StringHelper.trimOrEmpty;
import java.io.Serializable; import java.io.Serializable;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import li.strolch.privilege.base.PrivilegeConstants; import li.strolch.privilege.base.PrivilegeConstants;
import li.strolch.privilege.base.PrivilegeException; import li.strolch.privilege.base.PrivilegeException;
@ -70,17 +72,19 @@ public class UserRep implements Serializable {
*/ */
public UserRep(String userId, String username, String firstname, String lastname, UserState userState, public UserRep(String userId, String username, String firstname, String lastname, UserState userState,
Set<String> roles, Locale locale, Map<String, String> propertyMap, UserHistory history) { Set<String> roles, Locale locale, Map<String, String> propertyMap, UserHistory history) {
this.userId = userId; this.userId = trimOrEmpty(userId);
this.username = username; this.username = trimOrEmpty(username);
this.firstname = firstname; this.firstname = trimOrEmpty(firstname);
this.lastname = lastname; this.lastname = trimOrEmpty(lastname);
this.userState = userState; this.userState = userState;
this.roles = roles; this.roles = roles == null ? null : roles.stream().map(String::trim).collect(Collectors.toSet());
this.locale = locale; this.locale = locale;
if (propertyMap != null) { if (propertyMap != null) {
this.properties = new HashMap<>(propertyMap); this.properties = new HashMap<>();
this.properties.remove(""); propertyMap.forEach((key, value) -> {
this.properties.put(key.trim(), value.trim());
});
} }
this.history = history; this.history = history;
@ -139,7 +143,7 @@ public class UserRep implements Serializable {
* to set * to set
*/ */
public void setUserId(String userId) { public void setUserId(String userId) {
this.userId = userId; this.userId = trimOrEmpty(userId);
} }
/** /**
@ -154,7 +158,7 @@ public class UserRep implements Serializable {
* the username to set * the username to set
*/ */
public void setUsername(String username) { public void setUsername(String username) {
this.username = username; this.username = trimOrEmpty(username);
} }
/** /**
@ -169,7 +173,7 @@ public class UserRep implements Serializable {
* the firstname to set * the firstname to set
*/ */
public void setFirstname(String firstname) { public void setFirstname(String firstname) {
this.firstname = firstname; this.firstname = trimOrEmpty(firstname);
} }
/** /**
@ -184,7 +188,7 @@ public class UserRep implements Serializable {
* the lastname to set * the lastname to set
*/ */
public void setLastname(String lastname) { public void setLastname(String lastname) {
this.lastname = lastname; this.lastname = trimOrEmpty(lastname);
} }
/** /**
@ -214,7 +218,7 @@ public class UserRep implements Serializable {
* the roles to set * the roles to set
*/ */
public void setRoles(Set<String> roles) { public void setRoles(Set<String> roles) {
this.roles = roles; this.roles = roles.stream().map(String::trim).collect(Collectors.toSet());
} }
/** /**
@ -280,7 +284,7 @@ public class UserRep implements Serializable {
public void setProperty(String key, String value) { public void setProperty(String key, String value) {
if (this.properties == null) if (this.properties == null)
this.properties = new HashMap<>(1); this.properties = new HashMap<>(1);
this.properties.put(key, value); this.properties.put(key.trim(), value.trim());
} }
/** /**