AuthenticationServiceImpl implements AuthenticationService { @Override public boolean isOnlyDepartmentAdministrator() { boolean isDA = false; boolean isFacilityMemberOtherThanDA = false; for (String role: this.getRoles()){ if (role.equals("da-*")){ isDA = true; } if (role.equals("su") || role.equals("fm") || role.equals("ft") || role.equals("sa") || role.equals("ga")){ isFacilityMemberOtherThanDA = true; } } if(isDA == true && isFacilityMemberOtherThanDA == false){return true;} return false; }  void setUserDao(UserDao userDao); void setUserPendingDao(UserPendingDao userPendingDao); @Override User getAuthenticatedUser(); @Override boolean isAuthenticated(); @Override boolean isAuthenticatedWaspUser(); @SuppressWarnings("unchecked") @Override String[] getRoles(); @Override boolean isExternallyAuthenticated(); @Override boolean isAuthenticationSetExternal(); @Override boolean isAuthenticatedGuest(); @Override boolean hasRole(String theRole); @Override boolean hasRoleInRoleArray(String[] rolesToCompare, String[] rolesBaseline); @Override Integer getRoleValue(String role); @Override boolean hasRoleInRoleArray(String[] rolesToCompare); @Override boolean isSuperUser(); @Override boolean isFacilityMember(); @Override boolean isOnlyDepartmentAdministrator(); @Override boolean authenticate(String name, String password); @Override boolean authenticates(String name, String password); @Override boolean isLoginNameWellFormed(String login); @Override boolean isLoginAlreadyInUse(String login, String email); @Override String encodePassword(String s); @Override boolean validatePassword(String s); @Override boolean matchPassword(String s1, String s2); @Override String getRandomPassword(int length); @Override Set<Integer> idsOfDepartmentsManagedByCurrentUser(); @Override Set<Integer> idsOfLabsManagedByCurrentUser(); @Override boolean isThisExistingUserPIPending();  }
@Test public void isOnlyDepartmentAdministrator() { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new GrantedAuthorityImpl("da-*")); SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("","", authorities)); Assert.assertTrue(authServiceImpl.isOnlyDepartmentAdministrator()); }
@Test public void isOnlyDepartmentAdministrator2() { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new GrantedAuthorityImpl("da-*")); authorities.add(new GrantedAuthorityImpl("su")); SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("","", authorities)); Assert.assertFalse(authServiceImpl.isOnlyDepartmentAdministrator()); }
@Test public void isOnlyDepartmentAdministrator3() { List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); authorities.add(new GrantedAuthorityImpl("testrole1")); authorities.add(new GrantedAuthorityImpl("testrole2")); SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken("","", authorities)); Assert.assertFalse(authServiceImpl.isOnlyDepartmentAdministrator()); }