@Override
    protected Account getAccount(final IdentityContext context, final UsernamePasswordCredentials credentials) {
        List<? extends Account> result = findByLoginName(context, SaleAgent.class, credentials.getUsername());

        if (result.isEmpty()) {
            result = findByLoginName(context, Agent.class, credentials.getUsername());
        }

        if (result.isEmpty()) {
            result = findByLoginName(context, User.class, credentials.getUsername());
        }

        if (result.isEmpty()) {
            return null;
        }

        return result.get(0);
    }

    private <A extends Agent> List<A> findByLoginName(IdentityContext context, Class<A> type, String loginName) {
        IdentityManager identityManager = getIdentityManager(context);
        IdentityQuery<A> query = identityManager.createIdentityQuery(type);

        query.setParameter(Agent.LOGIN_NAME, loginName);

        List<A> result = query.getResultList();

        if (result.isEmpty()) {
            return Collections.emptyList();
        }

        return result;
    }