示例#1
0
文件: user.go 项目: Laller/nocrud
func _new(db iface.Db, f iface.Filter, hooks iface.Hooks, client iface.Client) (*User, error) {
	uidI, err := client.GetDecrypted("user")
	if err != nil {
		return nil, err
	}
	id, err := db.ToId(uidI.(string))
	if err != nil {
		return nil, err
	}
	q := map[string]interface{}{
		"_id": id,
	}
	f.AddQuery(q)
	userDoc, err := f.SelectOne()
	if err != nil {
		return nil, err
	}
	var langs []string
	if langz, ok := userDoc.Data()["languages"].([]interface{}); ok {
		for _, v := range langz {
			langs = append(langs, v.(string))
		}
	} else if client.Languages() != nil {
		langs = client.Languages()
	} else {
		langs = []string{"en"}
	}
	return &User{
		userDoc,
		numcon.IntP(userDoc.Data()["level"]),
		langs,
	}, nil
}
示例#2
0
func RegisterUser(a iface.Filter, db iface.Db, user map[string]interface{}) (iface.Id, error) {
	user["password"] = hashPass(user["password"].(string))
	if _, has := user["level"]; !has {
		user["level"] = 100
	}
	user_id := db.NewId()
	user["_id"] = user_id
	err := a.Insert(user)
	if err != nil {
		return nil, fmt.Errorf("Name is not unique.")
	}
	return user_id, nil
}
示例#3
0
func latestOptdoc(db iface.Db) iface.Document {
	fil, err := db.NewFilter("options", map[string]interface{}{
		"sort": "-created",
	})
	if err != nil {
		panic(err)
	}
	o, err := fil.SelectOne()
	if err != nil {
		panic(err)
	}
	return o
}
示例#4
0
文件: user.go 项目: crufter/nocrud
func _new(db iface.Db, client iface.Client) (*User, error) {
	uidI, err := client.GetDecrypted("user")
	if err != nil {
		return nil, err
	}
	uidStr := uidI.(string)
	coll := "users"
	spl := strings.Split(uidStr, "|")
	if len(spl) > 1 {
		coll = spl[0]
		uidStr = spl[1]
	}
	f, err := db.NewFilter(coll, nil)
	if err != nil {
		return nil, err
	}
	id, err := db.ToId(uidStr)
	if err != nil {
		return nil, err
	}
	q := map[string]interface{}{
		"_id": id,
	}
	f.AddQuery(q)
	userDoc, err := f.SelectOne()
	if err != nil {
		return nil, err
	}
	var langs []string
	if langz, ok := userDoc.Data()["languages"].([]interface{}); ok {
		for _, v := range langz {
			langs = append(langs, v.(string))
		}
	} else if client.Languages() != nil {
		langs = client.Languages()
	} else {
		langs = []string{"en"}
	}
	return &User{
		userDoc,
		numcon.IntP(userDoc.Data()["level"]),
		langs,
	}, nil
}
示例#5
0
文件: highlev.go 项目: Laller/nocrud
func (h *HighLev) Run(db iface.Db, usr iface.User, defminlev int) ([]interface{}, error) {
	desc := h.desc
	levi, ok := jsonp.Get(h.nouns, fmt.Sprintf("%v.verbs.%v.level", desc.Sentence.Noun, desc.Sentence.Verb))
	if !ok {
		levi = defminlev
	}
	lev, _ := numcon.Int(levi)
	if usr.Level() < lev {
		return nil, fmt.Errorf("Not allowed.")
	}
	filterCreator := func(c string, input map[string]interface{}) (iface.Filter, error) {
		return db.NewFilter(c, input)
	}
	inp, data, err := desc.CreateInputs(filterCreator)
	if err != nil {
		return nil, err
	}
	if data != nil {
		if desc.Sentence.Noun != "options" {
			data, err = h.validate(desc.Sentence.Noun, desc.Sentence.Verb, data)
			if err != nil {
				return nil, err
			}
		}
		inp = append(inp, data)
	}
	module := h.hooks.Module(desc.VerbLocation)
	if !module.Exists() {
		return nil, fmt.Errorf("Unkown module.")
	}
	ins := module.Instance()
	var ret []interface{}
	ret_rec := func(i ...interface{}) {
		ret = i
	}
	err = ins.Method(desc.Sentence.Verb).Call(ret_rec, inp...)
	if err != nil {
		return nil, err
	}
	return ret, nil
}
示例#6
0
文件: highlev.go 项目: crufter/nocrud
func (h *HighLev) Run(db iface.Db, usr iface.User, defminlev int) ([]interface{}, error) {
	desc := h.desc
	// Authentication.
	levi, ok := jsonp.Get(h.nouns, fmt.Sprintf("%v.verbs.%v.level", desc.Sentence.Noun, desc.Sentence.Verb))
	if !ok {
		levi = defminlev
	}
	lev, _ := numcon.Int(levi)
	if usr.Level() < lev {
		return nil, fmt.Errorf("Not allowed.")
	}
	err := h.userChecks(usr)
	if err != nil {
		return nil, err
	}
	filterCreator := func(c string, input map[string]interface{}) (iface.Filter, error) {
		return db.NewFilter(c, input)
	}
	inp, data, err := desc.CreateInputs(filterCreator)
	if err != nil {
		return nil, err
	}
	ownLev, own := h.ownLev()
	if len(inp) > 0 {
		if f, ok := inp[0].(iface.Filter); ok {
			// This hook allows you to modify a filter before a verb accesses it.
			h.hooks.Select("TopModFilter").Fire(f)
			h.hooks.Select(f.Subject() + "TopModFilter").Fire(f)
		}
		if own && lev <= ownLev {
			if f, ok := inp[0].(iface.Filter); ok {
				f.AddQuery(map[string]interface{}{
					"createdBy": usr.Id(),
				})
			}
		}
	}
	if data != nil {
		if desc.Sentence.Noun != "options" {
			data, err = h.validate(desc.Sentence.Noun, desc.Sentence.Verb, data)
			if err != nil {
				return nil, err
			}
		}
		inp = append(inp, data)
	}
	module := h.hooks.Module(desc.VerbLocation)
	if !module.Exists() {
		return nil, fmt.Errorf("Unkown module.")
	}
	ins := module.Instance()
	var ret []interface{}
	ret_rec := func(i ...interface{}) {
		ret = i
	}
	err = ins.Method(desc.Sentence.Verb).Call(ret_rec, inp...)
	if err != nil {
		return nil, err
	}
	return ret, nil
}