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