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.apache.commons.lang3.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.stereotype.Service;
38 import javax.annotation.PostConstruct;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.util.ArrayList;
42 import java.util.List;
45 * @author Olivier Lamy
47 @Service( "environmentCheck#archiva-locked-admin-check" )
48 public class ArchivaLockedAdminEnvironmentCheck
49 implements EnvironmentCheck
52 protected Logger log = LoggerFactory.getLogger( getClass() );
56 @Named( value = "rbacManager#cached" )
57 private RBACManager rbacManager;
60 * boolean detailing if this environment check has been executed
62 private boolean checked = false;
65 private ApplicationContext applicationContext;
68 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
70 private List<UserManager> userManagers;
73 protected void initialize()
74 throws RepositoryAdminException
76 List<String> userManagerImpls =
77 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
79 List<String> updated = new ArrayList<>( );
80 userManagers = new ArrayList<>( userManagerImpls.size() );
82 for ( String beanId : userManagerImpls )
84 // for migration purpose to help users
85 if ( StringUtils.equalsIgnoreCase( beanId, "jdo" ))
87 log.info( "jdo is not anymore supported we auto update to jpa" );
90 updated.add( beanId );
91 userManagers.add( applicationContext.getBean( "userManager#" + beanId, UserManager.class ) );
93 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().setUserManagerImpls( updated );
97 * This environment check will unlock system administrator accounts that are locked on the restart of the
98 * application when the environment checks are processed.
103 public void validateEnvironment( List<String> violations )
108 for ( UserManager userManager : userManagers )
110 if ( userManager.isReadOnly() )
114 List<String> roles = new ArrayList<>();
115 roles.add( RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE );
117 List<? extends UserAssignment> systemAdminstrators;
120 systemAdminstrators = rbacManager.getUserAssignmentsForRoles( roles );
122 for ( UserAssignment userAssignment : systemAdminstrators )
126 User admin = userManager.findUser( userAssignment.getPrincipal() );
128 if ( admin.isLocked() )
130 log.info( "Unlocking system administrator: {}", admin.getUsername() );
131 admin.setLocked( false );
132 userManager.updateUser( admin );
135 catch ( UserNotFoundException ne )
137 log.warn( "Dangling UserAssignment -> {}", userAssignment.getPrincipal() );
139 catch ( UserManagerException e )
141 log.warn( "fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(),
146 catch ( RbacManagerException e )
148 log.warn( "Exception when checking for locked admin user: {}", e.getMessage(), e );