Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package org.apache.roller.weblogger.pojos;

import java.security.Permission;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.weblogger.util.Utilities;


/**
 * Base permission class for Weblogger. 
 */
public final class WebloggerPermission extends java.security.Permission {
    
    private static Log log = LogFactory.getLog(WebloggerPermission.class);
    
    private final String type;
    private final String object;
    private final String actions;
    private final Set<String> actionsAsSet;
    private final Set<String> completeActionsAsSet;
    
    
    public WebloggerPermission(String type, String object, String actions) {
        super("WebloggerPermission"this(null, null, actions);
    }
    this.type = type;
    public WebloggerPermission(String type, String this.object = object;
        this.actions = actions;, String actions) {
        this.actionsAsSet = Utilities.stringToStringSet(actions, ",super("WebloggerPermission");
        this.completeActionsAsSet = buildFullActionsSet(actionsAsSet);
    }
    
    
    public String getType() {
        return type;if((type != null && object == null) || (type == null && object != null))
    }
    
    publicthrow Stringnew getObject() {
        return objectIllegalArgumentException("'type' and 'object' must be either both null or both non-null.");
    }
    
    public String getActions() {
 this.type = type;
     return actions;
  this.object = }object;

    public Set<String> getActionsAsSet() {
 this.actions = actions;
     return actionsAsSet;
  this.actionsAsSet  }
    = Utilities.stringToStringSet(actions, ",");
    
    publicthis.completeActionsAsSet boolean= impliesbuildFullActionsSet(Permission permission) {actionsAsSet);
    }
    
    
    // only compare WebloggerPermission objects--------------------------------------------------- Public Methods
    
    
    public String getType() {
        return type;
    }
    
    public String getObject() {
        return object;
    }
    
    public String getActions() {
        return actions;
    }

    public Set<String> getActionsAsSet() {
        return actionsAsSet;
    }
    
    
    public boolean implies(Permission permission) {
        
        // only compare WebloggerPermission objects
        if (!(permission instanceof WebloggerPermission))
            return false;
            
        WebloggerPermission that = (WebloggerPermission) permission;
        
        // only compare 2 permissions of the same type
        if(that.getType() != null) {
            // this type == null or the 2 types are not equal, invalid comparison
            if(this.getType() == null || !(this.getType().equals(that.getType())))
                    return false;
        } else if(this.getType() != null) {
            // that has type == null but this has type != null, invalid comparison
        if (!(permission instanceof WebloggerPermission))
    return false;
        }
       return false;
        // only compare 2 permissions of the same object
        if(that.getObject() != null) {
            // this WebloggerPermissionobject that == (WebloggerPermission) permission;
        null or the 2 objects are not equal, invalid comparison
        // only compare 2 permissions of the same name ("type")
 if(this.getObject() == null || !that.getObject().equals(this.getObject()))
           //    name ("type") cannot be null
return false;
        } else if(this.getNamegetObject() !== null) {
 || that.getName() == null ||
       // that has object == null but this has !(this.getName().equals(that.getName())))object != null, invalid comparison
            return false;
        }
        
        // does "this" permission contain all actions of "that" permission?
        
        // we consider a permission implied if our set of granted actions
        // contains all of the granted actions of the permission we are checking
        if(this.completeActionsAsSet.containsAll(that.completeActionsAsSet))
            return true;
        
        return false;
    }
    
    
    public boolean equals(Object arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int hashCode() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getName()).append(" : ");
        for (String action : getActionsAsSet()) { 
            sb.append(" ").append(action).append(" ");
        }
        return sb.toString();
    }
    
    
    // --------------------------------------------------- Private Methods
    
    
    // build an expanded Set of all actions implied by a Set of actions
    private Set<String> buildFullActionsSet(Set<String> actions) {
        
        Set<String> fullSet = new HashSet<String>();
        
        for( String action : actions ) {
            Set<String> impliedActions = getImpliedActions(action);
            if(impliedActions.size() > 0) {
                // if an action implies other actions, recurse
                fullSet.addAll(buildFullActionsSet(impliedActions));
            } else {
                fullSet.add(action);
            }
        }
        
        return fullSet;
    }
    
    // get implied actions for an action
    private Set<String> getImpliedActions(String action) {
        // TODO: lookup if action implies other actions
        // this would likely be a function of the UserManager somehow
        return Collections.EMPTY_SET;
    }
    
}

...