1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21
22 package org.apache.james.impl.jamesuser;
23
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.james.api.user.JamesUser;
29 import org.apache.james.api.user.User;
30 import org.apache.james.api.vut.ErrorMappingException;
31 import org.apache.james.impl.user.DefaultUser;
32
33 import java.util.ArrayList;
34 import java.util.Collection;
35
36 /**
37 * A partial implementation of a Repository to store users.
38 * <p>This implements common functionality found in different UsersRespository
39 * implementations, and makes it easier to create new User repositories.</p>
40 *
41 */
42 public abstract class AbstractUsersRepository
43 extends AbstractLogEnabled
44 implements Configurable, JamesUsersRepository {
45
46 /**
47 * Ignore case in usernames
48 */
49 protected boolean ignoreCase;
50
51 /**
52 * Enable Aliases frmo JamesUser
53 */
54 protected boolean enableAliases;
55
56 /**
57 * Wether to enable forwarding for JamesUser or not
58 */
59 protected boolean enableForwarding;
60
61 /**
62 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
63 */
64 public void configure(Configuration configuration)
65 throws ConfigurationException {
66 setIgnoreCase(configuration.getChild("usernames").getValueAsBoolean(false));
67 setEnableAliases(configuration.getChild("enableAliases").getValueAsBoolean(false));
68 setEnableForwarding(configuration.getChild("enableForwarding").getValueAsBoolean(false));
69 }
70
71 /**
72 * Adds a user to the underlying Repository. The user name must not clash
73 * with an existing user.
74 *
75 * @param user
76 * the user to add
77 */
78 protected abstract void doAddUser(User user);
79
80 /**
81 * Updates a user record to match the supplied User.
82 *
83 * @param user
84 * the user to update
85 */
86 protected abstract void doUpdateUser(User user);
87
88
89 /**
90 * Adds a user to the repository with the specified attributes. In current
91 * implementations, the Object attributes is generally a String password.
92 *
93 * @param name
94 * the name of the user to be added
95 * @param attributes
96 * the password value as a String
97 */
98 public void addUser(String name, Object attributes) {
99 if (attributes instanceof String) {
100 User newbie = new DefaultUser(name, "SHA");
101 newbie.setPassword((String) attributes);
102 addUser(newbie);
103 } else {
104 throw new RuntimeException("Improper use of deprecated method"
105 + " - use addUser(User user)");
106 }
107 }
108
109 //
110 // UsersRepository interface implementation.
111 //
112 /**
113 * Adds a user to the repository with the specified User object. Users names
114 * must be unique-case-insensitive in the repository.
115 *
116 * @param user
117 * the user to be added
118 *
119 * @return true if succesful, false otherwise
120 * @since James 1.2.2
121 */
122 public boolean addUser(User user) {
123 String username = user.getUserName();
124
125 if (contains(username)) {
126 return false;
127 }
128
129 doAddUser(user);
130 return true;
131 }
132
133 /**
134 * Update the repository with the specified user object. A user object with
135 * this username must already exist.
136 *
137 * @param user
138 * the user to be updated
139 *
140 * @return true if successful.
141 */
142 public boolean updateUser(User user) {
143 // Return false if it's not found.
144 if (!contains(user.getUserName())) {
145 return false;
146 } else {
147 doUpdateUser(user);
148 return true;
149 }
150 }
151
152 /**
153 * @see org.apache.james.api.vut.VirtualUserTable#getMappings(java.lang.String,
154 * java.lang.String)
155 */
156 public Collection getMappings(String username, String domain)
157 throws ErrorMappingException {
158 Collection mappings = new ArrayList();
159 User user = getUserByName(username);
160
161 if (user instanceof JamesUser) {
162 JamesUser jUser = (JamesUser) user;
163
164 if (enableAliases && jUser.getAliasing()) {
165 String alias = jUser.getAlias();
166 if (alias != null) {
167 mappings.add(alias + "@" + domain);
168 }
169 }
170
171 if (enableForwarding && jUser.getForwarding()) {
172 String forward = null;
173 if (jUser.getForwardingDestination() != null
174 && ((forward = jUser.getForwardingDestination()
175 .toString()) != null)) {
176 mappings.add(forward);
177 } else {
178 StringBuffer errorBuffer = new StringBuffer(128)
179 .append("Forwarding was enabled for ")
180 .append(username)
181 .append(
182 " but no forwarding address was set for this account.");
183 getLogger().error(errorBuffer.toString());
184 }
185 }
186 }
187 if (mappings.size() == 0) {
188 return null;
189 } else {
190 return mappings;
191 }
192 }
193
194 /**
195 * @see org.apache.james.impl.jamesuser.JamesUsersRepository#setEnableAliases(boolean)
196 */
197 public void setEnableAliases(boolean enableAliases) {
198 this.enableAliases = enableAliases;
199 }
200
201 /**
202 * @see org.apache.james.impl.jamesuser.JamesUsersRepository#setEnableForwarding(boolean)
203 */
204 public void setEnableForwarding(boolean enableForwarding) {
205 this.enableForwarding = enableForwarding;
206 }
207
208 /**
209 * @see org.apache.james.impl.jamesuser.JamesUsersRepository#setIgnoreCase(boolean)
210 */
211 public void setIgnoreCase(boolean ignoreCase) {
212 this.ignoreCase = ignoreCase;
213 }
214
215 }