@Test public void testGetGrantedAuthoritiesWithLoginAlias() throws Exception { String loginAlias = "testLoginAlias@testdomain.com"; String ambariUserName = "user"; PowerMock.mockStatic(AuthorizationHelper.class); expect(AuthorizationHelper.resolveLoginAliasToUserName(loginAlias)).andReturn(ambariUserName); PowerMock.replay(AuthorizationHelper.class); AmbariLdapAuthoritiesPopulator populator = createMockBuilder(AmbariLdapAuthoritiesPopulator.class) .withConstructor(helper, userDAO, memberDAO, privilegeDAO, users).createMock(); expect(userEntity.getActive()).andReturn(true); expect(users.getUserPrivileges(userEntity)).andReturn(Collections.singletonList(privilegeEntity)); expect(userDAO.findLdapUserByName(ambariUserName)).andReturn(userEntity); replayAll(); populator.getGrantedAuthorities(userData, loginAlias); PowerMock.verify(AuthorizationHelper.class); verifyAll(); }
@Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findLdapUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); }
AmbariLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { @Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findLdapUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); } }
AmbariLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { @Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findLdapUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); } @Inject  AmbariLdapAuthoritiesPopulator(AuthorizationHelper authorizationHelper,
                                        UserDAO userDAO, MemberDAO memberDAO, PrivilegeDAO privilegeDAO,
                                        Users users); }
AmbariLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { @Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findLdapUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); } @Inject  AmbariLdapAuthoritiesPopulator(AuthorizationHelper authorizationHelper,
                                        UserDAO userDAO, MemberDAO memberDAO, PrivilegeDAO privilegeDAO,
                                        Users users); @Override Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username); }
AmbariLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator { @Override public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) { username = AuthorizationHelper.resolveLoginAliasToUserName(username); log.info("Get authorities for user " + username + " from local DB"); UserEntity user; user = userDAO.findLdapUserByName(username); if (user == null) { log.error("Can't get authorities for user " + username + ", he is not present in local DB"); return Collections.emptyList(); } if(!user.getActive()){ throw new InvalidUsernamePasswordCombinationException(); } Collection<PrivilegeEntity> privilegeEntities = users.getUserPrivileges(user); return authorizationHelper.convertPrivilegesToAuthorities(privilegeEntities); } @Inject  AmbariLdapAuthoritiesPopulator(AuthorizationHelper authorizationHelper,
                                        UserDAO userDAO, MemberDAO memberDAO, PrivilegeDAO privilegeDAO,
                                        Users users); @Override Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username);  }