Skip to content

Commit

Permalink
feat(users): add shouldResetPassword flag to user create and update
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed Jan 22, 2025
1 parent af1c9b7 commit 0036695
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 17 deletions.
20 changes: 16 additions & 4 deletions api/spec/json/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,21 @@
"examples": {
"powershell": [
{
"description": "Create a user",
"description": "Create a user and force user to change their password when logging in",
"beforeEach": [
"$Username = \"testuser_\" + [guid]::NewGuid().Guid.Substring(1,10)",
"$NewPassword = New-RandomPassword"
],
"command": "New-user -Username \"$Username\" -Email \"[email protected]\" -Password \"$NewPassword\"",
"command": "New-user -Username \"$Username\" -Email \"[email protected]\" -Password \"$NewPassword\" -ShouldResetPassword",
"afterEach": [
"Get-UserByName -Name \"$Username\" | Remove-User"
]
}
],
"go": [
{
"description": "Create a user",
"command": "c8y users create --userName \"testuser1\" --email \"[email protected]\" --password \"a0)8k2kld9lm!\""
"description": "Create a user and force user to change their password when logging in",
"command": "c8y users create --userName \"testuser1\" --email \"[email protected]\" --password 'a0)8k2kld9lm' --shouldResetPassword"
},
{
"description": "Create a user using a template",
Expand Down Expand Up @@ -221,6 +221,12 @@
"required": false,
"description": "User password. Min: 6, max: 32 characters. Only Latin1 chars allowed"
},
{
"name": "shouldResetPassword",
"type": "boolean",
"required": false,
"description": "User must reset password on next login"
},
{
"name": "sendPasswordResetEmail",
"type": "boolean",
Expand Down Expand Up @@ -472,6 +478,12 @@
"required": false,
"description": "User password. Min: 6, max: 32 characters. Only Latin1 chars allowed"
},
{
"name": "shouldResetPassword",
"type": "boolean",
"required": false,
"description": "User must reset password on next login"
},
{
"name": "sendPasswordResetEmail",
"type": "boolean",
Expand Down
18 changes: 14 additions & 4 deletions api/spec/yaml/users.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ commands:
powershell: New-User
examples:
powershell:
- description: Create a user
- description: Create a user and force user to change their password when logging in
beforeEach:
- $Username = "testuser_" + [guid]::NewGuid().Guid.Substring(1,10)
- $NewPassword = New-RandomPassword
command: New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword"
command: New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword" -ShouldResetPassword
afterEach:
- Get-UserByName -Name "$Username" | Remove-User
go:
- description: Create a user
command: c8y users create --userName "testuser1" --email "[email protected]" --password "a0)8k2kld9lm!"
- description: Create a user and force user to change their password when logging in
command: c8y users create --userName "testuser1" --email "[email protected]" --password 'a0)8k2kld9lm' --shouldResetPassword

- description: Create a user using a template
command: |
Expand Down Expand Up @@ -169,6 +169,11 @@ commands:
required: false
description: 'User password. Min: 6, max: 32 characters. Only Latin1 chars allowed'

- name: shouldResetPassword
type: boolean
required: false
description: User must reset password on next login

# TODO: Only set value if it has changed!
- name: sendPasswordResetEmail
type: boolean
Expand Down Expand Up @@ -353,6 +358,11 @@ commands:
required: false
description: 'User password. Min: 6, max: 32 characters. Only Latin1 chars allowed'

- name: shouldResetPassword
type: boolean
required: false
description: User must reset password on next login

# TODO: Only set value if it has changed!
- name: sendPasswordResetEmail
type: boolean
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/users/create/create.auto.go

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

2 changes: 2 additions & 0 deletions pkg/cmd/users/update/update.auto.go

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

7 changes: 4 additions & 3 deletions tests/auto/users/tests/users_create.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
tests:
users_create_Create a user:
command: c8y users create --userName "testuser1" --email "[email protected]" --password "a0)8k2kld9lm!"
users_create_Create a user and force user to change their password when logging in:
command: c8y users create --userName "testuser1" --email "[email protected]" --password 'a0)8k2kld9lm' --shouldResetPassword
exit-code: 0
stdout:
json:
body.email: [email protected]
body.password: a0)8k2kld9lm!
body.password: a0)8k2kld9lm
body.shouldResetPassword: "true"
body.userName: testuser1
method: POST
path: /user/$C8Y_TENANT/users
Expand Down
9 changes: 7 additions & 2 deletions tools/PSc8y/Public/New-User.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Create a new user so that they can access the tenant
https://reubenmiller.github.io/go-c8y-cli/docs/cli/c8y/users_create
.EXAMPLE
PS> New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword"
PS> New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword" -ShouldResetPassword
Create a user
Create a user and force user to change their password when logging in
#>
Expand Down Expand Up @@ -58,6 +58,11 @@ Create a user
[string]
$Password,

# User must reset password on next login
[Parameter()]
[switch]
$ShouldResetPassword,

# Send password reset email to the user instead of setting a password
[Parameter()]
[ValidateSet('true','false')]
Expand Down
5 changes: 5 additions & 0 deletions tools/PSc8y/Public/Update-User.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ Update a user
[string]
$Password,

# User must reset password on next login
[Parameter()]
[switch]
$ShouldResetPassword,

# Send password reset email to the user instead of setting a password
[Parameter()]
[ValidateSet('true','false')]
Expand Down
4 changes: 2 additions & 2 deletions tools/PSc8y/Tests/New-User.auto.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Describe -Name "New-User" {

}

It "Create a user" {
$Response = PSc8y\New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword"
It "Create a user and force user to change their password when logging in" {
$Response = PSc8y\New-user -Username "$Username" -Email "[email protected]" -Password "$NewPassword" -ShouldResetPassword
$LASTEXITCODE | Should -Be 0
$Response | Should -Not -BeNullOrEmpty
}
Expand Down

0 comments on commit 0036695

Please sign in to comment.