dis_grant: Add option to add grant for every wisski
This commit is contained in:
parent
66e57183d6
commit
9d03c39e38
1 changed files with 38 additions and 1 deletions
|
|
@ -5,12 +5,14 @@ import (
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||||
|
"github.com/tkw1536/goprogram/exit"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DisGrant is the 'dis_grant' command
|
// DisGrant is the 'dis_grant' command
|
||||||
var DisGrant wisski_distillery.Command = disGrant{}
|
var DisGrant wisski_distillery.Command = disGrant{}
|
||||||
|
|
||||||
type disGrant struct {
|
type disGrant struct {
|
||||||
|
AddAll bool `short:"m" long:"add-all" description:"add grant to all WissKIs"`
|
||||||
AddUser bool `short:"a" long:"add" description:"add or update a user to a given wisski"`
|
AddUser bool `short:"a" long:"add" description:"add or update a user to a given wisski"`
|
||||||
RemoveUser bool `short:"r" long:"remove" description:"remove a user from a given wisski"`
|
RemoveUser bool `short:"r" long:"remove" description:"remove a user from a given wisski"`
|
||||||
|
|
||||||
|
|
@ -18,7 +20,7 @@ type disGrant struct {
|
||||||
|
|
||||||
Positionals struct {
|
Positionals struct {
|
||||||
User string `positional-arg-name:"USER" required:"1-1" description:"distillery username"`
|
User string `positional-arg-name:"USER" required:"1-1" description:"distillery username"`
|
||||||
Slug string `positional-arg-name:"SLUG" required:"1-1" description:"WissKI instance"`
|
Slug string `positional-arg-name:"SLUG" description:"WissKI instance"`
|
||||||
DrupalUser string `positional-arg-name:"DRUPAL" description:"drupal username"`
|
DrupalUser string `positional-arg-name:"DRUPAL" description:"drupal username"`
|
||||||
} `positional-args:"true"`
|
} `positional-args:"true"`
|
||||||
}
|
}
|
||||||
|
|
@ -33,11 +35,17 @@ func (disGrant) Description() wisski_distillery.Description {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errNoSlugSelect = exit.Error{
|
||||||
|
Message: "slug not provided",
|
||||||
|
ExitCode: exit.ExitCommandArguments,
|
||||||
|
}
|
||||||
|
|
||||||
func (dg disGrant) AfterParse() error {
|
func (dg disGrant) AfterParse() error {
|
||||||
var counter int
|
var counter int
|
||||||
for _, action := range []bool{
|
for _, action := range []bool{
|
||||||
dg.AddUser,
|
dg.AddUser,
|
||||||
dg.RemoveUser,
|
dg.RemoveUser,
|
||||||
|
dg.AddAll,
|
||||||
} {
|
} {
|
||||||
if action {
|
if action {
|
||||||
counter++
|
counter++
|
||||||
|
|
@ -48,6 +56,10 @@ func (dg disGrant) AfterParse() error {
|
||||||
return errNoActionSelected
|
return errNoActionSelected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !dg.AddAll && dg.Positionals.Slug == "" {
|
||||||
|
return errNoSlugSelect
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,6 +67,8 @@ func (dg disGrant) Run(context wisski_distillery.Context) error {
|
||||||
switch {
|
switch {
|
||||||
case dg.AddUser:
|
case dg.AddUser:
|
||||||
return dg.runAddUser(context)
|
return dg.runAddUser(context)
|
||||||
|
case dg.AddAll:
|
||||||
|
return dg.runAddAll(context)
|
||||||
case dg.RemoveUser:
|
case dg.RemoveUser:
|
||||||
return dg.runRemoveUser(context)
|
return dg.runRemoveUser(context)
|
||||||
}
|
}
|
||||||
|
|
@ -94,3 +108,26 @@ func (dg disGrant) runRemoveUser(context wisski_distillery.Context) error {
|
||||||
policy := context.Environment.Policy()
|
policy := context.Environment.Policy()
|
||||||
return policy.Remove(context.Context, dg.Positionals.User, dg.Positionals.Slug)
|
return policy.Remove(context.Context, dg.Positionals.User, dg.Positionals.Slug)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dg disGrant) runAddAll(context wisski_distillery.Context) error {
|
||||||
|
policy := context.Environment.Policy()
|
||||||
|
|
||||||
|
instances, err := context.Environment.Instances().All(context.Context)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, instance := range instances {
|
||||||
|
context.Printf("Adding grant for user %s to %s\n", dg.Positionals.User, instance.Slug)
|
||||||
|
if err := policy.Set(context.Context, models.Grant{
|
||||||
|
User: dg.Positionals.User,
|
||||||
|
Slug: instance.Slug,
|
||||||
|
DrupalUsername: dg.Positionals.User,
|
||||||
|
DrupalAdminRole: dg.DrupalAdmin,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue