Subversion Repositories XServices

Rev

Go to most recent revision | Details | 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
 
20
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
146 brianR 22
import org.apache.shiro.authz.Permission;
23
import org.apache.shiro.util.AntPathMatcher;
24
 
25
/**
26
 * @author Brian Rosenberger, bru(at)brutex.de
27
 *
28
 */
29
public class DirectoryPermission implements Permission {
30
 
185 brianR 31
	private final Logger logger = LogManager.getLogger();
32
 
146 brianR 33
	private final String path;
34
 
35
	public DirectoryPermission(String antlikepath) {
177 brianR 36
		logger.debug(String.format("Creating permission for path '%s'", antlikepath));
146 brianR 37
		path = antlikepath;
38
	}
39
 
40
	@Override
41
	public boolean implies(Permission p) {
42
		boolean result = false;
43
 
44
		/* is of same type */
45
		if(! (p instanceof DirectoryPermission)) {
46
			logger.debug(String.format("Testing if permission of type '%s' implies permission of type '%s'. Result was '%s'"  , this.getClass(), p.getClass(), result));
47
			return result;
48
		}
49
 
50
		/* comparing to non null directory */
51
		if( ((DirectoryPermission)p).getPath() == null) {
52
			logger.debug(String.format("Testing if DirectoryPermission '%s' implies permission to 'null'. Result was '%s'"  , this.getPath(), result));
53
			return result;
54
		}
55
 
56
		/* directory pattern implies other */
57
		if( (new AntPathMatcher()).matches(path, ((DirectoryPermission)p).getPath()) ) {
58
			result = true;
59
		}
60
		logger.debug(String.format("Testing if DirectoryPermission '%s' implies permission to '%s'. Result was '%s'"  , this.getPath(), ((DirectoryPermission) p).getPath(), result));
61
 
62
		return result;
63
	}
64
 
65
	public String getPath() {
66
		return path;
67
	}
68
 
69
}