1 package org.apache.archiva.redback.struts2.action.admin;
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 com.google.common.collect.Lists;
23 import com.opensymphony.xwork2.Action;
24 import com.opensymphony.xwork2.ActionProxy;
25 import org.apache.archiva.redback.authentication.AuthenticationException;
26 import org.apache.archiva.redback.rbac.RbacObjectInvalidException;
27 import org.apache.archiva.redback.rbac.Role;
28 import org.apache.archiva.redback.users.UserNotFoundException;
29 import org.apache.archiva.redback.authorization.AuthorizationResult;
30 import org.apache.archiva.redback.policy.AccountLockedException;
31 import org.apache.archiva.redback.rbac.RbacManagerException;
32 import org.apache.archiva.redback.struts2.model.ApplicationRoleDetails;
33 import org.apache.archiva.redback.struts2.model.ApplicationRoleDetails.RoleTableCell;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
45 * @todo missing tests for success/fail on standard show/edit functions (non security testing related)
47 public class AssignmentsActionTest
48 extends AbstractUserCredentialsActionTest
50 //@Rule public TestName name = new TestName();
51 // xxx help for jdk 7 investigation
52 private AssignmentsAction action;
54 /* public static final List<String> favorites =
55 Arrays.asList("user", "user2","user3","user-admin");
56 xxx help for jdk 7 investigation
57 private void displayInfo(boolean before) throws RbacObjectNotFoundException, RbacManagerException {
58 System.err.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
60 System.err.print("(b)");
62 System.err.print("(a)");
64 System.err.println(name.getMethodName());
65 for (String user : favorites) {
66 if (rbacManager.userAssignmentExists(user)) {
67 for (String s : rbacManager.getUserAssignment(user).getRoleNames()) {
68 System.err.println("--" + user + ">>" + s);
72 System.err.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
81 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
82 action = (AssignmentsAction) actionProxy.getAction();
84 login( action, "user", PASSWORD );
85 action.setPrincipal( "user2" );
86 //displayInfo(true);//xxx help for jdk 7 investigation
90 /*@After xxx help for jdk 7 investigation
96 } catch (RbacObjectNotFoundException ex) {
97 Logger.getLogger(AssignmentsActionTest.class.getName()).log(Level.SEVERE, null, ex);
98 } catch (RbacObjectInvalidException ex) {
99 Logger.getLogger(AssignmentsActionTest.class.getName()).log(Level.SEVERE, null, ex);
100 } catch (RbacManagerException ex) {
101 Logger.getLogger(AssignmentsActionTest.class.getName()).log(Level.SEVERE, null, ex);
105 * Check security - show/edituser should fail if the permission 'user-management-user-role' is not present, but a
106 * valid 'user-management-role-grant' is.
109 public void testUserWithOnlyRoleGrantHasNoAccess()
113 addAssignment( "user", "Grant Administrator - default" );
115 List<SecureActionBundle.AuthorizationTuple> authorizationTuples = getTuples();
116 for ( SecureActionBundle.AuthorizationTuple tuple : authorizationTuples )
118 AuthorizationResult authzResult = system.authorize( session, tuple.getOperation(), tuple.getResource() );
120 assertFalse( authzResult.isAuthorized() );
123 rbacManager.getUserAssignment( "user" ).removeRoleName( "Grant Administrator - default" );
127 * Check security - check success if the permission 'user-management-user-role' is present along with global
128 * 'user-management-role-grant'.
131 public void testUserWithOnlyRoleGrantHasAccess()
134 addAssignment( "user", "Project Administrator - default" );
136 List<SecureActionBundle.AuthorizationTuple> authorizationTuples = getTuples();
137 boolean result = false;
138 for ( SecureActionBundle.AuthorizationTuple tuple : authorizationTuples )
140 AuthorizationResult authzResult = system.authorize( session, tuple.getOperation(), tuple.getResource() );
142 result |= authzResult.isAuthorized();
144 assertTrue( result );
145 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
148 private List<SecureActionBundle.AuthorizationTuple> getTuples()
149 throws SecureActionException
151 return action.getSecureActionBundle().getAuthorizationTuples();
155 * Check roles can be assigned if the user has no previous assignments.
158 public void testShowWhenUserHasNoAssignments()
161 addAssignment( "user", "Project Administrator - default" );
163 action.setPrincipal( "user3" );
165 assertEquals( Action.SUCCESS, action.show() );
167 assertEquals( 2, action.getApplicationRoleDetails().size() );
168 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
172 * Check security - show should filter out roles that the 'user-management-role-grant' is not present for
175 public void testRoleGrantFilteringOnShow()
178 addAssignment( "user", "Project Administrator - default" );
180 assertEquals( Action.SUCCESS, action.show() );
182 assertEquals( 2, action.getApplicationRoleDetails().size() );
183 ApplicationRoleDetails details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 0 );
184 assertEquals( "System", details.getName() );
185 assertEquals( "Roles that apply system-wide, across all of the applications", details.getDescription() );
186 assertEquals( "found roles " + details.getAvailableRoles(), 0, details.getAvailableRoles().size() );
187 details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 1 );
188 assertEquals( "Continuum", details.getName() );
189 assertEquals( "found roles " + details.getAvailableRoles(), 0, details.getAvailableRoles().size() );
191 // This table rendering code clearly has to go
192 List<List<RoleTableCell>> table = details.getTable();
193 assertEquals( 1, table.size() );
194 assertRow( table, 0, "default", "Project Administrator - default", false );
195 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
198 @SuppressWarnings( "unchecked" )
199 private void assertRow( List table, int index, String name, String label, boolean assigned )
201 List<RoleTableCell> row = (List<RoleTableCell>) table.get( index );
202 assertEquals( name, row.get( 0 ).getName() );
203 assertEquals( label, row.get( 1 ).getName() );
204 assertEquals( assigned, row.get( 2 ).isAssigned() );
208 * Check security - show should not filter out roles if 'user-management-role-grant' is present for the global
211 // TODO: currently returns all roles - we really want all templated roles
212 // public void testRoleGrantFilteringOnShowGlobalGrant()
213 // throws RbacObjectInvalidException, RbacManagerException
215 // addAssignment( "user", "Global Grant Administrator" );
217 // assertEquals( Action.SUCCESS, action.show() );
219 // assertEquals( 2, action.getApplicationRoleDetails().size() );
220 // ApplicationRoleDetails details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 0 );
221 // assertEquals( "redback-xwork-integration-core", details.getName() );
222 // assertEquals( 0, details.getAvailableRoles().size() );
224 // details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 1 );
225 // assertEquals( "Continuum", details.getName() );
226 // assertEquals( 0, details.getAvailableRoles().size() );
228 // List table = details.getTable();
229 // assertEquals( 2, table.size() );
230 // assertRow( table, 0, "default", "Project Administrator - default", false );
231 // assertRow( table, 1, "other", "Project Administrator - other", false );
235 * Check security - edituser should skip adding a role that 'user-management-role-grant' is not present for a
239 public void testRoleGrantFilteringOnAddRolesNotPermittedTemplated()
240 throws RbacObjectInvalidException, RbacManagerException
242 addAssignment( "user", "Project Administrator - default" );
244 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
245 List<String> dSelectedRoles = new ArrayList<String>();
246 dSelectedRoles.add( "Project Administrator - other" );
248 action.setAddDSelectedRoles( dSelectedRoles );
250 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
252 assertEquals( Action.SUCCESS, action.edituser() );
254 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
255 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
259 * Check security - edituser should skip adding a role that 'user-management-role-grant' is not present for a
263 public void testRoleGrantFilteringOnAddRolesNotPermittedNotTemplated()
264 throws RbacObjectInvalidException, RbacManagerException
266 addAssignment( "user", "Project Administrator - default" );
268 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
269 List<String> ndSelectedRoles = new ArrayList<String>();
270 ndSelectedRoles.add( "Continuum Group Project Administrator" );
272 action.setAddNDSelectedRoles( ndSelectedRoles );
274 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
276 assertEquals( Action.SUCCESS, action.edituser() );
278 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
279 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
283 * Check security - edituser should succeed if adding a role that 'user-management-role-grant' is present for
287 public void testRoleGrantFilteringOnAddRolesPermittedNotTemplated()
288 throws RbacObjectInvalidException, RbacManagerException, AccountLockedException, AuthenticationException,
289 UserNotFoundException
291 addAssignment( "user", "Global Grant Administrator" );
293 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
294 List<String> ndSelectedRoles = new ArrayList<String>();
295 ndSelectedRoles.add( "Continuum Group Project Administrator" );
297 action.setAddNDSelectedRoles( ndSelectedRoles );
299 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
301 assertEquals( Action.SUCCESS, action.edituser() );
303 assertEquals( Lists.<String>newArrayList( "Continuum Group Project Administrator" ),
304 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
306 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Continuum Group Project Administrator" );
307 rbacManager.getUserAssignment( "user" ).removeRoleName( "Global Grant Administrator" );
311 * Check security - edituser should succeed if adding a role that 'user-management-role-grant' is present for
315 public void testRoleGrantFilteringOnAddRolesPermittedTemplated()
319 rbacManager.removeUserAssignment( "user" );
321 addAssignment( "user", "Project Administrator - default" );
323 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
324 List<String> dSelectedRoles = new ArrayList<String>();
325 dSelectedRoles.add( "Project Administrator - default" );
327 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
328 AssignmentsAction newAction = (AssignmentsAction) actionProxy.getAction();
330 login( newAction, "user", PASSWORD );
332 newAction.setPrincipal( "user2" );
334 newAction.setAddDSelectedRoles( dSelectedRoles );
336 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
338 assertEquals( Action.SUCCESS, newAction.edituser() );
340 assertEquals( Arrays.asList( "Project Administrator - default" ),
341 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
345 * Check security - edituser should succeed if adding a role that 'user-management-role-grant' is present for
349 public void testRoleGrantFilteringOnAddRolesPermittedTemplatedExistingRole()
352 addAssignment( "user", "Project Administrator - default" );
354 // cleanup before next test
355 rbacManager.removeUserAssignment( "user2" );
357 addAssignment( "user2", "Project Administrator - other" );
359 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
360 List<String> dSelectedRoles = new ArrayList<String>();
361 dSelectedRoles.add( "Project Administrator - default" );
363 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
364 AssignmentsAction newAction = (AssignmentsAction) actionProxy.getAction();
366 login( newAction, "user2", PASSWORD );
368 newAction.setPrincipal( "user2" );
370 newAction.setAddDSelectedRoles( dSelectedRoles );
372 assertEquals( Arrays.asList( "Project Administrator - other" ),
373 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
375 assertEquals( Action.SUCCESS, newAction.edituser() );
377 //assertEquals( Arrays.asList( "Project Administrator - default", "Project Administrator - other" ),
378 // rbacManager.getUserAssignment( "user2" ).getRoleNames() );
379 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
384 * Check security - edituser should fail if removing a role that 'user-management-role-grant' is not present for
388 public void testRoleGrantFilteringOnRemoveRolesNotPermittedNotTemplated()
392 rbacManager.removeUserAssignment( "user2" );
394 addAssignment( "user", "Project Administrator - default" );
396 addAssignment( "user2", "Continuum Group Project Administrator" );
398 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
399 AssignmentsAction newAction = (AssignmentsAction) actionProxy.getAction();
401 login( newAction, "user2", PASSWORD );
403 newAction.setPrincipal( "user2" );
405 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
406 List<String> ndSelectedRoles = new ArrayList<String>();
407 newAction.setAddNDSelectedRoles( ndSelectedRoles );
409 assertEquals( Arrays.asList( "Continuum Group Project Administrator" ),
410 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
412 assertEquals( Action.SUCCESS, newAction.edituser() );
414 assertEquals( Arrays.asList( "Continuum Group Project Administrator" ),
415 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
417 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Continuum Group Project Administrator" );
418 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
422 * Check security - edituser should fail if removing a role that 'user-management-role-grant' is not present for
426 public void testRoleGrantFilteringOnRemoveRolesNotPermittedTemplated()
429 rbacManager.removeUserAssignment( "user2" );
431 addAssignment( "user", "Project Administrator - other" );
433 addAssignment( "user2", "Project Administrator - default" );
435 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
436 List<String> dSelectedRoles = new ArrayList<String>();
438 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
439 AssignmentsAction newAction = (AssignmentsAction) actionProxy.getAction();
441 login( newAction, "user2", PASSWORD );
443 newAction.setPrincipal( "user2" );
445 newAction.setAddDSelectedRoles( dSelectedRoles );
447 assertEquals( Arrays.asList( "Project Administrator - default" ),
448 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
450 assertEquals( Action.SUCCESS, newAction.edituser() );
452 assertEquals( Arrays.asList( "Project Administrator - default" ),
453 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
457 * Check security - edituser should succeed if removing a role that 'user-management-role-grant' is present for
461 public void testRoleGrantFilteringOnRemoveRolesPermittedNotTemplated()
464 addAssignment( "user", "Global Grant Administrator" );
466 addAssignment( "user2", "Continuum Group Project Administrator" );
468 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
469 List<String> ndSelectedRoles = new ArrayList<String>();
470 action.setAddNDSelectedRoles( ndSelectedRoles );
472 assertEquals( Arrays.asList( "Continuum Group Project Administrator" ),
473 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
475 assertEquals( Action.SUCCESS, action.edituser() );
477 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
479 rbacManager.getUserAssignment( "user" ).removeRoleName( "Global Grant Administrator" );
483 * Check security - edituser should succeed if removing a role that 'user-management-role-grant' is present for
484 * templated roles and there is an existing role that is not assignable by the current user.
487 public void testRoleGrantFilteringOnRemoveRolesPermittedTemplatedExistingRole()
490 addAssignment( "user", "Project Administrator - default" );
492 rbacManager.removeUserAssignment( "user2" );
494 addAssignment( "user2", "Project Administrator - default" );
495 addAssignment( "user2", "Project Administrator - other" );
496 addAssignment( "user2", "Registered User" );
498 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
499 List<String> dSelectedRoles = new ArrayList<String>();
500 dSelectedRoles.add( "Project Administrator - other" );
501 dSelectedRoles.add( "Registered User" );
502 action.setAddDSelectedRoles( dSelectedRoles );
505 Arrays.asList( "Project Administrator - default", "Project Administrator - other", "Registered User" ),
506 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
508 assertEquals( Action.SUCCESS, action.edituser() );
510 // Roles may be out of order, due to removal and subsequent re-add
511 List<String> user2roles = rbacManager.getUserAssignment( "user2" ).getRoleNames();
512 assertTrue( user2roles.contains( "Project Administrator - other" ) );
513 assertTrue( user2roles.contains( "Registered User" ) );
517 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Registered User" );
518 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Project Administrator - other" );
519 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
523 * Check security - edituser should succeed if removing a role that 'user-management-role-grant' is present for
527 public void testRoleGrantFilteringOnRemoveRolesPermittedTemplated()
530 rbacManager.removeUserAssignment( "user2" );
532 addAssignment( "user", "Project Administrator - default" );
534 addAssignment( "user2", "Project Administrator - default" );
536 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
537 List<String> dSelectedRoles = new ArrayList<String>();
538 action.setAddDSelectedRoles( dSelectedRoles );
540 assertEquals( Arrays.asList( "Project Administrator - default" ),
541 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
543 assertEquals( Action.SUCCESS, action.edituser() );
545 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
547 rbacManager.getUserAssignment( "user" ).removeRoleName( "Project Administrator - default" );
551 * Check security - show should succeed and display all roles, even without 'user-management-role-grant' or
552 * 'user-management-user-role' for the user administrators.
554 * @throws org.apache.archiva.redback.policy.MustChangePasswordException
557 public void testSystemAdminCanShowRoles()
561 login( action, "admin", PASSWORD );
563 assertEquals( Action.SUCCESS, action.show() );
565 assertEquals( 2, action.getApplicationRoleDetails().size() );
566 ApplicationRoleDetails details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 0 );
567 assertEquals( "System", details.getName() );
568 assertEquals( "Roles that apply system-wide, across all of the applications", details.getDescription() );
569 assertEquals( 4, details.getAvailableRoles().size() );
570 assertEquals( "Guest", details.getAvailableRoles().get( 0 ) );
571 assertEquals( "Registered User", details.getAvailableRoles().get( 1 ) );
572 assertEquals( "System Administrator", details.getAvailableRoles().get( 2 ) );
573 assertEquals( "User Administrator", details.getAvailableRoles().get( 3 ) );
575 details = (ApplicationRoleDetails) action.getApplicationRoleDetails().get( 1 );
576 assertEquals( "Continuum", details.getName() );
578 assertEquals( 2, details.getAvailableRoles().size() );
579 assertEquals( "Continuum Group Project Administrator", details.getAvailableRoles().get( 0 ) );
580 assertEquals( "Global Grant Administrator", details.getAvailableRoles().get( 1 ) );
582 List<List<RoleTableCell>> table = details.getTable();
583 assertEquals( 2, table.size() );
584 assertRow( table, 0, "default", "Project Administrator - default", false );
585 assertRow( table, 1, "other", "Project Administrator - other", false );
589 * Check security - show should succeed and display all roles, even without 'user-management-role-grant' or
590 * 'user-management-user-role' for the user administrators.
593 public void testUserAdminCanShowRoles()
597 ActionProxy actionProxy = getActionProxy( "/security/assignments" );
598 AssignmentsAction newAction = (AssignmentsAction) actionProxy.getAction();
600 login( newAction, "user-admin", PASSWORD );
602 newAction.setPrincipal( "user-admin" );
604 assertEquals( Action.SUCCESS, newAction.show() );
606 assertEquals( 2, newAction.getApplicationRoleDetails().size() );
607 ApplicationRoleDetails details = (ApplicationRoleDetails) newAction.getApplicationRoleDetails().get( 0 );
608 assertEquals( "System", details.getName() );
609 assertEquals( "Roles that apply system-wide, across all of the applications", details.getDescription() );
610 // TODO assertEquals( 3, details.getAvailableRoles().size() );
611 assertEquals( "Guest", details.getAvailableRoles().get( 0 ) );
612 assertEquals( "not role Registered User roles : " + details.getAvailableRoles(), "Registered User",
613 details.getAvailableRoles().get( 1 ) );
614 // TODO: assertEquals( "User Administrator", details.getAvailableRoles().get( 2 ) );
616 details = newAction.getApplicationRoleDetails().get( 1 );
617 assertEquals( "Continuum", details.getName() );
619 assertEquals( 2, details.getAvailableRoles().size() );
620 assertEquals( "Continuum Group Project Administrator", details.getAvailableRoles().get( 0 ) );
621 assertEquals( "Global Grant Administrator", details.getAvailableRoles().get( 1 ) );
623 List<List<RoleTableCell>> table = details.getTable();
624 assertEquals( 2, table.size() );
625 assertRow( table, 0, "default", "Project Administrator - default", false );
626 assertRow( table, 1, "other", "Project Administrator - other", false );
630 * Check security - edituser should succeed in adding a role, even without 'user-management-role-grant' or
631 * 'user-management-user-role' for the user administrators.
634 public void testUserAdminCanAddRoles()
637 login( action, "user-admin", PASSWORD );
639 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
640 List<String> ndSelectedRoles = new ArrayList<String>();
641 ndSelectedRoles.add( "Continuum Group Project Administrator" );
643 action.setAddNDSelectedRoles( ndSelectedRoles );
645 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
646 List<String> dSelectedRoles = new ArrayList<String>();
647 dSelectedRoles.add( "Project Administrator - default" );
649 action.setAddDSelectedRoles( dSelectedRoles );
651 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
653 assertEquals( Action.SUCCESS, action.edituser() );
655 assertEquals( Arrays.asList( "Continuum Group Project Administrator", "Project Administrator - default" ),
656 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
659 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Continuum Group Project Administrator" );
660 rbacManager.getUserAssignment( "user2" ).removeRoleName( "Project Administrator - default" );
664 * Check security - edituser should succeed in removing a role, even without 'user-management-role-grant' or
665 * 'user-management-user-role' for the user administrators.
668 public void testUserAdminCanRemoveRoles()
671 login( action, "user-admin", PASSWORD );
673 rbacManager.removeUserAssignment( "user2" );
675 addAssignment( "user2", "Continuum Group Project Administrator" );
676 addAssignment( "user2", "Project Administrator - default" );
678 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
679 List<String> ndSelectedRoles = new ArrayList<String>();
680 action.setAddNDSelectedRoles( ndSelectedRoles );
682 List<String> dSelectedRoles = new ArrayList<String>();
683 action.setAddDSelectedRoles( dSelectedRoles );
685 assertEquals( Arrays.asList( "Continuum Group Project Administrator", "Project Administrator - default" ),
686 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
688 assertEquals( Action.SUCCESS, action.edituser() );
690 assertTrue( rbacManager.getUserAssignment( "user2" ).getRoleNames().isEmpty() );
694 * Check that a configured struts2 redback app only removes roles configured for the app. Without this, redback
695 * applications sharing a user database will remove each other's roles on save.
698 public void testUserAdminCannotRemoveNonAppRoles()
701 login( action, "user-admin", PASSWORD );
703 // Create a role that isn't configured for apps
704 String nonAppRoleName = "Other App Role";
705 Role nonAppRole = rbacManager.createRole( nonAppRoleName );
706 rbacManager.saveRole( nonAppRole );
708 rbacManager.removeUserAssignment( "user2" );
710 addAssignment( "user2", "Continuum Group Project Administrator" );
711 addAssignment( "user2", "Project Administrator - default" );
712 addAssignment( "user2", nonAppRoleName );
714 // set addDSelectedRoles (dynamic --> Resource Roles) and addNDSelectedRoles (non-dynamic --> Available Roles)
715 List<String> ndSelectedRoles = new ArrayList<String>();
716 action.setAddNDSelectedRoles( ndSelectedRoles );
718 List<String> dSelectedRoles = new ArrayList<String>();
719 action.setAddDSelectedRoles( dSelectedRoles );
722 Arrays.asList( "Continuum Group Project Administrator", "Project Administrator - default", nonAppRoleName ),
723 rbacManager.getUserAssignment( "user2" ).getRoleNames() );
725 assertEquals( Action.SUCCESS, action.edituser() );
727 // All roles except role from other app should be removed.
728 List<String> user2roles = rbacManager.getUserAssignment( "user2" ).getRoleNames();
729 assertTrue( !user2roles.contains( "Continuum Group Project Administrator" ) );
730 assertTrue( !user2roles.contains( "Project Administrator - default" ) );
731 assertTrue( user2roles.contains( nonAppRoleName ) );
734 rbacManager.removeRole( nonAppRole );
735 rbacManager.getUserAssignment( "user2" ).removeRoleName( nonAppRoleName );