001: package com.ice.jcvsweb.manager;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.Iterator;
010:
011: import javax.servlet.ServletContext;
012:
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015: import org.w3c.dom.Node;
016: import org.w3c.dom.NodeList;
017: import org.xml.sax.SAXException;
018:
019: import com.ice.jcvsweb.bean.JCVSConfiguration;
020: import com.ice.jcvsweb.bean.JCVSProject;
021: import com.ice.jcvsweb.bean.JCVSUser;
022: import com.ice.jcvsweb.bean.JCVSUserPerm;
023: import com.ice.jcvsweb.helper.ContextHelper;
024: import com.ice.jcvsweb.helper.XMLHelper;
025:
026: /**
027: * This class Manages our JCVSPermissions.
028: *
029: * @author Tim Endres, <a href="mailto:time@jcvs.org">time@jcvs.org</a>
030: * @see com.ice.cvsc
031: */
032:
033: public class JCVSPermManager {
034: public static final String ATTR_NAME = "jcvsPermManager";
035: private static final String KEY_SEPARATOR = "|";
036:
037: private HashMap userPermMap = null;
038: private JCVSUserPerm defUserPerm = null;
039: private ServletContext context = null;
040: private JCVSConfiguration config = null;
041:
042: public JCVSPermManager(ServletContext ctx, JCVSConfiguration config)
043: throws IOException {
044: this .context = ctx;
045: this .config = config;
046: this .userPermMap = new HashMap(256);
047: }
048:
049: private String getUserPermKey(String user, String project) {
050: return (user + KEY_SEPARATOR + project);
051: }
052:
053: public File getPermConfDir() {
054: return new File(ContextHelper.getRealAbsolutePath(this .context,
055: this .config.getConfigDir()), "permission");
056: }
057:
058: public File getUserPermFile() {
059: return new File(this .getPermConfDir(), "user.xml");
060: }
061:
062: public JCVSUserPerm getUserPermission(String user, String project) {
063: String key = this .getUserPermKey(user, project);
064:
065: JCVSUserPerm result = (JCVSUserPerm) this .userPermMap.get(key);
066:
067: if (result == null) {
068: result = this .defUserPerm;
069: }
070:
071: return result;
072: }
073:
074: public boolean getUserCanEdit(JCVSUser user, JCVSProject project) {
075: return (user.getIsAdmin() || project.getIsOwner(user.getName()));
076: }
077:
078: public boolean getUserCanView(JCVSUser user, JCVSProject project) {
079: boolean result = false;
080:
081: if (user != null
082: && (user.getIsAdmin() || project.getIsOwner(user
083: .getName()))) {
084: result = true;
085: } else if (user != null
086: && project.getDef().getViewingRestricted()) {
087: JCVSUserPerm perm = this .getUserPermission(user.getName(),
088: project.getDef().getKey());
089:
090: if (perm != null) {
091: result = perm.getViewPerm();
092: }
093: } else {
094: result = !this .config.getViewingRestricted();
095: }
096:
097: return result;
098: }
099:
100: public int getProjectGrantCount(String projectKey) {
101: int count = 0;
102: Iterator iter = this .userPermMap.keySet().iterator();
103: for (; iter.hasNext();) {
104: String key = (String) iter.next();
105: int idx = key.indexOf(KEY_SEPARATOR);
106: if (idx != -1) {
107: String userName = key.substring(0, idx);
108: String pKey = key.substring(idx + 1);
109: if (pKey != null && pKey.equals(projectKey)) {
110: ++count;
111: }
112: }
113: }
114:
115: return count;
116: }
117:
118: public String[] getProjectGrantees(String projectKey) {
119: ArrayList users = new ArrayList(32);
120: Iterator iter = this .userPermMap.keySet().iterator();
121: for (; iter.hasNext();) {
122: String key = (String) iter.next();
123: int idx = key.indexOf(KEY_SEPARATOR);
124: if (idx != -1) {
125: String userName = key.substring(0, idx);
126: String pKey = key.substring(idx + 1);
127: if (pKey != null && pKey.equals(projectKey)) {
128: users.add(userName);
129: }
130: }
131: }
132:
133: String[] result = new String[users.size()];
134: users.toArray(result);
135: return result;
136: }
137:
138: public JCVSUserPerm createUserPermission(String name,
139: String projectKey) throws IOException {
140: JCVSUserPerm perm = new JCVSUserPerm();
141:
142: perm.setUserName(name);
143: perm.setProjectKey(projectKey);
144: perm.setViewPerm(false);
145: perm.setAdminPerm(false);
146:
147: String key = this .getUserPermKey(perm.getUserName(), perm
148: .getProjectKey());
149:
150: this .userPermMap.put(key, perm);
151:
152: return perm;
153: }
154:
155: public JCVSUserPerm deleteUserPermission(String name,
156: String projectKey) throws IOException {
157: String key = this .getUserPermKey(name, projectKey);
158:
159: JCVSUserPerm perm = (JCVSUserPerm) this .userPermMap.remove(key);
160:
161: return perm;
162: }
163:
164: private void loadUserPermissions(File permFile) throws IOException {
165: try {
166: Document xmlDoc = XMLHelper.readXMLConfiguration(permFile);
167:
168: if (xmlDoc != null) {
169: NodeList nodes = XMLHelper.selectNodeList(xmlDoc
170: .getDocumentElement(),
171: "/jcvs-permissions/jcvs-user-perm");
172:
173: for (int pi = 0; pi < nodes.getLength(); ++pi) {
174: JCVSUserPerm perm = new JCVSUserPerm();
175: perm.loadConfiguration(nodes.item(pi));
176:
177: String key = this .getUserPermKey(
178: perm.getUserName(), perm.getProjectKey());
179:
180: this .userPermMap.put(key, perm);
181: }
182: }
183: } catch (SAXException ex) {
184: throw new IOException("loading user permissions from '"
185: + permFile + "', " + ex.getMessage());
186: }
187: }
188:
189: public void saveUserPermissions() throws IOException {
190: FileWriter fW = null;
191: PrintWriter pW = null;
192: try {
193: fW = new FileWriter(this .getUserPermFile());
194: pW = new PrintWriter(fW);
195:
196: pW.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
197: pW.println("<jcvs-permissions>");
198: pW.println("");
199:
200: Iterator permIter = this .userPermMap.values().iterator();
201: for (; permIter.hasNext();) {
202: JCVSUserPerm perm = (JCVSUserPerm) permIter.next();
203: perm.saveConfiguration(pW, "\t");
204: }
205:
206: pW.println("");
207: pW.println("</jcvs-permissions>");
208: } finally {
209: if (pW != null)
210: pW.close();
211: }
212: }
213:
214: public void ensureInitialState() {
215: JCVSUserPerm perm = new JCVSUserPerm();
216: this .defUserPerm = perm;
217: perm.setUserName("_DEFAULT_");
218: perm.setProjectKey("_DEFAULT_");
219: perm.setViewPerm(false);
220: perm.setAdminPerm(false);
221:
222: File dirF = this .getPermConfDir();
223: if (!dirF.exists()) {
224: if (!dirF.mkdirs()) {
225: this .context
226: .log("FATAL Could not establish directory '"
227: + dirF.getPath() + "'");
228: }
229: }
230:
231: this .ensureUserPermissions();
232:
233: File userPermF = this .getUserPermFile();
234: try {
235: this .loadUserPermissions(userPermF);
236: } catch (IOException ex) {
237: this .context.log("FATAL failed loading user permissions '"
238: + userPermF.getPath() + "'", ex);
239: }
240: }
241:
242: private void ensureUserPermissions() {
243: File f = this .getUserPermFile();
244: if (!f.exists()) {
245: try {
246: this .saveUserPermissions();
247: } catch (IOException ex) {
248: this .context.log("FATAL ensuring permissions", ex);
249: }
250: }
251: }
252:
253: }
|