Skip to content

Commit

Permalink
TT-13870 Fix POST update
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchaillot authored Jan 17, 2025
1 parent 216e7ee commit 460f0fc
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions resource/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,50 @@ func (res *Resource) findManyHandler(result interface{}, context *qor.Context) e
}

func (res *Resource) saveHandler(result interface{}, context *qor.Context) error {
if (context.GetDB().NewScope(result).PrimaryKeyZero() &&
res.HasPermission(roles.Create, context)) || // has create permission
res.HasPermission(roles.Update, context) { // has update permission
return context.GetDB().Save(result).Error
scope := context.GetDB().NewScope(result)
isPrimaryKeyZero := scope.PrimaryKeyZero()

if isPrimaryKeyZero {
// Create operation
if !res.HasPermission(roles.Create, context) {
return roles.ErrPermissionDenied
}
return context.GetDB().Create(result).Error

Check failure on line 147 in resource/crud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return statements should not be cuddled if block has more than two lines (wsl)
}
return roles.ErrPermissionDenied

// If we have a non-zero primary key, first check if it exists
var count int
primaryField := scope.PrimaryField()
if primaryField == nil {

Check failure on line 153 in resource/crud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

only one cuddle assignment allowed before if statement (wsl)
return fmt.Errorf("no primary key field found")
}

query := fmt.Sprintf("%v = ?", scope.Quote(primaryField.DBName))
if err := context.GetDB().Model(result).Where(query, scope.PrimaryKeyValue()).Count(&count).Error; err != nil {
return err
}

// For creation attempts with existing ID
if context.Request != nil && context.Request.Method == "POST" {
if count > 0 {
return fmt.Errorf("record with primary key %v already exists", scope.PrimaryKeyValue())
}
if !res.HasPermission(roles.Create, context) {

Check failure on line 167 in resource/crud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

if statements should only be cuddled with assignments (wsl)
return roles.ErrPermissionDenied
}
return context.GetDB().Create(result).Error

Check failure on line 170 in resource/crud.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return statements should not be cuddled if block has more than two lines (wsl)
}

// Update operation
if !res.HasPermission(roles.Update, context) {
return roles.ErrPermissionDenied
}

if count == 0 {
return fmt.Errorf("record with primary key %v not found", scope.PrimaryKeyValue())
}

return context.GetDB().Save(result).Error
}

func (res *Resource) deleteHandler(result interface{}, context *qor.Context) error {
Expand Down

0 comments on commit 460f0fc

Please sign in to comment.