mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
fix: remove user property of diagnostics as sent without auth
This commit is contained in:
parent
eae45baf38
commit
ec70d024fa
39
migrations/202203191_drop_diagnostics_user.go
Normal file
39
migrations/202203191_drop_diagnostics_user.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/emvi/logbuch"
|
||||||
|
"github.com/muety/wakapi/config"
|
||||||
|
"github.com/muety/wakapi/models"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
const name = "202203191-drop_diagnostics_user"
|
||||||
|
f := migrationFunc{
|
||||||
|
name: name,
|
||||||
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
||||||
|
if hasRun(name, db) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
migrator := db.Migrator()
|
||||||
|
|
||||||
|
if migrator.HasColumn(&models.Diagnostics{}, "user_id") {
|
||||||
|
logbuch.Info("running migration '%s'", name)
|
||||||
|
|
||||||
|
if err := migrator.DropConstraint(&models.Diagnostics{}, "fk_diagnostics_user"); err != nil {
|
||||||
|
logbuch.Warn("failed to drop 'fk_diagnostics_user' constraint (%v)", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := migrator.DropColumn(&models.Diagnostics{}, "user_id"); err != nil {
|
||||||
|
logbuch.Warn("failed to drop user_id column of diagnostics (%v)", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setHasRun(name, db)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
registerPostMigration(f)
|
||||||
|
}
|
@ -2,8 +2,6 @@ package models
|
|||||||
|
|
||||||
type Diagnostics struct {
|
type Diagnostics struct {
|
||||||
ID uint `gorm:"primary_key"`
|
ID uint `gorm:"primary_key"`
|
||||||
User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
||||||
UserID string `json:"-" gorm:"not null; index:idx_diagnostics_user"`
|
|
||||||
Platform string `json:"platform"`
|
Platform string `json:"platform"`
|
||||||
Architecture string `json:"architecture"`
|
Architecture string `json:"architecture"`
|
||||||
Plugin string `json:"plugin"`
|
Plugin string `json:"plugin"`
|
||||||
|
@ -46,20 +46,12 @@ func (h *DiagnosticsApiHandler) RegisterRoutes(router *mux.Router) {
|
|||||||
func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) {
|
func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) {
|
||||||
var diagnostics models.Diagnostics
|
var diagnostics models.Diagnostics
|
||||||
|
|
||||||
user := middlewares.GetPrincipal(r)
|
|
||||||
if user == nil {
|
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
|
||||||
w.Write([]byte(conf.ErrUnauthorized))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&diagnostics); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&diagnostics); err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte(conf.ErrBadRequest))
|
w.Write([]byte(conf.ErrBadRequest))
|
||||||
conf.Log().Request(r).Error("failed to parse diagnostics for user %s - %v", err)
|
conf.Log().Request(r).Error("failed to parse diagnostics for user %s - %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
diagnostics.UserID = user.ID
|
|
||||||
|
|
||||||
if _, err := h.diagnosticsSrvc.Create(&diagnostics); err != nil {
|
if _, err := h.diagnosticsSrvc.Create(&diagnostics); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
Loading…
Reference in New Issue
Block a user