minor fixes and usings

This commit is contained in:
Phallacy 2019-03-27 22:46:25 -07:00
parent b07c146fd9
commit 5e8496bc59

View File

@ -10,7 +10,6 @@ using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Users; using MediaBrowser.Model.Users;
using Microsoft.Win32.SafeHandles;
namespace Emby.Server.Implementations.Library namespace Emby.Server.Implementations.Library
{ {
@ -37,13 +36,15 @@ namespace Emby.Server.Implementations.Library
public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin) public async Task<PinRedeemResult> RedeemPasswordResetPin(string pin)
{ {
SerializablePasswordReset spr;
HashSet<string> usersreset = new HashSet<string>(); HashSet<string> usersreset = new HashSet<string>();
foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{_passwordResetFileBaseName}*")) foreach (var resetfile in Directory.EnumerateFiles(_passwordResetFileBaseDir, $"{_passwordResetFileBaseName}*"))
{ {
using (var str = File.OpenRead(resetfile)) using (var str = File.OpenRead(resetfile))
{ {
var spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false); spr = await _jsonSerializer.DeserializeFromStreamAsync<SerializablePasswordReset>(str).ConfigureAwait(false);
} }
if (spr.ExpirationDate < DateTime.Now) if (spr.ExpirationDate < DateTime.Now)
{ {
File.Delete(resetfile); File.Delete(resetfile);
@ -51,12 +52,14 @@ namespace Emby.Server.Implementations.Library
else if (spr.Pin == pin) else if (spr.Pin == pin)
{ {
var resetUser = _userManager.GetUserByName(spr.UserName); var resetUser = _userManager.GetUserByName(spr.UserName);
if (resetUser != null) if (resetUser == null)
{ {
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false); throw new Exception($"User with a username of {spr.UserName} not found");
usersreset.Add(resetUser.Name);
File.Delete(resetfile);
} }
await _userManager.ChangePassword(resetUser, pin).ConfigureAwait(false);
usersreset.Add(resetUser.Name);
File.Delete(resetfile);
} }
} }
@ -76,7 +79,7 @@ namespace Emby.Server.Implementations.Library
public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork) public async Task<ForgotPasswordResult> StartForgotPasswordProcess(MediaBrowser.Controller.Entities.User user, bool isInNetwork)
{ {
string pin = new Random().Next(99999999).ToString("00000000",CultureInfo.InvariantCulture); string pin = new Random().Next(99999999).ToString("00000000", CultureInfo.InvariantCulture);
DateTime expireTime = DateTime.Now.AddMinutes(30); DateTime expireTime = DateTime.Now.AddMinutes(30);
string filePath = _passwordResetFileBase + user.InternalId + ".json"; string filePath = _passwordResetFileBase + user.InternalId + ".json";
SerializablePasswordReset spr = new SerializablePasswordReset SerializablePasswordReset spr = new SerializablePasswordReset
@ -89,9 +92,11 @@ namespace Emby.Server.Implementations.Library
try try
{ {
FileStream fileStream = File.OpenWrite(filePath); using (FileStream fileStream = File.OpenWrite(filePath))
_jsonSerializer.SerializeToStream(spr,fileStream); {
await fileStream.FlushAsync().ConfigureAwait(false); _jsonSerializer.SerializeToStream(spr, fileStream);
await fileStream.FlushAsync().ConfigureAwait(false);
}
} }
catch (Exception e) catch (Exception e)
{ {