Skip to content

Commit

Permalink
Allow an admin to change another user's password
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 17, 2024
1 parent 45394e0 commit 7f909ee
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Shoko.Server/API/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ public ActionResult<object> Login(AuthUser auth)
/// Change the password. Invalidates the current user's apikeys. Reauth after using this!
/// </summary>
/// <param name="newPassword"></param>
/// <param name="userID">Optionally, an admin can change another user's passowrd</param>
/// <returns></returns>
[HttpPost("ChangePassword")]
[Authorize]
public ActionResult ChangePassword([FromBody] string newPassword)
public ActionResult ChangePassword([FromBody] string newPassword, [FromQuery] int? userID = null)
{
try
{
User.Password = Digest.Hash(newPassword.Trim());
RepoFactory.JMMUser.Save(User);
RepoFactory.AuthTokens.DeleteAllWithUserID(User.JMMUserID);
var user = User;
if (userID != null && User.IsAdmin == 1) user = RepoFactory.JMMUser.GetByID(userID.Value);
if (user == null) return BadRequest("Could not get user");
user.Password = Digest.Hash(newPassword.Trim());
RepoFactory.JMMUser.Save(user);
RepoFactory.AuthTokens.DeleteAllWithUserID(user.JMMUserID);
return Ok();
}
catch (Exception ex)
Expand Down

0 comments on commit 7f909ee

Please sign in to comment.