public final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); boolean flag = false; for (Authority authority: authentication.getAuthorities()) { if (authority.getAuthority().equals(role)) flag = true; }
public class CurrentUser extends User implements UserDetails { private final Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); public CurrentUser(User user) { super(user); initAuthorities(user); } private void initAuthorities(User user) { if (user.getRoles() == null) { return; } for (Authority role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getAuthority())); } } @Override public Collection<GrantedAuthority> getAuthorities() { return authorities; } // implement the rest of UserDetails interface accordingly }
@Override protected int updateAceEntity(AceEntity updatedAceEntity) { AceEntity existingAceEntity = getAceEntity(updatedAceEntity.getPermissionId(), updatedAceEntity.getAuthorityId(), updatedAceEntity.isAllowed(), updatedAceEntity.getAceType()); if(existingAceEntity != null) { for(AclMemberEntity aclMemberEntity : getAclMemberEntitiesByAuthority(getAuthority(updatedAceEntity.getAuthorityId()).getAuthority())) { aclMemberEntity.setAceId(updatedAceEntity.getId()); updateAclMember(aclMemberEntity); } deleteAceEntities(Collections.singletonList(existingAceEntity.getId())); } updatedAceEntity.incrementVersion(); return template.update(UPDATE_ACE, updatedAceEntity); }
@Override protected int updateAceEntity(AceEntity updatedAceEntity) { AceEntity existingAceEntity = getAceEntity(updatedAceEntity.getPermissionId(), updatedAceEntity.getAuthorityId(), updatedAceEntity.isAllowed(), updatedAceEntity.getAceType()); if(existingAceEntity != null) { for(AclMemberEntity aclMemberEntity : getAclMemberEntitiesByAuthority(getAuthority(updatedAceEntity.getAuthorityId()).getAuthority())) { aclMemberEntity.setAceId(updatedAceEntity.getId()); updateAclMember(aclMemberEntity); } deleteAceEntities(Collections.singletonList(existingAceEntity.getId())); } updatedAceEntity.incrementVersion(); return template.update(UPDATE_ACE, updatedAceEntity); }
entry.setAccessStatus(ace.isAllowed() ? AccessStatus.ALLOWED : AccessStatus.DENIED); entry.setAceType(ace.getAceType()); entry.setAuthority(authority.getAuthority());
entry.setAccessStatus(ace.isAllowed() ? AccessStatus.ALLOWED : AccessStatus.DENIED); entry.setAceType(ace.getAceType()); entry.setAuthority(authority.getAuthority());
public void testCreateUpdateAndDeleteAuth() throws Exception { final String authName = getName() + "-" + System.currentTimeMillis(); Authority authEntity= getAuth(authName); assertNull(authEntity); Authority createAuthEntity = createAuth(authName); assertNotNull(createAuthEntity); authEntity= getAuth(authName); assertEquals(createAuthEntity, authEntity); String newAuthName = authName+"-new"; updateAuth(authName, newAuthName); assertNull(getAuth(authName)); authEntity = getAuth(newAuthName); assertNotNull(authEntity); assertEquals(createAuthEntity.getId(), authEntity.getId()); assertEquals(newAuthName, authEntity.getAuthority()); deleteAuth(authEntity.getId()); assertNull(getAuth(newAuthName)); }