2020-08-20 13:53:53 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2019-07-02 10:21:54 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-24 18:25:46 +00:00
|
|
|
using Jellyfin.Api.Constants;
|
2019-11-24 14:27:58 +00:00
|
|
|
using Jellyfin.Api.Models.StartupDtos;
|
2019-07-02 10:21:54 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2019-11-23 15:31:02 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-04-21 14:42:48 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-07-02 10:21:54 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The startup wizard controller.
|
|
|
|
/// </summary>
|
2019-11-24 18:25:46 +00:00
|
|
|
[Authorize(Policy = Policies.FirstTimeSetupOrElevated)]
|
2019-11-23 15:31:02 +00:00
|
|
|
public class StartupController : BaseJellyfinApiController
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="StartupController" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="config">The server configuration manager.</param>
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
2019-07-02 10:21:54 +00:00
|
|
|
public StartupController(IServerConfigurationManager config, IUserManager userManager)
|
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Completes the startup wizard.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Startup wizard completed.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpPost("Complete")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-04-21 20:25:03 +00:00
|
|
|
public ActionResult CompleteWizard()
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
|
|
|
_config.Configuration.IsStartupWizardCompleted = true;
|
|
|
|
_config.SaveConfiguration();
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2019-07-02 10:21:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Gets the initial startup wizard configuration.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
2020-04-29 14:59:34 +00:00
|
|
|
/// <response code="200">Initial startup wizard configuration retrieved.</response>
|
2020-05-18 01:56:02 +00:00
|
|
|
/// <returns>An <see cref="OkResult"/> containing the initial startup wizard configuration.</returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpGet("Configuration")]
|
2020-04-21 20:25:03 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
public ActionResult<StartupConfigurationDto> GetStartupConfiguration()
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2020-07-22 18:57:29 +00:00
|
|
|
return new StartupConfigurationDto
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
|
|
|
UICulture = _config.Configuration.UICulture,
|
|
|
|
MetadataCountryCode = _config.Configuration.MetadataCountryCode,
|
|
|
|
PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Sets the initial startup wizard configuration.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
2020-08-20 13:53:53 +00:00
|
|
|
/// <param name="startupConfiguration">The updated startup configuration.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Configuration saved.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpPost("Configuration")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-08-20 13:53:53 +00:00
|
|
|
public ActionResult UpdateInitialConfiguration([FromBody, Required] StartupConfigurationDto startupConfiguration)
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2020-08-20 13:53:53 +00:00
|
|
|
_config.Configuration.UICulture = startupConfiguration.UICulture;
|
|
|
|
_config.Configuration.MetadataCountryCode = startupConfiguration.MetadataCountryCode;
|
|
|
|
_config.Configuration.PreferredMetadataLanguage = startupConfiguration.PreferredMetadataLanguage;
|
2019-07-02 10:21:54 +00:00
|
|
|
_config.SaveConfiguration();
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2019-07-02 10:21:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Sets remote access and UPnP.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
2020-08-20 13:53:53 +00:00
|
|
|
/// <param name="startupRemoteAccessDto">The startup remote access dto.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Configuration saved.</response>
|
|
|
|
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpPost("RemoteAccess")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-08-20 13:53:53 +00:00
|
|
|
public ActionResult SetRemoteAccess([FromBody, Required] StartupRemoteAccessDto startupRemoteAccessDto)
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2020-08-20 13:53:53 +00:00
|
|
|
_config.Configuration.EnableRemoteAccess = startupRemoteAccessDto.EnableRemoteAccess;
|
|
|
|
_config.Configuration.EnableUPnP = startupRemoteAccessDto.EnableAutomaticPortMapping;
|
2019-07-02 10:21:54 +00:00
|
|
|
_config.SaveConfiguration();
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2019-07-02 10:21:54 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Gets the first user.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
2020-04-29 14:59:34 +00:00
|
|
|
/// <response code="200">Initial user retrieved.</response>
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <returns>The first user.</returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpGet("User")]
|
2020-08-03 20:38:51 +00:00
|
|
|
[HttpGet("FirstUser", Name = "GetFirstUser_2")]
|
2020-04-21 20:25:03 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-07-22 18:57:29 +00:00
|
|
|
public async Task<StartupUserDto> GetFirstUser()
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2020-06-09 18:01:21 +00:00
|
|
|
// TODO: Remove this method when startup wizard no longer requires an existing user.
|
2020-07-22 18:57:29 +00:00
|
|
|
await _userManager.InitializeAsync().ConfigureAwait(false);
|
2019-07-02 10:21:54 +00:00
|
|
|
var user = _userManager.Users.First();
|
2019-11-23 15:31:02 +00:00
|
|
|
return new StartupUserDto
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
Name = user.Username,
|
2019-07-02 10:21:54 +00:00
|
|
|
Password = user.Password
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:31:17 +00:00
|
|
|
/// <summary>
|
2020-04-29 15:41:12 +00:00
|
|
|
/// Sets the user name and password.
|
2019-11-23 19:31:17 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="startupUserDto">The DTO containing username and password.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Updated user name and password.</response>
|
2020-05-18 01:56:02 +00:00
|
|
|
/// <returns>
|
|
|
|
/// A <see cref="Task" /> that represents the asynchronous update operation.
|
2020-06-12 10:38:13 +00:00
|
|
|
/// The task result contains a <see cref="NoContentResult"/> indicating success.
|
2020-05-18 01:56:02 +00:00
|
|
|
/// </returns>
|
2019-07-02 10:21:54 +00:00
|
|
|
[HttpPost("User")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-08-20 13:53:53 +00:00
|
|
|
public async Task<ActionResult> UpdateStartupUser([FromBody] StartupUserDto startupUserDto)
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
|
|
|
var user = _userManager.Users.First();
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
user.Username = startupUserDto.Name;
|
2019-07-02 10:21:54 +00:00
|
|
|
|
2020-06-09 18:01:21 +00:00
|
|
|
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
|
2019-07-02 10:21:54 +00:00
|
|
|
|
2019-11-23 15:31:02 +00:00
|
|
|
if (!string.IsNullOrEmpty(startupUserDto.Password))
|
2019-07-02 10:21:54 +00:00
|
|
|
{
|
2019-11-23 15:31:02 +00:00
|
|
|
await _userManager.ChangePassword(user, startupUserDto.Password).ConfigureAwait(false);
|
2019-07-02 10:21:54 +00:00
|
|
|
}
|
2020-04-21 14:42:48 +00:00
|
|
|
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2019-07-02 10:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|