Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-313: Add ability to view a user's assigned roles #604

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ test-unit: ## Run unit tests
test-integration: $(GO_BIN)/commander ## Run integration tests. To run a specific test pass the FILTER var. Usage: `make test-integration FILTER="attack protection"`
${call print, "Running integration tests"}
@$(MAKE) install # ensure fresh install prior to running test
auth0 login --domain ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \
exit_code=$$?; \
bash ./test/integration/scripts/test-cleanup.sh; \
exit $$exit_code
@bash ./test/integration/scripts/run-test-suites.sh

test-mocks: $(GO_BIN)/mockgen ## Generate testing mocks using mockgen
${call print, "Generating test mocks"}
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Manage resources for users.
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ auth0 users create [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ auth0 users delete [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ auth0 users import [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_open.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ auth0 users open [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
11 changes: 11 additions & 0 deletions docs/auth0_users_roles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
layout: default
---
# auth0 users roles

Manage a user's assigned roles. To learn more about roles and their behavior, read [Role-based Access Control](https://auth0.com/docs/manage-users/access-control/rbac).

## Commands

- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles

45 changes: 45 additions & 0 deletions docs/auth0_users_roles_show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: default
---
# auth0 users roles show

Display information about an existing user's assigned roles.

## Usage
```
auth0 users roles show [flags]
```

## Examples

```
auth0 users roles show
auth0 users roles show <user-id>
auth0 users roles show <user-id> --number 100
auth0 users roles show <user-id> -n 100 --json
```


## Flags

```
--json Output in json format.
-n, --number int Number of user roles to retrieve. Minimum 1, maximum 1000. (default 50)
```


## InheritedFlags

```
--debug Enable debug mode.
--no-color Disable colors.
--no-input Disable interactivity.
--tenant string Specific tenant to use.
```


## Related Commands

- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles


1 change: 1 addition & 0 deletions docs/auth0_users_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ auth0 users search [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ auth0 users show [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_unblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ auth0 users unblock [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
1 change: 1 addition & 0 deletions docs/auth0_users_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ auth0 users update [flags]
- [auth0 users delete](auth0_users_delete.md) - Delete a user
- [auth0 users import](auth0_users_import.md) - Import users from schema
- [auth0 users open](auth0_users_open.md) - Open the user's settings page
- [auth0 users roles](auth0_users_roles.md) - Manage a user's roles
- [auth0 users search](auth0_users_search.md) - Search for users
- [auth0 users show](auth0_users_show.md) - Show an existing user
- [auth0 users unblock](auth0_users_unblock.md) - Remove brute-force protection blocks for a given user
Expand Down
5 changes: 4 additions & 1 deletion internal/auth0/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package auth0
import "github.com/auth0/go-auth0/management"

type UserAPI interface {
// Retrieves a list of blocked IP addresses of a particular user.
// Blocks 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
Expand All @@ -27,4 +27,7 @@ type UserAPI interface {

// Search for users
Search(opts ...management.RequestOption) (us *management.UserList, err error)

// Roles lists all roles associated with a user.
Roles(id string, opts ...management.RequestOption) (r *management.RoleList, err error)
}
3 changes: 2 additions & 1 deletion internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ func usersCmd(cli *cli) *cobra.Command {
cmd.AddCommand(searchUsersCmd(cli))
cmd.AddCommand(createUserCmd(cli))
cmd.AddCommand(showUserCmd(cli))
cmd.AddCommand(deleteUserCmd(cli))
cmd.AddCommand(updateUserCmd(cli))
cmd.AddCommand(deleteUserCmd(cli))
cmd.AddCommand(userRolesCmd(cli))
cmd.AddCommand(openUserCmd(cli))
cmd.AddCommand(userBlocksCmd(cli))
cmd.AddCommand(deleteUserBlocksCmd(cli))
Expand Down
97 changes: 97 additions & 0 deletions internal/cli/users_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cli

import (
"fmt"

"github.com/auth0/go-auth0/management"
"github.com/spf13/cobra"
)

var (
userRolesNumber = Flag{
Name: "Number",
LongForm: "number",
ShortForm: "n",
Help: "Number of user roles to retrieve. Minimum 1, maximum 1000.",
}
)

func userRolesCmd(cli *cli) *cobra.Command {
cmd := &cobra.Command{
Use: "roles",
Short: "Manage a user's roles",
Long: "Manage a user's assigned roles. To learn more about roles and their behavior, read " +
"[Role-based Access Control](https://auth0.com/docs/manage-users/access-control/rbac).",
}

cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(showUserRolesCmd(cli))

return cmd
}

func showUserRolesCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Number int
}

cmd := &cobra.Command{
Use: "show",
Args: cobra.MaximumNArgs(1),
Short: "Show a user's roles",
Long: "Display information about an existing user's assigned roles.",
Example: ` auth0 users roles show
auth0 users roles show <user-id>
auth0 users roles show <user-id> --number 100
auth0 users roles show <user-id> -n 100 --json`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := userID.Ask(cmd, &inputs.ID); err != nil {
return err
}
} else {
inputs.ID = args[0]
}

if inputs.Number < 1 || inputs.Number > 1000 {
return fmt.Errorf("number flag invalid, please pass a number between 1 and 1000")
}

list, err := getWithPagination(
cmd.Context(),
inputs.Number,
func(opts ...management.RequestOption) (result []interface{}, hasNext bool, err error) {
userRoleList, err := cli.api.User.Roles(inputs.ID, opts...)
if err != nil {
return nil, false, err
}

var output []interface{}
for _, userRole := range userRoleList.Roles {
output = append(output, userRole)
}

return output, userRoleList.HasNext(), nil
},
)
if err != nil {
return fmt.Errorf("failed to find roles for user with ID %s: %w", inputs.ID, err)
}

var userRoles []*management.Role
for _, item := range list {
userRoles = append(userRoles, item.(*management.Role))
}

cli.renderer.UserRoleList(userRoles)

return nil
},
}

cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")
userRolesNumber.RegisterInt(cmd, &inputs.Number, defaultPageSize)

return cmd
}
18 changes: 18 additions & 0 deletions internal/display/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ func (r *Renderer) RoleList(roles []*management.Role) {
r.Results(res)
}

func (r *Renderer) UserRoleList(roles []*management.Role) {
resource := "roles assigned to user"
r.Heading(fmt.Sprintf("%s (%d)", resource, len(roles)))

if len(roles) == 0 {
r.Output("No roles assigned to user.\n\n")
r.Infof("Use 'auth0 users roles assign' to assign roles to a user.")
return
}

var res []View
for _, role := range roles {
res = append(res, makeRoleView(role))
}

r.Results(res)
sergiught marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *Renderer) RoleShow(role *management.Role) {
r.Heading("role")
r.Result(makeRoleView(role))
Expand Down
18 changes: 18 additions & 0 deletions test/integration/scripts/run-test-suites.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /bin/bash -v

set -e

auth0 login \
--domain "${AUTH0_CLI_CLIENT_DOMAIN}" \
--client-id "${AUTH0_CLI_CLIENT_ID}" \
--client-secret "${AUTH0_CLI_CLIENT_SECRET}"

set +e

commander test --filter "$FILTER" --dir ./test/integration
sergiught marked this conversation as resolved.
Show resolved Hide resolved

exit_code=$?

bash ./test/integration/scripts/test-cleanup.sh

exit $exit_code
4 changes: 4 additions & 0 deletions test/integration/test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ tests:
email: [email protected] # Name is not being displayed, hence using email
exit-code: 0

users roles show:
command: auth0 users roles show $(cat ./test/integration/identifiers/user-id)
exit-code: 0

# Test 'roles create'
roles create and check data:
command: auth0 roles create --name integration-test-role-new1 --description testRole --json --no-input
Expand Down