1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
41
42 public abstract class AbstractUsersRepository
43 extends AbstractLogEnabled
44 implements Configurable, JamesUsersRepository {
45
46
47
48
49 protected boolean ignoreCase;
50
51
52
53
54 protected boolean enableAliases;
55
56
57
58
59 protected boolean enableForwarding;
60
61
62
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
73
74
75
76
77
78 protected abstract void doAddUser(User user);
79
80
81
82
83
84
85
86 protected abstract void doUpdateUser(User user);
87
88
89
90
91
92
93
94
95
96
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
111
112
113
114
115
116
117
118
119
120
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
135
136
137
138
139
140
141
142 public boolean updateUser(User user) {
143
144 if (!contains(user.getUserName())) {
145 return false;
146 } else {
147 doUpdateUser(user);
148 return true;
149 }
150 }
151
152
153
154
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
196
197 public void setEnableAliases(boolean enableAliases) {
198 this.enableAliases = enableAliases;
199 }
200
201
202
203
204 public void setEnableForwarding(boolean enableForwarding) {
205 this.enableForwarding = enableForwarding;
206 }
207
208
209
210
211 public void setIgnoreCase(boolean ignoreCase) {
212 this.ignoreCase = ignoreCase;
213 }
214
215 }