001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.slide.util;
019:
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Date;
023: import java.util.Enumeration;
024: import java.util.List;
025:
026: import org.apache.slide.authenticate.CredentialsToken;
027: import org.apache.slide.common.NamespaceAccessToken;
028: import org.apache.slide.common.SlideToken;
029: import org.apache.slide.common.SlideTokenImpl;
030: import org.apache.slide.content.Content;
031: import org.apache.slide.content.NodeProperty;
032: import org.apache.slide.content.NodeRevisionDescriptor;
033: import org.apache.slide.content.NodeRevisionDescriptors;
034: import org.apache.slide.lock.Lock;
035: import org.apache.slide.lock.NodeLock;
036: import org.apache.slide.macro.Macro;
037: import org.apache.slide.macro.MacroParameters;
038: import org.apache.slide.security.NodePermission;
039: import org.apache.slide.security.Security;
040: import org.apache.slide.structure.ObjectNode;
041: import org.apache.slide.structure.ObjectNotFoundException;
042: import org.apache.slide.structure.Structure;
043: import org.apache.slide.structure.SubjectNode;
044:
045: /**
046: * Helper class for the slide samples administration application.
047: */
048: public class AdminHelper {
049:
050: private static final SlideToken ROOT = new SlideTokenImpl(
051: new CredentialsToken("root"));
052:
053: public static boolean login(NamespaceAccessToken nat,
054: String userId, String password) throws Exception {
055:
056: String usersPath = nat.getNamespaceConfig().getUsersPath();
057: String userUri = usersPath + "/" + userId;
058:
059: Content content = nat.getContentHelper();
060:
061: try {
062: NodeRevisionDescriptors revisions = content.retrieve(ROOT,
063: userUri);
064: NodeRevisionDescriptor revision = content.retrieve(ROOT,
065: revisions);
066: NodeProperty property = revision.getProperty("password",
067: NodeProperty.SLIDE_NAMESPACE);
068:
069: return property.getValue().equals(password);
070: } catch (Exception e) {
071: e.printStackTrace();
072: throw e;
073: }
074: }
075:
076: public static void addUser(NamespaceAccessToken nat, String caller,
077: String username, String password) throws Exception {
078:
079: String usersPath = nat.getNamespaceConfig().getUsersPath();
080: String userUri = usersPath + "/" + username;
081:
082: SlideToken slideToken = new SlideTokenImpl(
083: new CredentialsToken(caller));
084: Structure structure = nat.getStructureHelper();
085: Content content = nat.getContentHelper();
086:
087: try {
088:
089: nat.begin();
090:
091: ObjectNode user = new SubjectNode();
092: structure.create(slideToken, user, userUri);
093:
094: // create the user descriptor
095: NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
096: descriptor.setCreationDate(new Date());
097: descriptor.setLastModified(new Date());
098: descriptor.setProperty(new NodeProperty("password",
099: password, NodeProperty.SLIDE_NAMESPACE));
100: content.create(slideToken, userUri, descriptor, null);
101:
102: nat.commit();
103: } catch (Exception e) {
104: try {
105: nat.rollback();
106: } catch (Exception f) {
107: f.printStackTrace();
108: }
109: throw e;
110: }
111:
112: }
113:
114: public static void addGroup(NamespaceAccessToken nat,
115: String caller, String groupname) throws Exception {
116:
117: String groupsPath = nat.getNamespaceConfig().getGroupsPath();
118: String groupUri = groupsPath + "/" + groupname;
119:
120: SlideToken slideToken = new SlideTokenImpl(
121: new CredentialsToken(caller));
122: Structure structure = nat.getStructureHelper();
123: Content content = nat.getContentHelper();
124:
125: try {
126: nat.begin();
127:
128: ObjectNode group = new SubjectNode();
129: structure.create(slideToken, group, groupUri);
130:
131: NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
132: descriptor.setCreationDate(new Date());
133: descriptor.setLastModified(new Date());
134:
135: content.create(slideToken, groupUri, descriptor, null);
136:
137: nat.commit();
138: } catch (Exception e) {
139: try {
140: nat.rollback();
141: } catch (Exception f) {
142: f.printStackTrace();
143: }
144: throw e;
145: }
146:
147: }
148:
149: public static void addRole(NamespaceAccessToken nat, String caller,
150: String rolename) throws Exception {
151:
152: String rolesPath = nat.getNamespaceConfig().getRolesPath();
153: String roleUri = rolesPath + "/" + rolename;
154:
155: SlideToken slideToken = new SlideTokenImpl(
156: new CredentialsToken(caller));
157: Structure structure = nat.getStructureHelper();
158: Content content = nat.getContentHelper();
159:
160: try {
161: nat.begin();
162:
163: ObjectNode role = new SubjectNode();
164: structure.create(slideToken, role, roleUri);
165:
166: NodeRevisionDescriptor descriptor = new NodeRevisionDescriptor();
167: descriptor.setCreationDate(new Date());
168: descriptor.setLastModified(new Date());
169:
170: content.create(slideToken, roleUri, descriptor, null);
171:
172: nat.commit();
173: } catch (Exception e) {
174: try {
175: nat.rollback();
176: } catch (Exception f) {
177: f.printStackTrace();
178: }
179: throw e;
180: }
181:
182: }
183:
184: public static void removeObject(NamespaceAccessToken nat,
185: String caller, String objectUri) throws Exception {
186:
187: String usersPath = nat.getNamespaceConfig().getUsersPath();
188: String callerUri = usersPath + "/" + caller;
189:
190: // user cannot delete itself
191: if (callerUri.equals(objectUri)) {
192: return;
193: }
194:
195: SlideToken slideToken = new SlideTokenImpl(
196: new CredentialsToken(caller));
197: Macro macro = nat.getMacroHelper();
198:
199: try {
200: nat.begin();
201:
202: boolean recursive = true;
203: boolean overwrite = false;
204: MacroParameters parameters = new MacroParameters(recursive,
205: overwrite);
206:
207: macro.delete(slideToken, objectUri, parameters);
208:
209: nat.commit();
210: } catch (Exception e) {
211: try {
212: nat.rollback();
213: } catch (Exception f) {
214: f.printStackTrace();
215: }
216: throw e;
217: }
218: }
219:
220: public static void addMember(NamespaceAccessToken nat,
221: String caller, String objectUri, String subjectUri)
222: throws Exception {
223:
224: SlideToken slideToken = new SlideTokenImpl(
225: new CredentialsToken(caller));
226: Structure structure = nat.getStructureHelper();
227: Content content = nat.getContentHelper();
228:
229: try {
230:
231: // check if the subject exists
232: structure.retrieve(slideToken, subjectUri);
233:
234: NodeRevisionDescriptors descriptors = content.retrieve(
235: slideToken, objectUri);
236: NodeRevisionDescriptor descriptor = content.retrieve(
237: slideToken, descriptors);
238: NodeProperty property = descriptor.getProperty(
239: "group-member-set", "DAV:");
240:
241: String value = null;
242: if (property != null) {
243: value = (String) property.getValue();
244: if (value.indexOf(subjectUri) != -1) {
245: // user already a member of this group
246: return;
247: }
248: } else {
249: value = "";
250: }
251: value = value + "<D:href xmlns:D='DAV:'>" + subjectUri
252: + "</D:href>";
253:
254: descriptor.setProperty("group-member-set", "DAV:", value);
255: nat.begin();
256: content.store(slideToken, objectUri, descriptor, null);
257: nat.commit();
258: } catch (ObjectNotFoundException e) {
259: // no such user or group
260: } catch (Exception e) {
261: try {
262: nat.rollback();
263: } catch (Exception f) {
264: f.printStackTrace();
265: }
266: throw e;
267: }
268: }
269:
270: public static void removeMember(NamespaceAccessToken nat,
271: String caller, String objectUri, String subjectUri)
272: throws Exception {
273:
274: SlideToken slideToken = new SlideTokenImpl(
275: new CredentialsToken(caller));
276: Content content = nat.getContentHelper();
277:
278: try {
279:
280: NodeRevisionDescriptors revisions = content.retrieve(
281: slideToken, objectUri);
282: NodeRevisionDescriptor revision = content.retrieve(
283: slideToken, revisions);
284: NodeProperty property = revision.getProperty(
285: "group-member-set", "DAV:");
286:
287: if (property == null) {
288: // group has no members
289: return;
290: }
291: String value = (String) property.getValue();
292:
293: int index = value.indexOf(subjectUri);
294: if (index == -1) {
295: // subject is not a member of this group
296: return;
297: }
298:
299: // looking for the end of </D:href> after subjectUri
300: int end = index + subjectUri.length();
301: do {
302: end++;
303: } while (value.charAt(end) != '>');
304:
305: // looking for the start of <D:href> before subjectUri
306: int from = index;
307: do {
308: from--;
309: } while (value.charAt(from) != '<');
310:
311: // snip out the user
312: String before = value.substring(0, from);
313: String after = value.substring(end + 1);
314: value = before + after;
315:
316: revision.setProperty("group-member-set", "DAV:", value);
317: nat.begin();
318: content.store(slideToken, objectUri, revision, null);
319: nat.commit();
320: } catch (ObjectNotFoundException e) {
321: // no such user or group
322: } catch (Exception e) {
323: try {
324: nat.rollback();
325: } catch (Exception f) {
326: f.printStackTrace();
327: }
328: throw e;
329: }
330: }
331:
332: public static void changePassword(NamespaceAccessToken nat,
333: String caller, String userUri, String password)
334: throws Exception {
335:
336: SlideToken slideToken = new SlideTokenImpl(
337: new CredentialsToken(caller));
338: Content content = nat.getContentHelper();
339:
340: try {
341: nat.begin();
342:
343: NodeRevisionDescriptors revisions = content.retrieve(
344: slideToken, userUri);
345: NodeRevisionDescriptor revision = content.retrieve(
346: slideToken, revisions);
347: revision.setLastModified(new Date());
348: revision.setProperty(new NodeProperty("password", password,
349: NodeProperty.SLIDE_NAMESPACE));
350: content.store(slideToken, userUri, revision, null);
351:
352: nat.commit();
353: } catch (Exception e) {
354: try {
355: nat.rollback();
356: } catch (Exception f) {
357: f.printStackTrace();
358: }
359: throw e;
360: }
361: }
362:
363: public static List listPermissions(NamespaceAccessToken nat,
364: String caller, String path) throws Exception {
365:
366: String uri = getUriFromPath(nat, path);
367:
368: SlideToken slideToken = new SlideTokenImpl(
369: new CredentialsToken(caller));
370: Security security = nat.getSecurityHelper();
371:
372: List result = new ArrayList();
373: try {
374: nat.begin();
375: Enumeration permissions = security.enumeratePermissions(
376: slideToken, uri, false);
377: while (permissions.hasMoreElements()) {
378: result.add(permissions.nextElement());
379: }
380: nat.commit();
381: return result;
382: } catch (Exception e) {
383: try {
384: nat.rollback();
385: } catch (Exception f) {
386: f.printStackTrace();
387: }
388: throw e;
389: }
390: }
391:
392: public static List listLocks(NamespaceAccessToken nat,
393: String caller, String path) throws Exception {
394:
395: String uri = getUriFromPath(nat, path);
396:
397: SlideToken slideToken = new SlideTokenImpl(
398: new CredentialsToken(caller));
399: Lock lock = nat.getLockHelper();
400:
401: List result = new ArrayList();
402: try {
403: nat.begin();
404: Enumeration locks = lock.enumerateLocks(slideToken, uri,
405: false);
406: while (locks.hasMoreElements()) {
407: result.add(locks.nextElement());
408: }
409: nat.commit();
410: return result;
411: } catch (Exception e) {
412: try {
413: nat.rollback();
414: } catch (Exception f) {
415: f.printStackTrace();
416: }
417: throw e;
418: }
419: }
420:
421: public static List listGroups(NamespaceAccessToken nat,
422: String caller, String path) throws Exception {
423: List result = new ArrayList();
424:
425: SlideToken slideToken = new SlideTokenImpl(
426: new CredentialsToken(caller));
427: Structure structure = nat.getStructureHelper();
428: Content content = nat.getContentHelper();
429:
430: ObjectNode object = structure.retrieve(slideToken, path);
431: Enumeration enumeration = structure.getChildren(slideToken,
432: object);
433: while (enumeration.hasMoreElements()) {
434: String uri = ((ObjectNode) enumeration.nextElement())
435: .getUri();
436: NodeRevisionDescriptors revisions = content.retrieve(
437: slideToken, uri);
438: NodeRevisionDescriptor revision = content.retrieve(
439: slideToken, revisions);
440: NodeProperty property = revision.getProperty(
441: "group-member-set", "DAV:");
442: List members;
443: if (property != null) {
444: String value = (String) property.getValue();
445: members = new ArrayList(10);
446: int start = value.indexOf('>'), end = 0;
447: while (start != -1) {
448: end = value.indexOf('<', start);
449: if (end != -1) {
450: members.add(value.substring(start + 1, end));
451: }
452: end = value.indexOf('>', start + 1);
453: start = value.indexOf('>', end + 1);
454: }
455: } else {
456: members = Collections.EMPTY_LIST;
457: }
458: result.add(new Group(uri, members));
459: }
460:
461: return result;
462: }
463:
464: public static List listUsers(NamespaceAccessToken nat, String caller)
465: throws Exception {
466: return listObjects(nat, caller, nat.getNamespaceConfig()
467: .getUsersPath());
468: }
469:
470: public static List listPrivileges(NamespaceAccessToken nat,
471: String caller) throws Exception {
472: return listObjects(nat, caller, nat.getNamespaceConfig()
473: .getActionsPath());
474: }
475:
476: private static List listObjects(NamespaceAccessToken nat,
477: String caller, String path) throws Exception {
478:
479: List result = new ArrayList();
480:
481: SlideToken slideToken = new SlideTokenImpl(
482: new CredentialsToken(caller));
483: Structure structure = nat.getStructureHelper();
484:
485: ObjectNode object = structure.retrieve(slideToken, path);
486: Enumeration enumeration = structure.getChildren(slideToken,
487: object);
488: while (enumeration.hasMoreElements()) {
489: result.add(((ObjectNode) enumeration.nextElement())
490: .getUri());
491: }
492:
493: return result;
494: }
495:
496: public static void removePermission(NamespaceAccessToken nat,
497: String caller, String path, String subject, String action)
498: throws Exception {
499:
500: String uri = getUriFromPath(nat, path);
501:
502: SlideToken slideToken = new SlideTokenImpl(
503: new CredentialsToken(caller));
504: Security security = nat.getSecurityHelper();
505:
506: try {
507: NodePermission permission = new NodePermission(uri,
508: subject, action);
509: nat.begin();
510: security.revokePermission(slideToken, permission);
511: nat.commit();
512: } catch (Exception e) {
513: try {
514: nat.rollback();
515: } catch (Exception f) {
516: f.printStackTrace();
517: }
518: throw e;
519: }
520:
521: }
522:
523: public static void addPermission(NamespaceAccessToken nat,
524: String caller, String path, String subject, String action,
525: String inheritable, String negative) throws Exception {
526:
527: String uri = getUriFromPath(nat, path);
528:
529: SlideToken slideToken = new SlideTokenImpl(
530: new CredentialsToken(caller));
531: Security security = nat.getSecurityHelper();
532:
533: boolean isInheritable = Boolean.valueOf(inheritable)
534: .booleanValue();
535: boolean isNegative = Boolean.valueOf(negative).booleanValue();
536:
537: try {
538: NodePermission permission = new NodePermission(uri,
539: subject, action, isInheritable, isNegative);
540:
541: nat.begin();
542: if (isNegative) {
543: security.denyPermission(slideToken, permission);
544: } else {
545: security.grantPermission(slideToken, permission);
546: }
547: nat.commit();
548: } catch (Exception e) {
549: try {
550: nat.rollback();
551: } catch (Exception f) {
552: f.printStackTrace();
553: }
554: throw e;
555: }
556: }
557:
558: public static void removeLock(NamespaceAccessToken nat,
559: String caller, String uri, String lockId) throws Exception {
560:
561: SlideToken slideToken = new SlideTokenImpl(
562: new CredentialsToken(caller));
563: Lock lock = nat.getLockHelper();
564:
565: try {
566: nat.begin();
567: lock.unlock(slideToken, uri, lockId);
568: nat.commit();
569: } catch (Exception e) {
570: try {
571: nat.rollback();
572: } catch (Exception f) {
573: f.printStackTrace();
574: }
575: throw e;
576: }
577: }
578:
579: public static void addLock(NamespaceAccessToken nat, String caller,
580: String path, String subject, String type,
581: String expiration, String exclusive, String inherit)
582: throws Exception {
583:
584: String uri = getUriFromPath(nat, path);
585: boolean isExclusive = Boolean.valueOf(exclusive).booleanValue();
586: boolean isInherit = Boolean.valueOf(inherit).booleanValue();
587:
588: // expiration in minutes
589: int intExpiration = Integer.valueOf(expiration).intValue();
590: Date expire = new Date(System.currentTimeMillis()
591: + intExpiration * 1000 * 60);
592:
593: SlideToken slideToken = new SlideTokenImpl(
594: new CredentialsToken(caller));
595: Lock lock = nat.getLockHelper();
596:
597: try {
598: nat.begin();
599: lock.lock(slideToken, new NodeLock(uri, subject, type,
600: expire, isInherit, isExclusive, uri));
601: nat.commit();
602: } catch (Exception e) {
603: try {
604: nat.rollback();
605: } catch (Exception f) {
606: f.printStackTrace();
607: }
608: throw e;
609: }
610: }
611:
612: private static String getUriFromPath(NamespaceAccessToken nat,
613: String path) {
614: String filesPath = nat.getNamespaceConfig().getFilesPath();
615: String uri;
616: if (path.equals("/") || path.length() == 0) {
617: uri = filesPath;
618: } else {
619: uri = filesPath + "/" + path;
620: }
621: return uri;
622: }
623:
624: public static class Group {
625: private final String m_uri;
626: private final List m_members;
627:
628: private Group(String uri, List members) {
629: m_uri = uri;
630: m_members = members;
631: }
632:
633: public String getUri() {
634: return m_uri;
635: }
636:
637: public List getMembers() {
638: return m_members;
639: }
640:
641: public String toString() {
642: return m_uri;
643: }
644: }
645: }
|