001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.xml;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.KernelConstants;
038: import org.libresource.kernel.interfaces.KernelService;
039:
040: import org.libresource.membership.MembershipConstants;
041: import org.libresource.membership.interfaces.MembershipService;
042:
043: import java.net.URI;
044:
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.Vector;
048:
049: public class DefaultSecurityHandler implements SecurityImportHandler {
050: private KernelService kernelService;
051: private MembershipService membershipService;
052: private ImportExportLogger logger;
053: private Vector userAcls;
054: private Vector groupAcls;
055: private Vector userMemberInGroups;
056: private Vector groupMemberInGroups;
057: private boolean createProfile;
058:
059: public DefaultSecurityHandler(boolean createProfile)
060: throws Exception {
061: kernelService = (KernelService) Libresource
062: .getService(KernelConstants.SERVICE);
063: membershipService = (MembershipService) Libresource
064: .getService(MembershipConstants.SERVICE);
065: this .createProfile = createProfile;
066: }
067:
068: public void createProfile(String id, String name, String email,
069: String jabber) throws Exception {
070: if (createProfile) {
071: URI user = new URI(membershipService.getUsersRootURI()
072: + "/" + id);
073:
074: if (!kernelService.exist(user)) {
075: try {
076: membershipService.createProfile(id, name, email,
077: jabber, new HashMap());
078: logger.log(ImportExportLogger.NOTICE, "The user \""
079: + user + "\" has been created.");
080: } catch (Exception e) {
081: logger.log(ImportExportLogger.ERROR,
082: "Error in user \"" + user + "\" creation ("
083: + e.getMessage() + ").");
084: }
085: } else {
086: logger.log(ImportExportLogger.NOTICE, "The user \""
087: + user + "\" already exists...");
088: }
089: }
090: }
091:
092: public void handleUserAcl(URI forUri, URI aclOwner,
093: String[] permissions) throws Exception {
094: userAcls.add(new Object[] { forUri, aclOwner, permissions });
095: }
096:
097: public void handleGroupAcl(URI forUri, URI aclOwner,
098: String[] permissions) throws Exception {
099: groupAcls.add(new Object[] { forUri, aclOwner, permissions });
100: }
101:
102: public void handleUserMemberGroup(URI groupUri, URI member)
103: throws Exception {
104: userMemberInGroups.add(new Object[] { groupUri, member });
105: }
106:
107: public void handleGroupMemberGroup(URI groupUri, URI member)
108: throws Exception {
109: groupMemberInGroups.add(new Object[] { groupUri, member });
110: }
111:
112: public void destroy() throws Exception {
113: logger.log(ImportExportLogger.MESSAGE, "Setting up security :");
114:
115: // acls
116: logger.log(ImportExportLogger.APPEND, " + Creating acls ");
117:
118: // groups acls
119: for (Iterator i = groupAcls.iterator(); i.hasNext();) {
120: Object[] acl = (Object[]) i.next();
121: URI forUri = (URI) acl[0];
122: URI owner = (URI) acl[1];
123: String[] permissions = (String[]) acl[2];
124:
125: if (kernelService.exist(owner)) {
126: kernelService.createAcl(forUri, owner, permissions);
127: logger.log(ImportExportLogger.APPEND, ".");
128: }
129: }
130:
131: groupAcls = null;
132:
133: // users acls if createProfile
134: if (createProfile) {
135: for (Iterator i = userAcls.iterator(); i.hasNext();) {
136: Object[] acl = (Object[]) i.next();
137: URI forUri = (URI) acl[0];
138: URI owner = (URI) acl[1];
139: String[] permissions = (String[]) acl[2];
140:
141: if (kernelService.exist(owner)) {
142: kernelService.createAcl(forUri, owner, permissions);
143: logger.log(ImportExportLogger.APPEND, ".");
144: }
145: }
146: }
147:
148: userAcls = null;
149: logger.log(ImportExportLogger.APPEND, " done.<br/>");
150:
151: // members in groups
152: logger.log(ImportExportLogger.APPEND,
153: " + Add members in groups ");
154:
155: // member is group
156: for (Iterator i = groupMemberInGroups.iterator(); i.hasNext();) {
157: Object[] group = (Object[]) i.next();
158: URI groupUri = (URI) group[0];
159: URI member = (URI) group[1];
160:
161: if (kernelService.exist(member)) {
162: logger.log(ImportExportLogger.APPEND, ".");
163: membershipService.addMemberInGroup(groupUri, member);
164: }
165: }
166:
167: groupMemberInGroups = null;
168:
169: // member is user if createProfile
170: if (createProfile) {
171: for (Iterator i = userMemberInGroups.iterator(); i
172: .hasNext();) {
173: Object[] group = (Object[]) i.next();
174: URI groupUri = (URI) group[0];
175: URI member = (URI) group[1];
176:
177: if (kernelService.exist(member)) {
178: logger.log(ImportExportLogger.APPEND, ".");
179: membershipService
180: .addMemberInGroup(groupUri, member);
181: }
182: }
183: }
184:
185: userMemberInGroups = null;
186: logger.log(ImportExportLogger.APPEND, " done.<br/>");
187: }
188:
189: public void init(ImportExportLogger logger) throws Exception {
190: this .logger = logger;
191: userAcls = new Vector();
192: groupAcls = new Vector();
193: userMemberInGroups = new Vector();
194: groupMemberInGroups = new Vector();
195: }
196: }
|