grant: Handle update constraints better

This commit is contained in:
Tom Wiesing 2023-01-05 13:16:13 +01:00
parent 8b55fd74f9
commit f3939c5016
No known key found for this signature in database
2 changed files with 11 additions and 8 deletions

View file

@ -13,7 +13,10 @@ var (
ErrInvalid = errors.New("invalid parameters")
)
// Set sets a specific grant, overwriting a previous grant (if any)
// Set sets a specific grant, overwriting any previous grant.
//
// User and Slug must not be empty.
// If DrupalUsername is empty, sets the username to be equal to the user.
func (policy *Policy) Set(ctx context.Context, grant models.Grant) error {
if grant.DrupalUsername == "" {
grant.DrupalUsername = grant.User
@ -37,9 +40,9 @@ func (policy *Policy) Set(ctx context.Context, grant models.Grant) error {
}
// and create or update the given user / slug combination
return table.Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(&grant).Error
return table.Clauses(
clause.OnConflict{OnConstraint: "user_slug", UpdateAll: true},
).Create(&grant).Error
}
// Remove removes access for the given username form the given instance.