strolch/src/ch/eitchnet/privilege/model/internal/Role.java

146 lines
3.3 KiB
Java
Raw Normal View History

2010-05-29 20:47:04 +02:00
/*
* Copyright (c) 2010
*
* Robert von Burg
* eitch@eitchnet.ch
*
* All rights reserved.
*
*/
package ch.eitchnet.privilege.model.internal;
import java.util.Collections;
2010-09-18 22:00:20 +02:00
import java.util.HashSet;
import java.util.Set;
2010-05-29 20:47:04 +02:00
import ch.eitchnet.privilege.i18n.PrivilegeException;
2010-06-05 23:33:30 +02:00
import ch.eitchnet.privilege.model.RoleRep;
2010-05-29 20:47:04 +02:00
/**
* <p>
* A {@link User} is assigned a set of roles. These roles have a set of privileges assigned to them by name and they
* define the privileges granted to a user with this role
* </p>
*
* <p>
* Note: This is an internal object which is not to be serialized or passed to clients, {@link RoleRep}s are used for
* that
* </p>
2010-05-29 20:47:04 +02:00
*
* @author rvonburg
2010-05-29 20:47:04 +02:00
*/
2010-05-31 21:34:26 +02:00
public final class Role {
2010-05-29 20:47:04 +02:00
2010-06-05 23:06:03 +02:00
private final String name;
private final Set<String> privileges;
2010-05-29 20:47:04 +02:00
/**
* Default constructor
2010-06-05 23:06:03 +02:00
*
* @param name
* the name of the role
2010-06-05 23:06:03 +02:00
* @param privileges
* a set of names of privileges granted to this role
2010-05-29 20:47:04 +02:00
*/
2010-06-05 23:06:03 +02:00
public Role(String name, Set<String> privileges) {
if (name == null || name.isEmpty()) {
throw new PrivilegeException("No name defined!");
}
if (privileges == null) {
throw new PrivilegeException("No privileges defined!");
}
2010-06-05 23:06:03 +02:00
this.name = name;
this.privileges = Collections.unmodifiableSet(privileges);
2010-05-29 20:47:04 +02:00
}
2010-05-31 21:34:26 +02:00
/**
2010-06-05 23:06:03 +02:00
* @return the name
2010-05-31 21:34:26 +02:00
*/
2010-06-05 23:06:03 +02:00
public String getName() {
2010-09-19 20:57:23 +02:00
return this.name;
2010-05-31 21:34:26 +02:00
}
/**
2010-11-27 22:54:46 +01:00
* Returns the {@link Set} of {@link Privilege} names which is granted to this {@link Role}
*
* @return the {@link Set} of {@link Privilege} names which is granted to this
*/
public Set<String> getPrivileges() {
2010-09-19 20:57:23 +02:00
return this.privileges;
}
2010-05-29 20:47:04 +02:00
/**
2010-11-27 22:54:46 +01:00
* Determines if this {@link Role} has the {@link Privilege} with the given name
*
* @param name
* the name of the {@link Privilege}
*
* @return true if this {@link Role} has the {@link Privilege} with the given name
2010-05-29 20:47:04 +02:00
*/
2010-11-27 22:54:46 +01:00
public boolean hasPrivilege(String name) {
return this.privileges.contains(name);
2010-05-29 20:47:04 +02:00
}
2010-06-05 23:33:30 +02:00
/**
* @return a {@link RoleRep} which is a representation of this object used to serialize and view on clients
*/
public RoleRep asRoleRep() {
2010-09-19 20:57:23 +02:00
return new RoleRep(this.name, new HashSet<String>(this.privileges));
2010-06-05 23:33:30 +02:00
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Role [name=");
2010-09-19 20:57:23 +02:00
builder.append(this.name);
2010-06-05 23:33:30 +02:00
builder.append(", privileges=");
2010-09-19 20:57:23 +02:00
builder.append(this.privileges);
2010-06-05 23:33:30 +02:00
builder.append("]");
return builder.toString();
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
2010-09-19 20:57:23 +02:00
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
result = prime * result + ((this.privileges == null) ? 0 : this.privileges.hashCode());
2010-06-05 23:33:30 +02:00
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;
2010-09-19 20:57:23 +02:00
if (this.name == null) {
2010-06-05 23:33:30 +02:00
if (other.name != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.name.equals(other.name))
2010-06-05 23:33:30 +02:00
return false;
2010-09-19 20:57:23 +02:00
if (this.privileges == null) {
2010-06-05 23:33:30 +02:00
if (other.privileges != null)
return false;
2010-09-19 20:57:23 +02:00
} else if (!this.privileges.equals(other.privileges))
2010-06-05 23:33:30 +02:00
return false;
return true;
}
2010-05-29 20:47:04 +02:00
}