Merge pull request #3664 from Bond-009/createuserasync

Make CreateUser async
This commit is contained in:
Joshua M. Boniface 2020-07-23 18:08:29 -04:00 committed by GitHub
commit 845ee21ddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 24 deletions

View File

@ -46,14 +46,12 @@ namespace Jellyfin.Api.Controllers
[HttpGet("Configuration")] [HttpGet("Configuration")]
public StartupConfigurationDto GetStartupConfiguration() public StartupConfigurationDto GetStartupConfiguration()
{ {
var result = new StartupConfigurationDto return new StartupConfigurationDto
{ {
UICulture = _config.Configuration.UICulture, UICulture = _config.Configuration.UICulture,
MetadataCountryCode = _config.Configuration.MetadataCountryCode, MetadataCountryCode = _config.Configuration.MetadataCountryCode,
PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage
}; };
return result;
} }
/// <summary> /// <summary>
@ -92,10 +90,10 @@ namespace Jellyfin.Api.Controllers
/// </summary> /// </summary>
/// <returns>The first user.</returns> /// <returns>The first user.</returns>
[HttpGet("User")] [HttpGet("User")]
public StartupUserDto GetFirstUser() public async Task<StartupUserDto> GetFirstUser()
{ {
// TODO: Remove this method when startup wizard no longer requires an existing user. // TODO: Remove this method when startup wizard no longer requires an existing user.
_userManager.Initialize(); await _userManager.InitializeAsync().ConfigureAwait(false);
var user = _userManager.Users.First(); var user = _userManager.Users.First();
return new StartupUserDto return new StartupUserDto
{ {

View File

@ -203,8 +203,24 @@ namespace Jellyfin.Server.Implementations.Users
await dbContext.SaveChangesAsync().ConfigureAwait(false); await dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
internal async Task<User> CreateUserInternalAsync(string name, JellyfinDb dbContext)
{
// TODO: Remove after user item data is migrated.
var max = await dbContext.Users.AnyAsync().ConfigureAwait(false)
? await dbContext.Users.Select(u => u.InternalId).MaxAsync().ConfigureAwait(false)
: 0;
return new User(
name,
_defaultAuthenticationProvider.GetType().FullName,
_defaultPasswordResetProvider.GetType().FullName)
{
InternalId = max + 1
};
}
/// <inheritdoc/> /// <inheritdoc/>
public User CreateUser(string name) public async Task<User> CreateUserAsync(string name)
{ {
if (!IsValidUsername(name)) if (!IsValidUsername(name))
{ {
@ -213,18 +229,10 @@ namespace Jellyfin.Server.Implementations.Users
using var dbContext = _dbProvider.CreateContext(); using var dbContext = _dbProvider.CreateContext();
// TODO: Remove after user item data is migrated. var newUser = await CreateUserInternalAsync(name, dbContext).ConfigureAwait(false);
var max = dbContext.Users.Any() ? dbContext.Users.Select(u => u.InternalId).Max() : 0;
var newUser = new User(
name,
_defaultAuthenticationProvider.GetType().FullName,
_defaultPasswordResetProvider.GetType().FullName)
{
InternalId = max + 1
};
dbContext.Users.Add(newUser); dbContext.Users.Add(newUser);
dbContext.SaveChanges(); await dbContext.SaveChangesAsync().ConfigureAwait(false);
OnUserCreated?.Invoke(this, new GenericEventArgs<User>(newUser)); OnUserCreated?.Invoke(this, new GenericEventArgs<User>(newUser));
@ -581,12 +589,12 @@ namespace Jellyfin.Server.Implementations.Users
} }
/// <inheritdoc /> /// <inheritdoc />
public void Initialize() public async Task InitializeAsync()
{ {
// TODO: Refactor the startup wizard so that it doesn't require a user to already exist. // TODO: Refactor the startup wizard so that it doesn't require a user to already exist.
using var dbContext = _dbProvider.CreateContext(); using var dbContext = _dbProvider.CreateContext();
if (dbContext.Users.Any()) if (await dbContext.Users.AnyAsync().ConfigureAwait(false))
{ {
return; return;
} }
@ -604,13 +612,13 @@ namespace Jellyfin.Server.Implementations.Users
throw new ArgumentException("Provided username is not valid!", defaultName); throw new ArgumentException("Provided username is not valid!", defaultName);
} }
var newUser = CreateUser(defaultName); var newUser = await CreateUserInternalAsync(defaultName, dbContext).ConfigureAwait(false);
newUser.SetPermission(PermissionKind.IsAdministrator, true); newUser.SetPermission(PermissionKind.IsAdministrator, true);
newUser.SetPermission(PermissionKind.EnableContentDeletion, true); newUser.SetPermission(PermissionKind.EnableContentDeletion, true);
newUser.SetPermission(PermissionKind.EnableRemoteControlOfOtherUsers, true); newUser.SetPermission(PermissionKind.EnableRemoteControlOfOtherUsers, true);
dbContext.Users.Update(newUser); dbContext.Users.Add(newUser);
dbContext.SaveChanges(); await dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@ -525,7 +525,7 @@ namespace MediaBrowser.Api
/// <returns>System.Object.</returns> /// <returns>System.Object.</returns>
public async Task<object> Post(CreateUserByName request) public async Task<object> Post(CreateUserByName request)
{ {
var newUser = _userManager.CreateUser(request.Name); var newUser = await _userManager.CreateUserAsync(request.Name).ConfigureAwait(false);
// no need to authenticate password for new user // no need to authenticate password for new user
if (request.Password != null) if (request.Password != null)

View File

@ -54,7 +54,7 @@ namespace MediaBrowser.Controller.Library
/// <summary> /// <summary>
/// Initializes the user manager and ensures that a user exists. /// Initializes the user manager and ensures that a user exists.
/// </summary> /// </summary>
void Initialize(); Task InitializeAsync();
/// <summary> /// <summary>
/// Gets a user by Id. /// Gets a user by Id.
@ -105,7 +105,7 @@ namespace MediaBrowser.Controller.Library
/// <returns>The created user.</returns> /// <returns>The created user.</returns>
/// <exception cref="ArgumentNullException">name</exception> /// <exception cref="ArgumentNullException">name</exception>
/// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentException"></exception>
User CreateUser(string name); Task<User> CreateUserAsync(string name);
/// <summary> /// <summary>
/// Deletes the specified user. /// Deletes the specified user.