[Major] All methods in PrivilegeHandler now return a value after op
- also fixed JAXB (un)marshalling of list values on UserRep and RoleRep
This commit is contained in:
parent
eeb3356372
commit
5ef43eaebe
|
@ -184,7 +184,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
UserState selUserState = selectorRep.getUserState();
|
||||
Locale selLocale = selectorRep.getLocale();
|
||||
Set<String> selRoles = selectorRep.getRoles();
|
||||
Map<String, String> selPropertyMap = selectorRep.getProperties();
|
||||
Map<String, String> selPropertyMap = selectorRep.getPropertyMap();
|
||||
|
||||
List<UserRep> result = new ArrayList<>();
|
||||
List<User> allUsers = this.persistenceHandler.getAllUsers();
|
||||
|
@ -315,17 +315,22 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
return roles.containsAll(selectionRoles);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.privilege.handler.PrivilegeHandler#addOrReplaceUser(ch.eitchnet.privilege.model.Certificate,
|
||||
* ch.eitchnet.privilege.model.UserRep, byte[])
|
||||
*/
|
||||
@Override
|
||||
public void addUser(Certificate certificate, UserRep userRep, byte[] password) {
|
||||
public UserRep addUser(Certificate certificate, UserRep userRep, byte[] password) {
|
||||
try {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
||||
// make sure userId is not set
|
||||
if (StringHelper.isNotEmpty(userRep.getUserId())) {
|
||||
String msg = "UserId can not be set when adding a new user!";
|
||||
throw new PrivilegeException(MessageFormat.format(msg, userRep.getUsername()));
|
||||
}
|
||||
|
||||
// set userId
|
||||
userRep.setUserId(StringHelper.getUniqueId());
|
||||
|
||||
// first validate user
|
||||
userRep.validate();
|
||||
|
||||
|
@ -353,17 +358,15 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
// delegate to persistence handler
|
||||
this.persistenceHandler.addUser(user);
|
||||
|
||||
return user.asUserRep();
|
||||
|
||||
} finally {
|
||||
clearPassword(password);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.privilege.handler.PrivilegeHandler#addOrReplaceUser(ch.eitchnet.privilege.model.Certificate,
|
||||
* ch.eitchnet.privilege.model.UserRep, byte[])
|
||||
*/
|
||||
@Override
|
||||
public void replaceUser(Certificate certificate, UserRep userRep, byte[] password) {
|
||||
public UserRep replaceUser(Certificate certificate, UserRep userRep, byte[] password) {
|
||||
try {
|
||||
|
||||
// validate who is doing this
|
||||
|
@ -375,11 +378,19 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
validateRolesExist(userRep);
|
||||
|
||||
// validate user exists
|
||||
if (this.persistenceHandler.getUser(userRep.getUsername()) == null) {
|
||||
User user = this.persistenceHandler.getUser(userRep.getUsername());
|
||||
if (user == null) {
|
||||
String msg = "User {0} can not be replaced as it does not exist!";
|
||||
throw new PrivilegeException(MessageFormat.format(msg, userRep.getUsername()));
|
||||
}
|
||||
|
||||
// validate same userId
|
||||
if (!user.getUserId().equals(userRep.getUserId())) {
|
||||
String msg = "UserId of existing user {0} does not match userRep {1}";
|
||||
msg = MessageFormat.format(msg, user.getUserId(), userRep.getUserId());
|
||||
throw new PrivilegeException(MessageFormat.format(msg, userRep.getUsername()));
|
||||
}
|
||||
|
||||
String passwordHash = null;
|
||||
if (password != null) {
|
||||
|
||||
|
@ -390,11 +401,13 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
passwordHash = this.encryptionHandler.convertToHash(password);
|
||||
}
|
||||
|
||||
User user = createUser(userRep, passwordHash);
|
||||
user = createUser(userRep, passwordHash);
|
||||
|
||||
// delegate to persistence handler
|
||||
this.persistenceHandler.replaceUser(user);
|
||||
|
||||
return user.asUserRep();
|
||||
|
||||
} finally {
|
||||
clearPassword(password);
|
||||
}
|
||||
|
@ -414,12 +427,13 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
private User createUser(UserRep userRep, String passwordHash) {
|
||||
User user = new User(userRep.getUserId(), userRep.getUsername(), passwordHash, userRep.getFirstname(),
|
||||
userRep.getLastname(), userRep.getUserState(), userRep.getRoles(), userRep.getLocale(),
|
||||
userRep.getProperties());
|
||||
userRep.getPropertyMap());
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateUser(Certificate certificate, UserRep userRep) throws AccessDeniedException, PrivilegeException {
|
||||
public UserRep updateUser(Certificate certificate, UserRep userRep) throws AccessDeniedException,
|
||||
PrivilegeException {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -456,17 +470,19 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
if (userRep.getLocale() != null)
|
||||
locale = userRep.getLocale();
|
||||
if (userRep.getProperties() != null && !userRep.getProperties().isEmpty())
|
||||
propertyMap = userRep.getProperties();
|
||||
propertyMap = userRep.getPropertyMap();
|
||||
|
||||
// create new user
|
||||
user = new User(userId, username, password, firstname, lastname, userState, roles, locale, propertyMap);
|
||||
|
||||
// delegate to persistence handler
|
||||
this.persistenceHandler.replaceUser(user);
|
||||
|
||||
return user.asUserRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRole(Certificate certificate, RoleRep roleRep) {
|
||||
public RoleRep addRole(Certificate certificate, RoleRep roleRep) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -488,10 +504,12 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate to persistence handler
|
||||
this.persistenceHandler.addRole(role);
|
||||
|
||||
return role.asRoleRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceRole(Certificate certificate, RoleRep roleRep) {
|
||||
public RoleRep replaceRole(Certificate certificate, RoleRep roleRep) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -513,10 +531,12 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate to persistence handler
|
||||
this.persistenceHandler.replaceRole(role);
|
||||
|
||||
return role.asRoleRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOrReplacePrivilegeOnRole(Certificate certificate, String roleName, PrivilegeRep privilegeRep) {
|
||||
public RoleRep addOrReplacePrivilegeOnRole(Certificate certificate, String roleName, PrivilegeRep privilegeRep) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -541,6 +561,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// create new role with the additional privilege
|
||||
IPrivilege newPrivilege = new PrivilegeImpl(privilegeRep);
|
||||
|
||||
// copy existing privileges
|
||||
Set<String> existingPrivilegeNames = role.getPrivilegeNames();
|
||||
Map<String, IPrivilege> privilegeMap = new HashMap<>(existingPrivilegeNames.size() + 1);
|
||||
|
@ -548,6 +569,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
IPrivilege privilege = role.getPrivilege(name);
|
||||
privilegeMap.put(name, privilege);
|
||||
}
|
||||
|
||||
// add new one
|
||||
privilegeMap.put(newPrivilege.getName(), newPrivilege);
|
||||
|
||||
|
@ -555,10 +577,12 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate role replacement to persistence handler
|
||||
this.persistenceHandler.replaceRole(newRole);
|
||||
|
||||
return newRole.asRoleRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRoleToUser(Certificate certificate, String username, String roleName) {
|
||||
public UserRep addRoleToUser(Certificate certificate, String username, String roleName) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -569,12 +593,11 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
throw new PrivilegeException(MessageFormat.format("User {0} does not exist!", username)); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
// ignore if user already has role
|
||||
// check that user not already has role
|
||||
Set<String> currentRoles = user.getRoles();
|
||||
if (currentRoles.contains(roleName)) {
|
||||
String msg = MessageFormat.format("User {0} already has role {1}", username, roleName); //$NON-NLS-1$
|
||||
DefaultPrivilegeHandler.logger.error(msg);
|
||||
return;
|
||||
throw new PrivilegeException(msg);
|
||||
}
|
||||
|
||||
// validate that role exists
|
||||
|
@ -592,10 +615,12 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceUser(newUser);
|
||||
|
||||
return newUser.asUserRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePrivilegeFromRole(Certificate certificate, String roleName, String privilegeName) {
|
||||
public RoleRep removePrivilegeFromRole(Certificate certificate, String roleName, String privilegeName) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -626,6 +651,8 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceRole(newRole);
|
||||
|
||||
return newRole.asRoleRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -647,16 +674,17 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate role removal to persistence handler
|
||||
Role removedRole = this.persistenceHandler.removeRole(roleName);
|
||||
|
||||
if (removedRole == null)
|
||||
return null;
|
||||
if (removedRole == null) {
|
||||
String msg = "Can not remove Role {0} because role does not exist!";
|
||||
throw new PrivilegeException(MessageFormat.format(msg, roleName));
|
||||
}
|
||||
|
||||
// return role rep if it was removed
|
||||
return removedRole.asRoleRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeRoleFromUser(Certificate certificate, String username, String roleName) {
|
||||
public UserRep removeRoleFromUser(Certificate certificate, String username, String roleName) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -670,9 +698,8 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
// ignore if user does not have role
|
||||
Set<String> currentRoles = user.getRoles();
|
||||
if (!currentRoles.contains(roleName)) {
|
||||
String msg = MessageFormat.format("User {0} does not have role {1}", user, roleName); //$NON-NLS-1$
|
||||
logger.error(msg);
|
||||
return;
|
||||
String msg = MessageFormat.format("User {0} does not have role {1}", user.getUsername(), roleName); //$NON-NLS-1$
|
||||
throw new PrivilegeException(msg);
|
||||
}
|
||||
|
||||
// create new user
|
||||
|
@ -683,6 +710,8 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceUser(newUser);
|
||||
|
||||
return newUser.asUserRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -693,17 +722,17 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user removal to persistence handler
|
||||
User removedUser = this.persistenceHandler.removeUser(username);
|
||||
|
||||
// return user rep if it was removed
|
||||
if (removedUser == null)
|
||||
return null;
|
||||
if (removedUser == null) {
|
||||
String msg = "Can not remove User {0} because user does not exist!";
|
||||
throw new PrivilegeException(MessageFormat.format(msg, username));
|
||||
}
|
||||
|
||||
// return user rep if it was removed
|
||||
return removedUser.asUserRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserLocale(Certificate certificate, String username, Locale locale) {
|
||||
public UserRep setUserLocale(Certificate certificate, String username, Locale locale) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -720,10 +749,12 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceUser(newUser);
|
||||
|
||||
return newUser.asUserRep();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserName(Certificate certificate, String username, String firstname, String lastname) {
|
||||
public UserRep setUserName(Certificate certificate, String username, String firstname, String lastname) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -740,12 +771,10 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceUser(newUser);
|
||||
|
||||
return newUser.asUserRep();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.privilege.handler.PrivilegeHandler#setUserPassword(ch.eitchnet.privilege.model.Certificate,
|
||||
* java.lang.String, byte[])
|
||||
*/
|
||||
@Override
|
||||
public void setUserPassword(Certificate certificate, String username, byte[] password) {
|
||||
try {
|
||||
|
@ -796,7 +825,7 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setUserState(Certificate certificate, String username, UserState state) {
|
||||
public UserRep setUserState(Certificate certificate, String username, UserState state) {
|
||||
|
||||
// validate who is doing this
|
||||
assertIsPrivilegeAdmin(certificate);
|
||||
|
@ -813,14 +842,10 @@ public class DefaultPrivilegeHandler implements PrivilegeHandler {
|
|||
|
||||
// delegate user replacement to persistence handler
|
||||
this.persistenceHandler.replaceUser(newUser);
|
||||
|
||||
return newUser.asUserRep();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ch.eitchnet.privilege.handler.PrivilegeHandler#authenticate(java.lang.String, byte[])
|
||||
*
|
||||
* @throws AccessDeniedException
|
||||
* if the user credentials are not valid
|
||||
*/
|
||||
@Override
|
||||
public Certificate authenticate(String username, byte[] password) {
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate
|
||||
*/
|
||||
public void removeRoleFromUser(Certificate certificate, String username, String roleName)
|
||||
public UserRep removeRoleFromUser(Certificate certificate, String username, String roleName)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -182,7 +182,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate
|
||||
*/
|
||||
public void removePrivilegeFromRole(Certificate certificate, String roleName, String privilegeName)
|
||||
public RoleRep removePrivilegeFromRole(Certificate certificate, String roleName, String privilegeName)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -209,7 +209,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or the user already exists
|
||||
*/
|
||||
public void addUser(Certificate certificate, UserRep userRep, byte[] password) throws AccessDeniedException,
|
||||
public UserRep addUser(Certificate certificate, UserRep userRep, byte[] password) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -241,7 +241,8 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or if the user does not exist
|
||||
*/
|
||||
public void updateUser(Certificate certificate, UserRep userRep) throws AccessDeniedException, PrivilegeException;
|
||||
public UserRep updateUser(Certificate certificate, UserRep userRep) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -267,7 +268,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or if the user does not exist
|
||||
*/
|
||||
public void replaceUser(Certificate certificate, UserRep userRep, byte[] password) throws AccessDeniedException,
|
||||
public UserRep replaceUser(Certificate certificate, UserRep userRep, byte[] password) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -283,7 +284,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or if the role already exists
|
||||
*/
|
||||
public void addRole(Certificate certificate, RoleRep roleRep) throws AccessDeniedException, PrivilegeException;
|
||||
public RoleRep addRole(Certificate certificate, RoleRep roleRep) throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
* Replaces the existing role with the information from this {@link RoleRep}
|
||||
|
@ -298,7 +299,8 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or if the role does not exist
|
||||
*/
|
||||
public void replaceRole(Certificate certificate, RoleRep roleRep) throws AccessDeniedException, PrivilegeException;
|
||||
public RoleRep replaceRole(Certificate certificate, RoleRep roleRep) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
|
||||
/**
|
||||
* Adds the role with the given roleName to the {@link User} with the given username
|
||||
|
@ -315,8 +317,8 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or if the role does not exist
|
||||
*/
|
||||
public void addRoleToUser(Certificate certificate, String username, String roleName) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
public UserRep addRoleToUser(Certificate certificate, String username, String roleName)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
* Adds the {@link PrivilegeRep} to the {@link Role} with the given roleName or replaces it, if it already exists
|
||||
|
@ -333,7 +335,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate or the role does not exist
|
||||
*/
|
||||
public void addOrReplacePrivilegeOnRole(Certificate certificate, String roleName, PrivilegeRep privilegeRep)
|
||||
public RoleRep addOrReplacePrivilegeOnRole(Certificate certificate, String roleName, PrivilegeRep privilegeRep)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -382,7 +384,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate
|
||||
*/
|
||||
public void setUserName(Certificate certificate, String username, String firstname, String lastname)
|
||||
public UserRep setUserName(Certificate certificate, String username, String firstname, String lastname)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
|
@ -400,8 +402,8 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate
|
||||
*/
|
||||
public void setUserState(Certificate certificate, String username, UserState state) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
public UserRep setUserState(Certificate certificate, String username, UserState state)
|
||||
throws AccessDeniedException, PrivilegeException;
|
||||
|
||||
/**
|
||||
* Changes the {@link Locale} of the user
|
||||
|
@ -418,7 +420,7 @@ public interface PrivilegeHandler {
|
|||
* @throws PrivilegeException
|
||||
* if there is anything wrong with this certificate
|
||||
*/
|
||||
public void setUserLocale(Certificate certificate, String username, Locale locale) throws AccessDeniedException,
|
||||
public UserRep setUserLocale(Certificate certificate, String username, Locale locale) throws AccessDeniedException,
|
||||
PrivilegeException;
|
||||
|
||||
/**
|
||||
|
|
|
@ -112,7 +112,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
|||
|
||||
@Override
|
||||
public void addRole(Role role) {
|
||||
if (this.userMap.containsKey(role.getName()))
|
||||
if (this.roleMap.containsKey(role.getName()))
|
||||
throw new IllegalStateException(MessageFormat.format("The role {0} already exists!", role.getName()));
|
||||
this.roleMap.put(role.getName(), role);
|
||||
this.roleMapDirty = true;
|
||||
|
@ -120,9 +120,9 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
|||
|
||||
@Override
|
||||
public void replaceRole(Role role) {
|
||||
if (!this.userMap.containsKey(role))
|
||||
if (!this.roleMap.containsKey(role.getName()))
|
||||
throw new IllegalStateException(MessageFormat.format(
|
||||
"The role {0} can not be replaced as it does not exiset!", role.getName()));
|
||||
"The role {0} can not be replaced as it does not exist!", role.getName()));
|
||||
this.roleMap.put(role.getName(), role);
|
||||
this.roleMapDirty = true;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
package ch.eitchnet.privilege.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
|
@ -159,7 +160,7 @@ public class PrivilegeRep implements Serializable {
|
|||
* @return the denyList
|
||||
*/
|
||||
public Set<String> getDenyList() {
|
||||
return this.denyList;
|
||||
return this.denyList == null ? new HashSet<>() : this.denyList;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,7 +175,7 @@ public class PrivilegeRep implements Serializable {
|
|||
* @return the allowList
|
||||
*/
|
||||
public Set<String> getAllowList() {
|
||||
return this.allowList;
|
||||
return this.allowList == null ? new HashSet<>() : this.allowList;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,9 +18,7 @@ package ch.eitchnet.privilege.model;
|
|||
import java.io.Serializable;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
|
@ -48,19 +46,20 @@ public class RoleRep implements Serializable {
|
|||
@XmlAttribute(name = "name")
|
||||
private String name;
|
||||
|
||||
private Map<String, PrivilegeRep> privilegeMap;
|
||||
@XmlElement(name = "privileges")
|
||||
private List<PrivilegeRep> privileges;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param name
|
||||
* the name of this role
|
||||
* @param privilegeMap
|
||||
* the map of privileges granted to this role
|
||||
* @param privileges
|
||||
* the list of privileges granted to this role
|
||||
*/
|
||||
public RoleRep(String name, Map<String, PrivilegeRep> privilegeMap) {
|
||||
public RoleRep(String name, List<PrivilegeRep> privileges) {
|
||||
this.name = name;
|
||||
this.privilegeMap = privilegeMap;
|
||||
this.privileges = privileges;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,8 +77,8 @@ public class RoleRep implements Serializable {
|
|||
if (StringHelper.isEmpty(this.name))
|
||||
throw new PrivilegeException("name is null"); //$NON-NLS-1$
|
||||
|
||||
if (this.privilegeMap != null && !this.privilegeMap.isEmpty()) {
|
||||
for (PrivilegeRep privilege : this.privilegeMap.values()) {
|
||||
if (this.privileges != null && !this.privileges.isEmpty()) {
|
||||
for (PrivilegeRep privilege : this.privileges) {
|
||||
try {
|
||||
privilege.validate();
|
||||
} catch (Exception e) {
|
||||
|
@ -106,21 +105,13 @@ public class RoleRep implements Serializable {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the privilegeMap
|
||||
*/
|
||||
public Map<String, PrivilegeRep> getPrivilegeMap() {
|
||||
return this.privilegeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the privileges assigned to this Role as a list
|
||||
*
|
||||
* @return the privileges assigned to this Role as a list
|
||||
*/
|
||||
@XmlElement(name = "privileges")
|
||||
public List<PrivilegeRep> getPrivileges() {
|
||||
return new ArrayList<>(this.privilegeMap.values());
|
||||
return this.privileges == null ? new ArrayList<>() : this.privileges;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -130,14 +121,7 @@ public class RoleRep implements Serializable {
|
|||
* the list of privileges to assign to this role
|
||||
*/
|
||||
public void setPrivileges(List<PrivilegeRep> privileges) {
|
||||
if (this.privilegeMap == null)
|
||||
this.privilegeMap = new HashMap<>(privileges.size());
|
||||
else
|
||||
this.privilegeMap.clear();
|
||||
|
||||
for (PrivilegeRep privilegeRep : privileges) {
|
||||
this.privilegeMap.put(privilegeRep.getName(), privilegeRep);
|
||||
}
|
||||
this.privileges = privileges;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,7 +136,7 @@ public class RoleRep implements Serializable {
|
|||
builder.append("RoleRep [name=");
|
||||
builder.append(this.name);
|
||||
builder.append(", privilegeMap=");
|
||||
builder.append((this.privilegeMap == null ? "null" : this.privilegeMap));
|
||||
builder.append((this.privileges == null ? "null" : this.privileges));
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
package ch.eitchnet.privilege.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
@ -67,7 +70,8 @@ public class UserRep implements Serializable {
|
|||
@XmlElement(name = "roles")
|
||||
private Set<String> roles;
|
||||
|
||||
private Map<String, String> propertyMap;
|
||||
@XmlElement(name = "properties")
|
||||
private List<XmlKeyValue> properties;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
|
@ -98,7 +102,7 @@ public class UserRep implements Serializable {
|
|||
this.userState = userState;
|
||||
this.roles = roles;
|
||||
this.locale = locale;
|
||||
this.propertyMap = propertyMap;
|
||||
this.properties = propertyMap == null ? new ArrayList<>() : XmlKeyValue.valueOf(propertyMap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,6 +144,16 @@ public class UserRep implements Serializable {
|
|||
return this.userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the userId
|
||||
*
|
||||
* @param userId
|
||||
* to set
|
||||
*/
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the username
|
||||
*/
|
||||
|
@ -239,7 +253,13 @@ public class UserRep implements Serializable {
|
|||
* @return the property with the given key, or null if the property is not defined
|
||||
*/
|
||||
public String getProperty(String key) {
|
||||
return this.propertyMap.get(key);
|
||||
if (this.properties == null)
|
||||
return null;
|
||||
for (XmlKeyValue keyValue : this.properties) {
|
||||
if (keyValue.getKey().equals(key))
|
||||
return keyValue.getValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -251,7 +271,21 @@ public class UserRep implements Serializable {
|
|||
* the value of the property to set
|
||||
*/
|
||||
public void setProperty(String key, String value) {
|
||||
this.propertyMap.put(key, value);
|
||||
if (this.properties == null)
|
||||
this.properties = new ArrayList<>();
|
||||
|
||||
boolean updated = false;
|
||||
|
||||
for (XmlKeyValue keyValue : this.properties) {
|
||||
if (keyValue.getKey().equals(key)) {
|
||||
keyValue.setValue(value);
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!updated) {
|
||||
this.properties.add(new XmlKeyValue(key, value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,7 +294,13 @@ public class UserRep implements Serializable {
|
|||
* @return the {@link Set} of keys of all properties
|
||||
*/
|
||||
public Set<String> getPropertyKeySet() {
|
||||
return this.propertyMap.keySet();
|
||||
if (this.properties == null)
|
||||
return new HashSet<>();
|
||||
Set<String> keySet = new HashSet<>(this.properties.size());
|
||||
for (XmlKeyValue keyValue : this.properties) {
|
||||
keySet.add(keyValue.getKey());
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -268,8 +308,10 @@ public class UserRep implements Serializable {
|
|||
*
|
||||
* @return the map of properties
|
||||
*/
|
||||
public Map<String, String> getProperties() {
|
||||
return this.propertyMap;
|
||||
public Map<String, String> getPropertyMap() {
|
||||
if (this.properties == null)
|
||||
return new HashMap<>();
|
||||
return XmlKeyValue.toMap(this.properties);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -278,8 +320,8 @@ public class UserRep implements Serializable {
|
|||
* @return the string map properties of this user as a list of {@link XmlKeyValue} elements
|
||||
*/
|
||||
@XmlElement(name = "properties")
|
||||
public List<XmlKeyValue> getPropertiesAsKeyValue() {
|
||||
return XmlKeyValue.valueOf(this.propertyMap);
|
||||
public List<XmlKeyValue> getProperties() {
|
||||
return this.properties == null ? new ArrayList<>() : this.properties;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -288,8 +330,8 @@ public class UserRep implements Serializable {
|
|||
* @param values
|
||||
* the list of {@link XmlKeyValue} from which to set the properties
|
||||
*/
|
||||
public void setPropertiesAsKeyValue(List<XmlKeyValue> values) {
|
||||
this.propertyMap = XmlKeyValue.toMap(values);
|
||||
public void setProperties(List<XmlKeyValue> values) {
|
||||
this.properties = values;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -101,7 +101,7 @@ public final class PrivilegeImpl implements IPrivilege {
|
|||
*/
|
||||
public PrivilegeImpl(PrivilegeRep privilegeRep) {
|
||||
this(privilegeRep.getName(), privilegeRep.getPolicy(), privilegeRep.isAllAllowed(), privilegeRep.getDenyList(),
|
||||
privilegeRep.getDenyList());
|
||||
privilegeRep.getAllowList());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,9 +15,12 @@
|
|||
*/
|
||||
package ch.eitchnet.privilege.model.internal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import ch.eitchnet.privilege.base.PrivilegeException;
|
||||
|
@ -78,14 +81,14 @@ public final class Role {
|
|||
throw new PrivilegeException("No name defined!"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
if (roleRep.getPrivilegeMap() == null) {
|
||||
throw new PrivilegeException("No privileges defined!"); //$NON-NLS-1$
|
||||
if (roleRep.getPrivileges() == null) {
|
||||
throw new PrivilegeException("Privileges may not be null!"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
// build privileges from reps
|
||||
Map<String, IPrivilege> privilegeMap = new HashMap<String, IPrivilege>(roleRep.getPrivilegeMap().size());
|
||||
for (String privilegeName : roleRep.getPrivilegeMap().keySet()) {
|
||||
privilegeMap.put(privilegeName, new PrivilegeImpl(roleRep.getPrivilegeMap().get(privilegeName)));
|
||||
// build privileges from rep
|
||||
Map<String, IPrivilege> privilegeMap = new HashMap<String, IPrivilege>(roleRep.getPrivileges().size());
|
||||
for (PrivilegeRep privilege : roleRep.getPrivileges()) {
|
||||
privilegeMap.put(privilege.getName(), new PrivilegeImpl(privilege));
|
||||
}
|
||||
|
||||
this.name = name;
|
||||
|
@ -133,11 +136,11 @@ public final class Role {
|
|||
* @return a {@link RoleRep} which is a representation of this object used to serialize and view on clients
|
||||
*/
|
||||
public RoleRep asRoleRep() {
|
||||
Map<String, PrivilegeRep> privilegeMap = new HashMap<String, PrivilegeRep>();
|
||||
for (String privilegeName : this.privilegeMap.keySet()) {
|
||||
privilegeMap.put(privilegeName, this.privilegeMap.get(privilegeName).asPrivilegeRep());
|
||||
List<PrivilegeRep> privileges = new ArrayList<PrivilegeRep>();
|
||||
for (Entry<String, IPrivilege> entry : this.privilegeMap.entrySet()) {
|
||||
privileges.add(entry.getValue().asPrivilegeRep());
|
||||
}
|
||||
return new RoleRep(this.name, privilegeMap);
|
||||
return new RoleRep(this.name, privileges);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -21,11 +21,11 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
|
@ -213,8 +213,7 @@ public class PrivilegeTest {
|
|||
try {
|
||||
login(ADMIN, ArraysHelper.copyOf(PASS_ADMIN));
|
||||
|
||||
Map<String, PrivilegeRep> privilegeMap = new HashMap<String, PrivilegeRep>();
|
||||
RoleRep roleRep = new RoleRep(ROLE_TEMP, privilegeMap);
|
||||
RoleRep roleRep = new RoleRep(ROLE_TEMP, new ArrayList<>());
|
||||
|
||||
Certificate certificate = this.ctx.getCertificate();
|
||||
privilegeHandler.addRole(certificate, roleRep);
|
||||
|
@ -564,7 +563,7 @@ public class PrivilegeTest {
|
|||
// let's add a new user ted
|
||||
HashSet<String> roles = new HashSet<String>();
|
||||
roles.add(ROLE_USER);
|
||||
userRep = new UserRep("2", TED, "Ted", "Newman", UserState.ENABLED, roles, null,
|
||||
userRep = new UserRep(null, TED, "Ted", "Newman", UserState.ENABLED, roles, null,
|
||||
new HashMap<String, String>());
|
||||
Certificate certificate = this.ctx.getCertificate();
|
||||
privilegeHandler.addUser(certificate, userRep, null);
|
||||
|
@ -636,8 +635,7 @@ public class PrivilegeTest {
|
|||
try {
|
||||
// add role user
|
||||
login(ADMIN, ArraysHelper.copyOf(PASS_ADMIN));
|
||||
Map<String, PrivilegeRep> privilegeMap = new HashMap<String, PrivilegeRep>();
|
||||
RoleRep roleRep = new RoleRep(ROLE_USER, privilegeMap);
|
||||
RoleRep roleRep = new RoleRep(ROLE_USER, new ArrayList<PrivilegeRep>());
|
||||
Certificate certificate = this.ctx.getCertificate();
|
||||
privilegeHandler.addRole(certificate, roleRep);
|
||||
privilegeHandler.persist(certificate);
|
||||
|
@ -677,7 +675,7 @@ public class PrivilegeTest {
|
|||
login(ADMIN, ArraysHelper.copyOf(PASS_ADMIN));
|
||||
|
||||
// let's add a new user bob
|
||||
UserRep userRep = new UserRep("1", BOB, "Bob", "Newman", UserState.NEW, new HashSet<String>(
|
||||
UserRep userRep = new UserRep(null, BOB, "Bob", "Newman", UserState.NEW, new HashSet<String>(
|
||||
Arrays.asList(ROLE_MY)), null, new HashMap<String, String>());
|
||||
Certificate certificate = this.ctx.getCertificate();
|
||||
privilegeHandler.addUser(certificate, userRep, null);
|
||||
|
|
Loading…
Reference in New Issue