001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.user.lib;
021
022 import org.apache.commons.configuration.ConfigurationException;
023 import org.apache.commons.configuration.HierarchicalConfiguration;
024 import org.apache.james.rrt.api.RecipientRewriteTable;
025 import org.apache.james.rrt.api.RecipientRewriteTableException;
026 import org.apache.james.user.api.JamesUsersRepository;
027 import org.apache.james.user.api.UsersRepository;
028 import org.apache.james.user.api.UsersRepositoryException;
029 import org.apache.james.user.api.model.JamesUser;
030 import org.apache.james.user.api.model.User;
031 import org.apache.james.user.lib.model.DefaultJamesUser;
032
033 import java.util.ArrayList;
034 import java.util.Collection;
035 import java.util.HashMap;
036 import java.util.Iterator;
037 import java.util.Map;
038
039 /**
040 * A partial implementation of a Repository to store users.
041 * <p>
042 * This implements common functionality found in different UsersRespository
043 * implementations, and makes it easier to create new User repositories.
044 * </p>
045 *
046 * @deprecated Please implement {@link UsersRepository}
047 */
048 @Deprecated
049 public abstract class AbstractJamesUsersRepository extends AbstractUsersRepository implements JamesUsersRepository, RecipientRewriteTable {
050
051 /**
052 * Ignore case in usernames
053 */
054 protected boolean ignoreCase;
055
056 /**
057 * Enable Aliases frmo JamesUser
058 */
059 protected boolean enableAliases;
060
061 /**
062 * Wether to enable forwarding for JamesUser or not
063 */
064 protected boolean enableForwarding;
065
066 @Override
067 public void configure(HierarchicalConfiguration configuration) throws ConfigurationException {
068 setIgnoreCase(configuration.getBoolean("ignoreCase", false));
069 setEnableAliases(configuration.getBoolean("enableAliases", false));
070 setEnableForwarding(configuration.getBoolean("enableForwarding", false));
071 super.configure(configuration);
072 }
073
074 /**
075 * Adds a user to the underlying Repository. The user name must not clash
076 * with an existing user.
077 *
078 * @param user
079 * the user to add
080 */
081 protected abstract void doAddUser(User user) throws UsersRepositoryException;
082
083 /**
084 * Updates a user record to match the supplied User.
085 *
086 * @param user
087 * the user to update
088 */
089 protected abstract void doUpdateUser(User user) throws UsersRepositoryException;
090
091 /**
092 * @see
093 * org.apache.james.user.lib.AbstractUsersRepository#doAddUser(java.lang.String, java.lang.String)
094 */
095 protected void doAddUser(String username, String password) throws UsersRepositoryException {
096 User newbie = new DefaultJamesUser(username, "SHA");
097 newbie.setPassword(password);
098 doAddUser(newbie);
099 }
100
101 /**
102 * Update the repository with the specified user object. A user object with
103 * this username must already exist.
104 *
105 * @param user
106 * the user to be updated
107 * @throws UsersRepositoryException
108 */
109 public void updateUser(User user) throws UsersRepositoryException {
110 // Return false if it's not found.
111 if (!contains(user.getUserName())) {
112 throw new UsersRepositoryException("User " + user.getUserName() + " does not exist");
113 } else {
114 doUpdateUser(user);
115 }
116 }
117
118 /**
119 * @throws RecipientRewriteTableException
120 * @see org.apache.james.rrt.api.RecipientRewriteTable#getMappings(java.lang.String,
121 * java.lang.String)
122 */
123 public Collection<String> getMappings(String username, String domain) throws ErrorMappingException, RecipientRewriteTableException {
124 Collection<String> mappings = new ArrayList<String>();
125 try {
126 User user = getUserByName(username);
127
128 if (user instanceof JamesUser) {
129 JamesUser jUser = (JamesUser) user;
130
131 if (enableAliases && jUser.getAliasing()) {
132 String alias = jUser.getAlias();
133 if (alias != null) {
134 mappings.add(alias + "@" + domain);
135 }
136 }
137
138 if (enableForwarding && jUser.getForwarding()) {
139 String forward = null;
140 if (jUser.getForwardingDestination() != null && ((forward = jUser.getForwardingDestination().toString()) != null)) {
141 mappings.add(forward);
142 } else {
143 StringBuffer errorBuffer = new StringBuffer(128).append("Forwarding was enabled for ").append(username).append(" but no forwarding address was set for this account.");
144 getLogger().error(errorBuffer.toString());
145 }
146 }
147 }
148 } catch (UsersRepositoryException e) {
149 throw new RecipientRewriteTableException("Unable to lookup forwards/aliases", e);
150 }
151 if (mappings.size() == 0) {
152 return null;
153 } else {
154 return mappings;
155 }
156 }
157
158 /**
159 * @see org.apache.james.user.api.JamesUsersRepository#setEnableAliases(boolean)
160 */
161 public void setEnableAliases(boolean enableAliases) {
162 this.enableAliases = enableAliases;
163 }
164
165 /**
166 * @see org.apache.james.user.api.JamesUsersRepository#setEnableForwarding(boolean)
167 */
168 public void setEnableForwarding(boolean enableForwarding) {
169 this.enableForwarding = enableForwarding;
170 }
171
172 /**
173 * @see org.apache.james.user.api.JamesUsersRepository#setIgnoreCase(boolean)
174 */
175 public void setIgnoreCase(boolean ignoreCase) {
176 this.ignoreCase = ignoreCase;
177 }
178
179 /**
180 * @see org.apache.james.rrt.api.RecipientRewriteTable#getAllMappings()
181 */
182 public Map<String, Collection<String>> getAllMappings() throws RecipientRewriteTableException {
183 Map<String, Collection<String>> mappings = new HashMap<String, Collection<String>>();
184 if (enableAliases == true || enableForwarding == true) {
185 try {
186 Iterator<String> users = list();
187 while (users.hasNext()) {
188 String user = users.next();
189 int index = user.indexOf("@");
190 String username;
191 String domain;
192 if (index != -1) {
193 username = user.substring(0, index);
194 domain = user.substring(index + 1, user.length());
195 } else {
196 username = user;
197 domain = "localhost";
198 }
199 try {
200 mappings.put(user, getMappings(username, domain));
201 } catch (ErrorMappingException e) {
202 // shold never happen here
203 }
204 }
205 } catch (UsersRepositoryException e) {
206 throw new RecipientRewriteTableException("Unable to access forwards/aliases", e);
207 }
208 }
209
210 return mappings;
211 }
212
213 /**
214 * @see
215 * org.apache.james.rrt.api.RecipientRewriteTable#getUserDomainMappings(java.lang.String, java.lang.String)
216 */
217 public Collection<String> getUserDomainMappings(String user, String domain) throws RecipientRewriteTableException {
218 return new ArrayList<String>();
219 }
220
221 public void addRegexMapping(String user, String domain, String regex) throws RecipientRewriteTableException {
222 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
223 }
224
225 public void removeRegexMapping(String user, String domain, String regex) throws RecipientRewriteTableException {
226 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
227
228 }
229
230 public void addAddressMapping(String user, String domain, String address) throws RecipientRewriteTableException {
231 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
232
233 }
234
235 public void removeAddressMapping(String user, String domain, String address) throws RecipientRewriteTableException {
236 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
237
238 }
239
240 public void addErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException {
241 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
242
243 }
244
245 public void removeErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException {
246 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
247
248 }
249
250 public void addMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
251 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
252
253 }
254
255 public void removeMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
256 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
257
258 }
259
260 public void addAliasDomainMapping(String aliasDomain, String realDomain) throws RecipientRewriteTableException {
261 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
262
263 }
264
265 public void removeAliasDomainMapping(String aliasDomain, String realDomain) throws RecipientRewriteTableException {
266 throw new RecipientRewriteTableException("Read-Only RecipientRewriteTable");
267
268 }
269
270 }