diff options
| author | Bond_009 <bond.009@outlook.com> | 2020-01-10 21:16:46 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2020-01-10 21:16:46 +0100 |
| commit | e714b9930ef27e5a1ea870e04e2241eb7cf1fce0 (patch) | |
| tree | 95503cad3bea14e88c2030e3764fa3910fdd2d67 /Emby.Server.Implementations/Library/UserManager.cs | |
| parent | 5cab79c839d2212ae638db403ec4d7ba0699f9a1 (diff) | |
| parent | 162c1ac7b7fde0e4929cf262b0f275e3eb15524c (diff) | |
Merge branch 'master' into embytv
Diffstat (limited to 'Emby.Server.Implementations/Library/UserManager.cs')
| -rw-r--r-- | Emby.Server.Implementations/Library/UserManager.cs | 120 |
1 files changed, 50 insertions, 70 deletions
diff --git a/Emby.Server.Implementations/Library/UserManager.cs b/Emby.Server.Implementations/Library/UserManager.cs index 2b22129f3..1b9c317d8 100644 --- a/Emby.Server.Implementations/Library/UserManager.cs +++ b/Emby.Server.Implementations/Library/UserManager.cs @@ -1,3 +1,5 @@ +#pragma warning disable CS1591 + using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -36,19 +38,19 @@ using Microsoft.Extensions.Logging; namespace Emby.Server.Implementations.Library { /// <summary> - /// Class UserManager + /// Class UserManager. /// </summary> public class UserManager : IUserManager { + private readonly object _policySyncLock = new object(); + private readonly object _configSyncLock = new object(); /// <summary> - /// The _logger + /// The logger. /// </summary> private readonly ILogger _logger; - private readonly object _policySyncLock = new object(); - /// <summary> - /// Gets the active user repository + /// Gets the active user repository. /// </summary> /// <value>The user repository.</value> private readonly IUserRepository _userRepository; @@ -194,10 +196,6 @@ namespace Emby.Server.Implementations.Library return user; } - /// <inheritdoc /> - public User GetUserById(string id) - => GetUserById(new Guid(id)); - public User GetUserByName(string name) { if (string.IsNullOrWhiteSpace(name)) @@ -257,7 +255,12 @@ namespace Emby.Server.Implementations.Library return builder.ToString(); } - public async Task<User> AuthenticateUser(string username, string password, string hashedPassword, string remoteEndPoint, bool isUserSession) + public async Task<User> AuthenticateUser( + string username, + string password, + string hashedPassword, + string remoteEndPoint, + bool isUserSession) { if (string.IsNullOrWhiteSpace(username)) { @@ -358,6 +361,8 @@ namespace Emby.Server.Implementations.Library return success ? user : null; } +#nullable enable + private static string GetAuthenticationProviderId(IAuthenticationProvider provider) { return provider.GetType().FullName; @@ -378,7 +383,7 @@ namespace Emby.Server.Implementations.Library return GetPasswordResetProviders(user)[0]; } - private IAuthenticationProvider[] GetAuthenticationProviders(User user) + private IAuthenticationProvider[] GetAuthenticationProviders(User? user) { var authenticationProviderId = user?.Policy.AuthenticationProviderId; @@ -392,14 +397,14 @@ namespace Emby.Server.Implementations.Library if (providers.Length == 0) { // Assign the user to the InvalidAuthProvider since no configured auth provider was valid/found - _logger.LogWarning("User {UserName} was found with invalid/missing Authentication Provider {AuthenticationProviderId}. Assigning user to InvalidAuthProvider until this is corrected", user.Name, user.Policy.AuthenticationProviderId); + _logger.LogWarning("User {UserName} was found with invalid/missing Authentication Provider {AuthenticationProviderId}. Assigning user to InvalidAuthProvider until this is corrected", user?.Name, user?.Policy.AuthenticationProviderId); providers = new IAuthenticationProvider[] { _invalidAuthProvider }; } return providers; } - private IPasswordResetProvider[] GetPasswordResetProviders(User user) + private IPasswordResetProvider[] GetPasswordResetProviders(User? user) { var passwordResetProviderId = user?.Policy.PasswordResetProviderId; @@ -418,7 +423,11 @@ namespace Emby.Server.Implementations.Library return providers; } - private async Task<(string username, bool success)> AuthenticateWithProvider(IAuthenticationProvider provider, string username, string password, User resolvedUser) + private async Task<(string username, bool success)> AuthenticateWithProvider( + IAuthenticationProvider provider, + string username, + string password, + User? resolvedUser) { try { @@ -442,15 +451,15 @@ namespace Emby.Server.Implementations.Library } } - private async Task<(IAuthenticationProvider authenticationProvider, string username, bool success)> AuthenticateLocalUser( + private async Task<(IAuthenticationProvider? authenticationProvider, string username, bool success)> AuthenticateLocalUser( string username, string password, string hashedPassword, - User user, + User? user, string remoteEndPoint) { bool success = false; - IAuthenticationProvider authenticationProvider = null; + IAuthenticationProvider? authenticationProvider = null; foreach (var provider in GetAuthenticationProviders(user)) { @@ -468,7 +477,7 @@ namespace Emby.Server.Implementations.Library if (!success && _networkManager.IsInLocalNetwork(remoteEndPoint) - && user.Configuration.EnableLocalPassword + && user?.Configuration.EnableLocalPassword == true && !string.IsNullOrEmpty(user.EasyPassword)) { // Check easy password @@ -547,6 +556,8 @@ namespace Emby.Server.Implementations.Library _users[user.Id] = user; } +#nullable restore + public UserDto GetUserDto(User user, string remoteEndPoint = null) { if (user == null) @@ -748,13 +759,10 @@ namespace Emby.Server.Implementations.Library return user; } - /// <summary> - /// Deletes the user. - /// </summary> - /// <param name="user">The user.</param> - /// <returns>Task.</returns> - /// <exception cref="ArgumentNullException">user</exception> - /// <exception cref="ArgumentException"></exception> + /// <inheritdoc /> + /// <exception cref="ArgumentNullException">The <c>user</c> is <c>null</c>.</exception> + /// <exception cref="ArgumentException">The <c>user</c> doesn't exist, or is the last administrator.</exception> + /// <exception cref="InvalidOperationException">The <c>user</c> can't be deleted; there are no other users.</exception> public void DeleteUser(User user) { if (user == null) @@ -773,7 +781,7 @@ namespace Emby.Server.Implementations.Library if (_users.Count == 1) { - throw new ArgumentException(string.Format( + throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, "The user '{0}' cannot be deleted because there must be at least one user in the system.", user.Name)); @@ -794,17 +802,20 @@ namespace Emby.Server.Implementations.Library _userRepository.DeleteUser(user); - try - { - _fileSystem.DeleteFile(configPath); - } - catch (IOException ex) + // Delete user config dir + lock (_configSyncLock) + lock (_policySyncLock) { - _logger.LogError(ex, "Error deleting file {path}", configPath); + try + { + Directory.Delete(user.ConfigurationDirectoryPath, true); + } + catch (IOException ex) + { + _logger.LogError(ex, "Error deleting user config dir: {Path}", user.ConfigurationDirectoryPath); + } } - DeleteUserPolicy(user); - _users.TryRemove(user.Id, out _); OnUserDeleted(user); @@ -912,10 +923,9 @@ namespace Emby.Server.Implementations.Library public UserPolicy GetUserPolicy(User user) { var path = GetPolicyFilePath(user); - if (!File.Exists(path)) { - return GetDefaultPolicy(user); + return GetDefaultPolicy(); } try @@ -925,19 +935,15 @@ namespace Emby.Server.Implementations.Library return (UserPolicy)_xmlSerializer.DeserializeFromFile(typeof(UserPolicy), path); } } - catch (IOException) - { - return GetDefaultPolicy(user); - } catch (Exception ex) { - _logger.LogError(ex, "Error reading policy file: {path}", path); + _logger.LogError(ex, "Error reading policy file: {Path}", path); - return GetDefaultPolicy(user); + return GetDefaultPolicy(); } } - private static UserPolicy GetDefaultPolicy(User user) + private static UserPolicy GetDefaultPolicy() { return new UserPolicy { @@ -977,27 +983,6 @@ namespace Emby.Server.Implementations.Library } } - private void DeleteUserPolicy(User user) - { - var path = GetPolicyFilePath(user); - - try - { - lock (_policySyncLock) - { - _fileSystem.DeleteFile(path); - } - } - catch (IOException) - { - - } - catch (Exception ex) - { - _logger.LogError(ex, "Error deleting policy file"); - } - } - private static string GetPolicyFilePath(User user) { return Path.Combine(user.ConfigurationDirectoryPath, "policy.xml"); @@ -1024,19 +1009,14 @@ namespace Emby.Server.Implementations.Library return (UserConfiguration)_xmlSerializer.DeserializeFromFile(typeof(UserConfiguration), path); } } - catch (IOException) - { - return new UserConfiguration(); - } catch (Exception ex) { - _logger.LogError(ex, "Error reading policy file: {path}", path); + _logger.LogError(ex, "Error reading policy file: {Path}", path); return new UserConfiguration(); } } - private readonly object _configSyncLock = new object(); public void UpdateConfiguration(Guid userId, UserConfiguration config) { var user = GetUserById(userId); |
