Subversion Repositories XServices

Rev

Rev 146 | Blame | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
*/

package net.brutex.xservices.security;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;

/**
 * @author Brian Rosenberger, bru(at)brutex.de
 *
 */
public class DirectoryPermission implements Permission {

        private final Logger logger = Logger.getLogger(DirectoryPermission.class);
        private final String path;

        public DirectoryPermission(String antlikepath) {
                logger.debug(String.format("Creating permission for path '%s'", antlikepath));
                path = antlikepath;
        }
        
        @Override
        public boolean implies(Permission p) {
                boolean result = false;
                
                /* is of same type */
                if(! (p instanceof DirectoryPermission)) {                      
                        logger.debug(String.format("Testing if permission of type '%s' implies permission of type '%s'. Result was '%s'"  , this.getClass(), p.getClass(), result));
                        return result;
                }
                
                /* comparing to non null directory */
                if( ((DirectoryPermission)p).getPath() == null) {
                        logger.debug(String.format("Testing if DirectoryPermission '%s' implies permission to 'null'. Result was '%s'"  , this.getPath(), result));
                        return result;
                }
                
                /* directory pattern implies other */
                if( (new AntPathMatcher()).matches(path, ((DirectoryPermission)p).getPath()) ) {
                        result = true;
                }
                logger.debug(String.format("Testing if DirectoryPermission '%s' implies permission to '%s'. Result was '%s'"  , this.getPath(), ((DirectoryPermission) p).getPath(), result));
                
                return result;
        }
        
        public String getPath() {
                return path;
        }

}