using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Extensions;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
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;
}
var contextUser = context.User;
if (requirement.RequireAdmin && !contextUser.IsInRole(UserRoles.Administrator))
{
context.Fail();
return Task.CompletedTask;
}
var userId = contextUser.GetUserId();
if (userId.Equals(default))
{
context.Fail();
return Task.CompletedTask;
}
if (!requirement.ValidateParentalSchedule)
{
context.Succeed(requirement);
return Task.CompletedTask;
}
var user = _userManager.GetUserById(userId);
if (user is null)
{
throw new ResourceNotFoundException();
}
if (user.IsParentalScheduleAllowed())
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}