Optimize number of created DbContexts and fix default values for some fields

This commit is contained in:
Patrick Barron 2020-05-28 01:08:37 -04:00
parent 82b0786cc6
commit d116497912

View File

@ -321,11 +321,11 @@ namespace Jellyfin.Server.Implementations.Users
{ {
MaxParentalRating = user.MaxParentalAgeRating, MaxParentalRating = user.MaxParentalAgeRating,
EnableUserPreferenceAccess = user.EnableUserPreferenceAccess, EnableUserPreferenceAccess = user.EnableUserPreferenceAccess,
RemoteClientBitrateLimit = user.RemoteClientBitrateLimit.GetValueOrDefault(), RemoteClientBitrateLimit = user.RemoteClientBitrateLimit ?? -1,
AuthenticationProviderId = user.AuthenticationProviderId, AuthenticationProviderId = user.AuthenticationProviderId,
PasswordResetProviderId = user.PasswordResetProviderId, PasswordResetProviderId = user.PasswordResetProviderId,
InvalidLoginAttemptCount = user.InvalidLoginAttemptCount, InvalidLoginAttemptCount = user.InvalidLoginAttemptCount,
LoginAttemptsBeforeLockout = user.LoginAttemptsBeforeLockout.GetValueOrDefault(), LoginAttemptsBeforeLockout = user.LoginAttemptsBeforeLockout ?? -1,
IsAdministrator = user.HasPermission(PermissionKind.IsAdministrator), IsAdministrator = user.HasPermission(PermissionKind.IsAdministrator),
IsHidden = user.HasPermission(PermissionKind.IsHidden), IsHidden = user.HasPermission(PermissionKind.IsHidden),
IsDisabled = user.HasPermission(PermissionKind.IsDisabled), IsDisabled = user.HasPermission(PermissionKind.IsDisabled),
@ -490,8 +490,6 @@ namespace Jellyfin.Server.Implementations.Users
{ {
var user = string.IsNullOrWhiteSpace(enteredUsername) ? null : GetUserByName(enteredUsername); var user = string.IsNullOrWhiteSpace(enteredUsername) ? null : GetUserByName(enteredUsername);
var action = ForgotPasswordAction.InNetworkRequired;
if (user != null && isInNetwork) if (user != null && isInNetwork)
{ {
var passwordResetProvider = GetPasswordResetProvider(user); var passwordResetProvider = GetPasswordResetProvider(user);
@ -500,7 +498,7 @@ namespace Jellyfin.Server.Implementations.Users
return new ForgotPasswordResult return new ForgotPasswordResult
{ {
Action = action, Action = ForgotPasswordAction.InNetworkRequired,
PinFile = string.Empty PinFile = string.Empty
}; };
} }
@ -569,7 +567,8 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc/> /// <inheritdoc/>
public void UpdateConfiguration(Guid userId, UserConfiguration config) public void UpdateConfiguration(Guid userId, UserConfiguration config)
{ {
var user = GetUserById(userId); var dbContext = _dbProvider.CreateContext();
var user = dbContext.Users.Find(userId) ?? throw new ArgumentException("No user exists with given Id!");
user.SubtitleMode = config.SubtitleMode; user.SubtitleMode = config.SubtitleMode;
user.HidePlayedInLatest = config.HidePlayedInLatest; user.HidePlayedInLatest = config.HidePlayedInLatest;
user.EnableLocalPassword = config.EnableLocalPassword; user.EnableLocalPassword = config.EnableLocalPassword;
@ -587,13 +586,15 @@ namespace Jellyfin.Server.Implementations.Users
user.SetPreference(PreferenceKind.MyMediaExcludes, config.MyMediaExcludes); user.SetPreference(PreferenceKind.MyMediaExcludes, config.MyMediaExcludes);
user.SetPreference(PreferenceKind.LatestItemExcludes, config.LatestItemsExcludes); user.SetPreference(PreferenceKind.LatestItemExcludes, config.LatestItemsExcludes);
UpdateUser(user); dbContext.Update(user);
dbContext.SaveChanges();
} }
/// <inheritdoc/> /// <inheritdoc/>
public void UpdatePolicy(Guid userId, UserPolicy policy) public void UpdatePolicy(Guid userId, UserPolicy policy)
{ {
var user = GetUserById(userId); var dbContext = _dbProvider.CreateContext();
var user = dbContext.Users.Find(userId) ?? throw new ArgumentException("No user exists with given Id!");
int? maxLoginAttempts = policy.LoginAttemptsBeforeLockout switch int? maxLoginAttempts = policy.LoginAttemptsBeforeLockout switch
{ {
-1 => null, -1 => null,
@ -642,6 +643,9 @@ namespace Jellyfin.Server.Implementations.Users
user.SetPreference(PreferenceKind.EnabledDevices, policy.EnabledDevices); user.SetPreference(PreferenceKind.EnabledDevices, policy.EnabledDevices);
user.SetPreference(PreferenceKind.EnabledFolders, policy.EnabledFolders); user.SetPreference(PreferenceKind.EnabledFolders, policy.EnabledFolders);
user.SetPreference(PreferenceKind.EnableContentDeletionFromFolders, policy.EnableContentDeletionFromFolders); user.SetPreference(PreferenceKind.EnableContentDeletionFromFolders, policy.EnableContentDeletionFromFolders);
dbContext.Update(user);
dbContext.SaveChanges();
} }
private bool IsValidUsername(string name) private bool IsValidUsername(string name)