use of org.springframework.security.core.GrantedAuthority in project dhis2-core by dhis2.
the class DhisConvenienceTest method createUserAndInjectSecurityContext.
/**
* Creates a user and injects into the security context with username
* "username". Requires <code>identifiableObjectManager</code> and
* <code>userService</code> to be injected into the test.
*
* @param organisationUnits the organisation units of the user.
* @param dataViewOrganisationUnits user's data view organisation units.
* @param allAuth whether to grant the ALL authority.
* @param auths authorities to grant to user.
* @return the user.
*/
protected User createUserAndInjectSecurityContext(Set<OrganisationUnit> organisationUnits, Set<OrganisationUnit> dataViewOrganisationUnits, boolean allAuth, String... auths) {
Assert.notNull(userService, "UserService must be injected in test");
Set<String> authorities = new HashSet<>();
if (allAuth) {
authorities.add(UserAuthorityGroup.AUTHORITY_ALL);
}
if (auths != null) {
authorities.addAll(Lists.newArrayList(auths));
}
UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
userAuthorityGroup.setName("Superuser");
userAuthorityGroup.getAuthorities().addAll(authorities);
userService.addUserAuthorityGroup(userAuthorityGroup);
User user = createUser('A');
if (organisationUnits != null) {
user.setOrganisationUnits(organisationUnits);
}
if (dataViewOrganisationUnits != null) {
user.setDataViewOrganisationUnits(dataViewOrganisationUnits);
}
user.getUserCredentials().getUserAuthorityGroups().add(userAuthorityGroup);
userService.addUser(user);
user.getUserCredentials().setUserInfo(user);
userService.addUserCredentials(user.getUserCredentials());
Set<GrantedAuthority> grantedAuths = authorities.stream().map(a -> new SimpleGrantedAuthority(a)).collect(Collectors.toSet());
UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUserCredentials().getUsername(), user.getUserCredentials().getPassword(), grantedAuths);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, "", grantedAuths);
SecurityContextHolder.getContext().setAuthentication(authentication);
return user;
}
use of org.springframework.security.core.GrantedAuthority in project dhis2-core by dhis2.
the class ModuleAccessVoter method vote.
/**
* Votes. Votes ACCESS_ABSTAIN if the object class is not supported. Votes
* ACCESS_GRANTED if there is a granted authority which equals attribute
* prefix + module name, or the module name is in the always accessible set.
* Otherwise votes ACCESS_DENIED.
*/
@Override
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
if (!supports(object.getClass())) {
LOG.debug("ACCESS_ABSTAIN [" + object.toString() + "]: Class not supported.");
return ACCESS_ABSTAIN;
}
ActionConfig target = (ActionConfig) object;
if (alwaysAccessible.contains(target.getPackageName())) {
LOG.debug("ACCESS_GRANTED [" + target.getPackageName() + "] by configuration.");
return ACCESS_GRANTED;
}
String requiredAuthority = attributePrefix + target.getPackageName();
for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
if (grantedAuthority.getAuthority().equals(requiredAuthority)) {
LOG.debug("ACCESS_GRANTED [" + target.getPackageName() + "]");
return ACCESS_GRANTED;
}
}
LOG.debug("ACCESS_DENIED [" + target.getPackageName() + "]");
return ACCESS_DENIED;
}
use of org.springframework.security.core.GrantedAuthority in project dhis2-core by dhis2.
the class ActionAccessVoter method anyAuthority.
private int anyAuthority(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
int supported = 0;
boolean found = false;
for (ConfigAttribute attribute : attributes) {
if (supports(attribute)) {
++supported;
for (GrantedAuthority authority : authentication.getAuthorities()) {
if (authority.getAuthority().equals(attribute.getAttribute())) {
found = true;
break;
}
}
}
}
if (!found && supported > 0) {
LOG.debug("ACCESS_DENIED [" + object.toString() + "]");
return ACCESS_DENIED;
}
if (supported > 0) {
LOG.debug("ACCESS_GRANTED [" + object.toString() + "]");
return ACCESS_GRANTED;
}
LOG.debug("ACCESS_ABSTAIN [" + object.toString() + "]: No supported attributes.");
return ACCESS_ABSTAIN;
}
use of org.springframework.security.core.GrantedAuthority in project dhis2-core by dhis2.
the class AllRequiredRoleVoter method vote.
@Override
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
int supported = 0;
for (ConfigAttribute attribute : attributes) {
if (this.supports(attribute)) {
++supported;
boolean found = false;
for (GrantedAuthority authority : authentication.getAuthorities()) {
if (attribute.getAttribute().equals(authority.getAuthority())) {
found = true;
break;
}
}
if (!found) {
return ACCESS_DENIED;
}
}
}
if (supported > 0) {
return ACCESS_GRANTED;
}
return ACCESS_ABSTAIN;
}
use of org.springframework.security.core.GrantedAuthority in project oc-explorer by devgateway.
the class CustomJPAUserDetailsService method loadUserByUsername.
/**
* Returns a populated {@link UserDetails} object. The username is first
* retrieved from the database and then mapped to a {@link UserDetails}
* object. We are currently using the {@link User} implementation from
* Spring
*/
@Override
public Person loadUserByUsername(final String username) throws UsernameNotFoundException {
try {
Person domainUser = personRepository.findByUsername(username);
Set<GrantedAuthority> grantedAuthorities = getGrantedAuthorities(domainUser);
domainUser.setAuthorities(grantedAuthorities);
return domainUser;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations