Esempio n. 1
0
func (h *HighLev) userChecks(user iface.User) error {
	verbSpec, ok := jsonp.GetM(h.nouns, fmt.Sprint("%v.verbs.%v.userCrit", h.desc.Sentence.Noun, h.desc.Sentence.Verb))
	if ok {
		_, err := sanitize.Fast(verbSpec, user.Data())
		return err
	}
	nounSpec, ok := jsonp.GetM(h.nouns, fmt.Sprintf("%v.userCrit", h.desc.Sentence.Noun))
	if !ok {
		return nil
	}
	_, err := sanitize.Fast(nounSpec, user.Data())
	return err
}
Esempio n. 2
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
}
Esempio n. 3
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
}
Esempio n. 4
0
func OnlyAdmin(u iface.User) {
	if u.Level() < 300 {
		panic("Only an admin can do this operation.")
	}
}
Esempio n. 5
0
func NotAdmin(user iface.User) bool {
	return user.Level() < 300
}
Esempio n. 6
0
func IsStranger(user iface.User) bool {
	return user.Level() == 0
}
Esempio n. 7
0
func IsRegistered(user iface.User) bool {
	return user.Level() >= 100
}
Esempio n. 8
0
func IsModerator(user iface.User) bool {
	return user.Level() >= 200
}
Esempio n. 9
0
func IsAdmin(user iface.User) bool {
	return user.Level() >= 300
}
Esempio n. 10
0
func isProfessional(u iface.User) bool {
	_, ok := u.Data()["professional"]
	return ok
}