Add 'dis_grant' command

This commit is contained in:
Tom Wiesing 2023-01-05 12:55:28 +01:00
parent 6bf6d3a8f5
commit 8b55fd74f9
No known key found for this signature in database
10 changed files with 161 additions and 18 deletions

View file

@ -15,10 +15,21 @@ var (
// Set sets a specific grant, overwriting a previous grant (if any)
func (policy *Policy) Set(ctx context.Context, grant models.Grant) error {
if grant.User == "" || grant.Slug == "" || grant.DrupalUsername == "" {
if grant.DrupalUsername == "" {
grant.DrupalUsername = grant.User
}
if grant.User == "" || grant.Slug == "" {
return ErrInvalid
}
// check that the referenced user exists!
{
_, err := policy.Dependencies.Auth.User(ctx, grant.User)
if err != nil {
return err
}
}
// get the table
table, err := policy.table(ctx)
if err != nil {
@ -27,8 +38,7 @@ 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{
Columns: []clause.Column{{Name: "user"}, {Name: "slug"}},
DoUpdates: clause.AssignmentColumns([]string{"drupal_user", "admin"}),
UpdateAll: true,
}).Create(&grant).Error
}

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/auth"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"gorm.io/gorm"
@ -13,7 +14,8 @@ type Policy struct {
component.Base
Dependencies struct {
SQL *sql.SQL
SQL *sql.SQL
Auth *auth.Auth
}
}