-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI-80: users blocks commands (#219)
* CLI-80: user-blocks commands with user_id
- Loading branch information
1 parent
e5b542e
commit 7263e5a
Showing
6 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//go:generate mockgen -source=user.go -destination=user_mock.go -package=auth0 | ||
|
||
package auth0 | ||
|
||
import "gopkg.in/auth0.v5/management" | ||
|
||
type UserAPI interface { | ||
// Retrieves a list of blocked IP addresses of a particular user. | ||
Blocks(id string, opts ...management.RequestOption) ([]*management.UserBlock, error) | ||
|
||
// Unblock a user that was blocked due to an excessive amount of incorrectly | ||
// provided credentials. | ||
Unblock(id string, opts ...management.RequestOption) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/auth0/auth0-cli/internal/ansi" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/auth0.v5/management" | ||
) | ||
|
||
var ( | ||
userID = Argument{ | ||
Name: "User ID", | ||
Help: "Id of the user.", | ||
} | ||
) | ||
|
||
func usersCmd(cli *cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "users", | ||
Short: "Manage resources for users", | ||
} | ||
|
||
cmd.SetUsageTemplate(resourceUsageTemplate()) | ||
cmd.AddCommand(userBlocksCmd(cli)) | ||
cmd.AddCommand(deleteUserBlocksCmd(cli)) | ||
return cmd | ||
} | ||
|
||
func userBlocksCmd(cli *cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "blocks", | ||
Short: "Manage brute-force protection user blocks.", | ||
} | ||
|
||
cmd.SetUsageTemplate(resourceUsageTemplate()) | ||
cmd.AddCommand(listUserBlocksCmd(cli)) | ||
return cmd | ||
} | ||
|
||
func listUserBlocksCmd(cli *cli) *cobra.Command { | ||
var inputs struct { | ||
userID string | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "List brute-force protection blocks for a given user", | ||
Long: `List brute-force protection blocks for a given user: | ||
auth0 users blocks list <user-id> | ||
`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
prepareInteractivity(cmd) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
if err := userID.Ask(cmd, &inputs.userID); err != nil { | ||
return err | ||
} | ||
} else { | ||
inputs.userID = args[0] | ||
} | ||
|
||
var userBlocks []*management.UserBlock | ||
|
||
err := ansi.Waiting(func() error { | ||
var err error | ||
userBlocks, err = cli.api.User.Blocks(inputs.userID) | ||
return err | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Unable to load user blocks %v, error: %w", inputs.userID, err) | ||
} | ||
|
||
cli.renderer.UserBlocksList(userBlocks) | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func deleteUserBlocksCmd(cli *cli) *cobra.Command { | ||
var inputs struct { | ||
userID string | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "unblock", | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "Delete brute-force protection blocks for a given user", | ||
Long: `Delete brute-force protection blocks for a given user: | ||
auth0 users unblock <user-id> | ||
`, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
prepareInteractivity(cmd) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
if err := userID.Ask(cmd, &inputs.userID); err != nil { | ||
return err | ||
} | ||
} else { | ||
inputs.userID = args[0] | ||
} | ||
|
||
err := ansi.Spinner("Deleting blocks for user...", func() error { | ||
return cli.api.User.Unblock(inputs.userID) | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package display | ||
|
||
import ( | ||
"gopkg.in/auth0.v5/management" | ||
) | ||
|
||
type userBlockView struct { | ||
Identifier string | ||
IP string | ||
} | ||
|
||
func (v *userBlockView) AsTableHeader() []string { | ||
return []string{"Identifier", "IP"} | ||
} | ||
|
||
func (v *userBlockView) AsTableRow() []string { | ||
return []string{v.Identifier, v.IP} | ||
} | ||
|
||
func (v *userBlockView) KeyValues() [][]string { | ||
return [][]string{ | ||
[]string{"Identifier", v.Identifier}, | ||
[]string{"IP", v.IP}, | ||
} | ||
} | ||
|
||
func (r *Renderer) UserBlocksList(userBlocks []*management.UserBlock) { | ||
resource := "user blocks" | ||
|
||
r.Heading(resource) | ||
|
||
if len(userBlocks) == 0 { | ||
r.EmptyState(resource) | ||
return | ||
} | ||
|
||
var res []View | ||
|
||
for _, userBlock := range userBlocks { | ||
res = append(res, &userBlockView{ | ||
Identifier: *userBlock.Identifier, | ||
IP: *userBlock.IP, | ||
}) | ||
} | ||
|
||
r.Results(res) | ||
|
||
} |