Skip to content

Commit

Permalink
DXCDT-314: Add ability to remove roles from a user (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Jan 17, 2023
1 parent 7938702 commit 231ec9b
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/auth0_users_roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Manage a user's assigned roles. To learn more about roles and their behavior, re
## Commands

- [auth0 users roles assign](auth0_users_roles_assign.md) - Assign roles to a user
- [auth0 users roles remove](auth0_users_roles_remove.md) - Remove roles from a user
- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles

1 change: 1 addition & 0 deletions docs/auth0_users_roles_assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ auth0 users roles assign [flags]
## Related Commands

- [auth0 users roles assign](auth0_users_roles_assign.md) - Assign roles to a user
- [auth0 users roles remove](auth0_users_roles_remove.md) - Remove roles from a user
- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles


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

Remove existing roles from a user.

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

## Examples

```
auth0 users roles remove <user-id>
auth0 users roles remove <user-id> --roles <role-id1,role-id2>
auth0 users roles rm <user-id> -r "rol_1eKJp3jV04SiU04h,rol_2eKJp3jV04SiU04h" --json
```


## Flags

```
--json Output in json format.
-r, --roles strings Roles to assign to a user.
```


## InheritedFlags

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


## Related Commands

- [auth0 users roles assign](auth0_users_roles_assign.md) - Assign roles to a user
- [auth0 users roles remove](auth0_users_roles_remove.md) - Remove roles from a user
- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles


1 change: 1 addition & 0 deletions docs/auth0_users_roles_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ auth0 users roles show [flags]
## Related Commands

- [auth0 users roles assign](auth0_users_roles_assign.md) - Assign roles to a user
- [auth0 users roles remove](auth0_users_roles_remove.md) - Remove roles from a user
- [auth0 users roles show](auth0_users_roles_show.md) - Show a user's roles


3 changes: 3 additions & 0 deletions internal/auth0/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ type UserAPI interface {

// AssignRoles assigns roles to a user.
AssignRoles(id string, roles []*management.Role, opts ...management.RequestOption) error

// RemoveRoles removes roles from a user.
RemoveRoles(id string, roles []*management.Role, opts ...management.RequestOption) error
}
107 changes: 104 additions & 3 deletions internal/cli/users_roles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -29,6 +30,8 @@ var (
Help: "Roles to assign to a user.",
IsRequired: true,
}

errNoRolesSelected = errors.New("required to select at least one role")
)

type userRolesInput struct {
Expand All @@ -48,6 +51,7 @@ func userRolesCmd(cli *cli) *cobra.Command {
cmd.SetUsageTemplate(resourceUsageTemplate())
cmd.AddCommand(showUserRolesCmd(cli))
cmd.AddCommand(addUserRolesCmd(cli))
cmd.AddCommand(removeUserRolesCmd(cli))

return cmd
}
Expand Down Expand Up @@ -137,7 +141,7 @@ func addUserRolesCmd(cli *cli) *cobra.Command {
}

if len(inputs.Roles) == 0 {
if err := cli.pickUserRoles(&inputs); err != nil {
if err := cli.pickUserRolesToAdd(&inputs); err != nil {
return err
}
}
Expand Down Expand Up @@ -175,7 +179,67 @@ func addUserRolesCmd(cli *cli) *cobra.Command {
return cmd
}

func (cli *cli) pickUserRoles(inputs *userRolesInput) error {
func removeUserRolesCmd(cli *cli) *cobra.Command {
var inputs userRolesInput

cmd := &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Args: cobra.MaximumNArgs(1),
Short: "Remove roles from a user",
Long: "Remove existing roles from a user.",
Example: ` auth0 users roles remove <user-id>
auth0 users roles remove <user-id> --roles <role-id1,role-id2>
auth0 users roles rm <user-id> -r "rol_1eKJp3jV04SiU04h,rol_2eKJp3jV04SiU04h" --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 len(inputs.Roles) == 0 {
if err := cli.pickUserRolesToRemove(&inputs); err != nil {
return err
}
}

var rolesToRemove []*management.Role
for _, roleID := range inputs.Roles {
rolesToRemove = append(rolesToRemove, &management.Role{
ID: auth0.String(roleID),
})
}

if err := ansi.Waiting(func() (err error) {
return cli.api.User.RemoveRoles(inputs.ID, rolesToRemove)
}); err != nil {
return fmt.Errorf("failed to remove roles for user with ID %s: %w", inputs.ID, err)
}

var userRoleList *management.RoleList
if err := ansi.Waiting(func() (err error) {
userRoleList, err = cli.api.User.Roles(inputs.ID)
return err
}); err != nil {
return fmt.Errorf("failed to find roles for user with ID %s: %w", inputs.ID, err)
}

cli.renderer.UserRoleList(userRoleList.Roles)

return nil
},
}

userRoles.RegisterStringSlice(cmd, &inputs.Roles, nil)
cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")

return cmd
}

func (cli *cli) pickUserRolesToAdd(inputs *userRolesInput) error {
var currentUserRoleList *management.RoleList
if err := ansi.Waiting(func() (err error) {
currentUserRoleList, err = cli.api.User.Roles(inputs.ID, management.PerPage(100))
Expand Down Expand Up @@ -220,7 +284,44 @@ func (cli *cli) pickUserRoles(inputs *userRolesInput) error {
}

if len(inputs.Roles) == 0 {
return fmt.Errorf("required to select at least one role")
return errNoRolesSelected
}

return nil
}

func (cli *cli) pickUserRolesToRemove(inputs *userRolesInput) error {
var currentUserRoleList *management.RoleList
if err := ansi.Waiting(func() (err error) {
currentUserRoleList, err = cli.api.User.Roles(inputs.ID)
return err
}); err != nil {
return fmt.Errorf("failed to find the current roles for user with ID %s: %w", inputs.ID, err)
}

const emptySpace = " "
var options []string
for _, role := range currentUserRoleList.Roles {
options = append(options, fmt.Sprintf("%s%s(Name: %s)", role.GetID(), emptySpace, role.GetName()))
}

rolesPrompt := &survey.MultiSelect{
Message: "Roles",
Options: options,
}

var selectedRoles []string
if err := survey.AskOne(rolesPrompt, &selectedRoles); err != nil {
return err
}

for _, selectedRole := range selectedRoles {
indexOfFirstEmptySpace := strings.Index(selectedRole, emptySpace)
inputs.Roles = append(inputs.Roles, selectedRole[:indexOfFirstEmptySpace])
}

if len(inputs.Roles) == 0 {
return errNoRolesSelected
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions test/integration/test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,7 @@ tests:
users roles add:
command: auth0 users roles add $(cat ./test/integration/identifiers/user-id) -r $(cat ./test/integration/identifiers/role-id)
exit-code: 0

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

0 comments on commit 231ec9b

Please sign in to comment.