A shared account previously inherited its members' library access but none of their content restrictions, so a child could log into "alice+kid" with their own password and get around their own rating cap. The shared account now gets the strictest member's parental rating, unrated-item block, blocked tags and allowed tags, recomputed at creation, on membership change and at startup. An admin can raise the rating cap on a slider between the strictest and the loosest member; unrated and tag rules stay strictest-wins. What makes raising the cap safe is the unlock rule: after a member's password matches, both users' live policies are compared and the login is refused if the account is looser than the member on any field. So raising the cap above the child's rating means the child's password no longer opens the account, while the parent's still does. The same rule bounds the slider - past the loosest member nobody could unlock the account - so a chosen cap is clamped back into range whenever applied. Allowed tags need care: Jellyfin reads an empty list as "no whitelist", so an empty intersection of members' whitelists is written as a sentinel tag no item carries. Access schedules and channels are not inherited yet. The shared account is never an administrator. Groups created at the login screen always inherit and are restricted before the first session exists. The dashboard shows each member's cap, who a chosen cap shuts out, and the restrictions in effect, and gains a per-group edit form for the sync options. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
133 lines
4.7 KiB
C#
133 lines
4.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Plugin.WatchedTogether.Compat;
|
|
using MediaBrowser.Controller.Library;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.WatchedTogether.Services;
|
|
|
|
/// <summary>
|
|
/// Keeps group membership consistent with the set of users that actually exist.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Jellyfin raises no user-deleted event that carries the removed id, so instead of subscribing to
|
|
/// deletions this reconciles configuration against live users at startup. Stale entries are also
|
|
/// skipped at read time by <see cref="GroupService.GetEligibleMembers"/>, so this is about keeping
|
|
/// stored configuration tidy and disabling groups that have fallen below two members.
|
|
/// </remarks>
|
|
public sealed class UserLifecycleService : IHostedService
|
|
{
|
|
private readonly IUserManager _userManager;
|
|
private readonly IGroupService _groupService;
|
|
private readonly ILibraryAccessService _libraryAccessService;
|
|
private readonly IRestrictionService _restrictionService;
|
|
private readonly ILogger<UserLifecycleService> _logger;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="UserLifecycleService"/> class.
|
|
/// </summary>
|
|
/// <param name="userManager">The user manager.</param>
|
|
/// <param name="groupService">The group service.</param>
|
|
/// <param name="libraryAccessService">The library access service.</param>
|
|
/// <param name="restrictionService">The content restriction service.</param>
|
|
/// <param name="logger">The logger.</param>
|
|
public UserLifecycleService(
|
|
IUserManager userManager,
|
|
IGroupService groupService,
|
|
ILibraryAccessService libraryAccessService,
|
|
IRestrictionService restrictionService,
|
|
ILogger<UserLifecycleService> logger)
|
|
{
|
|
_userManager = userManager;
|
|
_groupService = groupService;
|
|
_libraryAccessService = libraryAccessService;
|
|
_restrictionService = restrictionService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
Reconcile();
|
|
await ReapplyDerivedPolicyAsync().ConfigureAwait(false);
|
|
}
|
|
#pragma warning disable CA1031 // Reconciliation must never prevent the server from starting.
|
|
catch (Exception ex)
|
|
#pragma warning restore CA1031
|
|
{
|
|
_logger.LogError(ex, "Failed to reconcile Watched Together groups at startup");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recomputes every group's library access and content restrictions.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A member's own access can be narrowed at any time through the normal user editor, which
|
|
/// would leave a group's stored policy too wide. Recomputing at startup brings shared accounts
|
|
/// back in line without needing to hook every policy change. Between restarts the unlock rule
|
|
/// in the authentication provider covers the security side of that drift.
|
|
/// </remarks>
|
|
/// <returns>A task representing the update.</returns>
|
|
private async Task ReapplyDerivedPolicyAsync()
|
|
{
|
|
var plugin = Plugin.Instance;
|
|
if (plugin is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var config = plugin.Configuration;
|
|
var changed = false;
|
|
|
|
foreach (var group in config.Groups.ToList())
|
|
{
|
|
await _libraryAccessService
|
|
.ApplyIntersectionAsync(group.SharedUserId, group.MemberUserIds)
|
|
.ConfigureAwait(false);
|
|
|
|
// A member's cap may have moved since the group's own cap was chosen.
|
|
var applied = await _restrictionService.ApplyAsync(group).ConfigureAwait(false);
|
|
changed |= applied.CapAdjusted;
|
|
}
|
|
|
|
if (changed)
|
|
{
|
|
plugin.UpdateConfiguration(config);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
|
|
/// <summary>
|
|
/// Removes references to users that no longer exist.
|
|
/// </summary>
|
|
private void Reconcile()
|
|
{
|
|
var config = Plugin.Instance?.Configuration;
|
|
if (config is null || config.Groups.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var liveIds = _userManager.GetAllUserIds().ToHashSet();
|
|
|
|
var referenced = config.Groups
|
|
.SelectMany(g => g.MemberUserIds.Append(g.SharedUserId))
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
foreach (var id in referenced.Where(id => !liveIds.Contains(id)))
|
|
{
|
|
_logger.LogInformation("Pruning deleted user {UserId} from Watched Together groups", id);
|
|
_groupService.PruneDeletedUser(id);
|
|
}
|
|
}
|
|
}
|