- Notifications
You must be signed in to change notification settings - Fork928
feat: Add update user password endpoint#1310
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
346e5e4
4bd7557
de39cf5
2fe1716
212020a
2699445
355f163
56b29fd
30b8f15
f6be255
5df5763
b9dbd64
69af903
d85092b
96ce751
4f9f506
671c56d
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -76,14 +76,6 @@ func ExtractUserParam(db database.Store) func(http.Handler) http.Handler { | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I removed this because it was "overriding" RBAC roles. I know we don’t have the RBAC in place for all the user routes, but I can try to do that next. Probably I can send a PR on Monday. Thoughts? cc.:@f0ssel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Discussion on Slack:https://codercom.slack.com/archives/CJURPL8DN/p1651843502606399 | ||
ctx := context.WithValue(r.Context(), userParamContextKey{}, user) | ||
next.ServeHTTP(rw, r.WithContext(ctx)) | ||
}) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -360,6 +360,36 @@ func (api *api) putUserSuspend(rw http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(rw, http.StatusOK, convertUser(suspendedUser, organizations)) | ||
} | ||
func (api *api) putUserPassword(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
user = httpmw.UserParam(r) | ||
params codersdk.UpdateUserPasswordRequest | ||
) | ||
if !httpapi.Read(rw, r, ¶ms) { | ||
return | ||
} | ||
hashedPassword, err := userpassword.Hash(params.Password) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("hash password: %s", err.Error()), | ||
}) | ||
BrunoQuaresma marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return | ||
} | ||
err = api.Database.UpdateUserHashedPassword(r.Context(), database.UpdateUserHashedPasswordParams{ | ||
ID: user.ID, | ||
HashedPassword: []byte(hashedPassword), | ||
}) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("put user password: %s", err.Error()), | ||
}) | ||
return | ||
} | ||
httpapi.Write(rw, http.StatusNoContent, nil) | ||
} | ||
func (api *api) userRoles(rw http.ResponseWriter, r *http.Request) { | ||
user := httpmw.UserParam(r) | ||
@@ -577,7 +607,6 @@ func (api *api) postLogin(rw http.ResponseWriter, r *http.Request) { | ||
} | ||
// If the user doesn't exist, it will be a default struct. | ||
equal, err := userpassword.Compare(string(user.HashedPassword), loginWithPassword.Password) | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -287,6 +287,44 @@ func TestUpdateUserProfile(t *testing.T) { | ||
}) | ||
} | ||
func TestUpdateUserPassword(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Let's also add some tests to make sure the rbac is working here, I'd like to ensure that the user itself cannot perform this action, and neither can other non-admin users. CollaboratorAuthor
| ||
t.Parallel() | ||
t.Run("MemberCantUpdateAdminPassword", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
admin := coderdtest.CreateFirstUser(t, client) | ||
member := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID) | ||
err := member.UpdateUserPassword(context.Background(), admin.UserID, codersdk.UpdateUserPasswordRequest{ | ||
Password: "newpassword", | ||
}) | ||
require.Error(t, err, "member should not be able to update admin password") | ||
}) | ||
t.Run("AdminCanUpdateMemberPassword", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
admin := coderdtest.CreateFirstUser(t, client) | ||
member, err := client.CreateUser(context.Background(), codersdk.CreateUserRequest{ | ||
Email: "coder@coder.com", | ||
Username: "coder", | ||
Password: "password", | ||
OrganizationID: admin.OrganizationID, | ||
}) | ||
require.NoError(t, err, "create member") | ||
err = client.UpdateUserPassword(context.Background(), member.ID, codersdk.UpdateUserPasswordRequest{ | ||
Password: "newpassword", | ||
}) | ||
require.NoError(t, err, "admin should be able to update member password") | ||
// Check if the member can login using the new password | ||
_, err = client.LoginWithPassword(context.Background(), codersdk.LoginWithPasswordRequest{ | ||
Email: "coder@coder.com", | ||
Password: "newpassword", | ||
}) | ||
require.NoError(t, err, "member should login successfully with the new password") | ||
}) | ||
} | ||
func TestGrantRoles(t *testing.T) { | ||
t.Parallel() | ||
t.Run("UpdateIncorrectRoles", func(t *testing.T) { | ||