Changeset 570 in code for trunk/service.go


Ignore:
Timestamp:
Jun 28, 2021, 2:55:44 PM (4 years ago)
Author:
contact
Message:

Merge "change-password" into "user update"

Add a -password flag to the "user update" command.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/service.go

    r569 r570  
    261261                                },
    262262                                "update": {
    263                                         usage:  "[-realname <realname>]",
     263                                        usage:  "[-password <password>] [-realname <realname>]",
    264264                                        desc:   "update the current user",
    265265                                        handle: handleUserUpdate,
     
    272272                                },
    273273                        },
    274                 },
    275                 "change-password": {
    276                         usage:  "<new password>",
    277                         desc:   "change your password",
    278                         handle: handlePasswordChange,
    279274                },
    280275                "channel": {
     
    735730}
    736731
    737 func handlePasswordChange(dc *downstreamConn, params []string) error {
    738         if len(params) != 1 {
    739                 return fmt.Errorf("expected exactly one argument")
    740         }
    741 
    742         hashed, err := bcrypt.GenerateFromPassword([]byte(params[0]), bcrypt.DefaultCost)
    743         if err != nil {
    744                 return fmt.Errorf("failed to hash password: %v", err)
    745         }
    746         if err := dc.user.updatePassword(string(hashed)); err != nil {
    747                 return err
    748         }
    749 
    750         sendServicePRIVMSG(dc, "password updated")
    751         return nil
    752 }
    753 
    754732func handleUserCreate(dc *downstreamConn, params []string) error {
    755733        fs := newFlagSet()
     
    789767
    790768func handleUserUpdate(dc *downstreamConn, params []string) error {
    791         var realname *string
     769        var password, realname *string
    792770        fs := newFlagSet()
     771        fs.Var(stringPtrFlag{&password}, "password", "")
    793772        fs.Var(stringPtrFlag{&realname}, "realname", "")
    794773
     
    797776        }
    798777
     778        if password != nil {
     779                hashed, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)
     780                if err != nil {
     781                        return fmt.Errorf("failed to hash password: %v", err)
     782                }
     783                if err := dc.user.updatePassword(string(hashed)); err != nil {
     784                        return err
     785                }
     786        }
    799787        if realname != nil {
    800788                if err := dc.user.updateRealname(*realname); err != nil {
Note: See TracChangeset for help on using the changeset viewer.