Skip to content

Commit 0df44da

Browse files
Add password reset command
1 parent 624a938 commit 0df44da

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Uchu.Core/Handlers/Commands/StandardCommandHandler.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ public static string AddUser(string[] arguments)
8585
return $"\nSuccessfully added user: {name}!";
8686
}
8787

88+
[CommandHandler(Signature = "resetpassword", Help = "Reset a user's password")]
89+
public static string ResetPassword(string[] arguments)
90+
{
91+
if (arguments == null)
92+
throw new ArgumentNullException(nameof(arguments),
93+
"arguments cannot be null");
94+
95+
if (arguments.Length != 1)
96+
return "resetpassword <username>";
97+
98+
var username = arguments[0];
99+
100+
using var ctx = new UchuContext();
101+
var user = ctx.Users.FirstOrDefault(u => string.Equals(u.Username.ToUpper(), username.ToUpper()));
102+
103+
if (user == null)
104+
return "No user with that username exists";
105+
106+
Console.Write("New password: ");
107+
var password = GetPassword();
108+
109+
if (password.Length > 42)
110+
return "\nPasswords with more than 42 characters are not supported";
111+
112+
user.Password = BCrypt.Net.BCrypt.EnhancedHashPassword(password);
113+
ctx.SaveChanges();
114+
115+
return $"\nSuccessfully reset password for user: {user.Username}!";
116+
}
117+
88118
[CommandHandler(Signature = "removeuser", Help = "Remove a user")]
89119
public static string RemoveUser(string[] arguments)
90120
{

0 commit comments

Comments
 (0)