Changeset 625 in code for trunk/service.go
- Timestamp:
- Oct 12, 2021, 7:11:14 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/service.go
r615 r625 126 126 } 127 127 if cmd.admin && !dc.user.Admin { 128 sendServicePRIVMSG(dc, fmt.Sprintf(`error: you must be an admin to use this command`))128 sendServicePRIVMSG(dc, "error: you must be an admin to use this command") 129 129 return 130 130 } … … 767 767 } 768 768 769 func popArg(params []string) (string, []string) { 770 if len(params) > 0 && !strings.HasPrefix(params[0], "-") { 771 return params[0], params[1:] 772 } 773 return "", params 774 } 775 769 776 func handleUserUpdate(dc *downstreamConn, params []string) error { 770 777 var password, realname *string 778 var admin *bool 771 779 fs := newFlagSet() 772 780 fs.Var(stringPtrFlag{&password}, "password", "") 773 781 fs.Var(stringPtrFlag{&realname}, "realname", "") 774 782 fs.Var(boolPtrFlag{&admin}, "admin", "") 783 784 username, params := popArg(params) 775 785 if err := fs.Parse(params); err != nil { 776 786 return err 777 787 } 778 779 // copy the user record because we'll mutate it 780 record := dc.user.User 781 788 if len(fs.Args()) > 0 { 789 return fmt.Errorf("unexpected argument") 790 } 791 792 var hashed *string 782 793 if password != nil { 783 hashed , err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost)794 hashedBytes, err := bcrypt.GenerateFromPassword([]byte(*password), bcrypt.DefaultCost) 784 795 if err != nil { 785 796 return fmt.Errorf("failed to hash password: %v", err) 786 797 } 787 record.Password = string(hashed) 788 } 789 if realname != nil { 790 record.Realname = *realname 791 } 792 793 if err := dc.user.updateUser(&record); err != nil { 794 return err 795 } 796 797 sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", dc.user.Username)) 798 hashedStr := string(hashedBytes) 799 hashed = &hashedStr 800 } 801 802 if username != "" && username != dc.user.Username { 803 if !dc.user.Admin { 804 return fmt.Errorf("you must be an admin to update other users") 805 } 806 if realname != nil { 807 return fmt.Errorf("cannot update -realname of other user") 808 } 809 810 u := dc.srv.getUser(username) 811 if u == nil { 812 return fmt.Errorf("unknown username %q", username) 813 } 814 815 done := make(chan error, 1) 816 u.events <- eventUserUpdate{ 817 password: hashed, 818 admin: admin, 819 done: done, 820 } 821 if err := <-done; err != nil { 822 return err 823 } 824 825 sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", username)) 826 } else { 827 // copy the user record because we'll mutate it 828 record := dc.user.User 829 830 if hashed != nil { 831 record.Password = *hashed 832 } 833 if realname != nil { 834 record.Realname = *realname 835 } 836 if admin != nil { 837 return fmt.Errorf("cannot update -admin of own user") 838 } 839 840 if err := dc.user.updateUser(&record); err != nil { 841 return err 842 } 843 844 sendServicePRIVMSG(dc, fmt.Sprintf("updated user %q", dc.user.Username)) 845 } 846 798 847 return nil 799 848 }
Note:
See TracChangeset
for help on using the changeset viewer.