コード例 #1
0
ファイル: test.go プロジェクト: croll/arkeogis-server
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
	}

}
コード例 #2
0
ファイル: users.go プロジェクト: croll/arkeogis-server
func loginAnswer(w http.ResponseWriter, tx *sqlx.Tx, user model.User, token string) (LoginAnswer, error) {
	// get langs
	lang1 := model.Lang{
		Isocode: user.First_lang_isocode,
	}
	lang2 := model.Lang{
		Isocode: user.Second_lang_isocode,
	}

	err := lang1.Get(tx)
	if err != nil {
		lang1.Isocode = "en"
		err = lang1.Get(tx)
		if err != nil {
			userSqlError(w, err)
			return LoginAnswer{}, err
		}
	}

	err = lang2.Get(tx)
	if err != nil {
		lang2.Isocode = "fr"
		err = lang2.Get(tx)
		if err != nil {
			tx.Rollback()
			log.Fatal("can't load lang2 !")
			return LoginAnswer{}, err
		}
	}

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

	permissions, err := user.GetPermissions(tx)
	if err != nil {
		log.Fatal("can't get permissions!")
		return LoginAnswer{}, err
	}
	log.Println("permissions : ", permissions)

	projectID, err := user.GetProjectId(tx)

	if err != nil {
		log.Fatal("can't get project!")
		return LoginAnswer{}, err
	}

	project := model.ProjectFullInfos{}
	if projectID > 0 {
		project.Id = projectID
		err = project.Get(tx)
		if err != nil {
			log.Fatal("can't get project informations!")
			return LoginAnswer{}, err
		}
	} else {
		project.Id = 0
	}

	log.Println("project: ", project)

	a := LoginAnswer{
		User:        user,
		Token:       token,
		Lang1:       lang1,
		Lang2:       lang2,
		Project:     project,
		Permissions: permissions,
	}

	return a, nil
}