using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Library;
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;
private readonly IUserManager _userManager;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
/// Instance of the interface.
public FirstTimeSetupHandler(
IConfigurationManager configurationManager,
IUserManager userManager)
{
_configurationManager = configurationManager;
_userManager = userManager;
}
///
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, FirstTimeSetupRequirement requirement)
{
if (!_configurationManager.CommonConfiguration.IsStartupWizardCompleted)
{
context.Succeed(requirement);
return Task.CompletedTask;
}
if (requirement.RequireAdmin && !context.User.IsInRole(UserRoles.Administrator))
{
context.Fail();
return Task.CompletedTask;
}
if (!requirement.ValidateParentalSchedule)
{
context.Succeed(requirement);
return Task.CompletedTask;
}
var user = _userManager.GetUserById(context.User.GetUserId());
if (user.IsParentalScheduleAllowed())
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}