Beispiel #1
0
func main() {
	fmt.Println("test")

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Panicln("Can't start transaction")
		return
	}

	var u1 model.User

	u1.Id = 1
	err = u1.Get(tx)
	if err != nil {
		log.Println("paf, u.Get: ", err)
	}

	//u1.Password = "******"
	ok, err := u1.Login(tx)
	if err != nil {
		log.Println("paf, u.Login: "******"Login OK")
	} else {
		log.Println("Login BAD")
	}

	fmt.Println("User: "******"paf, u.GetGroups: ", err)
	}

	permissions, err := u1.GetPermissions(tx)
	if err != nil {
		log.Println("paf, u.GetPermissions: ", err)
	}
	fmt.Println("permissions : ", permissions)

	ok, err = u1.HavePermissions(tx, "PermToLearn", "PermToSpeack")
	if err != nil {
		log.Println("paf, u.HavePermissions: ", err)
	}
	fmt.Println("Have ? : ", ok)

	newgroups := []model.Group{
		{
			Id: 2,
		},
		{
			Id: 3,
		},
	}
	err = newgroups[0].Get(tx)
	if err != nil {
		log.Println("paf1, u.GetGroups: ", err)
	}
	err = newgroups[1].Get(tx)
	if err != nil {
		log.Println("paf2, u.GetGroups: ", err)
	}

	fmt.Println("Groups: ", groups)
	fmt.Println("New Groups: ", newgroups)

	err = u1.SetGroups(tx, newgroups)
	if err != nil {
		log.Println("paf, u.SetGroups: ", err)
	}

	groups, err = u1.GetGroups(tx)
	if err != nil {
		log.Println("paf, u.GetGroups: ", err)
	}
	fmt.Println("Groups: ", groups)

	err = tx.Commit()
	if err != nil {
		if err, ok := err.(*pq.Error); ok {
			log.Println("commit user failed, pq error:", err.Code.Name())
		} else {
			log.Println("commit user failed !", err)
		}
		return
	}

}
Beispiel #2
0
func handledRoute(myroute *Route, rw http.ResponseWriter, r *http.Request) {

	start := time.Now()

	// debug transactions count
	count_before := 0
	db.DB.Get(&count_before, "SELECT count(*) from pg_stat_activity where state = 'idle in transaction'")

	defer func() {
		count_end := 0
		db.DB.Get(&count_end, "SELECT count(*) from pg_stat_activity where state = 'idle in transaction'")

		log.Println("idle in transaction : ", count_before, " => ", count_end)
	}()

	// session
	token := r.Header.Get("Authorization")
	log.Println("token ", token)
	s := session.GetSession(token)

	log.Print("user id from session : ", s.GetIntDef("user_id", -1))

	// Retrieve user id from session
	user := model.User{}
	user.Id = s.GetIntDef("user_id", 0)

	// Open a transaction to load the user from db
	tx, err := db.DB.Beginx()
	if err != nil {
		log.Panicln("Can't start transaction for loading user")
		ServerError(rw, 500, "Can't start transaction for loading user")
		return
	}

	// get langs
	lang1 := model.Lang{
		Isocode: "en",
	}
	lang2 := model.Lang{
		Isocode: "fr",
	}

	if _lang1, err := r.Cookie("arkeogis_user_lang_1"); err == nil {
		lang1.Isocode = _lang1.Value
	}
	if _lang2, err := r.Cookie("arkeogis_user_lang_2"); err == nil {
		lang2.Isocode = _lang2.Value
	}

	err = lang1.Get(tx)
	if err != nil {
		lang1.Isocode = "en"
		err = lang1.Get(tx)
		if err != nil {
			fmt.Println("can't load lang1 !")
		}
	}

	err = lang2.Get(tx)
	if err != nil {
		lang2.Isocode = "fr"
		err = lang2.Get(tx)
		if err != nil {
			fmt.Println("can't load lang2 !")
		}
	}

	fmt.Println("langs: ", lang1, lang2)

	// Retrieve the user from db
	user.Get(tx)
	log.Println("user is : ", user.Username)
	s.Set("user", user)

	restlog.WriteString(fmt.Sprintf(`[%s][%s] %s %s %s %s`+"\n", start.Format(time.RFC3339), user.Username, r.RemoteAddr, r.Method, r.URL.Path, myroute.Method))

	// Check global permsissions
	permok := true
	ok, err := user.HaveAtLeastOnePermission(tx, myroute.Permissions...)
	if err != nil {
		log.Println("user.HaveAtLeastOnePermission failed : ", err)
		permok = false
	} else if ok == false {
		log.Println("user has no permissions : ", myroute.Permissions)
		permok = false
	}

	// Close the transaction
	err = tx.Commit()
	if err != nil {
		if err, ok := err.(*pq.Error); ok {
			log.Println("commit while getting session user failed, pq error:", err.Code.Name())
		} else {
			log.Println("commit while getting session user failed !", err)
		}
		ServerError(rw, 500, "Can't commit transaction")
		return
	}

	// Print a log
	log.Printf("[%s] %s %s ; authorized: %t\n", user.Username, myroute.Method, myroute.Path, permok)
	if !permok {
		ServerError(rw, 403, "unauthorized")
		return
	}

	o := decodeContent(myroute, rw, r, s)
	if o != nil {
		errors := sanitizer.SanitizeStruct(o, "json")
		if len(errors) > 0 {
			Errors(rw, errors)
			return
		}
	}

	params := decodeParams(myroute, rw, r)
	if params != nil {
		log.Println("params    : ", params)
		errors := sanitizer.SanitizeStruct(params, "params")
		log.Println("Sanitized : ", params)
		if len(errors) > 0 {
			Errors(rw, errors)
			return
		}
	}

	proute := Proute{
		Json:    o,
		Params:  params,
		Session: s,
		Lang1:   lang1,
		Lang2:   lang2,
	}

	myroute.Func(rw, r, proute)

}