KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > model > Permission


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.permissions.impl.model;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.repo.security.permissions.PermissionReference;
25 import org.alfresco.repo.security.permissions.impl.PermissionReferenceImpl;
26 import org.alfresco.service.cmr.security.AccessStatus;
27 import org.alfresco.service.namespace.NamespacePrefixResolver;
28 import org.alfresco.service.namespace.QName;
29 import org.dom4j.Attribute;
30 import org.dom4j.Element;
31
32 /**
33  * Support to read and store the definition of a permission.
34  *
35  * @author andyh
36  */

37 public class Permission extends AbstractPermission implements XMLModelInitialisable
38 {
39     // XML Constants
40

41     private static final String JavaDoc GRANTED_TO_GROUP = "grantedToGroup";
42     
43     private static final String JavaDoc GTG_NAME = "permissionGroup";
44
45     private static final String JavaDoc GTG_TYPE = "type";
46
47     private Set JavaDoc<PermissionReference> grantedToGroups = new HashSet JavaDoc<PermissionReference>();
48     
49     private static final String JavaDoc DENY = "deny";
50     
51     private static final String JavaDoc ALLOW = "allow";
52     
53     private static final String JavaDoc DEFAULT_PERMISSION = "defaultPermission";
54
55     private static final String JavaDoc EXPOSE = "expose";
56     
57     private static final String JavaDoc REQUIRES_TYPE = "requiresType";
58
59     private AccessStatus defaultPermission;
60
61     private boolean isExposed;
62     
63     private boolean requiresType;
64
65     public Permission(QName typeQName)
66     {
67         super(typeQName);
68     }
69
70     public void initialise(Element element, NamespacePrefixResolver nspr, PermissionModel permissionModel)
71     {
72         super.initialise(element, nspr, permissionModel);
73         
74         Attribute att = element.attribute(EXPOSE);
75         if (att != null)
76         {
77             isExposed = Boolean.parseBoolean(att.getStringValue());
78         }
79         else
80         {
81             isExposed = true;
82         }
83         
84         att = element.attribute(REQUIRES_TYPE);
85         if (att != null)
86         {
87             requiresType = Boolean.parseBoolean(att.getStringValue());
88         }
89         else
90         {
91             requiresType = true;
92         }
93         
94         Attribute defaultPermissionAttribute = element.attribute(DEFAULT_PERMISSION);
95         if(defaultPermissionAttribute != null)
96         {
97             if(defaultPermissionAttribute.getStringValue().equalsIgnoreCase(ALLOW))
98             {
99                 defaultPermission = AccessStatus.ALLOWED;
100             }
101             else if(defaultPermissionAttribute.getStringValue().equalsIgnoreCase(DENY))
102             {
103                 defaultPermission = AccessStatus.DENIED;
104             }
105             else
106             {
107                 throw new PermissionModelException("The default permission must be deny or allow");
108             }
109         }
110         else
111         {
112             defaultPermission = AccessStatus.DENIED;
113         }
114         
115         for (Iterator JavaDoc gtgit = element.elementIterator(GRANTED_TO_GROUP); gtgit.hasNext(); /**/)
116         {
117             QName qName;
118             Element grantedToGroupsElement = (Element) gtgit.next();
119             Attribute typeAttribute = grantedToGroupsElement.attribute(GTG_TYPE);
120             if (typeAttribute != null)
121             {
122                 qName = QName.createQName(typeAttribute.getStringValue(), nspr);
123             }
124             else
125             {
126                 qName = getTypeQName();
127             }
128
129             String JavaDoc grantedName = grantedToGroupsElement.attributeValue(GTG_NAME);
130             
131             grantedToGroups.add(new PermissionReferenceImpl(qName, grantedName));
132         }
133     }
134
135     public AccessStatus getDefaultPermission()
136     {
137         return defaultPermission;
138     }
139
140     public Set JavaDoc<PermissionReference> getGrantedToGroups()
141     {
142         return Collections.unmodifiableSet(grantedToGroups);
143     }
144
145     public boolean isExposed()
146     {
147         return isExposed;
148     }
149     
150     public boolean isTypeRequired()
151     {
152         return requiresType;
153     }
154 }
155
Popular Tags