Skip to content

Commit

Permalink
Add ability to remove roles from a user
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 12, 2023
1 parent 822a55a commit 3aea807
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 4 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

3 changes: 2 additions & 1 deletion docs/auth0_users_roles_assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ auth0 users roles assign [flags]
```
auth0 users roles assign <user-id>
auth0 users roles associate <user-id> --roles <role-id1,role-id2>
auth0 users roles add <user-id> -r "rol_1eKJp3jV04SiU04h,rol_2eKJp3jV04SiU04h"
auth0 users roles add <user-id> -r "rol_1eKJp3jV04SiU04h,rol_2eKJp3jV04SiU04h" --json
```


Expand All @@ -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 @@ -39,6 +39,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 any roles associated to a user.
RemoveRoles(id string, roles []*management.Role, opts ...management.RequestOption) error
}
100 changes: 97 additions & 3 deletions internal/cli/users_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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 @@ -92,7 +93,7 @@ func addUserRolesCmd(cli *cli) *cobra.Command {
Long: "Assign existing roles to a user.",
Example: ` auth0 users roles assign <user-id>
auth0 users roles associate <user-id> --roles <role-id1,role-id2>
auth0 users roles add <user-id> -r "rol_1eKJp3jV04SiU04h,rol_2eKJp3jV04SiU04h"`,
auth0 users roles add <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 {
Expand All @@ -103,7 +104,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 @@ -141,7 +142,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)
Expand Down Expand Up @@ -188,6 +249,39 @@ func (cli *cli) pickUserRoles(inputs *userRolesInput) error {
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])
}

return nil
}

func containsRole(roles []*management.Role, roleID string) bool {
for _, role := range roles {
if role.GetID() == roleID {
Expand Down

0 comments on commit 3aea807

Please sign in to comment.