8000 feat: add edit-role within user command by brettkolodny · Pull Request #17341 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: add edit-role within user command #17341

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

Merged
Merged
Show file tree
Hide file tree
Changes from 10 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
19 changes: 10 additions & 9 deletions cli/testdata/coder_users_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ USAGE:
Aliases: user

SUBCOMMANDS:
activate Update a user's status to 'active'. Active users can fully
interact with the platform
create
delete Delete a user by username or user_id.
list
show Show a single user. Use 'me' to indicate the currently
authenticated user.
suspend Update a user's status to 'suspended'. A suspended user cannot
log into the platform
activate Update a user's status to 'active'. Active users can fully
interact with the platform
create
delete Delete a user by username or user_id.
edit-roles Edit a user's roles by username or id
list
show Show a single user. Use 'me' to indicate the currently
authenticated user.
suspend Update a user's status to 'suspended'. A suspended user cannot
log into the platform

———
Run `coder --help` for a list of global options.
9 changes: 9 additions & 0 deletions cli/testdata/coder_users_edit-roles_--help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coder v0.0.0-devel

USAGE:
coder users edit-roles <username|user_id>

Edit a user's roles by username or id

———
Run `coder --help` for a list of global options.
66 changes: 66 additions & 0 deletions cli/usereditroles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cli

import (
"sort"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)

func (r *RootCmd) userEditRoles() *serpent.Command {
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "edit-roles <username|user_id>",
Short: "Edit a user's roles by username or id",
Middleware: serpent.Chain(serpent.RequireNArgs(1), r.InitClient(client)),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()

user, err := client.User(ctx, inv.Args[0])
if err != nil {
return xerrors.Errorf("fetch user: %w", err)
}

roles, err := client.ListSiteRoles(ctx)
if err != nil {
return xerrors.Errorf("fetch site roles: %w", err)
}

siteRoles := make([]string, 0)
for _, role := range roles {
if role.Assignable {
siteRoles = append(siteRoles, role.Name)
}
}

userRoles, err := client.UserRoles(ctx, user.Username)
if err != nil {
return xerrors.Errorf("fetch user roles: %w", err)
}

sort.Strings(siteRoles)
selectedRoles, err := cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message: "Select the roles you'd like to assign to the user",
Options: siteRoles,
Defaults: userRoles.Roles,
})
if err != nil {
return xerrors.Errorf("selecting roles for user: %w", err)
}

_, err = client.UpdateUserRoles(ctx, user.Username, codersdk.UpdateRoles{
Roles: selectedRoles,
})
if err != nil {
return xerrors.Errorf("update user roles: %w", err)
}

return nil
},
}

return cmd
}
1 change: 1 addition & 0 deletions cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (r *RootCmd) users() *serpent.Command {
r.userList(),
r.userSingle(),
r.userDelete(),
r.userEditRoles(),
r.createUserStatusCommand(codersdk.UserStatusActive),
r.createUserStatusCommand(codersdk.UserStatusSuspended),
},
Expand Down
5 changes: 5 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,11 @@
"description": "Delete a user by username or user_id.",
"path": "reference/cli/users_delete.md"
},
{
"title": "users edit-roles",
"description": "Edit a user's roles by username or id",
"path": "reference/cli/users_edit-roles.md"
},
{
"title": "users list",
"path": "reference/cli/users_list.md"
Expand Down
17 changes: 9 additions & 8 deletions docs/reference/cli/users.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions docs/reference/cli/users_edit-roles.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0