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.AbstractUserManager;
25 import org.apache.archiva.redback.users.User;
26 import org.apache.archiva.redback.users.UserManager;
27 import org.apache.archiva.redback.users.UserManagerException;
28 import org.apache.archiva.redback.users.UserManagerListener;
29 import org.apache.archiva.redback.users.UserNotFoundException;
30 import org.apache.archiva.redback.users.UserQuery;
31 import org.apache.archiva.redback.users.configurable.ConfigurableUserManager;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.stereotype.Service;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
44 * @author Olivier Lamy
47 @Service( "userManager#archiva" )
48 public class ArchivaConfigurableUsersManager
49 extends AbstractUserManager
53 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
56 private ApplicationContext applicationContext;
58 private Map<String, UserManager> userManagerPerId;
61 @Named( value = "cache#users" )
62 private Cache<String, User> usersCache;
64 private boolean useUsersCache;
67 public void initialize()
71 List<String> userManagerImpls =
72 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
73 log.info( "use userManagerImpls: '{}'", userManagerImpls );
75 userManagerPerId = new LinkedHashMap<String, UserManager>( userManagerImpls.size() );
76 for ( String id : userManagerImpls )
78 UserManager userManagerImpl = applicationContext.getBean( "userManager#" + id, UserManager.class );
79 setUserManagerImpl( userManagerImpl );
80 userManagerPerId.put( id, userManagerImpl );
83 this.useUsersCache = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
85 catch ( RepositoryAdminException e )
87 // revert to a default one ?
88 log.error( e.getMessage(), e );
89 throw new RuntimeException( e.getMessage(), e );
93 protected boolean useUsersCache()
95 return this.useUsersCache;
98 public User addUser( User user )
99 throws UserManagerException
101 user = userManagerPerId.get( user.getUserManagerId() ).addUser( user );
103 if ( useUsersCache() )
105 usersCache.put( user.getUsername(), user );
111 public void addUserUnchecked( User user )
112 throws UserManagerException
114 userManagerPerId.get( user.getUserManagerId() ).addUserUnchecked( user );
116 if ( useUsersCache() )
118 usersCache.put( user.getUsername(), user );
122 public User createUser( String username, String fullName, String emailAddress )
123 throws UserManagerException
125 Exception lastException = null;
126 boolean allFailed = true;
128 for ( UserManager userManager : userManagerPerId.values() )
132 if ( !userManager.isReadOnly() )
134 user = userManager.createUser( username, fullName, emailAddress );
138 catch ( Exception e )
143 if ( lastException != null && allFailed )
145 throw new UserManagerException( lastException.getMessage(), lastException );
150 public UserQuery createUserQuery()
152 return userManagerPerId.values().iterator().next().createUserQuery();
156 public void deleteUser( String username )
157 throws UserNotFoundException, UserManagerException
159 Exception lastException = null;
160 boolean allFailed = true;
162 for ( UserManager userManager : userManagerPerId.values() )
166 if ( !userManager.isReadOnly() )
168 userManager.deleteUser( username );
172 catch ( Exception e )
177 if ( lastException != null && allFailed )
179 throw new UserManagerException( lastException.getMessage(), lastException );
183 public void eraseDatabase()
185 for ( UserManager userManager : userManagerPerId.values() )
187 userManager.eraseDatabase();
191 public User findUser( String username )
192 throws UserManagerException
196 if ( useUsersCache() )
198 user = usersCache.get( username );
205 Exception lastException = null;
206 for ( UserManager userManager : userManagerPerId.values() )
210 user = userManager.findUser( username );
213 if ( useUsersCache() )
215 usersCache.put( username, user );
220 catch ( UserNotFoundException e )
224 catch ( Exception e )
232 if ( lastException != null )
234 throw new UserManagerException( lastException.getMessage(), lastException );
243 public User getGuestUser()
244 throws UserNotFoundException, UserManagerException
246 return findUser( GUEST_USERNAME );
249 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
250 throws UserManagerException
252 List<User> users = new ArrayList<User>();
254 for ( UserManager userManager : userManagerPerId.values() )
256 List<User> found = userManager.findUsersByEmailKey( emailKey, orderAscending );
259 users.addAll( found );
265 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
266 throws UserManagerException
268 List<User> users = new ArrayList<User>();
270 for ( UserManager userManager : userManagerPerId.values() )
272 List<User> found = userManager.findUsersByFullNameKey( fullNameKey, orderAscending );
275 users.addAll( found );
281 public List<User> findUsersByQuery( UserQuery query )
282 throws UserManagerException
284 List<User> users = new ArrayList<User>();
286 for ( UserManager userManager : userManagerPerId.values() )
288 List<User> found = userManager.findUsersByQuery( query );
291 users.addAll( found );
297 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
298 throws UserManagerException
300 List<User> users = new ArrayList<User>();
302 for ( UserManager userManager : userManagerPerId.values() )
304 List<User> found = userManager.findUsersByUsernameKey( usernameKey, orderAscending );
307 users.addAll( found );
313 public String getId()
318 public List<User> getUsers()
319 throws UserManagerException
321 List<User> users = new ArrayList<User>();
323 for ( UserManager userManager : userManagerPerId.values() )
325 List<User> found = userManager.getUsers();
328 users.addAll( found );
334 public List<User> getUsers( boolean orderAscending )
335 throws UserManagerException
337 List<User> users = new ArrayList<User>();
339 for ( UserManager userManager : userManagerPerId.values() )
341 List<User> found = userManager.getUsers( orderAscending );
344 users.addAll( found );
350 public boolean isReadOnly()
352 boolean readOnly = false;
354 for ( UserManager userManager : userManagerPerId.values() )
356 readOnly = readOnly || userManager.isReadOnly();
361 public User updateUser( User user )
362 throws UserNotFoundException, UserManagerException
364 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user );
366 if ( useUsersCache() )
368 usersCache.put( user.getUsername(), user );
374 public User updateUser( User user, boolean passwordChangeRequired )
375 throws UserNotFoundException, UserManagerException
377 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user, passwordChangeRequired );
379 if ( useUsersCache() )
381 usersCache.put( user.getUsername(), user );
387 public void setUserManagerImpl( UserManager userManagerImpl )
389 // not possible here but we know so no need of log.error
390 log.debug( "setUserManagerImpl cannot be used in this implementation" );
394 public User createGuestUser()
395 throws UserManagerException
397 Exception lastException = null;
398 boolean allFailed = true;
400 for ( UserManager userManager : userManagerPerId.values() )
404 if ( !userManager.isReadOnly() )
406 user = userManager.createGuestUser();
410 catch ( Exception e )
415 if ( lastException != null && allFailed )
417 throw new UserManagerException( lastException.getMessage(), lastException );
423 public boolean userExists( String userName )
424 throws UserManagerException
426 Exception lastException = null;
427 boolean allFailed = true;
428 boolean exists = false;
429 for ( UserManager userManager : userManagerPerId.values() )
434 if ( userManager.userExists( userName ) )
441 catch ( Exception e )
446 if ( lastException != null && allFailed )
448 throw new UserManagerException( lastException.getMessage(), lastException );
456 public boolean isFinalImplementation()
461 public String getDescriptionKey()
463 return "archiva.redback.usermanager.configurable.archiva";