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.UserNotFoundException;
29 import org.apache.archiva.redback.users.UserQuery;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.stereotype.Service;
33 import javax.annotation.PostConstruct;
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 AbstractUserManager
51 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
54 private ApplicationContext applicationContext;
56 private Map<String, UserManager> userManagerPerId;
59 @Named(value = "cache#users")
60 private Cache<String, User> usersCache;
62 private boolean useUsersCache;
66 public void initialize()
70 List<String> userManagerImpls =
71 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
72 log.info( "use userManagerImpls: '{}'", userManagerImpls );
74 userManagerPerId = new LinkedHashMap<>( userManagerImpls.size() );
75 for ( String id : userManagerImpls )
77 UserManager userManagerImpl = applicationContext.getBean( "userManager#" + id, UserManager.class );
78 setUserManagerImpl( userManagerImpl );
79 userManagerPerId.put( id, userManagerImpl );
82 this.useUsersCache = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
84 catch ( RepositoryAdminException e )
86 // revert to a default one ?
87 log.error( e.getMessage(), e );
88 throw new RuntimeException( e.getMessage(), e );
92 protected boolean useUsersCache()
94 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 );
112 public void addUserUnchecked( User user )
113 throws UserManagerException
115 userManagerPerId.get( user.getUserManagerId() ).addUserUnchecked( user );
117 if ( useUsersCache() )
119 usersCache.put( user.getUsername(), user );
124 public User createUser( String username, String fullName, String emailAddress )
125 throws UserManagerException
127 Exception lastException = null;
128 boolean allFailed = true;
130 for ( UserManager userManager : userManagerPerId.values() )
134 if ( !userManager.isReadOnly() )
136 user = userManager.createUser( username, fullName, emailAddress );
140 catch ( Exception e )
145 if ( lastException != null && allFailed )
147 throw new UserManagerException( lastException.getMessage(), lastException );
153 public UserQuery createUserQuery()
155 return userManagerPerId.values().iterator().next().createUserQuery();
160 public void deleteUser( String username )
161 throws UserNotFoundException, UserManagerException
163 Exception lastException = null;
164 boolean allFailed = true;
166 for ( UserManager userManager : userManagerPerId.values() )
170 if ( !userManager.isReadOnly() )
172 userManager.deleteUser( username );
176 catch ( Exception e )
181 if ( lastException != null && allFailed )
183 throw new UserManagerException( lastException.getMessage(), lastException );
188 public void eraseDatabase()
190 for ( UserManager userManager : userManagerPerId.values() )
192 userManager.eraseDatabase();
197 public User findUser( String username, boolean useCache )
198 throws UserNotFoundException, UserManagerException
201 if ( useUsersCache() && useCache )
203 user = usersCache.get( username );
210 Exception lastException = null;
211 for ( UserManager userManager : userManagerPerId.values() )
215 user = userManager.findUser( username );
218 if ( useUsersCache() )
220 usersCache.put( username, user );
225 catch ( UserNotFoundException e )
229 catch ( Exception e )
237 if ( lastException != null )
239 if ( lastException instanceof UserNotFoundException )
241 throw (UserNotFoundException) lastException;
243 throw new UserManagerException( lastException.getMessage(), lastException );
251 public User findUser( String username )
252 throws UserManagerException
254 return findUser( username, useUsersCache() );
259 public User getGuestUser()
260 throws UserNotFoundException, UserManagerException
262 return findUser( GUEST_USERNAME );
266 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
267 throws UserManagerException
269 List<User> users = new ArrayList<>();
271 for ( UserManager userManager : userManagerPerId.values() )
273 List<User> found = userManager.findUsersByEmailKey( emailKey, orderAscending );
276 users.addAll( found );
283 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
284 throws UserManagerException
286 List<User> users = new ArrayList<>();
288 for ( UserManager userManager : userManagerPerId.values() )
290 List<User> found = userManager.findUsersByFullNameKey( fullNameKey, orderAscending );
293 users.addAll( found );
300 public List<User> findUsersByQuery( UserQuery query )
301 throws UserManagerException
303 List<User> users = new ArrayList<>();
305 for ( UserManager userManager : userManagerPerId.values() )
307 List<User> found = userManager.findUsersByQuery( query );
310 users.addAll( found );
317 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
318 throws UserManagerException
320 List<User> users = new ArrayList<>();
322 for ( UserManager userManager : userManagerPerId.values() )
324 List<User> found = userManager.findUsersByUsernameKey( usernameKey, orderAscending );
327 users.addAll( found );
334 public String getId()
340 public List<User> getUsers()
341 throws UserManagerException
343 List<User> users = new ArrayList<>();
345 for ( UserManager userManager : userManagerPerId.values() )
347 List<User> found = userManager.getUsers();
350 users.addAll( found );
357 public List<User> getUsers( boolean orderAscending )
358 throws UserManagerException
360 List<User> users = new ArrayList<>();
362 for ( UserManager userManager : userManagerPerId.values() )
364 List<User> found = userManager.getUsers( orderAscending );
367 users.addAll( found );
374 public boolean isReadOnly()
376 boolean readOnly = false;
378 for ( UserManager userManager : userManagerPerId.values() )
380 readOnly = readOnly || userManager.isReadOnly();
386 public User updateUser( User user )
387 throws UserNotFoundException, UserManagerException
390 UserManager userManager = userManagerPerId.get( user.getUserManagerId() );
392 user = userManager.updateUser( user );
394 if ( useUsersCache() )
396 usersCache.put( user.getUsername(), user );
403 public User updateUser( User user, boolean passwordChangeRequired )
404 throws UserNotFoundException, UserManagerException
406 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user, passwordChangeRequired );
408 if ( useUsersCache() )
410 usersCache.put( user.getUsername(), user );
416 public void setUserManagerImpl( UserManager userManagerImpl )
418 // not possible here but we know so no need of log.error
419 log.debug( "setUserManagerImpl cannot be used in this implementation" );
423 public User createGuestUser()
424 throws UserManagerException
426 Exception lastException = null;
427 boolean allFailed = true;
429 for ( UserManager userManager : userManagerPerId.values() )
433 if ( !userManager.isReadOnly() )
435 user = userManager.createGuestUser();
439 catch ( Exception e )
444 if ( lastException != null && allFailed )
446 throw new UserManagerException( lastException.getMessage(), lastException );
453 public boolean userExists( String userName )
454 throws UserManagerException
456 Exception lastException = null;
457 boolean allFailed = true;
458 boolean exists = false;
459 for ( UserManager userManager : userManagerPerId.values() )
464 if ( userManager.userExists( userName ) )
471 catch ( Exception e )
476 if ( lastException != null && allFailed )
478 throw new UserManagerException( lastException.getMessage(), lastException );
485 public boolean isFinalImplementation()
491 public String getDescriptionKey()
493 return "archiva.redback.usermanager.configurable.archiva";