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 }
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 }
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 }
func OnlyAdmin(u iface.User) { if u.Level() < 300 { panic("Only an admin can do this operation.") } }
func NotAdmin(user iface.User) bool { return user.Level() < 300 }
func IsStranger(user iface.User) bool { return user.Level() == 0 }
func IsRegistered(user iface.User) bool { return user.Level() >= 100 }
func IsModerator(user iface.User) bool { return user.Level() >= 200 }
func IsAdmin(user iface.User) bool { return user.Level() >= 300 }
func isProfessional(u iface.User) bool { _, ok := u.Data()["professional"] return ok }