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.integration.security.role.RedbackRoleConstants;
24 import org.apache.archiva.redback.rbac.RBACManager;
25 import org.apache.archiva.redback.rbac.RbacManagerException;
26 import org.apache.archiva.redback.rbac.UserAssignment;
27 import org.apache.archiva.redback.system.check.EnvironmentCheck;
28 import org.apache.archiva.redback.users.User;
29 import org.apache.archiva.redback.users.UserManager;
30 import org.apache.archiva.redback.users.UserManagerException;
31 import org.apache.archiva.redback.users.UserNotFoundException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.stereotype.Service;
37 import javax.annotation.PostConstruct;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.util.ArrayList;
41 import java.util.List;
44 * @author Olivier Lamy
46 @Service( "environmentCheck#archiva-locked-admin-check" )
47 public class ArchivaLockedAdminEnvironmentCheck
48 implements EnvironmentCheck
51 protected Logger log = LoggerFactory.getLogger( getClass() );
55 @Named( value = "rBACManager#cached" )
56 private RBACManager rbacManager;
59 * boolean detailing if this environment check has been executed
61 private boolean checked = false;
64 private ApplicationContext applicationContext;
67 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
69 private List<UserManager> userManagers;
72 protected void initialize()
73 throws RepositoryAdminException
75 List<String> userManagerImpls =
76 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
78 userManagers = new ArrayList<UserManager>( userManagerImpls.size() );
80 for ( String beanId : userManagerImpls )
82 userManagers.add( applicationContext.getBean( "userManager#" + beanId, UserManager.class ) );
87 * This environment check will unlock system administrator accounts that are locked on the restart of the
88 * application when the environment checks are processed.
92 public void validateEnvironment( List<String> violations )
97 for ( UserManager userManager : userManagers )
99 if ( userManager.isReadOnly() )
103 List<String> roles = new ArrayList<String>();
104 roles.add( RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE );
106 List<UserAssignment> systemAdminstrators;
109 systemAdminstrators = rbacManager.getUserAssignmentsForRoles( roles );
111 for ( UserAssignment userAssignment : systemAdminstrators )
115 User admin = userManager.findUser( userAssignment.getPrincipal() );
117 if ( admin.isLocked() )
119 log.info( "Unlocking system administrator: {}", admin.getUsername() );
120 admin.setLocked( false );
121 userManager.updateUser( admin );
124 catch ( UserNotFoundException ne )
126 log.warn( "Dangling UserAssignment -> {}", userAssignment.getPrincipal() );
128 catch ( UserManagerException e )
130 log.warn( "fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(),
135 catch ( RbacManagerException e )
137 log.warn( "Exception when checking for locked admin user: " + e.getMessage(), e );