001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.users.filesystem;
014:
015: import java.util.ArrayList;
016: import java.util.Collection;
017: import java.util.HashSet;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Set;
021:
022: import com.openedit.users.Group;
023: import com.openedit.users.User;
024: import com.openedit.users.UserManagerException;
025: import com.openedit.util.StringEncryption;
026:
027: /**
028: * This class represents a user as an XML file.
029: *
030: * @author Eric and Matt
031: */
032: public class FileSystemUser extends FileSystemObject implements User {
033: protected Set fieldGroups;
034: protected String fieldPassword;
035: protected String fieldUserName;
036: protected boolean fieldVirtual;
037:
038: public FileSystemUser() {
039: super ();
040: }
041:
042: public String getEmail() {
043: return getString(EMAIL_PROPERTY);
044: }
045:
046: public String getFirstName() {
047: return getString(FIRST_NAME_PROPERTY);
048: }
049:
050: public void setFirstName(String inName) {
051: safePut(FIRST_NAME_PROPERTY, inName);
052: }
053:
054: /**
055: * @see com.openedit.users.User#getGroups()
056: */
057: public Collection getGroups() {
058: if (fieldGroups == null) {
059: fieldGroups = new HashSet(3);
060: }
061: return fieldGroups;
062: }
063:
064: public String getLastName() {
065: return getString(LAST_NAME_PROPERTY);
066: }
067:
068: public void setLastName(String inName) {
069: safePut(LAST_NAME_PROPERTY, inName);
070: }
071:
072: public void setEmail(String inEmail) {
073: safePut(EMAIL_PROPERTY, inEmail);
074: }
075:
076: /**
077: * @see com.openedit.users.User#setPassword(String)
078: */
079: public void setPassword(String inPassword)
080: throws UserManagerException {
081: fieldPassword = inPassword;
082: }
083:
084: /**
085: * @see com.openedit.users.User#getUserName()
086: */
087: public String getUserName() {
088: return fieldUserName;
089: }
090:
091: public void setUserName(String inName) {
092: fieldUserName = inName;
093: }
094:
095: public String getShortDescription() {
096: StringBuffer out = new StringBuffer();
097: if (getFirstName() != null) {
098: out.append(getFirstName());
099: out.append(" ");
100: }
101: if (getLastName() != null) {
102: out.append(getLastName());
103: }
104: if (out.length() == 0) {
105: if (getEmail() != null
106: && Character.isDigit(getUserName().charAt(0))) {
107: out.append(getEmail());
108: } else {
109: out.append(getUserName());
110: }
111: }
112: return out.toString();
113: }
114:
115: // public String getClearPassword() throws UserManagerException
116: // {
117: // String password = getPassword();
118: // if( !password.startsWith("DES:") )
119: // {
120: // return password;
121: // }
122: // else
123: // {
124: // return decrypt(password);
125: // }
126: // }
127:
128: // protected String decrypt(String inPassword) throws UserManagerException
129: // {
130: // long encryptionKey = 7939805759879765L; //TODO: Move this to authenticator
131: // encryptionKey++;
132: // try
133: // {
134: // StringEncryption encrypter = new StringEncryption( StringEncryption.DES_ENCRYPTION_SCHEME, encryptionKey + "42" + encryptionKey );
135: // String code = inPassword.substring(4,inPassword.length()); //take off the DES:
136: // String decryptedString = encrypter.decrypt( code );
137: // return decryptedString;
138: // } catch ( Exception ex)
139: // {
140: // throw new UserManagerException(ex);
141: // }
142: // }
143:
144: /**
145: * @see com.openedit.users.User#hasPermission(String)
146: */
147: public boolean hasPermission(String inPermission) {
148: for (Iterator iter = getGroups().iterator(); iter.hasNext();) {
149: Group group = (Group) iter.next();
150:
151: if (group.hasPermission(inPermission)) {
152: return true;
153: }
154: }
155:
156: //cburkey, seems like users may need custom permissions so I added this
157: String ok = getPropertyContainer().getString(inPermission);
158:
159: if (Boolean.parseBoolean(ok)) {
160: return true;
161: }
162:
163: return false;
164: }
165:
166: public Object getProperty(String inPropertyName) {
167: Object value = getPropertyContainer().get(inPropertyName);
168: if (value == null && fieldGroups != null) //this might be a new user
169: {
170: for (Iterator iterator = getGroups().iterator(); iterator
171: .hasNext();) {
172: Group group = (Group) iterator.next();
173: value = group.get(inPropertyName);
174: if (value != null) {
175: return value;
176: }
177: }
178: }
179: return value;
180: }
181:
182: public Object get(String inPropertyName) {
183: return getProperty(inPropertyName);
184: }
185:
186: public boolean hasProperty(String inName) {
187: boolean has = getProperties().containsKey(inName);
188: return has;
189: }
190:
191: public List listGroupPermissions() {
192: List all = new ArrayList();
193: for (Iterator iter = getGroups().iterator(); iter.hasNext();) {
194: Group group = (Group) iter.next();
195: all.addAll(group.getPermissions());
196: }
197: return all;
198: }
199:
200: /**
201: * Returns the password.
202: *
203: * @return String
204: */
205: public String getPassword() {
206: return fieldPassword;
207: }
208:
209: /**
210: * Add the given group to the list of groups to which this user belongs. If the given group is
211: * already in the list of groups, this method does nothing.
212: *
213: * @param inGroup The group to which to add this user
214: *
215: * @throws UserManagerException DOCUMENT ME!
216: */
217: public void addGroup(Group inGroup) {
218: getGroups().add(inGroup);
219: }
220:
221: /**
222: * Remove the given group from the list of groups to which this user belongs. If the given
223: * group is not in the list of groups, this method does nothing.
224: *
225: * @param inGroup The group from which to remove this user
226: *
227: * @throws UserManagerException DOCUMENT ME!
228: */
229: public void removeGroup(Group inGroup) {
230: getGroups().remove(inGroup);
231: }
232:
233: public String toString() {
234: return getShortDescription();
235: }
236:
237: public void clearGroups() {
238: if (fieldGroups != null) {
239: getGroups().clear();
240: }
241: }
242:
243: public boolean isInGroup(Group inGroup) {
244: return getGroups().contains(inGroup);
245: }
246:
247: public boolean isVirtual() {
248: return fieldVirtual;
249: }
250:
251: public void setVirtual(boolean inVirtual) {
252: fieldVirtual = inVirtual;
253: }
254: }
|