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<>( 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.
93 public void validateEnvironment( List<String> violations )
98 for ( UserManager userManager : userManagers )
100 if ( userManager.isReadOnly() )
104 List<String> roles = new ArrayList<>();
105 roles.add( RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE );
107 List<UserAssignment> systemAdminstrators;
110 systemAdminstrators = rbacManager.getUserAssignmentsForRoles( roles );
112 for ( UserAssignment userAssignment : systemAdminstrators )
116 User admin = userManager.findUser( userAssignment.getPrincipal() );
118 if ( admin.isLocked() )
120 log.info( "Unlocking system administrator: {}", admin.getUsername() );
121 admin.setLocked( false );
122 userManager.updateUser( admin );
125 catch ( UserNotFoundException ne )
127 log.warn( "Dangling UserAssignment -> {}", userAssignment.getPrincipal() );
129 catch ( UserManagerException e )
131 log.warn( "fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(),
136 catch ( RbacManagerException e )
138 log.warn( "Exception when checking for locked admin user: {}", e.getMessage(), e );