This project has retired. For details please refer to its Attic page.
GuestUserEnvironmentCheck xref
View Javadoc

1   package org.apache.archiva.redback.integration.checks.security;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.configuration.UserConfiguration;
23  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
24  import org.apache.archiva.redback.policy.UserSecurityPolicy;
25  import org.apache.archiva.redback.role.RoleManagerException;
26  import org.apache.archiva.redback.users.User;
27  import org.apache.archiva.redback.users.UserManagerException;
28  import org.apache.archiva.redback.users.UserNotFoundException;
29  import org.apache.archiva.redback.role.RoleManager;
30  import org.apache.archiva.redback.system.SecuritySystem;
31  import org.apache.archiva.redback.system.check.EnvironmentCheck;
32  import org.apache.archiva.redback.users.UserManager;
33  import org.springframework.stereotype.Service;
34  
35  import javax.inject.Inject;
36  import javax.inject.Named;
37  import java.util.List;
38  
39  /**
40   * RequiredRolesEnvironmentCheck:
41   *
42   * @author: Jesse McConnell <jesse@codehaus.org>
43   */
44  @Service("environmentCheck#guest-user-check")
45  public class GuestUserEnvironmentCheck
46      implements EnvironmentCheck
47  {
48  
49      @Inject
50      private RoleManager roleManager;
51  
52      @Inject
53      private SecuritySystem securitySystem;
54  
55      @Inject
56      @Named(value = "userConfiguration#default")
57      private UserConfiguration config;
58  
59      /**
60       * boolean detailing if this environment check has been executed
61       */
62      private boolean checked = false;
63  
64      /**
65       * @param violations
66       */
67      public void validateEnvironment( List<String> violations )
68      {
69          if ( !checked )
70          {
71              UserManager userManager = securitySystem.getUserManager();
72              UserSecurityPolicy policy = securitySystem.getPolicy();
73  
74              User guest = null;
75              try
76              {
77                  guest = userManager.getGuestUser();
78              }
79              catch ( UserManagerException e )
80              {
81                  policy.setEnabled( false );
82                  try
83                  {
84                      guest = userManager.createGuestUser();
85                  }
86                  catch ( UserManagerException ume )
87                  {
88                      violations.add( "unable to initialize guest user properly: " + ume.getMessage() );
89                      checked = true;
90                      return;
91                  }
92                  policy.setEnabled( true );
93              }
94  
95              if ( guest != null )
96              {
97  
98                  try
99                  {
100                     roleManager.assignRole( config.getString( UserConfigurationKeys.DEFAULT_GUEST_ROLE_ID, "guest" ),
101                                             guest.getUsername() );
102                 }
103                 catch ( RoleManagerException rpe )
104                 {
105                     violations.add( "unable to initialize guest user properly: " + rpe.getMessage() );
106                 }
107             }
108             else
109             {
110                 violations.add( "cannot find neither create guest user" );
111             }
112             checked = true;
113         }
114     }
115 }