From 525ae3d2d6509908f10940d7e567c446dec580ca Mon Sep 17 00:00:00 2001 From: djelusic Date: Tue, 8 Mar 2022 13:05:56 +0100 Subject: [PATCH] add user-remove command --- cli/cmd/commands.go | 16 ++++++++++++++++ cli/controller/node.go | 24 ++++++++++++++++++++++-- node/api/node/store.go | 4 ++++ node/dto/dto.go | 5 +++++ node/functions/node/main.go | 11 +++++++++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cli/cmd/commands.go b/cli/cmd/commands.go index 1104c80..55842a1 100644 --- a/cli/cmd/commands.go +++ b/cli/cmd/commands.go @@ -504,6 +504,7 @@ func newNodeCommand() *cobra.Command { Hidden: true, } addCommand(cmd, newNodeUserAddCommand()) + addCommand(cmd, newNodeUserRemoveCommand()) addCommand(cmd, newNodeLoginCommand()) addCommand(cmd, newNodeLogoutCommand()) return cmd @@ -524,6 +525,21 @@ func newNodeUserAddCommand() *cobra.Command { return cmd } +func newNodeUserRemoveCommand() *cobra.Command { + var a controller.NodeUserRemoveArgs + cmd := &cobra.Command{ + Use: "user-remove", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + a.Username = args[0] + return controller.NodeUserRemove(a) + }, + } + setUsageTemplate(cmd, texts.Deploy.Arguments) + cmd.Flags().StringVarP(&a.Node, "node", "n", "", "Node in which the user will be removed") + return cmd +} + func newNodeLoginCommand() *cobra.Command { var a controller.NodeLoginArgs cmd := &cobra.Command{ diff --git a/cli/controller/node.go b/cli/controller/node.go index 544e0c4..e9e12f3 100644 --- a/cli/controller/node.go +++ b/cli/controller/node.go @@ -8,8 +8,9 @@ import ( ) const ( - UserAddHTTPMethod = "node/addUser" - LoginHTTPMethod = "auth/login" + UserAddHTTPMethod = "node/addUser" + UserRemoveHTTPMethod = "node/removeUser" + LoginHTTPMethod = "auth/login" ) type NodeUserAddArgs struct { @@ -37,6 +38,25 @@ func NodeUserAdd(a NodeUserAddArgs) error { }, nil) } +type NodeUserRemoveArgs struct { + Node string + Username string +} + +func NodeUserRemove(a NodeUserRemoveArgs) error { + n, err := findNode(a.Node) + if err != nil { + return err + } + i, err := nodeInvoker(n) + if err != nil { + return err + } + return i.Do(UserRemoveHTTPMethod, &dto.RemoveUserRequest{ + Username: a.Username, + }, nil) +} + func findNode(name string) (*domain.Node, error) { fs, err := domain.NewSingleDeveloperWorkspaceStore() if err != nil { diff --git a/node/api/node/store.go b/node/api/node/store.go index 70b1a22..9587f3d 100644 --- a/node/api/node/store.go +++ b/node/api/node/store.go @@ -41,6 +41,10 @@ func (s *Store) StoreUser(name string, role domain.Role) error { }) } +func (s *Store) RemoveUser(name string) error { + return s.users.Delete(name) +} + func (s *Store) FindUser(name string) (*user, error) { u := &user{} if err := s.users.Get(name, u); err != nil { diff --git a/node/dto/dto.go b/node/dto/dto.go index 8e58367..4e82451 100644 --- a/node/dto/dto.go +++ b/node/dto/dto.go @@ -121,6 +121,11 @@ type AddUserRequest struct { Role domain.Role } +type RemoveUserRequest struct { + Username string + Role domain.Role +} + type LoginResponse struct { Node *domain.Node } diff --git a/node/functions/node/main.go b/node/functions/node/main.go index 52826c2..a1595d2 100644 --- a/node/functions/node/main.go +++ b/node/functions/node/main.go @@ -35,6 +35,17 @@ func (n *Node) AddUser(ctx context.Context, req *dto.AddUserRequest) error { return n.store.StoreUser(req.Username, req.Role) } +func (n *Node) RemoveUser(ctx context.Context, req *dto.RemoveUserRequest) error { + ok, err := domain.IsAdmin(ctx) + if err != nil { + return err + } + if !ok { + return domain.ErrNotAuthorized + } + return n.store.RemoveUser(req.Username) +} + func main() { var api = New() mantil.LambdaHandler(api)