This commit is contained in:
eitch 2010-06-05 21:33:30 +00:00
parent dc414218e6
commit aa28ab0fc3
11 changed files with 423 additions and 14 deletions

View File

@ -391,7 +391,7 @@ public class DefaultPersistenceHandler implements PersistenceHandler {
boolean allAllowed = Boolean.valueOf(allAllowedS);
List<Element> denyElements = privilegeElement.elements(XmlConstants.XML_DENY);
List<String> denyList = new ArrayList<String>(denyElements.size());
Set<String> denyList = new HashSet<String>(denyElements.size());
for (Element denyElement : denyElements) {
String denyValue = denyElement.getTextTrim();
if (denyValue.isEmpty()) {
@ -402,7 +402,7 @@ public class DefaultPersistenceHandler implements PersistenceHandler {
}
List<Element> allowElements = privilegeElement.elements(XmlConstants.XML_ALLOW);
List<String> allowList = new ArrayList<String>(allowElements.size());
Set<String> allowList = new HashSet<String>(allowElements.size());
for (Element allowElement : allowElements) {
String allowValue = allowElement.getTextTrim();
if (allowValue.isEmpty()) {

View File

@ -10,6 +10,7 @@
package ch.eitchnet.privilege.handler;
import ch.eitchnet.privilege.base.PrivilegeContainer;
import ch.eitchnet.privilege.base.PrivilegeContainerObject;
import ch.eitchnet.privilege.model.Certificate;
import ch.eitchnet.privilege.model.internal.Privilege;
@ -17,13 +18,16 @@ import ch.eitchnet.privilege.model.internal.Role;
import ch.eitchnet.privilege.model.internal.User;
/**
* TODO {@link PersistenceHandler} may not be freely accessible via {@link PrivilegeContainer}
*
* @author rvonburg
*
*/
public interface PersistenceHandler extends PrivilegeContainerObject {
public User getUser(String username);
// public void setUserPassword(String username, String password);
// public void setUserState(String username, UserState state);
public void addUser(Certificate certificate, User user);
public Role getRole(String roleName);

View File

@ -19,6 +19,7 @@ import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import ch.eitchnet.privilege.base.PrivilegeContainer;
import ch.eitchnet.privilege.handler.PersistenceHandler;
import ch.eitchnet.privilege.model.Certificate;
/**
@ -39,12 +40,23 @@ public class TestConfigurationHelper {
// initialize container
String pwd = System.getProperty("user.dir");
File privilegeContainerXml = new File(pwd + "/config/PrivilegeContainer.xml");
PrivilegeContainer.getInstance().initialize(privilegeContainerXml);
PrivilegeContainer privilegeContainer = PrivilegeContainer.getInstance();
privilegeContainer.initialize(privilegeContainerXml);
PersistenceHandler persistenceHandler = privilegeContainer.getPersistenceHandler();
for (int i = 0; i < 10; i++) {
// let's authenticate a session
auth("eitch", "1234567890");
}
// TODO let's add a user
// persistenceHandler.addUser(certificate, user);
// TODO let's add a role
// TODO let's add a privilege
}
/**

View File

@ -161,6 +161,15 @@ public final class Certificate implements Serializable {
*/
@Override
public String toString() {
return "Certificate [sessionId=" + sessionId + ", username=" + username + ", locale=" + locale + "]";
StringBuilder builder = new StringBuilder();
builder.append("Certificate [sessionId=");
builder.append(sessionId);
builder.append(", username=");
builder.append(username);
builder.append(", locale=");
builder.append(locale);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,120 @@
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* @author rvonburg
*
*/
public class PrivilegeRep implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String policy;
private boolean allAllowed;
private Set<String> denyList;
private Set<String> allowList;
/**
* @param name
* @param policy
* @param allAllowed
* @param denyList
* @param allowList
*/
public PrivilegeRep(String name, String policy, boolean allAllowed, Set<String> denyList, Set<String> allowList) {
this.name = name;
this.policy = policy;
this.allAllowed = allAllowed;
this.denyList = new HashSet<String>(denyList);
this.allowList = new HashSet<String>(allowList);
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the policy
*/
public String getPolicy() {
return policy;
}
/**
* @param policy
* the policy to set
*/
public void setPolicy(String policy) {
this.policy = policy;
}
/**
* @return the allAllowed
*/
public boolean isAllAllowed() {
return allAllowed;
}
/**
* @param allAllowed
* the allAllowed to set
*/
public void setAllAllowed(boolean allAllowed) {
this.allAllowed = allAllowed;
}
/**
* @return the denyList
*/
public Set<String> getDenyList() {
return denyList;
}
/**
* @param denyList
* the denyList to set
*/
public void setDenyList(Set<String> denyList) {
this.denyList = denyList;
}
/**
* @return the allowList
*/
public Set<String> getAllowList() {
return allowList;
}
/**
* @param allowList
* the allowList to set
*/
public void setAllowList(Set<String> allowList) {
this.allowList = allowList;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.model;
import java.io.Serializable;
import java.util.Set;
/**
* @author rvonburg
*
*/
public class RoleRep implements Serializable {
private static final long serialVersionUID = 1L;
public final String name;
public final Set<String> privileges;
/**
* @param name
* @param privileges
*/
public RoleRep(String name, Set<String> privileges) {
this.name = name;
this.privileges = privileges;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.model;
import java.io.Serializable;
import java.util.Locale;
import java.util.Set;
/**
* @author rvonburg
*
*/
public class UserRep implements Serializable {
private static final long serialVersionUID = 1L;
public final String username;
public final String firstname;
public final String surname;
public final UserState userState;
public final Set<String> roles;
public final Locale locale;
/**
* @param username
* @param firstname
* @param surname
* @param userState
* @param roles
* @param locale
*/
public UserRep(String username, String firstname, String surname, UserState userState, Set<String> roles,
Locale locale) {
this.username = username;
this.firstname = firstname;
this.surname = surname;
this.userState = userState;
this.roles = roles;
this.locale = locale;
}
}

View File

@ -11,7 +11,9 @@
package ch.eitchnet.privilege.model.internal;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import ch.eitchnet.privilege.model.PrivilegeRep;
/**
* @author rvonburg
@ -22,20 +24,20 @@ public final class Privilege {
private final String name;
private final String policy;
private final boolean allAllowed;
private final List<String> denyList;
private final List<String> allowList;
private final Set<String> denyList;
private final Set<String> allowList;
/**
* @param allAllowed
* @param denyList
* @param allowList
*/
public Privilege(String name, String policy, boolean allAllowed, List<String> denyList, List<String> allowList) {
public Privilege(String name, String policy, boolean allAllowed, Set<String> denyList, Set<String> allowList) {
this.name = name;
this.policy = policy;
this.allAllowed = allAllowed;
this.denyList = Collections.unmodifiableList(denyList);
this.allowList = Collections.unmodifiableList(allowList);
this.denyList = Collections.unmodifiableSet(denyList);
this.allowList = Collections.unmodifiableSet(allowList);
}
/**
@ -62,15 +64,93 @@ public final class Privilege {
/**
* @return the allowList
*/
public List<String> getAllowList() {
public Set<String> getAllowList() {
return allowList;
}
/**
* @return the denyList
*/
public List<String> getDenyList() {
public Set<String> getDenyList() {
return denyList;
}
/**
* @return a {@link PrivilegeRep} which is a representation of this object used to serialize and view on clients
*/
public PrivilegeRep asPrivilegeRep() {
return new PrivilegeRep(name, policy, allAllowed, denyList, allowList);
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Privilege [name=");
builder.append(name);
builder.append(", policy=");
builder.append(policy);
builder.append(", allAllowed=");
builder.append(allAllowed);
builder.append(", denyList=");
builder.append(denyList);
builder.append(", allowList=");
builder.append(allowList);
builder.append("]");
return builder.toString();
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (allAllowed ? 1231 : 1237);
result = prime * result + ((allowList == null) ? 0 : allowList.hashCode());
result = prime * result + ((denyList == null) ? 0 : denyList.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((policy == null) ? 0 : policy.hashCode());
return result;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Privilege other = (Privilege) obj;
if (allAllowed != other.allAllowed)
return false;
if (allowList == null) {
if (other.allowList != null)
return false;
} else if (!allowList.equals(other.allowList))
return false;
if (denyList == null) {
if (other.denyList != null)
return false;
} else if (!denyList.equals(other.denyList))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (policy == null) {
if (other.policy != null)
return false;
} else if (!policy.equals(other.policy))
return false;
return true;
}
}

View File

@ -13,6 +13,8 @@ package ch.eitchnet.privilege.model.internal;
import java.util.Collections;
import java.util.Set;
import ch.eitchnet.privilege.model.RoleRep;
/**
* @author rvonburg
*
@ -53,4 +55,63 @@ public final class Role {
public boolean hasPrivilege(String key) {
return privileges.contains(key);
}
/**
* @return a {@link RoleRep} which is a representation of this object used to serialize and view on clients
*/
public RoleRep asRoleRep() {
return new RoleRep(name, privileges);
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Role [name=");
builder.append(name);
builder.append(", privileges=");
builder.append(privileges);
builder.append("]");
return builder.toString();
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((privileges == null) ? 0 : privileges.hashCode());
return result;
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Role other = (Role) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (privileges == null) {
if (other.privileges != null)
return false;
} else if (!privileges.equals(other.privileges))
return false;
return true;
}
}

View File

@ -125,6 +125,15 @@ public final class Session {
*/
@Override
public String toString() {
return "Session [username=" + username + ", sessionId=" + sessionId + ", loginTime=" + loginTime + "]";
StringBuilder builder = new StringBuilder();
builder.append("Session [sessionId=");
builder.append(sessionId);
builder.append(", username=");
builder.append(username);
builder.append(", loginTime=");
builder.append(loginTime);
builder.append("]");
return builder.toString();
}
}

View File

@ -15,6 +15,7 @@ import java.util.Locale;
import java.util.Set;
import ch.eitchnet.privilege.i18n.PrivilegeException;
import ch.eitchnet.privilege.model.UserRep;
import ch.eitchnet.privilege.model.UserState;
/**
@ -112,6 +113,35 @@ public final class User {
return locale;
}
/**
* @return a {@link UserRep} which is a representation of this object used to serialize and view on clients
*/
public UserRep asUserRep() {
return new UserRep(username, firstname, surname, userState, roles, locale);
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("User [username=");
builder.append(username);
builder.append(", firstname=");
builder.append(firstname);
builder.append(", surname=");
builder.append(surname);
builder.append(", locale=");
builder.append(locale);
builder.append(", userState=");
builder.append(userState);
builder.append(", roles=");
builder.append(roles);
builder.append("]");
return builder.toString();
}
/**
* @return a new {@link User} object which is authenticated on the current Java Virtual Machine
*/