/** * Parses the header and assigns roles to the given user object. The header value of the given header name will be parsed for roles. See parseRoles(String, * Set) method for details on the parsing process. * * @param applicationUser - the user object to populate the roles to * @param httpHeaders - the HTTP headers given in the current request * @param headerName - the name of the header containing the roles */ protected void buildRoles(ApplicationUser applicationUser, Map<String, String> httpHeaders, String headerName) { Set<String> roles = new HashSet<>(); applicationUser.setRoles(roles); String rolesHeaderValue = getHeaderValueString(headerName, httpHeaders); if (rolesHeaderValue != null) { parseRoles(rolesHeaderValue, roles); } /* * If we need to have a mechanism to retrieve roles such that a single header represents a unique role, then the extra code to handle this situation * could be added here. */ }
/** * Builds the application user. * * @param request the HTTP servlet request. * @param includeRoles If true, the user's roles will be included. Otherwise, not. * * @return the application user. */ protected ApplicationUser buildUser(HttpServletRequest request, boolean includeRoles) { ApplicationUser applicationUser = new ApplicationUser(this.getClass()); applicationUser.setUserId(TRUSTED_USER_ID); applicationUser.setFirstName(TRUSTED_USER_FIRST_NAME); applicationUser.setLastName(TRUSTED_USER_LAST_NAME); applicationUser.setEmail(TRUSTED_USER_EMAIL); applicationUser.setSessionId(request.getSession().getId()); applicationUser.setNamespaceAuthorizations(userNamespaceAuthorizationHelper.getAllNamespaceAuthorizations()); if (includeRoles) { Set<String> roles = new HashSet<>(); roles.add(TRUSTED_USER_ROLE); applicationUser.setRoles(roles); } return applicationUser; } }
@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 getPrincipal() { List<SimpleGrantedAuthority> authorities = Lists.newArrayList(new SimpleGrantedAuthority(SECURITY_FUNCTION)); 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); }