Merge "change-password" into "user update"

Add a -password flag to the "user update" command.
This commit is contained in:
Simon Ser 2021-06-28 16:49:16 +02:00
parent f8e853fab1
commit 09b04792b9
2 changed files with 13 additions and 28 deletions

View File

@ -309,15 +309,12 @@ abbreviated form, for instance *network* can be abbreviated as *net* or just
Set the user's realname. This is used as a fallback if there is no Set the user's realname. This is used as a fallback if there is no
realname set for a network. realname set for a network.
*user update* [-realname <realname>] *user update* [-password <password>] [-realname <realname>]
Update the current user. Update the current user.
*user delete* <username> *user delete* <username>
Delete a soju user. Only admins can delete accounts. Delete a soju user. Only admins can delete accounts.
*change-password* <new password>
Change current user password.
# AUTHORS # AUTHORS
Maintained by Simon Ser <contact@emersion.fr>, who is assisted by other Maintained by Simon Ser <contact@emersion.fr>, who is assisted by other

View File

@ -260,7 +260,7 @@ func init() {
admin: true, admin: true,
}, },
"update": { "update": {
usage: "[-realname <realname>]", usage: "[-password <password>] [-realname <realname>]",
desc: "update the current user", desc: "update the current user",
handle: handleUserUpdate, handle: handleUserUpdate,
}, },
@ -272,11 +272,6 @@ func init() {
}, },
}, },
}, },
"change-password": {
usage: "<new password>",
desc: "change your password",
handle: handlePasswordChange,
},
"channel": { "channel": {
children: serviceCommandSet{ children: serviceCommandSet{
"status": { "status": {
@ -734,23 +729,6 @@ func handleServiceSASLReset(dc *downstreamConn, params []string) error {
return nil return nil
} }
func handlePasswordChange(dc *downstreamConn, params []string) error {
if len(params) != 1 {
return fmt.Errorf("expected exactly one argument")
}
hashed, err := bcrypt.GenerateFromPassword([]byte(params[0]), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %v", err)
}
if err := dc.user.updatePassword(string(hashed)); err != nil {
return err
}
sendServicePRIVMSG(dc, "password updated")
return nil
}
func handleUserCreate(dc *downstreamConn, params []string) error { func handleUserCreate(dc *downstreamConn, params []string) error {
fs := newFlagSet() fs := newFlagSet()
username := fs.String("username", "", "") username := fs.String("username", "", "")
@ -788,14 +766,24 @@ func handleUserCreate(dc *downstreamConn, params []string) error {
} }
func handleUserUpdate(dc *downstreamConn, params []string) error { func handleUserUpdate(dc *downstreamConn, params []string) error {
var realname *string var password, realname *string
fs := newFlagSet() fs := newFlagSet()
fs.Var(stringPtrFlag{&password}, "password", "")
fs.Var(stringPtrFlag{&realname}, "realname", "") fs.Var(stringPtrFlag{&realname}, "realname", "")
if err := fs.Parse(params); err != nil { if err := fs.Parse(params); err != nil {
return err return err
} }
if password != nil {
hashed, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("failed to hash password: %v", err)
}
if err := dc.user.updatePassword(string(hashed)); err != nil {
return err
}
}
if realname != nil { if realname != nil {
if err := dc.user.updateRealname(*realname); err != nil { if err := dc.user.updateRealname(*realname); err != nil {
return err return err