KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > explorer > CmsExplorerTypeAccess


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/CmsExplorerTypeAccess.java,v $
3  * Date : $Date: 2006/03/27 14:52:30 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.explorer;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsUser;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.OpenCms;
39 import org.opencms.security.CmsAccessControlEntry;
40 import org.opencms.security.CmsAccessControlList;
41 import org.opencms.security.CmsPermissionSet;
42 import org.opencms.security.I_CmsPrincipal;
43 import org.opencms.util.CmsUUID;
44
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 /**
53  * Explorer type access object, encapsulates access control entires and lists of a explorer type.<p>
54  *
55  * @author Michael Emmerich
56  *
57  * @version $Revision: 1.12 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsExplorerTypeAccess {
62
63     /** Principal key name for the default permission settings. */
64     public static final String JavaDoc PRINCIPAL_DEFAULT = "DEFAULT";
65
66     /** The log object for this class. */
67     private static final Log LOG = CmsLog.getLog(CmsExplorerTypeAccess.class);
68
69     private Map JavaDoc m_accessControl;
70     private CmsAccessControlList m_accessControlList;
71
72     /**
73      * Constructor, creates an empty, CmsExplorerTypeAccess object.<p>
74      */

75     public CmsExplorerTypeAccess() {
76
77         m_accessControl = new HashMap JavaDoc();
78         m_accessControlList = new CmsAccessControlList();
79     }
80
81     /**
82      * Adds a single access entry to the map of access entries of the explorer type setting.<p>
83      *
84      * This stores the configuration data in a map which is used in the initialize process
85      * to create the access control list.<p>
86      *
87      * @param key the principal of the ace
88      * @param value the permissions for the principal
89      */

90     public void addAccessEntry(String JavaDoc key, String JavaDoc value) {
91
92         m_accessControl.put(key, value);
93         if (LOG.isDebugEnabled()) {
94             LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_ACCESS_ENTRY_2, key, value));
95         }
96     }
97
98     /**
99      * Creates the access control list from the temporary map.<p>
100      *
101      * @throws CmsException if reading a group or user fails
102      */

103     public void createAccessControlList() throws CmsException {
104
105         if (OpenCms.getRunLevel() < OpenCms.RUNLEVEL_2_INITIALIZING) {
106             // we don't need this for simple test cases
107
return;
108         }
109
110         m_accessControlList = new CmsAccessControlList();
111         Iterator JavaDoc i = m_accessControl.keySet().iterator();
112         while (i.hasNext()) {
113             String JavaDoc key = (String JavaDoc)i.next();
114             if (!PRINCIPAL_DEFAULT.equals(key)) {
115                 String JavaDoc value = (String JavaDoc)m_accessControl.get(key);
116                 CmsUUID principalId = new CmsUUID();
117                 // get the principal name from the principal String
118
String JavaDoc principal = key.substring(key.indexOf('.') + 1, key.length());
119
120                 // create an OpenCms user context with "Guest" permissions
121
CmsObject cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest());
122
123                 if (key.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
124                     // read the group
125
principal = OpenCms.getImportExportManager().translateGroup(principal);
126                     principalId = cms.readGroup(principal).getId();
127                 } else {
128                     // read the user
129
principal = OpenCms.getImportExportManager().translateUser(principal);
130                     principalId = cms.readUser(principal).getId();
131                 }
132                 // create a new entry for the principal
133
CmsAccessControlEntry entry = new CmsAccessControlEntry(null, principalId, value);
134                 m_accessControlList.add(entry);
135             }
136         }
137     }
138
139     /**
140      * Returns the map of access entries of the explorer type setting.<p>
141      *
142      * @return the map of access entries of the explorer type setting
143      */

144     public Map JavaDoc getAccessEntries() {
145
146         return m_accessControl;
147     }
148
149     /**
150      * Calculates the permissions for this explorer type settings
151      * for the user in the given OpenCms user context.<p>
152      *
153      * @param cms the OpenCms user context to calculate the permissions for
154      *
155      * @return the permissions for this explorer type settings for the user in the given OpenCms user context
156      */

157     public CmsPermissionSet getPermissions(CmsObject cms) {
158
159         CmsAccessControlList acl = (CmsAccessControlList)m_accessControlList.clone();
160
161         CmsUser user = cms.getRequestContext().currentUser();
162         List groups = null;
163         try {
164             groups = cms.getGroupsOfUser(user.getName());
165         } catch (CmsException e) {
166             // error reading the groups of the current user
167
LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_GROUPS_OF_USER_FAILED_1, user.getName()));
168         }
169         String JavaDoc defaultPermissions = (String JavaDoc)m_accessControl.get(PRINCIPAL_DEFAULT);
170         // add the default permissions to the acl
171
if ((defaultPermissions != null) && !user.isGuestUser()) {
172             boolean found = false;
173             if (acl.getPermissions(user) != null) {
174                 // acl already contains the user, no need for default
175
found = true;
176             }
177             if (!found && (groups != null)) {
178                 // look up all groups to see if we need the default
179
Iterator JavaDoc i = groups.iterator();
180                 while (i.hasNext()) {
181                     I_CmsPrincipal principal = (I_CmsPrincipal)i.next();
182                     if (acl.getPermissions(principal) != null) {
183                         // acl already contains the group, no need for default
184
found = true;
185                         break;
186                     }
187                 }
188             }
189             if (!found) {
190                 // add default access control settings for current user
191
CmsAccessControlEntry entry = new CmsAccessControlEntry(null, user.getId(), defaultPermissions);
192                 acl.add(entry);
193             }
194         }
195
196         // get permissions of the current user
197
return acl.getPermissions(user, groups);
198     }
199
200     /**
201      * Tests if there are any access information stored.<p>
202      * @return true or false
203      */

204     public boolean isEmpty() {
205
206         return m_accessControl.isEmpty();
207     }
208 }
Popular Tags