func NewPhpDecoder(phpSession string) *PhpDecoder {
	decoder := &PhpDecoder{
		source:  strings.NewReader(phpSession),
		decoder: php_serialize.NewUnSerializer(""),
	}
	decoder.decoder.SetReader(decoder.source)
	return decoder
}
Beispiel #2
0
func (a *Adaptor) AddUserPartner(signup client.Signup) *adaptor.AdaptorError {
	data, err := base64.StdEncoding.DecodeString(padBase64(signup.SendGridPartner))
	if err != nil {
		ln.Err("unable to decode sendgrid partner data", ln.Map{"err": err.Error(), "value": signup.SendGridPartner})
		return adaptor.NewError(ErrorDataStorage)
	}

	decoder := php_serialize.NewUnSerializer(string(data))
	decodedSendGridPartner, err := decoder.Decode()
	if err != nil {
		ln.Err(fmt.Sprintf("unable to unserialize data for signup sendgridPartner - '%s'", string(data)), ln.Map{"err": err.Error()})
		return adaptor.NewError(ErrorDataStorage)
	}

	// Deserialize php object
	var partnerName string
	var partnerCredential string

	sendGridPartnerPhpArray, ok := decodedSendGridPartner.(php_serialize.PhpArray)

	if !ok {
		errMsg := fmt.Sprintf("Unable to convert %v to PhpArray", decodedSendGridPartner)
		ln.Err(errMsg, ln.Map{"err": errMsg})
		return adaptor.NewError(ErrorDataStorage)
	}

	if partnerNameObject, ok := sendGridPartnerPhpArray["partner"]; !ok {
		errMsg := "Array value decoded incorrectly, key `partner` doest not exists"
		ln.Err(errMsg, ln.Map{"err": errMsg})
		return adaptor.NewError(ErrorDataStorage)
	} else if partnerName, ok = partnerNameObject.(string); !ok {
		errMsg := fmt.Sprintf("Unable to convert %v to string", partnerNameObject)
		ln.Err(errMsg, ln.Map{"err": errMsg})
		return adaptor.NewError(ErrorDataStorage)
	}

	if partnerCredentialObject, ok := sendGridPartnerPhpArray["partner_credential"]; !ok {
		errMsg := "Array value decoded incorrectly, key `partner_credential` doest not exists"
		ln.Err(errMsg, ln.Map{"err": errMsg})
		return adaptor.NewError(ErrorDataStorage)
	} else if partnerCredentialObject == nil {
		partnerCredential = ""
	} else if partnerCredentialInt, ok := partnerCredentialObject.(int); !ok {
		if partnerCredential, ok = partnerCredentialObject.(string); !ok {
			errMsg := fmt.Sprintf("Unable to convert %v to string or int", partnerCredentialObject)
			ln.Err(errMsg, ln.Map{"err": errMsg})
			return adaptor.NewError(ErrorDataStorage)
		}
	} else {
		partnerCredential = strconv.Itoa(partnerCredentialInt)
	}

	getResult := make([]PartnerRecord, 0)

	err = a.apidClient.DoFunction("get", url.Values{
		"tableName": []string{"partner"},
		"where":     []string{fmt.Sprintf(`{"label":"%s"}`, partnerName)},
	}, &getResult)
	if err != nil {
		ln.Err("unable to fetch partner details for label", ln.Map{"err": err.Error(), "partner": partnerName})
		return adaptor.NewError(ErrorDataStorage)
	}

	if len(getResult) != 1 {
		ln.Err(fmt.Sprintf("wrong number or results for partner search. got %d, want 1", len(getResult)), ln.Map{"err": err.Error(), "partner": partnerName})
		return adaptor.NewError(ErrorDataStorage)
	}

	type UserPartner struct {
		UserID            int    `json:"user_id"`
		PartnerID         int    `json:"partner_id"`
		PartnerCredential string `json:"partner_credential"`
	}
	insertData := UserPartner{
		UserID:            signup.UserID,
		PartnerID:         getResult[0].ID,
		PartnerCredential: partnerCredential,
	}

	jsonBytes, err := json.Marshal([]UserPartner{insertData})

	if err != nil {
		ln.Err(fmt.Sprintf("unable to marshal insertData"), ln.Map{"err": err.Error()})
		return adaptor.NewError(ErrorDataStorage)
	}

	// store the partner data
	var addResult string
	err = a.apidClient.DoFunction("add", url.Values{
		"tableName": []string{"user_partner"},
		"values":    []string{string(jsonBytes)},
	}, &addResult)

	if err != nil {
		ln.Err(fmt.Sprintf("unable to call apid add method on user_partner"), ln.Map{"err": err.Error()})
		return adaptor.NewError(ErrorDataStorage)
	}

	if addResult != ApidSuccess {
		ln.Err(fmt.Sprintf("apid set for user_partner returned '%s', want 'success'", addResult), nil)
		return adaptor.NewError(ErrorDataStorage)
	}

	return nil
}