KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > security > AccessManagerImpl


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security;
14
15 import java.util.List JavaDoc;
16
17 import org.apache.commons.lang.StringUtils;
18
19
20 /**
21  * @author Sameer Charles
22  * @version 2.01
23  */

24 public class AccessManagerImpl implements AccessManager {
25
26     private List JavaDoc userPermissions;
27
28     public boolean isGranted(String JavaDoc path, long permissions) {
29         if (StringUtils.isEmpty(path)) {
30             return (getPermissions("/") & permissions) == permissions; //$NON-NLS-1$
31
}
32         return (getPermissions(path) & permissions) == permissions;
33     }
34
35     public void setPermissionList(List JavaDoc permissions) {
36         this.userPermissions = permissions;
37     }
38
39     public long getPermissions(String JavaDoc path) {
40         if (userPermissions == null) {
41             return Permission.ALL;
42         }
43         long permission = 0;
44         int patternLength = 0;
45         for (int i = 0; i < userPermissions.size(); i++) {
46             info.magnolia.cms.security.Permission p = (info.magnolia.cms.security.Permission) userPermissions.get(i);
47             if (p.match(path)) {
48                 int l = p.getPattern().getLength();
49                 if (patternLength == l && (permission > p.getPermissions())) {
50                     permission = p.getPermissions();
51                 }
52                 else if (patternLength <= l) {
53                     patternLength = l;
54                     permission = p.getPermissions();
55                 }
56             }
57         }
58         return permission;
59     }
60 }
61
Popular Tags