Ejemplo n.º 1
0
func chronologiesGetTree(tx *sqlx.Tx, id int, user model.User) (answer *ChronologiesUpdateStruct, err error) {

	// answer structure that will be printed when everything is done
	answer = &ChronologiesUpdateStruct{}

	// get the chronology_root row
	answer.Chronology_root.Root_chronology_id = id
	err = answer.Chronology_root.Get(tx)
	if err != nil {
		return nil, err
	}

	// get the chronology (root)
	answer.Chronology.Id = id
	err = answer.Chronology.Get(tx)
	if err != nil {
		return nil, err
	}

	// now get the chronology translations and all childrens
	err = getChronoRecursive(tx, &answer.ChronologyTreeStruct)
	if err != nil {
		return nil, err
	}

	// get users of the chrono group
	group := model.Group{
		Id: answer.Chronology_root.Admin_group_id,
	}
	err = group.Get(tx)
	if err != nil {
		return nil, err
	}
	answer.UsersInGroup, err = group.GetUsers(tx)
	if err != nil {
		return nil, err
	}

	for i := range answer.UsersInGroup {
		answer.UsersInGroup[i].Password = ""
	}

	// get the author user
	answer.Author.Id = answer.Chronology_root.Author_user_id
	err = answer.Author.Get(tx)
	answer.Author.Password = ""
	if err != nil {
		return nil, err
	}

	return answer, nil
}
Ejemplo n.º 2
0
func characsGetTree(w http.ResponseWriter, tx *sqlx.Tx, id int, project_id int, user model.User) (answer *CharacsUpdateStruct, err error) {

	// answer structure that will be printed when everything is done
	answer = &CharacsUpdateStruct{}

	// get the charac_root row
	answer.Charac_root.Root_charac_id = id
	err = answer.Charac_root.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// get the charac (root)
	answer.Charac.Id = id
	err = answer.Charac.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// now get the charac translations and all childrens
	err = getCharacRecursive(tx, &answer.CharacTreeStruct, project_id)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	// get users of the charac group
	group := model.Group{
		Id: answer.Charac_root.Admin_group_id,
	}
	err = group.Get(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}
	answer.UsersInGroup, err = group.GetUsers(tx)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return nil, err
	}

	for i := range answer.UsersInGroup {
		answer.UsersInGroup[i].Password = ""
	}

	// get the author user
	/*	answer.Author.Id = answer.Charac_root.Author_user_id
		err = answer.Author.Get(tx)
		answer.Author.Password = ""
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return nil, err
		}*/ // no author in characs vs chrono

	return answer, nil
}
Ejemplo n.º 3
0
// CharacsUpdate Create/Update a charac
func CharacsUpdate(w http.ResponseWriter, r *http.Request, proute routes.Proute) {

	// get the post
	c := proute.Json.(*CharacsUpdateStruct)

	// transaction begin...
	tx, err := db.DB.Beginx()
	if err != nil {
		userSqlError(w, err)
		return
	}

	// get the user
	_user, ok := proute.Session.Get("user")
	if !ok {
		log.Println("CharacsUpdate: can't get user in session...", _user)
		_ = tx.Rollback()
		return
	}
	user, ok := _user.(model.User)
	if !ok {
		log.Println("CharacsUpdate: can't cast user...", _user)
		_ = tx.Rollback()
		return
	}
	err = user.Get(tx)
	user.Password = "" // immediatly erase password field, we don't need it
	if err != nil {
		log.Println("CharacsUpdate: can't load user...", _user)
		userSqlError(w, err)
		_ = tx.Rollback()
		return
	}

	// boolean create, true if we are creating a totaly new charac
	var create bool
	if c.Charac.Id > 0 {
		create = false
		// @TODO: check that you are in group of this charac when updating one
	} else {
		create = true
	}

	// save recursively this charac
	err = setCharacRecursive(tx, &c.CharacTreeStruct, nil)
	if err != nil {
		userSqlError(w, err)
		_ = tx.Rollback()
		return
	}

	//log.Println("geom: ", c.Charac_root.Geom)

	// save the charac_root row, but search/create it's group first
	c.Charac_root.Root_charac_id = c.Charac.Id
	if create {
		// when creating, we also must create it's working group
		group := model.Group{
			Type: "charac",
		}
		err = group.Create(tx)
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}

		// also save group name in langs...
		for isocode, name := range c.CharacTreeStruct.Name {
			group_tr := model.Group_tr{
				Group_id:     group.Id,
				Lang_isocode: isocode,
				Name:         name,
			}
			err = group_tr.Create(tx)
		}

		// create the charac root
		c.Charac_root.Admin_group_id = group.Id
		err = c.Charac_root.Create(tx)
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}

	} else {
		// search the characroot to verify permissions
		characroot := model.Charac_root{
			Root_charac_id: c.Charac.Id,
		}
		err = characroot.Get(tx)
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}

		// take the group
		group := model.Group{
			Id: characroot.Admin_group_id,
		}
		err = group.Get(tx)
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}

		// update translations of the group
		_, err = tx.Exec("DELETE FROM group_tr WHERE group_id = " + strconv.Itoa(group.Id))
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}
		for isocode, name := range c.CharacTreeStruct.Name {
			group_tr := model.Group_tr{
				Group_id:     group.Id,
				Lang_isocode: isocode,
				Name:         name,
			}
			err = group_tr.Create(tx)
		}

		// check that the user is in the group
		var ok bool
		ok, err = user.HaveGroups(tx, group)
		if err != nil {
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}

		if !ok {
			/*
				routes.ServerError(w, 403, "unauthorized")
				_ = tx.Rollback()
				return
			*/
		}

		// only theses fields can be modified
		//characroot.Credits = c.Charac_root.Credits
		//characroot.Active = c.Charac_root.Active
		//characroot.Geom = c.Charac_root.Geom
		//characroot.Author_user_id = c.Charac_root.Author_user_id
		//characroot.Admin_group_id = c.Charac_root.Admin_group_id
		characroot.Cached_langs = c.Charac_root.Cached_langs

		err = characroot.Update(tx)
		if err != nil {
			log.Println("characroot update failed")
			userSqlError(w, err)
			_ = tx.Rollback()
			return
		}
	}

	answer, err := characsGetTree(w, tx, c.Id, 0, user)

	// commit...
	err = tx.Commit()
	if err != nil {
		log.Println("commit failed")
		userSqlError(w, err)
		_ = tx.Rollback()
		return
	}

	j, err := json.Marshal(answer)
	if err != nil {
		log.Println("marshal failed: ", err)
	}
	//log.Println("result: ", string(j))
	w.Write(j)
}