Exemple #1
0
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
}
Exemple #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
}
Exemple #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
}
Exemple #4
0
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
}
Exemple #5
0
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
}
Exemple #6
0
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
}