]> source.dussan.org Git - archiva.git/blob
2c14b31160c984f83dd3d35f5da1ae3c3cd45f56
[archiva.git] /
1 package org.apache.archiva.redback.rbac.memory;
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.rbac.AbstractRBACManager;
23 import org.apache.archiva.redback.rbac.Operation;
24 import org.apache.archiva.redback.rbac.Permission;
25 import org.apache.archiva.redback.rbac.RBACManager;
26 import org.apache.archiva.redback.rbac.RBACObjectAssertions;
27 import org.apache.archiva.redback.rbac.RbacManagerException;
28 import org.apache.archiva.redback.rbac.RbacObjectInvalidException;
29 import org.apache.archiva.redback.rbac.RbacObjectNotFoundException;
30 import org.apache.archiva.redback.rbac.Resource;
31 import org.apache.archiva.redback.rbac.Role;
32 import org.apache.archiva.redback.rbac.UserAssignment;
33 import org.apache.archiva.redback.rbac.RbacPermanentException;
34 import org.codehaus.plexus.util.StringUtils;
35 import org.springframework.stereotype.Service;
36
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 /**
45  * MemoryRbacManager: a very quick and dirty implementation of a rbac store
46  * <p/>
47  * WARNING: not for actual usage, its not sound - jesse
48  *
49  * @author Jesse McConnell <jmcconnell@apache.org>
50  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
51  * @version $Id$
52  */
53 @Service( "rBACManager#memory" )
54 public class MemoryRbacManager
55     extends AbstractRBACManager
56     implements RBACManager
57 {
58     private Map<String, Role> roles = new HashMap<String, Role>();
59
60     private Map<String, Permission> permissions = new HashMap<String, Permission>();
61
62     private Map<String, Operation> operations = new HashMap<String, Operation>();
63
64     private Map<String, Resource> resources = new HashMap<String, Resource>();
65
66     private Map<String, UserAssignment> userAssignments = new HashMap<String, UserAssignment>();
67
68     // ----------------------------------------------------------------------
69     // Role methods
70     // ----------------------------------------------------------------------
71
72     public Role saveRole( Role role )
73         throws RbacManagerException
74     {
75         RBACObjectAssertions.assertValid( "Save Role", role );
76
77         triggerInit();
78
79         roles.put( role.getName(), role );
80
81         fireRbacRoleSaved( role );
82
83         if ( role.getPermissions() != null )
84         {
85             for ( Permission p : role.getPermissions() )
86             {
87                 savePermission( p );
88             }
89         }
90
91         return role;
92     }
93
94     public void saveRoles( Collection<Role> roles )
95         throws RbacObjectInvalidException, RbacManagerException
96     {
97         if ( roles == null )
98         {
99             // Nothing to do.
100             return;
101         }
102
103         for ( Role role : roles )
104         {
105             saveRole( role );
106         }
107     }
108
109     private void assertRoleExists( String roleName )
110         throws RbacObjectNotFoundException
111     {
112         if ( !roles.containsKey( roleName ) )
113         {
114             throw new RbacObjectNotFoundException( "Role '" + roleName + "' does not exist." );
115         }
116     }
117
118     public Role getRole( String roleName )
119         throws RbacObjectNotFoundException
120     {
121         triggerInit();
122
123         assertRoleExists( roleName );
124
125         return roles.get( roleName );
126     }
127
128     public void removeRole( Role role )
129         throws RbacManagerException, RbacObjectNotFoundException
130     {
131         RBACObjectAssertions.assertValid( "Remove Role", role );
132
133         if ( role.isPermanent() )
134         {
135             throw new RbacPermanentException( "Unable to delete permanent role [" + role.getName() + "]" );
136         }
137
138         assertRoleExists( role.getName() );
139
140         fireRbacRoleRemoved( role );
141
142         roles.remove( role.getName() );
143     }
144
145     public List<Role> getAllRoles()
146         throws RbacManagerException
147     {
148         triggerInit();
149
150         return Collections.unmodifiableList( new ArrayList<Role>( roles.values() ) );
151     }
152
153     // ----------------------------------------------------------------------
154     // Permission methods
155     // ----------------------------------------------------------------------
156
157     public Operation saveOperation( Operation operation )
158         throws RbacManagerException
159     {
160         triggerInit();
161
162         RBACObjectAssertions.assertValid( "Save Operation", operation );
163
164         operations.put( operation.getName(), operation );
165         return operation;
166     }
167
168     public Permission savePermission( Permission permission )
169         throws RbacManagerException
170     {
171         triggerInit();
172
173         RBACObjectAssertions.assertValid( "Save Permission", permission );
174
175         permissions.put( permission.getName(), permission );
176
177         fireRbacPermissionSaved( permission );
178
179         saveOperation( permission.getOperation() );
180         saveResource( permission.getResource() );
181         return permission;
182     }
183
184     public Resource saveResource( Resource resource )
185         throws RbacManagerException
186     {
187         triggerInit();
188
189         RBACObjectAssertions.assertValid( "Save Resource", resource );
190
191         resources.put( resource.getIdentifier(), resource );
192         return resource;
193     }
194
195     public UserAssignment saveUserAssignment( UserAssignment userAssignment )
196         throws RbacManagerException
197     {
198         triggerInit();
199
200         RBACObjectAssertions.assertValid( "Save UserAssignment", userAssignment );
201
202         fireRbacUserAssignmentSaved( userAssignment );
203
204         userAssignments.put( userAssignment.getPrincipal(), userAssignment );
205         return userAssignment;
206     }
207
208     public Operation createOperation( String name )
209         throws RbacManagerException
210     {
211         Operation operation;
212
213         try
214         {
215             operation = getOperation( name );
216         }
217         catch ( RbacObjectNotFoundException e )
218         {
219             operation = new MemoryOperation();
220             operation.setName( name );
221         }
222
223         return operation;
224     }
225
226     public Permission createPermission( String name )
227         throws RbacManagerException
228     {
229         Permission permission;
230
231         try
232         {
233             permission = getPermission( name );
234         }
235         catch ( RbacObjectNotFoundException e )
236         {
237             permission = new MemoryPermission();
238             permission.setName( name );
239         }
240
241         return permission;
242     }
243
244     public Permission createPermission( String name, String operationName, String resourceIdentifier )
245         throws RbacManagerException
246     {
247         Permission permission;
248
249         try
250         {
251             permission = getPermission( name );
252
253             if ( StringUtils.equals( operationName, permission.getOperation().getName() ) )
254             {
255                 throw new RbacManagerException( "Attempted to create a permission named '" + name +
256                                                     "' with an operation named '" + operationName
257                                                     + "', but that overides the existing '" + name +
258                                                     "' permission with operation '"
259                                                     + permission.getOperation().getName() + "'" );
260             }
261
262         }
263         catch ( RbacObjectNotFoundException e )
264         {
265             permission = new MemoryPermission();
266             permission.setName( name );
267
268             permission.setOperation( createOperation( operationName ) );
269             permission.setResource( createResource( resourceIdentifier ) );
270         }
271
272         return permission;
273     }
274
275     public Resource createResource( String identifier )
276         throws RbacManagerException
277     {
278         Resource resource;
279
280         try
281         {
282             resource = getResource( identifier );
283         }
284         catch ( RbacObjectNotFoundException e )
285         {
286             resource = new MemoryResource();
287             resource.setIdentifier( identifier );
288         }
289
290         return resource;
291     }
292
293     public Role createRole( String name )
294     {
295         Role role = new MemoryRole();
296         role.setName( name );
297
298         return role;
299     }
300
301     private void assertPermissionExists( String permissionName )
302         throws RbacObjectNotFoundException
303     {
304         if ( !permissions.containsKey( permissionName ) )
305         {
306             throw new RbacObjectNotFoundException( "Permission '" + permissionName + "' does not exist." );
307         }
308     }
309
310     public Permission getPermission( String permissionName )
311         throws RbacObjectNotFoundException, RbacManagerException
312     {
313         triggerInit();
314
315         assertPermissionExists( permissionName );
316
317         return permissions.get( permissionName );
318     }
319
320     public List<Resource> getResources()
321         throws RbacManagerException
322     {
323         triggerInit();
324
325         return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
326     }
327
328     public void removeOperation( Operation operation )
329         throws RbacObjectNotFoundException, RbacManagerException
330     {
331         RBACObjectAssertions.assertValid( "Remove Operation", operation );
332
333         if ( operation.isPermanent() )
334         {
335             throw new RbacPermanentException( "Unable to delete permanent operation [" + operation.getName() + "]" );
336         }
337
338         assertOpertionExists( operation.getName() );
339
340         operations.remove( operation.getName() );
341     }
342
343     private void assertOpertionExists( String operationName )
344         throws RbacObjectNotFoundException
345     {
346         if ( !operations.containsKey( operationName ) )
347         {
348             throw new RbacObjectNotFoundException( "Operation '" + operationName + "' not found." );
349         }
350     }
351
352     public void removePermission( Permission permission )
353         throws RbacObjectNotFoundException, RbacManagerException
354     {
355         RBACObjectAssertions.assertValid( "Remove Permission", permission );
356
357         if ( permission.isPermanent() )
358         {
359             throw new RbacPermanentException( "Unable to delete permanent permission [" + permission.getName() + "]" );
360         }
361
362         assertPermissionExists( permission.getName() );
363
364         fireRbacPermissionRemoved( permission );
365
366         permissions.remove( permission.getName() );
367     }
368
369     public void removeResource( Resource resource )
370         throws RbacObjectNotFoundException, RbacManagerException
371     {
372         RBACObjectAssertions.assertValid( "Remove Resource", resource );
373
374         if ( resource.isPermanent() )
375         {
376             throw new RbacPermanentException(
377                 "Unable to delete permanent resource [" + resource.getIdentifier() + "]" );
378         }
379
380         assertResourceExists( resource.getIdentifier() );
381
382         resources.remove( resource.getIdentifier() );
383     }
384
385     private void assertResourceExists( String resourceIdentifier )
386         throws RbacObjectNotFoundException
387     {
388         if ( !resources.containsKey( resourceIdentifier ) )
389         {
390             throw new RbacObjectNotFoundException( "Resource '" + resourceIdentifier + "' not found." );
391         }
392     }
393
394     private void assertUserAssignmentExists( String principal )
395         throws RbacObjectNotFoundException
396     {
397         if ( !userAssignments.containsKey( principal ) )
398         {
399             throw new RbacObjectNotFoundException( "UserAssignment '" + principal + "' not found." );
400         }
401     }
402
403     public void removeUserAssignment( UserAssignment userAssignment )
404         throws RbacObjectNotFoundException, RbacManagerException
405     {
406         RBACObjectAssertions.assertValid( "Remove User Assignment", userAssignment );
407
408         if ( userAssignment.isPermanent() )
409         {
410             throw new RbacPermanentException(
411                 "Unable to delete permanent user assignment [" + userAssignment.getPrincipal() + "]" );
412         }
413
414         fireRbacUserAssignmentRemoved( userAssignment );
415
416         assertUserAssignmentExists( userAssignment.getPrincipal() );
417
418         userAssignments.remove( userAssignment.getPrincipal() );
419     }
420
421     public void eraseDatabase()
422     {
423         userAssignments.clear();
424         resources.clear();
425         operations.clear();
426         permissions.clear();
427         roles.clear();
428     }
429
430     public UserAssignment createUserAssignment( String principal )
431         throws RbacManagerException
432     {
433         try
434         {
435             return getUserAssignment( principal );
436         }
437         catch ( RbacObjectNotFoundException e )
438         {
439             UserAssignment ua = new MemoryUserAssignment();
440             ua.setPrincipal( principal );
441
442             fireRbacUserAssignmentSaved( ua );
443
444             return ua;
445         }
446     }
447
448     public List<Operation> getAllOperations()
449         throws RbacManagerException
450     {
451         triggerInit();
452
453         return Collections.unmodifiableList( new ArrayList<Operation>( operations.values() ) );
454     }
455
456     public List<Permission> getAllPermissions()
457         throws RbacManagerException
458     {
459         triggerInit();
460
461         return Collections.unmodifiableList( new ArrayList<Permission>( permissions.values() ) );
462     }
463
464     public List<Resource> getAllResources()
465         throws RbacManagerException
466     {
467         triggerInit();
468
469         return Collections.unmodifiableList( new ArrayList<Resource>( resources.values() ) );
470     }
471
472     public List<UserAssignment> getAllUserAssignments()
473         throws RbacManagerException
474     {
475         triggerInit();
476
477         return Collections.unmodifiableList( new ArrayList<UserAssignment>( userAssignments.values() ) );
478     }
479
480     public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
481         throws RbacManagerException
482     {
483
484         List<UserAssignment> allUserAssignments = getAllUserAssignments();
485         List<UserAssignment> userAssignments = new ArrayList<UserAssignment>( allUserAssignments.size() );
486
487         for ( UserAssignment ua : allUserAssignments )
488         {
489             for ( String roleName : roleNames )
490             {
491                 if ( ua.getRoleNames().contains( roleName ) )
492                 {
493                     userAssignments.add( ua );
494                     break;
495                 }
496             }
497         }
498
499         return userAssignments;
500     }
501
502     public UserAssignment getUserAssignment( String principal )
503         throws RbacObjectNotFoundException, RbacManagerException
504     {
505         triggerInit();
506
507         assertUserAssignmentExists( principal );
508
509         return userAssignments.get( principal );
510     }
511
512     public Operation getOperation( String operationName )
513         throws RbacObjectNotFoundException, RbacManagerException
514     {
515         triggerInit();
516
517         assertOpertionExists( operationName );
518
519         return operations.get( operationName );
520     }
521
522     public Resource getResource( String resourceIdentifier )
523         throws RbacObjectNotFoundException, RbacManagerException
524     {
525         triggerInit();
526
527         assertResourceExists( resourceIdentifier );
528
529         return resources.get( resourceIdentifier );
530     }
531
532     private boolean hasTriggeredInit = false;
533
534     public void triggerInit()
535     {
536         if ( !hasTriggeredInit )
537         {
538             fireRbacInit( roles.isEmpty() );
539             hasTriggeredInit = true;
540         }
541     }
542 }