예제 #1
0
func CityGet(w http.ResponseWriter, r *http.Request, proute routes.Proute) {

	params := proute.Params.(*CityGetParams)

	res := model.CityAndCountry_wtr{}

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Println("err on begin", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	err = res.Get(tx, params.Id_city)
	if err != nil {
		log.Println("err while getting city and country: ", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	err = tx.Commit()
	if err != nil {
		log.Println("err on commit", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	j, err := json.Marshal(res)
	w.Write(j)
}
예제 #2
0
func groupGet(w http.ResponseWriter, r *http.Request, params GroupGetParams) {
	type Answer struct {
		model.Group
		Users []model.User `json:"users"`
	}

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Println("can't start transaction", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	answer := Answer{}
	answer.Id = params.Id

	answer.Group.Get(tx)

	answer.Users, err = answer.Group.GetUsers(tx)

	err = tx.Commit()
	if err != nil {
		log.Println("can't commit", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	j, _ := json.Marshal(answer)
	w.Write(j)
}
예제 #3
0
func userSqlError(w http.ResponseWriter, err error) {
	log.Printf("paf: %#v\n", err)
	if pgerr, ok := err.(*pq.Error); ok {
		log.Printf("pgerr: %#v\n", pgerr)
		switch pgerr.Code.Name() {
		case "foreign_key_violation":
			switch pgerr.Constraint {
			case "c_user.first_lang_isocode":
				routes.FieldError(w, "json.first_lang_isocode", "first_lang_isocode", "USER.FIELD_LANG.T_CHECK_BADLANG")
			case "c_user.second_lang_isocode":
				routes.FieldError(w, "json.second_lang_isocode", "second_lang_isocode", "USER.FIELD_LANG.T_CHECK_BADLANG")
			case "c_user.city_geonameid":
				routes.FieldError(w, "json.searchTextCity", "searchTextCity", "USER.FIELD_CITY.T_CHECK_MANDATORY")
			default:
				routes.ServerError(w, 500, "INTERNAL ERROR")
			}
		case "unique_violation":
			switch pgerr.Constraint {
			case "i_user.username":
				routes.FieldError(w, "json.username", "username", "USER.FIELD_USERNAME.T_CHECK_ALREADYEXISTS")
			default:
				routes.ServerError(w, 500, "INTERNAL ERROR")
			}
		default:
			log.Printf("unhandled postgresql error ! : %#v\n", pgerr)
			routes.ServerError(w, 500, "INTERNAL ERROR")
		}
	} else {
		log.Println("not a postgresql error !", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
	}
}
예제 #4
0
// LicensesList returns the list of licenses which can be assigned to databases
func LicensesList(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
	databases := []model.License{}
	err := db.DB.Select(&databases, "SELECT * FROM \"license\"")
	if err != nil {
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}
	l, _ := json.Marshal(databases)
	w.Header().Set("Content-Type", "application/json")
	w.Write(l)
}
예제 #5
0
func CompanyGet(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
	params := proute.Params.(*CompanyGetParams)

	res := CompanyGetResult{}
	res.Id = params.Id

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Println("err on begin", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	err = res.Company.Get(tx)
	if err != nil {
		log.Println("err on get", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	err = res.CityAndCountry.Get(tx, res.Company.City_geonameid)
	if err != nil {
		log.Println("err on get", err)
		//routes.ServerError(w, 500, "INTERNAL ERROR")
		//return
		_ = tx.Rollback()
	}

	err = tx.Commit()
	if err != nil {
		log.Println("err on commit", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	j, err := json.Marshal(res)
	w.Write(j)
}
예제 #6
0
func GroupSet(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
	o := proute.Json.(*GroupSetPost)

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Println("can't start transaction", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	err = o.Group.Update(tx)
	if err != nil {
		log.Println("can't update group", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	err = o.Group.SetUsers(tx, o.Users)
	if err != nil {
		log.Println("can't set group users", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	err = tx.Commit()
	if err != nil {
		log.Println("can't commit", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		_ = tx.Rollback()
		return
	}

	groupGet(w, r, GroupGetParams{
		Id: o.Id,
	})
}
예제 #7
0
// GroupList List the groups.
func GroupList(w http.ResponseWriter, r *http.Request, proute routes.Proute) {
	type TRGroup struct {
		model.Group
		model.Group_tr
	}
	type Answer struct {
		Data  []TRGroup `json:"data"`
		Count int       `json:"count"`
	}

	answer := Answer{}

	params := proute.Params.(*GroupListParams)

	// decode order...
	order := params.Order
	orderdir := "ASC"
	if strings.HasPrefix(order, "-") {
		order = order[1:]
		orderdir = "DESC"
	}
	/////

	offset := (params.Page - 1) * params.Limit

	// get groups
	err := db.DB.Select(&answer.Data,
		" SELECT * FROM \"group\" g "+
			" LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id "+
			" WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0 AND g_tr.lang_isocode=$3"+
			" ORDER BY "+order+" "+orderdir+
			" OFFSET $4 "+
			" LIMIT $5",
		"%"+params.Filter+"%", params.Type, proute.Lang1.Isocode, offset, params.Limit)
	/*
		// get groups
		err := db.DB.Select(&answer.Data,
			" SELECT * FROM \"group\" g "+
				" LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id "+
				" WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0"+
				" ORDER BY "+order+" "+orderdir+
				" OFFSET $3 "+
				" LIMIT $4",
			"%"+params.Filter+"%", params.Type, offset, params.Limit)
	*/
	if err != nil {
		userSqlError(w, err)
		log.Println("get groups failed", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	// get total count
	err = db.DB.Get(&answer.Count, "SELECT count(g.*) FROM \"group\" g LEFT JOIN \"group_tr\" g_tr ON g.id = g_tr.group_id WHERE g_tr.name ILIKE $1 AND g.type=$2 AND g.id > 0 AND g_tr.lang_isocode=$3", "%"+params.Filter+"%", params.Type, proute.Lang1.Isocode)
	if err != nil {
		log.Println("get total count failed", err)
		routes.ServerError(w, 500, "INTERNAL ERROR")
		return
	}

	j, _ := json.Marshal(answer)
	w.Write(j)
}
예제 #8
0
// userSet is for UserCreate or UserUpdate
func userSet(w http.ResponseWriter, r *http.Request, proute routes.Proute, create bool) {

	u := proute.Json.(*Usercreate)

	// hack overrides
	//u.User.Password = u.Password

	// hack for city
	u.City_geonameid = u.CityAndCountry.City.Geonameid
	log.Println("city : ", u.City_geonameid)

	tx, err := db.DB.Beginx()
	if err != nil {
		log.Println("1")
		userSqlError(w, err)
		return
	}

	_me, ok := proute.Session.Get("user")
	if !ok {
		log.Println("userSet: can't get user in session...", _me)
		_ = tx.Rollback()
		return
	}

	me, ok := _me.(model.User)
	if !ok {
		log.Println("userSet: can't cast user...", _me)
		_ = tx.Rollback()
		return
	}

	permAdminUsers, err := me.HavePermissions(tx, "adminusers")
	if err != nil {
		tx.Rollback()
		userSqlError(w, err)
		return
	}

	if create || u.User.Id != me.Id {
		if !permAdminUsers {
			tx.Rollback()
			routes.ServerError(w, 403, "unauthorized")
			return
		}
	}

	// password
	if len(u.Password) > 0 {

		if len(u.Password) < 7 {
			tx.Rollback()
			routes.FieldError(w, "json.password", "password", "USER.FIELD_PASSWORD.T_CHECK_LENGTH")
			return
		}

		err = u.User.MakeNewPassword(u.Password)
		if err != nil {
			fmt.Println("password generate failed")
			tx.Rollback()
			userSqlError(w, err)
			return
		}
	}

	// photo...
	if u.File != nil {
		photo := model.Photo{
			Photo: string(u.File.Content),
		}
		err = photo.Create(tx)
		if err != nil {
			log.Println("1")
			userSqlError(w, err)
			tx.Rollback()
			return
		}
		u.Photo_id = photo.Id
	}

	// save the user
	if create {
		err = u.Create(tx)
	} else {
		tmpuser := model.User{
			Id: u.Id,
		}
		err = tmpuser.Get(tx)
		if err != nil {
			log.Println("can't get user for update", err)
			userSqlError(w, err)
			tx.Rollback()
			return
		}

		if !permAdminUsers { // in this case, we update tmpuser with only authorized fields
			// if we set a new password
			if len(u.User.Password) != 0 {
				tmpuser.Password = u.User.Password
			}

			// authorized self modified user field
			tmpuser.Username = u.User.Username
			tmpuser.Firstname = u.User.Firstname
			tmpuser.Lastname = u.User.Lastname
			tmpuser.First_lang_isocode = u.User.First_lang_isocode
			tmpuser.Second_lang_isocode = u.User.Second_lang_isocode
			tmpuser.Description = u.User.Description
			tmpuser.Email = u.User.Email
			tmpuser.City_geonameid = u.User.City_geonameid

			err = tmpuser.Update(tx)
		} else {
			// if we don't set a new password, we take it back from the db
			if len(u.User.Password) == 0 {
				u.User.Password = tmpuser.Password
			}

			log.Println("updating user id : ", u.Id, u)
			err = u.Update(tx)
		}
	}
	if err != nil {
		log.Println("2")
		userSqlError(w, err)
		tx.Rollback()
		return
	}

	// save the companies
	var companies []model.Company
	for _, form_company := range u.Companies {
		if form_company.Id > 0 {
			form_company.City_geonameid = form_company.CityAndCountry.City.Geonameid
			log.Println("updating company : ", form_company.Company)
			err = form_company.Update(tx)
			if err != nil {
				log.Println("error while updating a company", err)
				tx.Rollback()
				userSqlError(w, err)
				return
			}
			companies = append(companies, form_company.Company)
		} else if len(form_company.Name) > 0 {
			form_company.City_geonameid = form_company.CityAndCountry.City.Geonameid
			log.Println("creating company : ", form_company.Company)
			err = form_company.Create(tx)
			if err != nil {
				log.Println("error while adding a company", err)
				tx.Rollback()
				userSqlError(w, err)
				return
			}
			companies = append(companies, form_company.Company)
		}
	}

	err = u.SetCompanies(tx, companies)
	if err != nil {
		log.Println("7")
		tx.Rollback()
		userSqlError(w, err)
		return
	}

	if permAdminUsers {
		// save the groups
		err = u.SetGroups(tx, u.Groups)
		if err != nil {
			log.Println("set groups")
			tx.Rollback()
			userSqlError(w, err)
			return
		}
	}

	err = tx.Commit()
	if err != nil {
		log.Println("8")
		userSqlError(w, err)
		return
	}

	j, err := json.Marshal("ok")
	w.Write(j)
}