1 package org.apache.archiva.redback.authorization.rbac;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.redback.authorization.AuthorizationDataSource;
23 import org.apache.archiva.redback.authorization.AuthorizationException;
24 import org.apache.archiva.redback.authorization.AuthorizationResult;
25 import org.apache.archiva.redback.authorization.Authorizer;
26 import org.apache.archiva.redback.authorization.NotAuthorizedException;
27 import org.apache.archiva.redback.authorization.rbac.evaluator.PermissionEvaluationException;
28 import org.apache.archiva.redback.authorization.rbac.evaluator.PermissionEvaluator;
29 import org.apache.archiva.redback.rbac.Permission;
30 import org.apache.archiva.redback.rbac.RBACManager;
31 import org.apache.archiva.redback.rbac.RbacManagerException;
32 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
33 import org.apache.archiva.redback.users.User;
34 import org.apache.archiva.redback.users.UserManager;
35 import org.apache.archiva.redback.users.UserManagerException;
36 import org.apache.archiva.redback.users.UserNotFoundException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Service;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import java.util.List;
49 * @author Jesse McConnell <jmcconnell@apache.org>
51 @Service("authorizer#rbac")
52 public class RbacAuthorizer
55 private Logger log = LoggerFactory.getLogger( getClass() );
58 @Named(value = "rBACManager#cached")
59 private RBACManager manager;
62 @Named(value = "userManager#configurable")
63 private UserManager userManager;
66 private PermissionEvaluator evaluator;
70 return "RBAC Authorizer - " + this.getClass().getName();
76 * @throws AuthorizationException
78 public AuthorizationResult isAuthorized( AuthorizationDataSource source )
79 throws AuthorizationException
81 Object principal = source.getPrincipal();
82 Object operation = source.getPermission();
83 Object resource = source.getResource();
87 if ( principal != null )
89 // Set permissions = manager.getAssignedPermissions( principal.toString(), operation );
90 Map<String, List<Permission>> permissionMap = manager.getAssignedPermissionMap( principal.toString() );
92 if ( permissionMap.keySet().contains( operation.toString() ) )
94 for ( Permission permission : permissionMap.get( operation.toString() ) )
97 log.debug( "checking permission {} for operation {} resource {}",
98 ( permission != null ? permission.getName() : "null" ), operation, resource );
100 if ( evaluator.evaluate( permission, operation, resource, principal ) )
102 return new AuthorizationResult( true, permission, null );
106 log.debug( "no permission found for operation {} resource {}", operation.toString(), resource );
110 log.debug( "permission map does not contain operation: {}", operation.toString() );
113 // check if guest user is enabled, if so check the global permissions
114 User guest = userManager.getGuestUser();
116 if ( !guest.isLocked() )
118 // Set permissions = manager.getAssignedPermissions( principal.toString(), operation );
119 Map<String, List<Permission>> permissionMap = manager.getAssignedPermissionMap( guest.getUsername() );
121 if ( permissionMap.keySet().contains( operation.toString() ) )
123 for ( Permission permission : permissionMap.get( operation.toString() ) )
125 log.debug( "checking permission {}", permission.getName() );
127 if ( evaluator.evaluate( permission, operation, resource, guest.getUsername() ) )
129 return new AuthorizationResult( true, permission, null );
135 return new AuthorizationResult( false, null, new NotAuthorizedException( "no matching permissions" ) );
137 catch ( PermissionEvaluationException pe )
139 return new AuthorizationResult( false, null, pe );
141 catch ( RbacObjectNotFoundException nfe )
143 return new AuthorizationResult( false, null, nfe );
145 catch ( UserNotFoundException ne )
147 return new AuthorizationResult( false, null,
148 new NotAuthorizedException( "no matching permissions, guest not found" ) );
150 catch ( RbacManagerException rme )
152 return new AuthorizationResult( false, null, rme );
154 catch ( UserManagerException e )
156 return new AuthorizationResult( false, null, e );
160 public RBACManager getManager()
165 public void setManager( RBACManager manager )
167 this.manager = manager;
170 public UserManager getUserManager()
175 public void setUserManager( UserManager userManager )
177 this.userManager = userManager;
180 public PermissionEvaluator getEvaluator()
185 public void setEvaluator( PermissionEvaluator evaluator )
187 this.evaluator = evaluator;