using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Configuration;
using Microsoft.AspNetCore.Authorization;
namespace Jellyfin.Api.Auth.FirstTimeSetupPolicy
{
///
/// Authorization handler for requiring first time setup or default privileges.
///
public class FirstTimeSetupHandler : AuthorizationHandler
{
private readonly IConfigurationManager _configurationManager;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
public FirstTimeSetupHandler(IConfigurationManager configurationManager)
{
_configurationManager = configurationManager;
}
///
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, FirstTimeSetupRequirement requirement)
{
if (!_configurationManager.CommonConfiguration.IsStartupWizardCompleted)
{
context.Succeed(requirement);
}
else if (requirement.RequireAdmin && !context.User.IsInRole(UserRoles.Administrator))
{
context.Fail();
}
else
{
// Any user-specific checks are handled in the DefaultAuthorizationHandler.
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}