jellyfin-server/Jellyfin.Api/Controllers/StartupController.cs

138 lines
5.2 KiB
C#
Raw Normal View History

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;
using Jellyfin.Api.Models.StartupDtos;
2019-07-02 10:21:54 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
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)]
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>
/// Api endpoint for completing the startup wizard.
/// </summary>
/// <returns>Status.</returns>
2019-07-02 10:21:54 +00:00
[HttpPost("Complete")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-21 20:25:03 +00:00
public ActionResult CompleteWizard()
2019-07-02 10:21:54 +00:00
{
_config.Configuration.IsStartupWizardCompleted = true;
_config.SetOptimalValues();
_config.SaveConfiguration();
return Ok();
2019-07-02 10:21:54 +00:00
}
2019-11-23 19:31:17 +00:00
/// <summary>
/// Endpoint for getting the initial startup wizard configuration.
/// </summary>
/// <returns>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
{
var result = new StartupConfigurationDto
2019-07-02 10:21:54 +00:00
{
UICulture = _config.Configuration.UICulture,
MetadataCountryCode = _config.Configuration.MetadataCountryCode,
PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage
};
2020-04-23 13:51:04 +00:00
return result;
2019-07-02 10:21:54 +00:00
}
2019-11-23 19:31:17 +00:00
/// <summary>
/// Endpoint for updating the initial startup wizard configuration.
/// </summary>
/// <param name="uiCulture">The UI language culture.</param>
/// <param name="metadataCountryCode">The metadata country code.</param>
/// <param name="preferredMetadataLanguage">The preferred language for metadata.</param>
/// <returns>Status.</returns>
2019-07-02 10:21:54 +00:00
[HttpPost("Configuration")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-21 20:25:03 +00:00
public ActionResult UpdateInitialConfiguration(
2019-11-23 18:59:45 +00:00
[FromForm] string uiCulture,
[FromForm] string metadataCountryCode,
[FromForm] string preferredMetadataLanguage)
2019-07-02 10:21:54 +00:00
{
_config.Configuration.UICulture = uiCulture;
_config.Configuration.MetadataCountryCode = metadataCountryCode;
_config.Configuration.PreferredMetadataLanguage = preferredMetadataLanguage;
_config.SaveConfiguration();
return Ok();
2019-07-02 10:21:54 +00:00
}
2019-11-23 19:31:17 +00:00
/// <summary>
/// Endpoint for (dis)allowing remote access and UPnP.
/// </summary>
/// <param name="enableRemoteAccess">Enable remote access.</param>
/// <param name="enableAutomaticPortMapping">Enable UPnP.</param>
/// <returns>Status.</returns>
2019-07-02 10:21:54 +00:00
[HttpPost("RemoteAccess")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-21 20:25:03 +00:00
public ActionResult SetRemoteAccess([FromForm] bool enableRemoteAccess, [FromForm] bool enableAutomaticPortMapping)
2019-07-02 10:21:54 +00:00
{
_config.Configuration.EnableRemoteAccess = enableRemoteAccess;
_config.Configuration.EnableUPnP = enableAutomaticPortMapping;
_config.SaveConfiguration();
return Ok();
2019-07-02 10:21:54 +00:00
}
2019-11-23 19:31:17 +00:00
/// <summary>
/// Endpoint for returning the first user.
/// </summary>
/// <returns>The first user.</returns>
2019-07-02 10:21:54 +00:00
[HttpGet("User")]
2020-04-21 20:25:03 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<StartupUserDto> GetFirstUser()
2019-07-02 10:21:54 +00:00
{
var user = _userManager.Users.First();
2020-04-23 13:51:04 +00:00
return new StartupUserDto { Name = user.Name, Password = user.Password };
2019-07-02 10:21:54 +00:00
}
2019-11-23 19:31:17 +00:00
/// <summary>
/// Endpoint for updating the user name and password.
/// </summary>
/// <param name="startupUserDto">The DTO containing username and password.</param>
/// <returns>The async task.</returns>
2019-07-02 10:21:54 +00:00
[HttpPost("User")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-04-21 20:25:03 +00:00
public async Task<ActionResult> UpdateUser([FromForm] StartupUserDto startupUserDto)
2019-07-02 10:21:54 +00:00
{
var user = _userManager.Users.First();
user.Name = startupUserDto.Name;
2019-07-02 10:21:54 +00:00
_userManager.UpdateUser(user);
if (!string.IsNullOrEmpty(startupUserDto.Password))
2019-07-02 10:21:54 +00:00
{
await _userManager.ChangePassword(user, startupUserDto.Password).ConfigureAwait(false);
2019-07-02 10:21:54 +00:00
}
return Ok();
2019-07-02 10:21:54 +00:00
}
}
}