KickJava   Java API By Example, From Geeks To Geeks.

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


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-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.security;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.core.ItemType;
19 import info.magnolia.cms.core.Path;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.jcr.PathNotFoundException;
25 import javax.jcr.RepositoryException;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * Wraps a role jcr-node.
33  * @author philipp
34  * @version $Revision: 6341 $ ($Author: philipp $)
35  */

36 public class MgnlRole implements Role {
37
38     public static Logger log = LoggerFactory.getLogger(MgnlRole.class);
39
40     /**
41      * Add or remove any permission
42      */

43     public static long PERMISSION_ANY = -1;
44
45     /**
46      * the content object
47      */

48     private Content roleNode;
49
50     /**
51      * @param roleNode the Content object representing this role
52      */

53     protected MgnlRole(Content roleNode) {
54         super();
55         this.roleNode = roleNode;
56     }
57
58     public String JavaDoc getName() {
59         return roleNode.getName();
60     }
61
62     public void addPermission(String JavaDoc repository, String JavaDoc path, long permission) {
63         try {
64             Content aclNode = getAclNode(repository);
65             if (!this.existsPermission(aclNode, path, permission)) {
66                 HierarchyManager hm = ContentRepository.getHierarchyManager(ContentRepository.USER_ROLES);
67                 String JavaDoc nodename = Path.getUniqueLabel(hm, aclNode.getHandle(), "0");
68                 Content node = aclNode.createContent(nodename, ItemType.CONTENTNODE);
69                 node.createNodeData("path").setValue(path);
70                 node.createNodeData("permissions").setValue(String.valueOf(permission));
71                 roleNode.save();
72             }
73         }
74         catch (Exception JavaDoc e) {
75             log.error("can't add permission", e);
76         }
77     }
78
79     public void removePermission(String JavaDoc repository, String JavaDoc path) {
80         this.removePermission(repository, path, MgnlRole.PERMISSION_ANY);
81     }
82
83     public void removePermission(String JavaDoc repository, String JavaDoc path, long permission) {
84         try {
85             Content aclNode = getAclNode(repository);
86             Collection JavaDoc children = aclNode.getChildren();
87             for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
88                 Content child = (Content) iter.next();
89                 if (child.getNodeData("path").getString().equals("path")) {
90                     if (permission == MgnlRole.PERMISSION_ANY
91                         || child.getNodeData("permissions").getString().equals(String.valueOf(permission))) {
92                         child.delete();
93                     }
94                 }
95             }
96             roleNode.save();
97         }
98         catch (Exception JavaDoc e) {
99             log.error("can't remove permission", e);
100         }
101     }
102
103     /**
104      * Get the acl node for the current role node
105      * @param repository
106      * @return
107      * @throws RepositoryException
108      * @throws PathNotFoundException
109      * @throws AccessDeniedException
110      */

111     private Content getAclNode(String JavaDoc repository) throws RepositoryException, PathNotFoundException,
112         AccessDeniedException {
113         Content aclNode;
114         if (!roleNode.hasContent("acl_" + repository)) {
115             aclNode = roleNode.createContent("acl_" + repository, ItemType.CONTENTNODE);
116         }
117         else {
118             aclNode = roleNode.getContent("acl_" + repository);
119         }
120         return aclNode;
121     }
122
123     /**
124      * Does this permission exist?
125      * @param aclNode
126      * @param path
127      * @param permission
128      */

129     private boolean existsPermission(Content aclNode, String JavaDoc path, long permission) {
130         Collection JavaDoc children = aclNode.getChildren();
131         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext();) {
132             Content child = (Content) iter.next();
133             if (child.getNodeData("path").getString().equals(path)) {
134                 if (permission == MgnlRole.PERMISSION_ANY
135                     || child.getNodeData("permission").getString().equals(String.valueOf(permission))) {
136                     return true;
137                 }
138             }
139         }
140         return false;
141     }
142 }
143
Popular Tags