List of usage examples for org.springframework.security.ldap.userdetails LdapUserDetailsImpl getAuthorities
@Override
public Collection<GrantedAuthority> getAuthorities()
From source file:org.schedoscope.metascope.service.MetascopeUserService.java
public boolean isAdmin() { if (getUser().isAdmin()) { return true; }// www .j a v a 2s .c o m Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof LdapUserDetailsImpl) { LdapUserDetailsImpl ldapUser = (LdapUserDetailsImpl) principal; for (GrantedAuthority authoritiy : ldapUser.getAuthorities()) { for (String adminGroup : config.getAdminGroups().split(",")) { String role = "ROLE_" + adminGroup.toUpperCase(); if (authoritiy.getAuthority().equalsIgnoreCase(role)) { return true; } } } } return false; }
From source file:org.schedoscope.metascope.service.MetascopeUserService.java
/** * Get the user object for the logged in user * * @return/*from w w w.j av a 2 s . com*/ * @throws NamingException */ public MetascopeUser getUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return null; } Object principal = authentication.getPrincipal(); if (principal instanceof LdapUserDetailsImpl) { LdapUserDetailsImpl ldapUser = (LdapUserDetailsImpl) principal; MetascopeUser userEntity = metascopeUserRepository.findByUsername(ldapUser.getUsername()); if (userEntity == null) { createUser(ldapUser.getUsername(), "", "", sha256("" + System.currentTimeMillis()), false, null); } // sync user with ldap userEntity = metascopeUserRepository.findByUsername(ldapUser.getUsername()); DirContextAdapter dca = (DirContextAdapter) ldap.lookup(ldapUser.getDn()); Attributes attr = dca.getAttributes(); String mail = ""; String fullname = ""; try { mail = (String) attr.get("mail").get(); fullname = (String) attr.get("displayName").get(); } catch (NamingException e) { // if not found, ignore .. } boolean admin = false; for (GrantedAuthority authoritiy : ldapUser.getAuthorities()) { for (String adminGroup : config.getAdminGroups().split(",")) { String role = "ROLE_" + adminGroup.toUpperCase(); if (authoritiy.getAuthority().equalsIgnoreCase(role)) { admin = true; } } } boolean changes = false; if (userEntity.getEmail() == null || !userEntity.getEmail().equals(mail)) { userEntity.setEmail(mail); changes = true; } if (userEntity.getFullname() == null || !userEntity.getFullname().equals(fullname)) { userEntity.setFullname(fullname); changes = true; } if (admin) { if (!userEntity.isAdmin()) { changes = true; } userEntity.setUserrole(Role.ROLE_ADMIN); } else { if (userEntity.isAdmin()) { changes = true; } userEntity.setUserrole(Role.ROLE_USER); } if (changes) { metascopeUserRepository.save(userEntity); } return userEntity; } else if (principal instanceof User) { User userDetails = (User) principal; MetascopeUser user = metascopeUserRepository.findByUsername(userDetails.getUsername()); if (user == null) { LOG.warn("User from session not found. username={}", userDetails.getUsername()); return null; } return user; } return null; }