func (t *Top) route() (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf(fmt.Sprint(r) + string(debug.Stack())) } }() ctx := t.ctx odoc := ctx.Options().Document() nouns := scut.GetNouns(odoc) np := ctx.NonPortable() hl, err := highlev.New(ctx.Conducting().Hooks(), np.Resource(), nouns, np.Params()) if err != nil { display.New(ctx).Do(nil) return nil } deflev_i, _ := odoc.Get("default_level") deflev, _ := numcon.Int(deflev_i) ret, err := hl.Run(ctx.Db(), ctx.User(), deflev) if err != nil { return err } if ctx.NonPortable().View() { ctx.ViewContext().Publish("main_noun", hl.Noun()) files := []string{ hl.Noun() + "/" + hl.Verb(), hl.VerbLocation() + "/" + hl.Verb(), } return t.Get(ret, files) } else { return t.Post(ret, hl.Verb()) } return nil }
// Handles both Minute and date inputs func GenericToInterval(fromI, toI interface{}) (*Interval, error) { from, err := numcon.Int(fromI) if err != nil { return nil, err } to, err := numcon.Int(toI) if err != nil { return nil, err } if from > 1440 { from = int(DateToMinute(int64(from))) // Ouch... } if to > 1440 { to = int(DateToMinute(int64(to))) } interval, err := NewInterval(Minute(from), Minute(to)) if err != nil { return nil, err } return interval, nil }
// Immediately terminate the run of the action in case the user level is lower than the required level of the given action. // By default, if not otherwise specified, every action requires a level of 300 (admin rights). // // Made public to be able to call separately from PuzzlesSolved. // This way one can implement moderation. func UserAllowed(uni *context.Uni, auth_options map[string]interface{}) error { minlev := 300 lev_in_opt := auth_options["min_lev"] num, err := numcon.Int(lev_in_opt) if err == nil { minlev = num } if scut.Ulev(uni.Dat["_user"]) < minlev { return fmt.Errorf("You have no rights to do that.") } return nil }
func getList(ctx iface.Context, noun string, params ...interface{}) []interface{} { nouns, ok := ctx.Options().Document().GetM("nouns") if !ok { panic("Can't find nouns.") } inp := convert.ListToMap(params...) hl, err := highlev.New(ctx.Conducting().Hooks(), "/"+noun, nouns, inp) if err != nil { panic(err) } deflev_i, _ := ctx.Options().Document().Get("default_level") deflev, _ := numcon.Int(deflev_i) ret, err := hl.Run(ctx.Db(), ctx.User(), deflev) if err != nil { panic(err) } return ret }
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 (t *Top) routeWS() (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf(fmt.Sprint(r) + string(debug.Stack())) } }() ctx := t.ctx odoc := ctx.Options().Document() nouns, ok := odoc.GetM("nouns") if !ok { return fmt.Errorf("No nouns, cant route websocket.") } np := ctx.NonPortable() hl, err := highlev.New(ctx.Conducting().Hooks(), np.Resource(), nouns, np.Params()) if err != nil { return err } deflev_i, _ := odoc.Get("default_level") deflev, _ := numcon.Int(deflev_i) _, err = hl.Run(ctx.Db(), ctx.User(), deflev) return err }
// Works from Get or GetSingle only. func getSub(ctx iface.Context, noun string, params ...interface{}) []interface{} { nouns, ok := ctx.Options().Document().GetM("nouns") if !ok { panic("Can't find nouns.") } np := ctx.NonPortable() hl, err := highlev.New(ctx.Conducting().Hooks(), np.Resource(), nouns, np.Params()) if err != nil { panic(err) } inp := convert.ListToMap(params...) subhl, err := hl.Sub(noun, inp) if err != nil { panic(err) } deflev_i, _ := ctx.Options().Document().Get("defaultLevel") deflev, _ := numcon.Int(deflev_i) ret, err := subhl.Run(ctx.Db(), ctx.User(), deflev) if err != nil { panic(err) } return ret }
func toScheme(a interface{}) (Scheme, error) { empty := Scheme{} s := Scheme{} s.Specific = map[string]interface{}{} ai, err := numcon.Int(a) if err == nil && ai == 1 { s.Type = "string" return s, nil } am, ok := a.(map[string]interface{}) if !ok { return empty, fmt.Errorf("Can't interpret scheme.") } s.SliceMin = -1 // 0 is not good as a default value since it can be a legal one too. s.SliceMax = -1 for i, v := range am { switch i { case "must": s.Must, ok = v.(bool) if !ok { return empty, fmt.Errorf("Bad scheme: must should be a slice.") } case "type": s.Type, ok = v.(string) if !ok { return empty, fmt.Errorf("Bad scheme: type should be a string.") } case "slice": s.Slice, ok = v.(bool) if !ok { return empty, fmt.Errorf("Bad scheme: slice should be a bool.") } case "sliceMin": s.SliceMin, err = numcon.Int(v) if err != nil { return empty, fmt.Errorf("Bad scheme: sliceMin should be int compatible.") } case "sliceMax": s.SliceMax, err = numcon.Int(v) if err != nil { return empty, fmt.Errorf("Bad scheme: sliceMax should be int compatible.") } case "allOrNothing": s.AllOrNothing, ok = v.(bool) if !ok { return empty, fmt.Errorf("Bad scheme: allOrNothing should be a bool.") } case "min": s.Min, err = numcon.Int64(v) if err != nil { return empty, fmt.Errorf("Bad scheme: min should be int64 compatible.") } case "max": s.Max, err = numcon.Int64(v) if err != nil { return empty, fmt.Errorf("Bad scheme: max should be int64 compatible.") } case "regexp": s.Regexp, ok = v.(string) if !ok { return empty, fmt.Errorf("Bad scheme: regexp should be a string.") } case "ignore": s.Ignore, ok = v.(bool) if !ok { return empty, fmt.Errorf("Bad scheme: ignore should be a string.") } default: s.Specific[i] = v } } return s, 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 (t *Top) route() error { uni := t.uni paths := strings.Split(uni.Path, "/") if t.config.ServeFiles && strings.Index(paths[len(paths)-1], ".") != -1 { t.serveFile() return nil } t.buildUser() var ret []interface{} ret_rec := func(i ...interface{}) { ret = i } nouns, ok := uni.Opt["nouns"].(map[string]interface{}) if !ok { nouns = map[string]interface{}{ "options": opt_def, } } if _, ok := nouns["options"]; !ok { nouns["options"] = opt_def } uni.FilterCreator = func(c string, input map[string]interface{}) iface.Filter { return filterCreator(uni.Db, uni.Ev, nouns, input, c) } desc, err := glue.Identify(uni.Path, nouns, convert.Mapify(uni.Req.Form)) if err != nil { display.D(uni) return nil } default_level, _ := numcon.Int(uni.Opt["default_level"]) levi, ok := jsonp.Get(uni.Opt, fmt.Sprintf("nouns.%v.verbs.%v.level", desc.Sentence.Noun, desc.Sentence.Verb)) if !ok { levi = default_level } lev, _ := numcon.Int(levi) if scut.Ulev(uni.Dat["_user"]) < lev { return fmt.Errorf("Not allowed.") } inp, data, err := desc.CreateInputs(uni.FilterCreator) if err != nil { return err } if data != nil { if desc.Sentence.Noun != "options" { data, err = t.validate(desc.Sentence.Noun, desc.Sentence.Verb, data) if err != nil { return err } } inp = append(inp, data) } uni.Route = desc.Route uni.Sentence = desc.Sentence module := t.uni.NewModule(desc.VerbLocation) if !module.Exists() { return fmt.Errorf("Unkown module.") } ins := module.Instance() ins.Method(uni.Sentence.Verb).Call(ret_rec, inp...) if uni.Req.Method == "GET" { uni.Dat["main_noun"] = desc.Sentence.Noun uni.Dat["_points"] = []string{desc.Sentence.Noun + "/" + desc.Sentence.Verb, desc.VerbLocation + "/" + desc.Sentence.Verb} t.Get(ret) } else { t.Post(ret) } return nil }