From 09c1e0504ecbbd34ba5bdd8999029678c78b7f15 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Thu, 23 Nov 2017 12:01:17 +0100 Subject: [PATCH] Add 'acl update' command (fix #4) --- CHANGELOG.md | 1 + README.md | 3 ++- shell.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e2686f..3011188 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index be08d12..52b6155 100644 --- a/README.md +++ b/README.md @@ -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=] [--action=] [--weight=] [--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=] [--password=] [--fingerprint=] [ host inspect [-h] HOST... host ls [-h] host rm [-h] HOST... -host update [-h] USER [--name=] [--comment=] [--fingerprint=] [--key=KEY] [--assign-group=HOSTGROUP...] [--unassign-group=HOSTGROUP...] +host update [-h] [--name=] [--comment=] [--fingerprint=] [--key=KEY] [--assign-group=HOSTGROUP...] [--unassign-group=HOSTGROUP...] HOST... # hostgroup management hostgroup help diff --git a/shell.go b/shell.go index d23ece5..851bbe3 100644 --- a/shell.go +++ b/shell.go @@ -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 + }, }, }, }, {