1 package org.apache.archiva.web.security;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
23 import org.apache.archiva.redback.components.cache.Cache;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.archiva.redback.users.UserManager;
26 import org.apache.archiva.redback.users.UserManagerException;
27 import org.apache.archiva.redback.users.UserManagerListener;
28 import org.apache.archiva.redback.users.UserNotFoundException;
29 import org.apache.archiva.redback.users.UserQuery;
30 import org.apache.archiva.redback.users.configurable.ConfigurableUserManager;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.stereotype.Service;
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import java.util.ArrayList;
37 import java.util.LinkedHashMap;
38 import java.util.List;
42 * @author Olivier Lamy
45 @Service("userManager#archiva")
46 public class ArchivaConfigurableUsersManager
47 extends ConfigurableUserManager
51 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
54 private ApplicationContext applicationContext;
56 private Map<String, UserManager> userManagerPerId;
58 private List<UserManagerListener> listeners = new ArrayList<UserManagerListener>();
61 @Named(value = "cache#users")
62 private Cache usersCache;
65 public void initialize()
69 List<String> userManagerImpls =
70 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
71 log.info( "use userManagerImpls: '{}'", userManagerImpls );
73 userManagerPerId = new LinkedHashMap<String, UserManager>( userManagerImpls.size() );
74 for ( String id : userManagerImpls )
76 UserManager userManagerImpl = applicationContext.getBean( "userManager#" + id, UserManager.class );
77 setUserManagerImpl( userManagerImpl );
78 userManagerPerId.put( id, userManagerImpl );
81 catch ( RepositoryAdminException e )
83 // revert to a default one ?
84 log.error( e.getMessage(), e );
85 throw new RuntimeException( e.getMessage(), e );
89 protected boolean useUsersCache()
93 return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
95 catch ( RepositoryAdminException e )
97 log.warn( "skip fail to get RedbackRuntimeConfiguration: {}, use false", e.getMessage(), e );
103 public User addUser( User user )
104 throws UserManagerException
106 user = userManagerPerId.get( user.getUserManagerId() ).addUser( user );
108 if ( useUsersCache() )
110 usersCache.put( user.getUsername(), user );
117 public void addUserUnchecked( User user )
118 throws UserManagerException
120 userManagerPerId.get( user.getUserManagerId() ).addUserUnchecked( user );
122 if ( useUsersCache() )
124 usersCache.put( user.getUsername(), user );
128 protected UserManager findFirstWritable()
130 for ( UserManager userManager : userManagerPerId.values() )
132 if ( !userManager.isReadOnly() )
141 public User createUser( String username, String fullName, String emailAddress )
142 throws UserManagerException
144 UserManager userManager = findFirstWritable();
145 if ( userManager.isReadOnly() )
147 log.warn( "cannot find writable user manager implementation, skip user creation" );
150 User user = userManager.createUser( username, fullName, emailAddress );
151 if ( useUsersCache() )
153 usersCache.put( user.getUsername(), user );
159 public UserQuery createUserQuery()
161 return super.createUserQuery();
166 public void deleteUser( String username )
167 throws UserNotFoundException, UserManagerException
169 UserManager userManager = findFirstWritable();
170 if ( userManager.isReadOnly() )
172 log.warn( "cannot find writable user manager implementation, skip delete user" );
175 userManager.deleteUser( username );
176 if ( useUsersCache() )
178 usersCache.remove( username );
183 public void eraseDatabase()
185 for ( UserManager userManager : userManagerPerId.values() )
187 userManager.eraseDatabase();
192 public User findUser( String username )
193 throws UserManagerException
197 if ( useUsersCache() )
199 user = (User) usersCache.get( username );
206 UserManagerException lastException = null;
207 for ( UserManager userManager : userManagerPerId.values() )
211 user = userManager.findUser( username );
214 if ( useUsersCache() )
216 usersCache.put( username, user );
221 catch ( UserNotFoundException e )
225 catch ( UserManagerException e )
233 if ( lastException != null )
244 public User getGuestUser()
245 throws UserNotFoundException, UserManagerException
247 return findUser( GUEST_USERNAME );
251 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
252 throws UserManagerException
254 List<User> users = new ArrayList<User>();
256 for ( UserManager userManager : userManagerPerId.values() )
258 List<User> found = userManager.findUsersByEmailKey( emailKey, orderAscending );
261 users.addAll( found );
268 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
269 throws UserManagerException
271 List<User> users = new ArrayList<User>();
273 for ( UserManager userManager : userManagerPerId.values() )
275 List<User> found = userManager.findUsersByFullNameKey( fullNameKey, orderAscending );
278 users.addAll( found );
285 public List<User> findUsersByQuery( UserQuery query )
286 throws UserManagerException
288 List<User> users = new ArrayList<User>();
290 for ( UserManager userManager : userManagerPerId.values() )
292 List<User> found = userManager.findUsersByQuery( query );
295 users.addAll( found );
302 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
303 throws UserManagerException
305 List<User> users = new ArrayList<User>();
307 for ( UserManager userManager : userManagerPerId.values() )
309 List<User> found = userManager.findUsersByUsernameKey( usernameKey, orderAscending );
312 users.addAll( found );
319 public String getId()
325 public List<User> getUsers()
326 throws UserManagerException
328 List<User> users = new ArrayList<User>();
330 for ( UserManager userManager : userManagerPerId.values() )
332 List<User> found = userManager.getUsers();
335 users.addAll( found );
342 public List<User> getUsers( boolean orderAscending )
343 throws UserManagerException
345 List<User> users = new ArrayList<User>();
347 for ( UserManager userManager : userManagerPerId.values() )
349 List<User> found = userManager.getUsers( orderAscending );
352 users.addAll( found );
359 public boolean isReadOnly()
361 return findFirstWritable() != null;
365 public User updateUser( User user )
366 throws UserNotFoundException, UserManagerException
368 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user );
370 if ( useUsersCache() )
372 usersCache.put( user.getUsername(), user );
379 public User updateUser( User user, boolean passwordChangeRequired )
380 throws UserNotFoundException, UserManagerException
382 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user, passwordChangeRequired );
384 if ( useUsersCache() )
386 usersCache.put( user.getUsername(), user );
393 public void setUserManagerImpl( UserManager userManagerImpl )
395 // not possible here but we know so no need of log.error
396 log.debug( "setUserManagerImpl cannot be used in this implementation" );
400 public void addUserManagerListener( UserManagerListener listener )
402 this.listeners.add( listener );
406 public void removeUserManagerListener( UserManagerListener listener )
408 this.listeners.remove( listener );
412 protected void fireUserManagerInit( boolean freshDatabase )
414 for ( UserManagerListener listener : listeners )
416 listener.userManagerInit( freshDatabase );
421 protected void fireUserManagerUserAdded( User addedUser )
423 for ( UserManagerListener listener : listeners )
425 listener.userManagerUserAdded( addedUser );
430 protected void fireUserManagerUserRemoved( User removedUser )
432 for ( UserManagerListener listener : listeners )
434 listener.userManagerUserRemoved( removedUser );
439 protected void fireUserManagerUserUpdated( User updatedUser )
441 for ( UserManagerListener listener : listeners )
443 listener.userManagerUserUpdated( updatedUser );
448 public User createGuestUser()
449 throws UserManagerException
451 UserManager userManager = findFirstWritable();
452 if ( userManager.isReadOnly() )
454 log.warn( "cannot find writable user manager implementation, skip guest user creation" );
457 return userManager.createGuestUser();
461 public boolean isFinalImplementation()
466 public String getDescriptionKey()
468 return "archiva.redback.usermanager.configurable.archiva";