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") 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 { func (policy *Policy) Set(ctx context.Context, grant models.Grant) error {
if grant.DrupalUsername == "" { if grant.DrupalUsername == "" {
grant.DrupalUsername = grant.User 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 // and create or update the given user / slug combination
return table.Clauses(clause.OnConflict{ return table.Clauses(
UpdateAll: true, clause.OnConflict{OnConstraint: "user_slug", UpdateAll: true},
}).Create(&grant).Error ).Create(&grant).Error
} }
// Remove removes access for the given username form the given instance. // Remove removes access for the given username form the given instance.

View file

@ -7,9 +7,9 @@ const GrantTable = "grant"
type Grant struct { type Grant struct {
Pk uint `gorm:"column:pk;primaryKey"` Pk uint `gorm:"column:pk;primaryKey"`
User string `gorm:"column:user;not null;uniqueIndex:user_slug"` // (distillery) username User string `gorm:"column:user;not null;index:user_slug,unique"` // (distillery) username
Slug string `gorm:"column:slug;not null;uniqueIndex:user_slug"` // (distillery) instance slug Slug string `gorm:"column:slug;not null;index:user_slug;index:drupal_slug"` // (distillery) instance slug
DrupalUsername string `gorm:"column:drupal_user;not null"` // drupal username DrupalUsername string `gorm:"column:drupal_user;not null;index:drupal_slug,unique"` // drupal username
DrupalAdminRole bool `gorm:"column:admin;not null"` // drupal admin rights DrupalAdminRole bool `gorm:"column:admin;not null"` // drupal admin rights
} }