Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ArchivaRbacManager.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
  22. import org.apache.archiva.redback.components.cache.Cache;
  23. import org.apache.archiva.redback.rbac.AbstractRBACManager;
  24. import org.apache.archiva.redback.rbac.Operation;
  25. import org.apache.archiva.redback.rbac.Permission;
  26. import org.apache.archiva.redback.rbac.RBACManager;
  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.springframework.context.ApplicationContext;
  34. import org.springframework.stereotype.Service;
  35. import javax.inject.Inject;
  36. import javax.inject.Named;
  37. import java.util.ArrayList;
  38. import java.util.Collection;
  39. import java.util.HashMap;
  40. import java.util.LinkedHashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. /**
  45. * @author Olivier Lamy
  46. * @since 1.4-M4
  47. */
  48. @Service( "rbacManager#archiva" )
  49. public class ArchivaRbacManager
  50. extends AbstractRBACManager
  51. implements RBACManager
  52. {
  53. private Map<String, RBACManager> rbacManagersPerId;
  54. @Inject
  55. private ApplicationContext applicationContext;
  56. @Inject
  57. private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
  58. @Inject
  59. @Named( value = "cache#operations" )
  60. private Cache<String, Operation> operationsCache;
  61. @Inject
  62. @Named( value = "cache#permissions" )
  63. private Cache<String, Permission> permissionsCache;
  64. @Inject
  65. @Named( value = "cache#resources" )
  66. private Cache<String, Resource> resourcesCache;
  67. @Inject
  68. @Named( value = "cache#roles" )
  69. private Cache<String, Role> rolesCache;
  70. @Inject
  71. @Named( value = "cache#userAssignments" )
  72. private Cache<String, UserAssignment> userAssignmentsCache;
  73. @Inject
  74. @Named( value = "cache#userPermissions" )
  75. private Cache<String, Map<String, List<Permission>>> userPermissionsCache;
  76. @Inject
  77. @Named( value = "cache#effectiveRoleSet" )
  78. private Cache<String, Set<Role>> effectiveRoleSetCache;
  79. @Override
  80. public void initialize()
  81. {
  82. try
  83. {
  84. List<String> rbacManagerIds =
  85. redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getRbacManagerImpls();
  86. log.info( "use rbacManagerIds: '{}'", rbacManagerIds );
  87. this.rbacManagersPerId = new LinkedHashMap<String, RBACManager>( rbacManagerIds.size() );
  88. for ( String id : rbacManagerIds )
  89. {
  90. RBACManager rbacManager = applicationContext.getBean( "rbacManager#" + id, RBACManager.class );
  91. rbacManagersPerId.put( id, rbacManager );
  92. }
  93. }
  94. catch ( RepositoryAdminException e )
  95. {
  96. // revert to a default one ?
  97. log.error( e.getMessage(), e );
  98. throw new RuntimeException( e.getMessage(), e );
  99. }
  100. }
  101. protected RBACManager getRbacManagerForWrite()
  102. {
  103. for ( RBACManager rbacManager : this.rbacManagersPerId.values() )
  104. {
  105. if ( !rbacManager.isReadOnly() )
  106. {
  107. return rbacManager;
  108. }
  109. }
  110. return this.rbacManagersPerId.values().iterator().next();
  111. }
  112. public Role createRole( String name )
  113. {
  114. return getRbacManagerForWrite().createRole( name );
  115. }
  116. public Role saveRole( Role role )
  117. throws RbacObjectInvalidException, RbacManagerException
  118. {
  119. Exception lastException = null;
  120. boolean allFailed = true;
  121. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  122. {
  123. try
  124. {
  125. if ( !rbacManager.isReadOnly() )
  126. {
  127. role = rbacManager.saveRole( role );
  128. allFailed = false;
  129. }
  130. }
  131. catch ( Exception e )
  132. {
  133. lastException = e;
  134. }
  135. }
  136. if ( lastException != null && allFailed )
  137. {
  138. throw new RbacManagerException( lastException.getMessage(), lastException );
  139. }
  140. return role;
  141. }
  142. public void saveRoles( Collection<Role> roles )
  143. throws RbacObjectInvalidException, RbacManagerException
  144. {
  145. Exception lastException = null;
  146. boolean allFailed = true;
  147. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  148. {
  149. try
  150. {
  151. if ( !rbacManager.isReadOnly() )
  152. {
  153. rbacManager.saveRoles( roles );
  154. allFailed = false;
  155. }
  156. }
  157. catch ( Exception e )
  158. {
  159. lastException = e;
  160. }
  161. }
  162. if ( lastException != null && allFailed )
  163. {
  164. throw new RbacManagerException( lastException.getMessage(), lastException );
  165. }
  166. }
  167. public Role getRole( String roleName )
  168. throws RbacObjectNotFoundException, RbacManagerException
  169. {
  170. Role el = rolesCache.get( roleName );
  171. if ( el != null )
  172. {
  173. return el;
  174. }
  175. Exception lastException = null;
  176. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  177. {
  178. try
  179. {
  180. Role role = rbacManager.getRole( roleName );
  181. if ( role != null )
  182. {
  183. rolesCache.put( role.getName(), role );
  184. return role;
  185. }
  186. }
  187. catch ( Exception e )
  188. {
  189. lastException = e;
  190. }
  191. }
  192. log.debug( "cannot find role for name: ‘{}", roleName );
  193. if ( lastException != null )
  194. {
  195. throw new RbacManagerException( lastException.getMessage(), lastException );
  196. }
  197. return null;
  198. }
  199. public List<Role> getAllRoles()
  200. throws RbacManagerException
  201. {
  202. Map<String, Role> allRoles = new HashMap<String, Role>();
  203. boolean allFailed = true;
  204. Exception lastException = null;
  205. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  206. {
  207. try
  208. {
  209. List<Role> roles = rbacManager.getAllRoles();
  210. for ( Role role : roles )
  211. {
  212. allRoles.put( role.getName(), role );
  213. }
  214. allFailed = false;
  215. }
  216. catch ( Exception e )
  217. {
  218. lastException = e;
  219. }
  220. }
  221. if ( lastException != null && allFailed )
  222. {
  223. throw new RbacManagerException( lastException.getMessage(), lastException );
  224. }
  225. return new ArrayList<>( allRoles.values() );
  226. }
  227. public void removeRole( Role role )
  228. throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
  229. {
  230. boolean allFailed = true;
  231. Exception lastException = null;
  232. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  233. {
  234. try
  235. {
  236. rbacManager.removeRole( role );
  237. rolesCache.remove( role.getName() );
  238. allFailed = false;
  239. }
  240. catch ( Exception e )
  241. {
  242. lastException = e;
  243. }
  244. }
  245. if ( lastException != null && allFailed )
  246. {
  247. throw new RbacManagerException( lastException.getMessage(), lastException );
  248. }
  249. }
  250. public Permission createPermission( String name )
  251. throws RbacManagerException
  252. {
  253. return getRbacManagerForWrite().createPermission( name );
  254. }
  255. public Permission createPermission( String name, String operationName, String resourceIdentifier )
  256. throws RbacManagerException
  257. {
  258. return getRbacManagerForWrite().createPermission( name, operationName, resourceIdentifier );
  259. }
  260. public Permission savePermission( Permission permission )
  261. throws RbacObjectInvalidException, RbacManagerException
  262. {
  263. boolean allFailed = true;
  264. Exception lastException = null;
  265. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  266. {
  267. try
  268. {
  269. if ( rbacManager.isReadOnly() )
  270. {
  271. permission = rbacManager.savePermission( permission );
  272. allFailed = false;
  273. }
  274. }
  275. catch ( Exception e )
  276. {
  277. lastException = e;
  278. }
  279. }
  280. if ( lastException != null && allFailed )
  281. {
  282. throw new RbacManagerException( lastException.getMessage(), lastException );
  283. }
  284. return permission;
  285. }
  286. public Permission getPermission( String permissionName )
  287. throws RbacObjectNotFoundException, RbacManagerException
  288. {
  289. Permission el = permissionsCache.get( permissionName );
  290. if ( el != null )
  291. {
  292. return el;
  293. }
  294. Exception lastException = null;
  295. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  296. {
  297. try
  298. {
  299. Permission p = rbacManager.getPermission( permissionName );
  300. if ( p != null )
  301. {
  302. permissionsCache.put( permissionName, p );
  303. return p;
  304. }
  305. }
  306. catch ( Exception e )
  307. {
  308. lastException = e;
  309. }
  310. }
  311. if ( lastException != null )
  312. {
  313. throw new RbacManagerException( lastException.getMessage(), lastException );
  314. }
  315. return null;
  316. }
  317. public List<Permission> getAllPermissions()
  318. throws RbacManagerException
  319. {
  320. Map<String, Permission> allPermissions = new HashMap<String, Permission>();
  321. boolean allFailed = true;
  322. Exception lastException = null;
  323. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  324. {
  325. try
  326. {
  327. List<Permission> permissions = rbacManager.getAllPermissions();
  328. for ( Permission p : permissions )
  329. {
  330. allPermissions.put( p.getName(), p );
  331. }
  332. allFailed = false;
  333. }
  334. catch ( Exception e )
  335. {
  336. lastException = e;
  337. }
  338. }
  339. if ( lastException != null && allFailed )
  340. {
  341. throw new RbacManagerException( lastException.getMessage(), lastException );
  342. }
  343. return new ArrayList<>( allPermissions.values() );
  344. }
  345. public void removePermission( Permission permission )
  346. throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
  347. {
  348. boolean allFailed = true;
  349. Exception lastException = null;
  350. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  351. {
  352. try
  353. {
  354. rbacManager.removePermission( permission );
  355. permissionsCache.remove( permission.getName() );
  356. allFailed = false;
  357. }
  358. catch ( Exception e )
  359. {
  360. lastException = e;
  361. }
  362. }
  363. if ( lastException != null && allFailed )
  364. {
  365. throw new RbacManagerException( lastException.getMessage(), lastException );
  366. }
  367. }
  368. public Operation createOperation( String name )
  369. throws RbacManagerException
  370. {
  371. return getRbacManagerForWrite().createOperation( name );
  372. }
  373. public Operation saveOperation( Operation operation )
  374. throws RbacObjectInvalidException, RbacManagerException
  375. {
  376. boolean allFailed = true;
  377. Exception lastException = null;
  378. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  379. {
  380. try
  381. {
  382. if ( !rbacManager.isReadOnly() )
  383. {
  384. operation = rbacManager.saveOperation( operation );
  385. allFailed = false;
  386. }
  387. }
  388. catch ( Exception e )
  389. {
  390. lastException = e;
  391. }
  392. }
  393. if ( lastException != null && allFailed )
  394. {
  395. throw new RbacManagerException( lastException.getMessage(), lastException );
  396. }
  397. return operation;
  398. }
  399. public Operation getOperation( String operationName )
  400. throws RbacObjectNotFoundException, RbacManagerException
  401. {
  402. Operation el = operationsCache.get( operationName );
  403. if ( el != null )
  404. {
  405. return el;
  406. }
  407. Exception lastException = null;
  408. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  409. {
  410. try
  411. {
  412. Operation o = rbacManager.getOperation( operationName );
  413. if ( o != null )
  414. {
  415. operationsCache.put( operationName, o );
  416. return o;
  417. }
  418. }
  419. catch ( Exception e )
  420. {
  421. lastException = e;
  422. }
  423. }
  424. if ( lastException != null )
  425. {
  426. throw new RbacManagerException( lastException.getMessage(), lastException );
  427. }
  428. return null;
  429. }
  430. public List<Operation> getAllOperations()
  431. throws RbacManagerException
  432. {
  433. Map<String, Operation> allOperations = new HashMap<String, Operation>();
  434. boolean allFailed = true;
  435. Exception lastException = null;
  436. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  437. {
  438. try
  439. {
  440. List<Operation> operations = rbacManager.getAllOperations();
  441. for ( Operation o : operations )
  442. {
  443. allOperations.put( o.getName(), o );
  444. }
  445. allFailed = false;
  446. }
  447. catch ( Exception e )
  448. {
  449. lastException = e;
  450. }
  451. }
  452. if ( lastException != null && allFailed )
  453. {
  454. throw new RbacManagerException( lastException.getMessage(), lastException );
  455. }
  456. return new ArrayList<>( allOperations.values() );
  457. }
  458. public void removeOperation( Operation operation )
  459. throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
  460. {
  461. boolean allFailed = true;
  462. Exception lastException = null;
  463. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  464. {
  465. try
  466. {
  467. rbacManager.removeOperation( operation );
  468. operationsCache.remove( operation.getName() );
  469. allFailed = false;
  470. }
  471. catch ( Exception e )
  472. {
  473. lastException = e;
  474. }
  475. }
  476. if ( lastException != null && allFailed )
  477. {
  478. throw new RbacManagerException( lastException.getMessage(), lastException );
  479. }
  480. }
  481. public Resource createResource( String identifier )
  482. throws RbacManagerException
  483. {
  484. return getRbacManagerForWrite().createResource( identifier );
  485. }
  486. public Resource saveResource( Resource resource )
  487. throws RbacObjectInvalidException, RbacManagerException
  488. {
  489. boolean allFailed = true;
  490. Exception lastException = null;
  491. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  492. {
  493. try
  494. {
  495. if ( !rbacManager.isReadOnly() )
  496. {
  497. resource = rbacManager.saveResource( resource );
  498. allFailed = false;
  499. }
  500. }
  501. catch ( Exception e )
  502. {
  503. lastException = e;
  504. }
  505. }
  506. if ( lastException != null && allFailed )
  507. {
  508. throw new RbacManagerException( lastException.getMessage(), lastException );
  509. }
  510. return resource;
  511. }
  512. public Resource getResource( String resourceIdentifier )
  513. throws RbacObjectNotFoundException, RbacManagerException
  514. {
  515. Resource el = resourcesCache.get( resourceIdentifier );
  516. if ( el != null )
  517. {
  518. return el;
  519. }
  520. Exception lastException = null;
  521. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  522. {
  523. try
  524. {
  525. Resource r = rbacManager.getResource( resourceIdentifier );
  526. if ( r != null )
  527. {
  528. resourcesCache.put( resourceIdentifier, r );
  529. return r;
  530. }
  531. }
  532. catch ( Exception e )
  533. {
  534. lastException = e;
  535. }
  536. }
  537. if ( lastException != null )
  538. {
  539. throw new RbacManagerException( lastException.getMessage(), lastException );
  540. }
  541. return null;
  542. }
  543. public List<Resource> getAllResources()
  544. throws RbacManagerException
  545. {
  546. Map<String, Resource> allResources = new HashMap<String, Resource>();
  547. boolean allFailed = true;
  548. Exception lastException = null;
  549. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  550. {
  551. try
  552. {
  553. List<Resource> resources = rbacManager.getAllResources();
  554. for ( Resource r : resources )
  555. {
  556. allResources.put( r.getIdentifier(), r );
  557. }
  558. allFailed = false;
  559. }
  560. catch ( Exception e )
  561. {
  562. lastException = e;
  563. }
  564. }
  565. if ( lastException != null && allFailed )
  566. {
  567. throw new RbacManagerException( lastException.getMessage(), lastException );
  568. }
  569. return new ArrayList<>( allResources.values() );
  570. }
  571. public void removeResource( Resource resource )
  572. throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
  573. {
  574. boolean allFailed = true;
  575. Exception lastException = null;
  576. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  577. {
  578. try
  579. {
  580. rbacManager.removeResource( resource );
  581. resourcesCache.remove( resource.getIdentifier() );
  582. allFailed = false;
  583. }
  584. catch ( Exception e )
  585. {
  586. lastException = e;
  587. }
  588. }
  589. if ( lastException != null && allFailed )
  590. {
  591. throw new RbacManagerException( lastException.getMessage(), lastException );
  592. }
  593. }
  594. public UserAssignment createUserAssignment( String principal )
  595. throws RbacManagerException
  596. {
  597. return getRbacManagerForWrite().createUserAssignment( principal );
  598. }
  599. public UserAssignment saveUserAssignment( UserAssignment userAssignment )
  600. throws RbacObjectInvalidException, RbacManagerException
  601. {
  602. boolean allFailed = true;
  603. Exception lastException = null;
  604. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  605. {
  606. try
  607. {
  608. if ( !rbacManager.isReadOnly() )
  609. {
  610. userAssignment = rbacManager.saveUserAssignment( userAssignment );
  611. allFailed = false;
  612. }
  613. }
  614. catch ( Exception e )
  615. {
  616. lastException = e;
  617. }
  618. }
  619. if ( lastException != null && allFailed )
  620. {
  621. throw new RbacManagerException( lastException.getMessage(), lastException );
  622. }
  623. return userAssignment;
  624. }
  625. public UserAssignment getUserAssignment( String principal )
  626. throws RbacObjectNotFoundException, RbacManagerException
  627. {
  628. UserAssignment el = userAssignmentsCache.get( principal );
  629. if ( el != null )
  630. {
  631. return el;
  632. }
  633. UserAssignment ua = null;
  634. Exception lastException = null;
  635. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  636. {
  637. try
  638. {
  639. if ( ua == null )
  640. {
  641. ua = rbacManager.getUserAssignment( principal );
  642. }
  643. else
  644. {
  645. UserAssignment userAssignment = rbacManager.getUserAssignment( principal );
  646. if ( userAssignment != null )
  647. {
  648. for ( String roleName : userAssignment.getRoleNames() )
  649. {
  650. ua.addRoleName( roleName );
  651. }
  652. }
  653. }
  654. }
  655. catch ( Exception e )
  656. {
  657. lastException = e;
  658. }
  659. }
  660. if ( ua != null )
  661. {
  662. userAssignmentsCache.put( principal, ua );
  663. return ua;
  664. }
  665. if ( lastException != null )
  666. {
  667. throw new RbacManagerException( lastException.getMessage(), lastException );
  668. }
  669. return null;
  670. }
  671. @Override
  672. public boolean userAssignmentExists( String principal )
  673. {
  674. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  675. {
  676. try
  677. {
  678. boolean exists = rbacManager.userAssignmentExists( principal );
  679. if ( exists )
  680. {
  681. return true;
  682. }
  683. }
  684. catch ( Exception e )
  685. {
  686. // no op
  687. }
  688. }
  689. return false;
  690. }
  691. @Override
  692. public boolean userAssignmentExists( UserAssignment assignment )
  693. {
  694. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  695. {
  696. try
  697. {
  698. boolean exists = rbacManager.userAssignmentExists( assignment );
  699. if ( exists )
  700. {
  701. return true;
  702. }
  703. }
  704. catch ( Exception e )
  705. {
  706. // no op
  707. }
  708. }
  709. return false;
  710. }
  711. public List<UserAssignment> getAllUserAssignments()
  712. throws RbacManagerException
  713. {
  714. Map<String, UserAssignment> allUserAssignments = new HashMap<String, UserAssignment>();
  715. boolean allFailed = true;
  716. Exception lastException = null;
  717. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  718. {
  719. try
  720. {
  721. List<UserAssignment> userAssignments = rbacManager.getAllUserAssignments();
  722. for ( UserAssignment ua : userAssignments )
  723. {
  724. UserAssignment userAssignment = allUserAssignments.get( ua.getPrincipal() );
  725. if ( userAssignment != null )
  726. {
  727. for ( String roleName : ua.getRoleNames() )
  728. {
  729. userAssignment.addRoleName( roleName );
  730. }
  731. }
  732. allUserAssignments.put( ua.getPrincipal(), ua );
  733. }
  734. allFailed = false;
  735. }
  736. catch ( Exception e )
  737. {
  738. lastException = e;
  739. }
  740. }
  741. if ( lastException != null && allFailed )
  742. {
  743. throw new RbacManagerException( lastException.getMessage(), lastException );
  744. }
  745. return new ArrayList<>( allUserAssignments.values() );
  746. }
  747. public List<UserAssignment> getUserAssignmentsForRoles( Collection<String> roleNames )
  748. throws RbacManagerException
  749. {
  750. List<UserAssignment> allUserAssignments = new ArrayList<>();
  751. boolean allFailed = true;
  752. Exception lastException = null;
  753. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  754. {
  755. try
  756. {
  757. List<UserAssignment> userAssignments = rbacManager.getUserAssignmentsForRoles( roleNames );
  758. allUserAssignments.addAll( userAssignments );
  759. allFailed = false;
  760. }
  761. catch ( Exception e )
  762. {
  763. lastException = e;
  764. }
  765. }
  766. if ( lastException != null && allFailed )
  767. {
  768. throw new RbacManagerException( lastException.getMessage(), lastException );
  769. }
  770. return allUserAssignments;
  771. }
  772. public void removeUserAssignment( UserAssignment userAssignment )
  773. throws RbacObjectNotFoundException, RbacObjectInvalidException, RbacManagerException
  774. {
  775. boolean allFailed = true;
  776. Exception lastException = null;
  777. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  778. {
  779. try
  780. {
  781. rbacManager.removeUserAssignment( userAssignment );
  782. userAssignmentsCache.remove( userAssignment.getPrincipal() );
  783. allFailed = false;
  784. }
  785. catch ( Exception e )
  786. {
  787. lastException = e;
  788. }
  789. }
  790. if ( lastException != null && allFailed )
  791. {
  792. throw new RbacManagerException( lastException.getMessage(), lastException );
  793. }
  794. }
  795. @Override
  796. public boolean roleExists( String name )
  797. throws RbacManagerException
  798. {
  799. Role r = rolesCache.get( name );
  800. if ( r != null )
  801. {
  802. return true;
  803. }
  804. boolean allFailed = true;
  805. Exception lastException = null;
  806. for ( RBACManager rbacManager : rbacManagersPerId.values() )
  807. {
  808. try
  809. {
  810. boolean exists = rbacManager.roleExists( name );
  811. if ( exists )
  812. {
  813. return true;
  814. }
  815. }
  816. catch ( Exception e )
  817. {
  818. lastException = e;
  819. }
  820. }
  821. if ( lastException != null && allFailed )
  822. {
  823. throw new RbacManagerException( lastException.getMessage(), lastException );
  824. }
  825. return false;
  826. }
  827. @Override
  828. public boolean roleExists( Role role )
  829. throws RbacManagerException
  830. {
  831. return roleExists( role.getName() );
  832. }
  833. public void eraseDatabase()
  834. {
  835. log.warn( "eraseDatabase not implemented" );
  836. }
  837. @Override
  838. public boolean isFinalImplementation()
  839. {
  840. return false;
  841. }
  842. public String getDescriptionKey()
  843. {
  844. return "archiva.redback.rbacmanager.archiva";
  845. }
  846. public boolean isReadOnly()
  847. {
  848. return false;
  849. }
  850. }