package de.memtext.rights; import java.io.Serializable; import java.util.Collection; import java.util.Iterator; import java.util.TreeSet; import de.memtext.util.EqualsUtil; /** * A class for managing rights. In particular c */ public class Rights implements Serializable { private static final long serialVersionUID = 1; private boolean isAdmin = false; private boolean hasAllRights; private Collection keyColl = new TreeSet(); public Rights() { } public Rights(boolean isAdmin, boolean hasAllRights) { setAdmin(isAdmin); setHasAllRights(hasAllRights); } public boolean isAdmin() { return isAdmin; } public void setAdmin(boolean isAdmin) { this.isAdmin = isAdmin; } public void addAllowed(RightsKeyEntry rke) { keyColl.add(rke); } public void clear() { keyColl.clear(); } public boolean isEmpty() { return keyColl.isEmpty(); } public boolean hasNoRights() { return !isAdmin && !hasAllRights && isEmpty(); } public Iterator iterator() { return keyColl.iterator(); } /** * @return */ public boolean hasAllRights() { return hasAllRights; } /** * @param b */ public void setHasAllRights(boolean b) { hasAllRights = b; } /** * @param dn * @return */ public boolean isAllowed(Object id) { boolean result = false; if (isAdmin || hasAllRights()) { result = true; } else { for (Iterator it = keyColl.iterator(); it.hasNext();) { RightsKeyEntry entry = (RightsKeyEntry) it.next(); if (EqualsUtil.areEqual(entry.getKey(), id)) { result = true; break; } } } return result; } public boolean isSubordinatesAllowed(Object key) { boolean result = false; if (isAdmin || hasAllRights()) { result = true; } else { for (Iterator it = keyColl.iterator(); it.hasNext();) { RightsKeyEntry entry = (RightsKeyEntry) it.next(); if (EqualsUtil.areEqual(entry.getKey(), key)) { result = entry.isSubordinatesAllowed(); break; } } } return result; } public Object clone() throws CloneNotSupportedException { Rights cl = new Rights(isAdmin, hasAllRights); for (Iterator it = keyColl.iterator(); it.hasNext();) { RightsKeyEntry key = (RightsKeyEntry) it.next(); cl.keyColl.add(key.clone()); } return cl; } public String toString() { StringBuffer result = new StringBuffer("Rechte: "); for (Iterator it = keyColl.iterator(); it.hasNext();) { RightsKeyEntry entry = (RightsKeyEntry) it.next(); result.append(entry.toString() + ","); } de.memtext.util.StringUtils.removeLastChar(result); return result.toString(); } } // Created on 07.09.2004 at 11:10:06