Add 'acl update' command (fix #4)

This commit is contained in:
Manfred Touron 2017-11-23 12:01:17 +01:00
parent 37d7c839dd
commit 09c1e0504e
3 changed files with 77 additions and 2 deletions

View file

@ -5,6 +5,7 @@
* More details in 'ls' commands
* Add 'host update' command (fix [#2](https://github.com/moul/sshportal/issues/2))
* Add 'user update' command (fix [#3](https://github.com/moul/sshportal/issues/3))
* Add 'acl update' command (fix [#4](https://github.com/moul/sshportal/issues/4))
## v1.2.0 (2017-11-22)

View file

@ -143,6 +143,7 @@ acl create [-h] [--hostgroup=HOSTGROUP...] [--usergroup=USERGROUP...] [--pattern
acl inspect [-h] ACL...
acl ls [-h]
acl rm [-h] ACL...
acl update [-h] [--comment=<value>] [--action=<value>] [--weight=<value>] [--assign-hostgroup=HOSTGROUP...] [--unassign-hostgroup=HOSTGROUP...] [--assign-usergroup=USERGROUP...] [--unassign-usergroup=USERGROUP...] ACL...
# config management
config help
@ -155,7 +156,7 @@ host create [-h] [--name=<value>] [--password=<value>] [--fingerprint=<value>] [
host inspect [-h] HOST...
host ls [-h]
host rm [-h] HOST...
host update [-h] USER [--name=<value>] [--comment=<value>] [--fingerprint=<value>] [--key=KEY] [--assign-group=HOSTGROUP...] [--unassign-group=HOSTGROUP...]
host update [-h] [--name=<value>] [--comment=<value>] [--fingerprint=<value>] [--key=KEY] [--assign-group=HOSTGROUP...] [--unassign-group=HOSTGROUP...] HOST...
# hostgroup management
hostgroup help

View file

@ -137,7 +137,7 @@ GLOBAL OPTIONS:
return err
}
table := tablewriter.NewWriter(s)
table.SetHeader([]string{"ID", "User groups", "Host groups", "Host pattern", "Action", "Comment"})
table.SetHeader([]string{"ID", "Weight", "User groups", "Host groups", "Host pattern", "Action", "Comment"})
table.SetBorder(false)
table.SetCaption(true, fmt.Sprintf("Total: %d acls.", len(acls)))
for _, acl := range acls {
@ -152,6 +152,7 @@ GLOBAL OPTIONS:
table.Append([]string{
fmt.Sprintf("%d", acl.ID),
fmt.Sprintf("%d", acl.Weight),
strings.Join(userGroups, ", "),
strings.Join(hostGroups, ", "),
acl.HostPattern,
@ -173,6 +174,78 @@ GLOBAL OPTIONS:
return ACLsByIdentifiers(db, c.Args()).Delete(&ACL{}).Error
},
}, {
Name: "update",
Usage: "Updates an existing acl",
ArgsUsage: "ACL...",
Flags: []cli.Flag{
cli.StringFlag{Name: "action, a", Usage: "Update action"},
cli.StringFlag{Name: "pattern, p", Usage: "Update host-pattern"},
cli.UintFlag{Name: "weight, w", Usage: "Update weight"},
cli.StringFlag{Name: "comment, c", Usage: "Update comment"},
cli.StringSliceFlag{Name: "assign-usergroup, ug", Usage: "Assign the ACL to new `USERGROUPS`"},
cli.StringSliceFlag{Name: "unassign-usergroup", Usage: "Unassign the ACL from `USERGROUPS`"},
cli.StringSliceFlag{Name: "assign-hostgroup, hg", Usage: "Assign the ACL to new `HOSTGROUPS`"},
cli.StringSliceFlag{Name: "unassign-hostgroup", Usage: "Unassign the ACL from `HOSTGROUPS`"},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowSubcommandHelp(c)
}
var acls []ACL
if err := ACLsByIdentifiers(db, c.Args()).Find(&acls).Error; err != nil {
return err
}
tx := db.Begin()
for _, acl := range acls {
model := tx.Model(&acl)
update := ACL{
Action: c.String("action"),
HostPattern: c.String("pattern"),
Weight: c.Uint("weight"),
Comment: c.String("comment"),
}
if err := model.Updates(update).Error; err != nil {
tx.Rollback()
return err
}
// associations
var appendUserGroups []UserGroup
var deleteUserGroups []UserGroup
if err := UserGroupsByIdentifiers(db, c.StringSlice("assign-usergroup")).Find(&appendUserGroups).Error; err != nil {
tx.Rollback()
return err
}
if err := UserGroupsByIdentifiers(db, c.StringSlice("unassign-usergroup")).Find(&deleteUserGroups).Error; err != nil {
tx.Rollback()
return err
}
if err := model.Association("UserGroups").Append(&appendUserGroups).Delete(deleteUserGroups).Error; err != nil {
tx.Rollback()
return err
}
var appendHostGroups []HostGroup
var deleteHostGroups []HostGroup
if err := HostGroupsByIdentifiers(db, c.StringSlice("assign-hostgroup")).Find(&appendHostGroups).Error; err != nil {
tx.Rollback()
return err
}
if err := HostGroupsByIdentifiers(db, c.StringSlice("unassign-hostgroup")).Find(&deleteHostGroups).Error; err != nil {
tx.Rollback()
return err
}
if err := model.Association("HostGroups").Append(&appendHostGroups).Delete(deleteHostGroups).Error; err != nil {
tx.Rollback()
return err
}
}
return tx.Commit().Error
},
},
},
}, {