class ProxyUserValidation {

    public static void validateImpersonator(String impersonator, String impersonated) {
        String mappedImpersonated = AbstractUserMapper.getInstance().map(impersonated);
        EmailValidation.validateEmail(mappedImpersonated);
        MDC.put(LoggingUtils.MDC_AUTH_MODE_PROXY_IMPERSONATED_USER_KEY, impersonated);
        List<? extends Config> proxyConfigs = AppSettings.getInstance().getConfigList(AppSettings.PROXY_USERS);
        for (Config proxyConfig : proxyConfigs) {
            String proxy = proxyConfig.getString(CONFIG_PROXY);
            if (impersonator.equals(proxy)) {
                if (isWhitelistedByUsername(proxyConfig, mappedImpersonated)) {
                    // The user is directly whitelisted by its username
                    return;
                }
                else if (isWhitelistedByGroupMembership(proxyConfig, mappedImpersonated)) {
                    // The user is whitelisted by group membership
                    return;
                }
            }
        }
        throw Status.PERMISSION_DENIED
            .withDescription("Impersonation disallowed for `" + impersonator + "`")
            .asRuntimeException();
    }

}