Example #1
0
func userLoginParamCheck(doc interface{}) (username string, password string, paraErr error) {
	var document interface{}
	document, paraErr = mejson.Marshal(doc)
	if paraErr != nil {
		logrus.Errorf("marshal user err is %v", paraErr)
		return
	}

	docJson := document.(map[string]interface{})
	usernameDoc := docJson["username"]
	if usernameDoc == nil {
		logrus.Errorln("invalid parameter ! username can not be null")
		paraErr = errors.New("Invalid parameter!")
		return
	} else {
		username = usernameDoc.(string)
	}

	passwordDoc := docJson["password"]
	if passwordDoc == nil {
		logrus.Errorln("invalid parameter ! password can not be null")
		paraErr = errors.New("Invalid parameter!")
		return
	} else {
		password = passwordDoc.(string)
	}
	return
}
Example #2
0
func (p *AuthService) getValueFromField(authParam *AuthParam, key string, data interface{}) *[]string {
	ret := []string{}
	originData := data
	if originData == nil {
		logrus.Infoln("getValueFromField data object is null, will do query first ")
		originData = p.getObjectById(authParam.auth_instanceId, authParam.auth_collectionName)
	}

	if originData == nil {
		return &ret
	}

	//convert to json format
	formatdata, err := mejson.Marshal(originData)
	if err != nil {
		logrus.Errorln("convert origindata object error %v", err)
		return &ret
	}

	value := p.getValueFromObject(formatdata, key)
	if len(value) != 0 {
		ret = append(ret, value)
	}

	return &ret
}
Example #3
0
func marsh() {
	session, err := mgo.Dial("localhost")
	if err != nil {
		panic(err)
	}
	defer session.Close()

	c := session.DB("test").C("people")
	result := []bson.M{}
	err = c.Find(bson.M{}).All(&result)
	if err != nil {
		panic(err)
	}
	bytes, err := mejson.Marshal(result)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", bytes)
}
func tokenCreateParamCheck(doc interface{}) (email string, passwd string, tenant string, paraErr error) {
	var document interface{}
	document, paraErr = mejson.Marshal(doc)
	if paraErr != nil {
		logrus.Errorf("marshal user credential err is %v", paraErr)
		return
	}

	docJson := document.(map[string]interface{})
	usernameDoc := docJson["username"]
	if usernameDoc == nil {
		logrus.Errorln("invalid parameter ! username can not be null")
		paraErr = errors.New("invalid parameter!")
		return
	} else {
		email = usernameDoc.(string)
	}

	passwordDoc := docJson["password"]
	if passwordDoc == nil {
		logrus.Errorln("invalid parameter ! password can not be null")
		paraErr = errors.New("invalid parameter!")
		return
	} else {
		passwd = passwordDoc.(string)
	}

	tenantDoc := docJson["tenantname"]
	if tenantDoc == nil {
		logrus.Errorln("invalid parameter ! tenantname can not be null")
		paraErr = errors.New("invalid parameter!")
		return
	} else {
		tenant = tenantDoc.(string)
	}

	return
}
func getValueFromField(authParam *AuthParam, key string, data interface{}) string {
	originData := data
	if originData == nil {
		logrus.Infoln("getValueFromField data object is null, will do query first ")
		originData = getObjectById(authParam.auth_instanceId, authParam.auth_collectionName)
	}

	if originData == nil {
		return ""
	}

	//convert to json format
	formatdata, err := mejson.Marshal(originData)
	if err != nil {
		logrus.Errorln("convert origindata object error %v", err)
		return ""
	}

	record := formatdata.(map[string]interface{})

	if key == "user_id" {
		oid := record["_id"].(map[string]interface{})
		return oid["$oid"].(string)
	} else if key == "tenant_id" {
		return record["tenantid"].(string)
	} else if key == "token_user_id" { //for token
		userobj := record["user"]
		if userobj != nil {
			userjson := userobj.(map[string]interface{})
			return userjson["id"].(string)
		} else {
			return ""
		}
	} else {
		logrus.Errorln("not supported field key:", key)
		return ""
	}
}
Example #6
0
func userRegistryParamCheck(doc interface{}) (username string, email string, password string, company string, paraErr error) {
	var document interface{}
	document, paraErr = mejson.Marshal(doc)
	if paraErr != nil {
		logrus.Errorf("marshal user err is %v", paraErr)
		return
	}

	docJson := document.(map[string]interface{})
	emailDoc := docJson["email"]
	if emailDoc == nil {
		logrus.Errorln("invalid parameter ! email can not be null")
		paraErr = errors.New("Invalid parameter!")
		return
	} else {
		email = emailDoc.(string)
	}

	usernameDoc := docJson["username"]
	if usernameDoc == nil {
		logrus.Errorln("invalid parameter ! username can not be null")
		paraErr = errors.New("Invalid parameter!")
		return
	} else {
		username = usernameDoc.(string)
	}

	password = "******"

	companyDoc := docJson["company"]
	if companyDoc != nil {
		company = companyDoc.(string)
	}

	return
}
Example #7
0
//
// Finds document(/s) in collection
//
func (d *Resource) CollectionFindHandler(req *restful.Request, resp *restful.Response) {
	// Mongo session
	session, needclose, err := d.SessMng.Get(getParam("alias", req))
	if err != nil {
		WriteError(err, resp)
		return
	}

	// Close session if it's needed
	if needclose {
		defer session.Close()
	}

	// Mongo Collection
	col := d.GetMongoCollection(req, session)

	// Compose a query from request
	query, one, err := d.ComposeQuery(col, req)
	if err != nil {
		WriteStatusError(400, err, resp)
		return
	}

	// If _id parameter is included in path
	// 	queries only one document.
	// Get documents from database
	if one {
		// Get one document
		document := bson.M{}
		err = query.One(&document)
		if err != nil {
			WriteError(err, resp)
			return
		}
		var jsonDocument interface{}
		if req.QueryParameter("extended_json") == "true" {
			jsonDocument, err = mejson.Marshal(document)
			if err != nil {
				WriteError(err, resp)
				return
			}
		} else {
			jsonDocument = document
		}
		WriteResponse(jsonDocument, resp)
		return
	}

	// Get all documents
	documents := []bson.M{}
	err = query.All(&documents)
	if err != nil {
		WriteError(err, resp)
		return
	}

	var jsonDocuments interface{}
	if req.QueryParameter("extended_json") == "true" {
		jsonDocuments, err = mejson.Marshal(documents)
		if err != nil {
			WriteError(err, resp)
			return
		}
	} else {
		jsonDocuments = documents
	}

	res := struct {
		Success bool        `json:"success"`
		Count   interface{} `json:"count,omitempty"`
		Prev    string      `json:"prev_url,omitempty"`
		Next    string      `json:"next_url,omitempty"`
		Data    interface{} `json:"data"`
	}{Success: true, Data: jsonDocuments}

	// Get limit amount
	limitnum := 10
	if limit := req.QueryParameter("limit"); len(limit) > 0 {
		limitnum, _ = strconv.Atoi(limit)
	}

	// If got full limit set next link
	if len(documents) == limitnum {
		res.Prev, res.Next = d.prevnexturl(req)
	}

	// Count documents if count parameter is included in query
	if c, _ := strconv.ParseBool(req.QueryParameter("count")); c {
		query.Skip(0)
		query.Limit(0)
		if n, err := query.Count(); err == nil {
			res.Count = n
			resp.AddHeader("X-Object-Count", strconv.Itoa(n))
		}
	}

	// Write result back to client
	resp.WriteEntity(res)
}
Example #8
0
func (t *Transformer) transformOne(msg *message.Msg) (*message.Msg, error) {

	var (
		doc    interface{}
		value  otto.Value
		outDoc otto.Value
		result interface{}
		err    error
	)

	// short circuit for deletes and commands
	if msg.Op == message.Delete || msg.Op == message.Command {
		return msg, nil
	}

	now := time.Now().Nanosecond()
	if msg.IsMap() {
		if doc, err = mejson.Marshal(msg.Data); err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
	} else {
		doc = msg.Data
	}

	if value, err = t.vm.ToValue(doc); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	// now that we have finished casting our map to a bunch of different types,
	// lets run our transformer on the document
	beforeVM := time.Now().Nanosecond()
	if outDoc, err = t.vm.Call(`module.exports`, nil, value); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	if result, err = outDoc.Export(); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	afterVM := time.Now().Nanosecond()

	switch r := result.(type) {
	case map[string]interface{}:
		doc, err := mejson.Unmarshal(r)
		if err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		msg.Data = map[string]interface{}(doc)
	default:
		msg.Data = r
	}

	if t.debug {
		then := time.Now().Nanosecond()
		fmt.Printf("document transformed in %dus.  %d to marshal, %d in the vm, %d to unmarshal\n", (then-now)/1000, (beforeVM-now)/1000, (afterVM-beforeVM)/1000, (then-afterVM)/1000)
	}

	return msg, nil
}
Example #9
0
func (t *Transformer) transformOne(msg *message.Msg) (*message.Msg, error) {

	var (
		doc    interface{}
		value  otto.Value
		outDoc otto.Value
		result interface{}
		err    error
	)

	// short circuit for deletes and commands
	if msg.Op == message.Command {
		return msg, nil
	}

	now := time.Now().Nanosecond()
	currMsg := map[string]interface{}{
		"data": msg.Data,
		"ts":   msg.Timestamp,
		"op":   msg.Op.String(),
		"ns":   msg.Namespace,
	}
	if msg.IsMap() {
		if doc, err = mejson.Marshal(msg.Data); err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		currMsg["data"] = doc
	}

	if value, err = t.vm.ToValue(currMsg); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	// now that we have finished casting our map to a bunch of different types,
	// lets run our transformer on the document
	beforeVM := time.Now().Nanosecond()
	if outDoc, err = t.vm.Call(`module.exports`, nil, value); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	if result, err = outDoc.Export(); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	afterVM := time.Now().Nanosecond()

	if err = t.toMsg(result, msg); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, err
	}

	if t.debug {
		then := time.Now().Nanosecond()
		fmt.Printf("document transformed in %dus.  %d to marshal, %d in the vm, %d to unmarshal\n", (then-now)/1000, (beforeVM-now)/1000, (afterVM-beforeVM)/1000, (then-afterVM)/1000)
	}

	return msg, nil
}
func (t *Transformer) transformOne(msg *message.Msg) (*message.Msg, error) {

	var (
		doc    interface{}
		value  otto.Value
		outDoc otto.Value
		result interface{}
		err    error
	)

	// short circuit for deletes and commands
	if msg.Op == message.Command {
		return msg, nil
	}

	now := time.Now().Nanosecond()
	fullDoc := map[string]interface{}{
		"data": msg.Data,
		"ts":   msg.Timestamp,
		"op":   msg.Op.String(),
	}
	if msg.IsMap() {
		if doc, err = mejson.Marshal(msg.Data); err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		fullDoc["data"] = doc
	}

	if value, err = t.vm.ToValue(fullDoc); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	// now that we have finished casting our map to a bunch of different types,
	// lets run our transformer on the document
	beforeVM := time.Now().Nanosecond()
	if outDoc, err = t.vm.Call(`module.exports`, nil, value); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	if result, err = outDoc.Export(); err != nil {
		t.pipe.Err <- t.transformerError(ERROR, err, msg)
		return msg, nil
	}

	afterVM := time.Now().Nanosecond()

	fullDoc, ok := result.(map[string]interface{})
	if !ok {
		t.pipe.Err <- t.transformerError(ERROR, fmt.Errorf("returned doc was not a map[string]interface{}"), msg)
		return msg, fmt.Errorf("returned doc was not a map[string]interface{}")
	}

	msg.Op = message.OpTypeFromString(fullDoc["op"].(string))
	msg.Timestamp = fullDoc["ts"].(int64)
	switch data := fullDoc["data"].(type) {
	case otto.Value:
		exported, err := data.Export()
		if err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		d, err := mejson.Unmarshal(exported.(map[string]interface{}))
		if err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		msg.Data = map[string]interface{}(d)
	case map[string]interface{}:
		d, err := mejson.Unmarshal(data)
		if err != nil {
			t.pipe.Err <- t.transformerError(ERROR, err, msg)
			return msg, nil
		}
		msg.Data = map[string]interface{}(d)
	default:
		msg.Data = data
	}

	if t.debug {
		then := time.Now().Nanosecond()
		fmt.Printf("document transformed in %dus.  %d to marshal, %d in the vm, %d to unmarshal\n", (then-now)/1000, (beforeVM-now)/1000, (afterVM-beforeVM)/1000, (then-afterVM)/1000)
	}

	return msg, nil
}