Skip to content

Commit

Permalink
add user-remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Mar 8, 2022
1 parent e0ea1c0 commit 525ae3d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
16 changes: 16 additions & 0 deletions cli/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ func newNodeCommand() *cobra.Command {
Hidden: true,
}
addCommand(cmd, newNodeUserAddCommand())
addCommand(cmd, newNodeUserRemoveCommand())
addCommand(cmd, newNodeLoginCommand())
addCommand(cmd, newNodeLogoutCommand())
return cmd
Expand All @@ -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{
Expand Down
24 changes: 22 additions & 2 deletions cli/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

const (
UserAddHTTPMethod = "node/addUser"
LoginHTTPMethod = "auth/login"
UserAddHTTPMethod = "node/addUser"
UserRemoveHTTPMethod = "node/removeUser"
LoginHTTPMethod = "auth/login"
)

type NodeUserAddArgs struct {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions node/api/node/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions node/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ type AddUserRequest struct {
Role domain.Role
}

type RemoveUserRequest struct {
Username string
Role domain.Role
}

type LoginResponse struct {
Node *domain.Node
}
11 changes: 11 additions & 0 deletions node/functions/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 525ae3d

Please sign in to comment.