Subversion Repositories XServices

Rev

Rev 198 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
146 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
*/
16
 
17
package net.brutex.xservices.security;
18
 
185 brianR 19
 
199 brianR 20
import lombok.extern.slf4j.Slf4j;
146 brianR 21
import org.apache.shiro.authz.Permission;
22
import org.apache.shiro.util.AntPathMatcher;
23
 
24
/**
25
 * @author Brian Rosenberger, bru(at)brutex.de
26
 *
27
 */
199 brianR 28
@Slf4j
146 brianR 29
public class DirectoryPermission implements Permission {
30
 
31
	private final String path;
32
 
33
	public DirectoryPermission(String antlikepath) {
199 brianR 34
		log.debug(String.format("Creating permission for path '{}'", antlikepath));
146 brianR 35
		path = antlikepath;
36
	}
37
 
38
	@Override
39
	public boolean implies(Permission p) {
40
		boolean result = false;
41
 
42
		/* is of same type */
43
		if(! (p instanceof DirectoryPermission)) {
199 brianR 44
			log.debug(String.format("Testing if permission of type '{}' implies permission of type '{}'. Result was '{}'"  , this.getClass(), p.getClass(), result));
146 brianR 45
			return result;
46
		}
47
 
48
		/* comparing to non null directory */
49
		if( ((DirectoryPermission)p).getPath() == null) {
199 brianR 50
			log.debug(String.format("Testing if DirectoryPermission '{}' implies permission to 'null'. Result was '{}'"  , this.getPath(), result));
146 brianR 51
			return result;
52
		}
53
 
54
		/* directory pattern implies other */
55
		if( (new AntPathMatcher()).matches(path, ((DirectoryPermission)p).getPath()) ) {
56
			result = true;
57
		}
199 brianR 58
		log.debug(String.format("Testing if DirectoryPermission '{}' implies permission to '{}'. Result was '{}'"  , this.getPath(), ((DirectoryPermission) p).getPath(), result));
146 brianR 59
 
60
		return result;
61
	}
62
 
63
	public String getPath() {
64
		return path;
65
	}
66
 
67
}