]> source.dussan.org Git - archiva.git/blob
cd599fa516ee39fd4d16adcb38eb35d8abf75374
[archiva.git] /
1 package org.apache.archiva.web.security;
2 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
23 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
24 import org.apache.archiva.redback.authorization.AuthorizationDataSource;
25 import org.apache.archiva.redback.authorization.AuthorizationException;
26 import org.apache.archiva.redback.authorization.AuthorizationResult;
27 import org.apache.archiva.redback.authorization.Authorizer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.stereotype.Service;
32
33 import javax.inject.Inject;
34 import javax.inject.Named;
35
36 /**
37  * @author Olivier Lamy
38  * @since 1.4-M4
39  */
40 @Service( "authorizer#archiva" )
41 public class ArchivaAuthorizer
42     implements Authorizer
43 {
44     private Logger log = LoggerFactory.getLogger( getClass() );
45
46     @Inject
47     private ApplicationContext applicationContext;
48
49     @Inject
50     private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
51
52     public String getId()
53     {
54         return "archiva";
55     }
56
57     public AuthorizationResult isAuthorized( AuthorizationDataSource source )
58         throws AuthorizationException
59     {
60         log.debug( "isAuthorized source: {}", source );
61
62         try
63         {
64             RedbackRuntimeConfiguration redbackRuntimeConfiguration =
65                 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
66
67             AuthorizationException authorizationException = null;
68
69             AuthorizationResult lastResult = null;
70
71             for ( String id : redbackRuntimeConfiguration.getAuthorizerImpls() )
72             {
73                 Authorizer authorizer = getAuthorizer( id );
74
75                 AuthorizationResult result = null;
76                 try
77                 {
78                     result = authorizer.isAuthorized( source );
79                     log.debug( "AuthorizationResult {} with id '{}", result, id );
80                 }
81                 catch ( AuthorizationException e )
82                 {
83                     log.debug( "AuthorizationException {} with id '{}", e.getMessage(), id );
84                     authorizationException = e;
85                 }
86
87                 if ( result.isAuthorized() )
88                 {
89                     return result;
90                 }
91
92                 lastResult = result;
93             }
94             if ( authorizationException != null )
95             {
96                 throw authorizationException;
97             }
98             return lastResult;
99         }
100         catch ( RepositoryAdminException e )
101         {
102             throw new AuthorizationException( e.getMessage(), e );
103         }
104
105     }
106
107     private Authorizer getAuthorizer( String id )
108     {
109         return applicationContext.getBean( "authorizer#" + id, Authorizer.class );
110     }
111
112     public boolean isFinalImplementation()
113     {
114         return false;
115     }
116
117     public String getDescriptionKey()
118     {
119         return "archiva.redback.authorizer.archiva";
120     }
121 }