Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.1 kB
2
Indexable
Never
public without sharing class CEP_VFC_PrivacyRenewalController {

    public Boolean isPersonaFisica {set;get;}
    public Integer step {get; set;}
    public Boolean isScaduto {set;get;}
    public Boolean isEsistente {set;get;}
    public Boolean isCompleto {set;get;}
    public List<String> consensiList {get; set;}

    public String consensoCommericaleAcea {get;set;}
    public String consensoCommericaleTerzi {get;set;}
    public String consensoIscrizioneProgrammi {get;set;}
    public String consensoProfilazione {get;set;}

    public String nomePersonaFisica {get;set;}
    
    /*OGGETTI*/
    public String idCriptato {set;get;}
    public String idDecriptato {set;get;}
    public FileMetadata__c touchPoint {set;get;}
    public PrivacyChange__c privacy {get;set;}
    
    /*CLASSI*/
    public CEP_UTL_TouchPoint tpServ = new CEP_UTL_TouchPoint();
    
    /*CAPTCHA*/
    public Boolean verified {get; set;}
    public String response  { 
        get {
            return ApexPages.currentPage().getParameters().get('g-recaptcha-response').escapeHtml4();
        }
    }
    public String publicKey {get;set;}  
    private String remoteHost{
        get {
            String ret = '127.0.0.1';
            Map<String, String> hdrs = ApexPages.currentPage().getHeaders();
            if (hdrs.get('x-original-remote-addr')!= null)
                ret = hdrs.get('x-original-remote-addr');
            else if (hdrs.get('X-Salesforce-SIP')!= null)
                ret = hdrs.get('X-Salesforce-SIP');
            return ret;
        }
    }
    private static String secretKey = '6LdQat8ZAAAAAEvSFlT-tjfosVuDzZzh6HcmlAk_';
    private static String baseUrl = 'https://www.google.com/recaptcha/api/siteverify';
    
    public CEP_VFC_PrivacyRenewalController() {
        this.publicKey = '6LdQat8ZAAAAALHZB7c6MpQGLTkh4Y_sSj2SrCjA';
        this.verified = false;
        this.isEsistente = true;
        this.isScaduto = false;
        this.isPersonaFisica = false;
        List<CEP_TouchPointEnvironment__mdt> touchPointEnvironment = tpServ.getTouchPointEnvironment();

        

        this.idCriptato = ApexPages.currentPage().getParameters().get('tp').escapeHtml4();
        if(idCriptato == null) {
            this.isScaduto = true;
            this.isEsistente = false;
            return;
        }

        this.idDecriptato = tpServ.decryptId (idCriptato, touchPointEnvironment[0].PrivateKey__c);
        if(idDecriptato == null) {
            this.isScaduto = true;
            this.isEsistente = false;
            return;
        }

        this.touchPoint = tpServ.getFileMetadataByRecordId(idDecriptato);
        if(touchPoint == null) {
            this.isScaduto = true;
            this.isEsistente = false;
            return;
        }

        if (touchPoint.Account__r.RecordType.DeveloperName == 'CEP_RT_PhysicalPerson') {
            this.isPersonaFisica = true;
            this.nomePersonaFisica = touchPoint.Account__r.FirstName__c;
        }

        if(touchPoint.Phase__c == 'Expired' || touchPoint.Expired__c) {
            this.isScaduto = true;
        }

        if(touchPoint.Phase__c == 'Confirmed') {
            this.isScaduto = true;
            this.isCompleto = true;
        }
        this.step = 1;
        this.privacy = tpServ.getPrivacyChangeById(this.touchPoint.Case__r.PrivacyChange__c);
        this.consensoCommericaleAcea = privacy.SolicitationConsent__c == 'Si' ? 'Presto il consenso' : privacy.SolicitationConsent__c == 'No' ? 'Nego il consenso' : null;
        this.consensoCommericaleTerzi = privacy.ThirdPartySolicitationConsent__c == 'Si' ? 'Presto il consenso' : privacy.ThirdPartySolicitationConsent__c == 'No' ? 'Nego il consenso' : null;
        this.consensoIscrizioneProgrammi = privacy.LoyaltyConsent__c == 'Si' ? 'Presto il consenso' : privacy.LoyaltyConsent__c == 'No' ? 'Nego il consenso' : null;
        this.consensoProfilazione = privacy.ProfilingConsent__c == 'Si' ? 'Presto il consenso' : privacy.ProfilingConsent__c == 'No' ? 'Nego il consenso' : null;
        
    }
    
    public PageReference submit() {
        HttpResponse res = getGoogleReCAPTCHAResponse(baseUrl, 'secret=' + secretKey + '&remoteip=' + remoteHost + '&response=' + response);
        if (res != null ) {
            JSONParser parser = JSON.createParser(res.getBody());
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'success')) {
                    parser.nextToken();
                    verified = parser.getBooleanValue();
                    break;
                }
            }
        }

        if(verified){
            return null;
        }
        else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'CAPTCHA non verificato'));
            return null;
        }
    }
     
    private static HttpResponse getGoogleReCAPTCHAResponse(String requestURL, String body) {
        HttpResponse response = null;
        HttpRequest req = new HttpRequest();
        req.setEndpoint(requestURL);
        req.setMethod('POST');
        req.setBody (body);
        try{
            Http http = new Http();
            response = http.send(req);
        }
        catch(System.Exception ex){
            System.debug('ERROR Message-' + ex.getMessage());
        }
        return response;
    }
    
    public void step2() {
        touchPoint.SolicitationConsent__c = consensoCommericaleAcea == 'Presto il consenso' ? 'Si' : 'No';
        touchPoint.ThirdPartySolicitationConsent__c = consensoCommericaleTerzi == 'Presto il consenso' ? 'Si' : 'No';
        touchPoint.LoyaltyConsent__c = consensoIscrizioneProgrammi == 'Presto il consenso' ? 'Si' : 'No';
        touchPoint.ProfilingConsent__c = consensoProfilazione == 'Presto il consenso' ? 'Si' : 'No';
        tpServ.accept(touchPoint);
        step = 2;
    }
}