]> git.r.bdr.sh - rbdr/mobius/commitdiff
Add support for account login rename
authorJeff Halter <redacted>
Sat, 8 Jun 2024 17:54:16 +0000 (10:54 -0700)
committerJeff Halter <redacted>
Sat, 8 Jun 2024 17:54:51 +0000 (10:54 -0700)
hotline/server.go
hotline/transaction_handlers.go

index a77e79295e0e8e318a68855ac4fe5111328939e7..aca2221a1adc22676c6bf676e7f7d43f05722d53 100644 (file)
@@ -447,11 +447,12 @@ func (s *Server) UpdateUser(login, newLogin, name, password string, access acces
 
        // update renames the user login
        if login != newLogin {
 
        // update renames the user login
        if login != newLogin {
-               err := os.Rename(filepath.Join(s.ConfigDir, "Users", login+".yaml"), filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"))
+               err := os.Rename(filepath.Join(s.ConfigDir, "Users", path.Join("/", login)+".yaml"), filepath.Join(s.ConfigDir, "Users", path.Join("/", newLogin)+".yaml"))
                if err != nil {
                if err != nil {
-                       return err
+                       return fmt.Errorf("unable to rename account: %w", err)
                }
                s.Accounts[newLogin] = s.Accounts[login]
                }
                s.Accounts[newLogin] = s.Accounts[login]
+               s.Accounts[newLogin].Login = newLogin
                delete(s.Accounts, login)
        }
 
                delete(s.Accounts, login)
        }
 
index d0651221a0921a7a440cfcbbea447c132a47d79c..d4269af7dab773bb0c163982720913d15a4ed9ed 100644 (file)
@@ -776,26 +776,44 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                        return res, err
                }
 
                        return res, err
                }
 
+               // If there's only one subfield, that indicates this is a delete operation for the login in FieldData
                if len(subFields) == 1 {
                if len(subFields) == 1 {
-                       login := decodeString(getField(FieldData, &subFields).Data)
-                       cc.logger.Infow("DeleteUser", "login", login)
-
                        if !cc.Authorize(accessDeleteUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
                                return res, err
                        }
 
                        if !cc.Authorize(accessDeleteUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts."))
                                return res, err
                        }
 
+                       login := decodeString(getField(FieldData, &subFields).Data)
+                       cc.logger.Infow("DeleteUser", "login", login)
+
                        if err := cc.Server.DeleteUser(login); err != nil {
                                return res, err
                        }
                        continue
                }
 
                        if err := cc.Server.DeleteUser(login); err != nil {
                                return res, err
                        }
                        continue
                }
 
-               login := decodeString(getField(FieldUserLogin, &subFields).Data)
+               // login of the account to update
+               var accountToUpdate, loginToRename string
+
+               // If FieldData is included, this is a rename operation where FieldData contains the login of the existing
+               // account and FieldUserLogin contains the new login.
+               if getField(FieldData, &subFields) != nil {
+                       loginToRename = decodeString(getField(FieldData, &subFields).Data)
+               }
+               userLogin := decodeString(getField(FieldUserLogin, &subFields).Data)
+               if loginToRename != "" {
+                       accountToUpdate = loginToRename
+               } else {
+                       accountToUpdate = userLogin
+               }
 
 
-               // check if the login dataFile; if so, we know we are updating an existing user
-               if acc, ok := cc.Server.Accounts[login]; ok {
-                       cc.logger.Infow("UpdateUser", "login", login)
+               // Check if accountToUpdate has an existing account.  If so, we know we are updating an existing user.
+               if acc, ok := cc.Server.Accounts[accountToUpdate]; ok {
+                       if loginToRename != "" {
+                               cc.logger.Infow("RenameUser", "prevLogin", accountToUpdate, "newLogin", userLogin)
+                       } else {
+                               cc.logger.Infow("UpdateUser", "login", accountToUpdate)
+                       }
 
                        // account exists, so this is an update action
                        if !cc.Authorize(accessModifyUser) {
 
                        // account exists, so this is an update action
                        if !cc.Authorize(accessModifyUser) {
@@ -834,13 +852,13 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                return res, err
                        }
                } else {
                                return res, err
                        }
                } else {
-                       cc.logger.Infow("CreateUser", "login", login)
-
                        if !cc.Authorize(accessCreateUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
                                return res, nil
                        }
 
                        if !cc.Authorize(accessCreateUser) {
                                res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts."))
                                return res, nil
                        }
 
+                       cc.logger.Infow("CreateUser", "login", userLogin)
+
                        newAccess := accessBitmap{}
                        copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
 
                        newAccess := accessBitmap{}
                        copy(newAccess[:], getField(FieldUserAccess, &subFields).Data)
 
@@ -853,7 +871,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er
                                }
                        }
 
                                }
                        }
 
-                       err = cc.Server.NewUser(login, string(getField(FieldUserName, &subFields).Data), string(getField(FieldUserPassword, &subFields).Data), newAccess)
+                       err = cc.Server.NewUser(userLogin, string(getField(FieldUserName, &subFields).Data), string(getField(FieldUserPassword, &subFields).Data), newAccess)
                        if err != nil {
                                return append(res, cc.NewErrReply(t, "Cannot create account because there is already an account with that login.")), nil
                        }
                        if err != nil {
                                return append(res, cc.NewErrReply(t, "Cannot create account because there is already an account with that login.")), nil
                        }