/*
 *   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 lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.Permission;

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

	private final String permissionString;

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

}
