use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.
the class CurrentUserRestControllerTest method testGetCurrentUser.
@Test
public void testGetCurrentUser() throws Exception {
// Create a set of test namespace authorizations.
Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
UserAuthorizations userAuthorizations = new UserAuthorizations();
userAuthorizations.setNamespaceAuthorizations(new ArrayList(namespaceAuthorizations));
when(currentUserService.getCurrentUser()).thenReturn(userAuthorizations);
// Get the current user information.
UserAuthorizations resultUserAuthorizations = currentUserRestController.getCurrentUser();
// Verify the external calls.
verify(currentUserService).getCurrentUser();
verifyNoMoreInteractions(currentUserService);
// Validate the returned object.
assertEquals(userAuthorizations, resultUserAuthorizations);
}
use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.
the class CurrentUserServiceTest method testGetCurrentUser.
@Test
public void testGetCurrentUser() throws Exception {
// Create a set of test namespace authorizations.
Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
// Create test roles
List<SecurityRoleEntity> securityRoleEntities = securityRoleDaoTestHelper.createTestSecurityRoles();
// Fetch the security role codes to add to the application user.
Set<String> roles = securityRoleEntities.stream().map(SecurityRoleEntity::getCode).collect(Collectors.toSet());
// Override the security context to return an application user populated with test values.
Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
try {
SecurityContextHolder.getContext().setAuthentication(new Authentication() {
@Override
public String getName() {
return null;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public Object getPrincipal() {
List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(SECURITY_FUNCTION), new SimpleGrantedAuthority(SECURITY_FUNCTION_2));
ApplicationUser applicationUser = new ApplicationUser(this.getClass());
applicationUser.setUserId(USER_ID);
applicationUser.setRoles(roles);
applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
return new SecurityUserWrapper(USER_ID, STRING_VALUE, true, true, true, true, authorities, applicationUser);
}
@Override
public Object getDetails() {
return null;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
});
// Get the current user information.
UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
// Validate the response object.
assertEquals(new UserAuthorizations(USER_ID, new ArrayList<>(namespaceAuthorizations), new ArrayList<>(roles), Arrays.asList(SECURITY_FUNCTION, SECURITY_FUNCTION_2)), userAuthorizations);
} finally {
// Restore the original authentication.
SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
}
}
use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.
the class CurrentUserServiceTest method testGetCurrentUserNoAuthentication.
@Test
public void testGetCurrentUserNoAuthentication() throws Exception {
// Override the security context to have no authentication.
Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
try {
// Get the current user information.
UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
// Validate the response object.
assertEquals(new UserAuthorizations(null, null, NO_SECURITY_ROLES, NO_SECURITY_FUNCTIONS), userAuthorizations);
} finally {
// Restore the original authentication.
SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
}
}
use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.
the class CurrentUserServiceTest method testGetCurrentUserNoSecurityRolesAndFunctions.
@Test
public void testGetCurrentUserNoSecurityRolesAndFunctions() throws Exception {
// Create a set of test namespace authorizations.
Set<NamespaceAuthorization> namespaceAuthorizations = new LinkedHashSet<>();
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE, SUPPORTED_NAMESPACE_PERMISSIONS));
namespaceAuthorizations.add(new NamespaceAuthorization(NAMESPACE_2, SUPPORTED_NAMESPACE_PERMISSIONS));
// Override the security context to return an application user populated with test values.
Authentication originalAuthentication = SecurityContextHolder.getContext().getAuthentication();
try {
SecurityContextHolder.getContext().setAuthentication(new Authentication() {
@Override
public String getName() {
return null;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
}
@Override
public boolean isAuthenticated() {
return false;
}
@Override
public Object getPrincipal() {
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
ApplicationUser applicationUser = new ApplicationUser(this.getClass());
applicationUser.setUserId(USER_ID);
applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
return new SecurityUserWrapper(USER_ID, STRING_VALUE, true, true, true, true, authorities, applicationUser);
}
@Override
public Object getDetails() {
return null;
}
@Override
public Object getCredentials() {
return null;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
});
// Get the current user information.
UserAuthorizations userAuthorizations = currentUserService.getCurrentUser();
// Validate the response object.
assertEquals(new UserAuthorizations(USER_ID, new ArrayList<>(namespaceAuthorizations), NO_SECURITY_ROLES, NO_SECURITY_FUNCTIONS), userAuthorizations);
} finally {
// Restore the original authentication.
SecurityContextHolder.getContext().setAuthentication(originalAuthentication);
}
}
use of org.finra.herd.model.api.xml.UserAuthorizations in project herd by FINRAOS.
the class CurrentUserServiceImpl method getCurrentUser.
@Override
public UserAuthorizations getCurrentUser() {
// Create the user authorizations.
UserAuthorizations userAuthorizations = new UserAuthorizations();
// Get the application user.
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
SecurityUserWrapper securityUserWrapper = (SecurityUserWrapper) authentication.getPrincipal();
ApplicationUser applicationUser = securityUserWrapper.getApplicationUser();
userAuthorizations.setUserId(applicationUser.getUserId());
// If roles are present on the application user then filter the herd-specific security roles and add that information to the Current user.
if (CollectionUtils.isNotEmpty(applicationUser.getRoles())) {
userAuthorizations.setSecurityRoles(new ArrayList<>(getValidSecurityRoles(applicationUser.getRoles())));
}
// Get all granted authorities for this user.
Collection<GrantedAuthority> grantedAuthorities = securityUserWrapper.getAuthorities();
// Add relative security functions as per granted authorities, if any are present.
if (CollectionUtils.isNotEmpty(grantedAuthorities)) {
userAuthorizations.setSecurityFunctions(grantedAuthorities.stream().map(grantedAuthority -> new String(grantedAuthority.getAuthority())).collect(Collectors.toList()));
}
userAuthorizations.setNamespaceAuthorizations(new ArrayList<>(applicationUser.getNamespaceAuthorizations()));
}
return userAuthorizations;
}
Aggregations