DeleteUser permanently removes a user record, their YAML file, and all index entries. It refuses to delete a user that is currently online.
(userId int)
| 699 | // DeleteUser permanently removes a user record, their YAML file, and all index |
| 700 | // entries. It refuses to delete a user that is currently online. |
| 701 | func DeleteUser(userId int) error { |
| 702 | u, err := LoadUserById(userId) |
| 703 | if err != nil { |
| 704 | return fmt.Errorf("user %d not found: %w", userId, err) |
| 705 | } |
| 706 | |
| 707 | userManagerMu.Lock() |
| 708 | defer userManagerMu.Unlock() |
| 709 | |
| 710 | if _, online := userManager.Users[userId]; online { |
| 711 | return fmt.Errorf("user %d is currently online and cannot be deleted", userId) |
| 712 | } |
| 713 | |
| 714 | filePath := util.FilePath(string(configs.GetFilePathsConfig().DataFiles), `/`, `users`, `/`, strconv.Itoa(userId)+`.yaml`) |
| 715 | if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) { |
| 716 | return fmt.Errorf("failed to remove user file: %w", err) |
| 717 | } |
| 718 | |
| 719 | idx := GetUserIndex() |
| 720 | _ = idx.RemoveByUsername(u.Username) |
| 721 | |
| 722 | GetCharacterIndex().Remove(u.Character.Name) |
| 723 | |
| 724 | delete(userManager.Users, userId) |
| 725 | delete(userManager.Usernames, u.Username) |
| 726 | if connId, ok := userManager.UserConnections[userId]; ok { |
| 727 | delete(userManager.Connections, connId) |
| 728 | delete(userManager.LinkDeadConnections, connId) |
| 729 | } |
| 730 | delete(userManager.UserConnections, userId) |
| 731 | |
| 732 | return nil |
| 733 | } |
| 734 | |
| 735 | func Exists(name string) bool { |
| 736 |
nothing calls this directly
no test coverage detected