diff --git a/Projects/Server.Tests/Tests/Network/Packets/Outgoing/AccountPacketTests.cs b/Projects/Server.Tests/Tests/Network/Packets/Outgoing/AccountPacketTests.cs index ed3bfd4e3..a384a2d44 100644 --- a/Projects/Server.Tests/Tests/Network/Packets/Outgoing/AccountPacketTests.cs +++ b/Projects/Server.Tests/Tests/Network/Packets/Outgoing/AccountPacketTests.cs @@ -25,7 +25,7 @@ public class AccountPacketTests : IClassFixture public int CompareTo(IAccount other) => throw new NotImplementedException(); - public string Username { get; set; } + public string Username { get; } public string Email { get; set; } public AccessLevel AccessLevel { get; set; } public int Length { get; } @@ -60,6 +60,7 @@ public class AccountPacketTests : IClassFixture public bool Deleted { get; } public void Delete() => throw new NotImplementedException(); + public bool TrySetUsername(string username) => throw new NotImplementedException(); public void SetPassword(string password) => throw new NotImplementedException(); diff --git a/Projects/Server/IAccount.cs b/Projects/Server/IAccount.cs index 911bb8c0a..57b70baaa 100644 --- a/Projects/Server/IAccount.cs +++ b/Projects/Server/IAccount.cs @@ -97,7 +97,7 @@ public interface IGoldAccount public interface IAccount : ISerializable, IGoldAccount, IComparable { - string Username { get; set; } + string Username { get; } string Email { get; set; } AccessLevel AccessLevel { get; set; } @@ -107,6 +107,7 @@ public interface IAccount : ISerializable, IGoldAccount, IComparable Mobile this[int index] { get; set; } void Delete(); + bool TrySetUsername(string username); void SetPassword(string password); bool CheckPassword(string password); } diff --git a/Projects/UOContent/Accounting/Account.cs b/Projects/UOContent/Accounting/Account.cs index 73519eb04..9e554fd72 100644 --- a/Projects/UOContent/Accounting/Account.cs +++ b/Projects/UOContent/Accounting/Account.cs @@ -21,7 +21,7 @@ public partial class Account : IAccount, IComparable public static readonly TimeSpan EmptyInactiveDuration = TimeSpan.FromDays(30.0); [InternString] - [SerializableField(0)] + [SerializableField(0, setter: "private")] private string _username; [SerializableField(1)] @@ -365,6 +365,19 @@ public partial class Account : IAccount, IComparable public bool Deleted { get; private set; } + public bool TrySetUsername(string username) + { + if (username == _username || Accounts.GetAccount(username) != null) + { + return false; + } + + Accounts.Remove(this); + Username = username; + Accounts.Add(this); + return true; + } + public void SetPassword(string plainPassword) { var phrase = _passwordAlgorithm is PasswordProtectionAlgorithm.SHA1 or PasswordProtectionAlgorithm.SHA2