Files
WatchedTogether/Jellyfin.Plugin.WatchedTogether.Tests/UserManagerMockExtensions.cs
T
dtourolleandClaude Opus 5 27cd2a3d37 Inherit parental restrictions on shared accounts, with a chosen rating cap
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>
2026-09-19 12:30:50 +02:00

120 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data;
using Jellyfin.Data.Enums;
using Jellyfin.Database.Implementations.Entities;
using Jellyfin.Database.Implementations.Enums;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Users;
using Moq;
namespace Jellyfin.Plugin.WatchedTogether.Tests;
/// <summary>
/// Sets up the <see cref="IUserManager"/> members whose signatures differ between Jellyfin 10.11
/// and 12, so the suites read the same whichever generation they are compiled against.
/// </summary>
internal static class UserManagerMockExtensions
{
/// <summary>
/// Routes <c>ChangePassword</c> to <paramref name="onChange"/> with the user the real server
/// would act on: the instance handed in on 10.11, the stored row looked up by id on 12.
/// </summary>
/// <param name="mock">The user manager mock.</param>
/// <param name="users">The users the mock knows about, used to resolve ids on 12.</param>
/// <param name="onChange">The behaviour to run for the change.</param>
public static void SetupChangePassword(
this Mock<IUserManager> mock,
List<User> users,
Func<User, string, Task> onChange)
{
#if JELLYFIN_12
mock.Setup(m => m.ChangePassword(It.IsAny<Guid>(), It.IsAny<string>()))
.Returns((Guid id, string password) =>
{
var user = users.Find(u => u.Id == id)
?? throw new KeyNotFoundException($"No user with id {id}");
return onChange(user, password);
});
#else
mock.Setup(m => m.ChangePassword(It.IsAny<User>(), It.IsAny<string>()))
.Returns((User user, string password) => onChange(user, password));
#endif
}
/// <summary>
/// Models the policy round trip the way Jellyfin's UserManager does it: <c>GetUserDto</c>
/// projects the user's live fields into a <see cref="UserPolicy"/>, and
/// <c>UpdatePolicyAsync</c> writes a policy back onto the user entity, so code that reads the
/// user afterwards sees what was written.
/// </summary>
/// <param name="mock">The user manager mock.</param>
/// <param name="users">The users the mock knows about.</param>
public static void SetupPolicyRoundTrip(this Mock<IUserManager> mock, List<User> users)
{
mock.Setup(m => m.GetUserDto(It.IsAny<User>(), It.IsAny<string?>()))
.Returns((User user, string? _) => new UserDto
{
Id = user.Id,
Name = user.Username,
Policy = new UserPolicy
{
IsAdministrator = user.HasPermission(PermissionKind.IsAdministrator),
EnableAllFolders = user.HasPermission(PermissionKind.EnableAllFolders),
EnabledFolders = user.GetPreferenceValues<Guid>(PreferenceKind.EnabledFolders),
MaxParentalRating = user.MaxParentalRatingScore,
MaxParentalSubRating = user.MaxParentalRatingSubScore,
BlockUnratedItems = user.GetPreferenceValues<UnratedItem>(PreferenceKind.BlockUnratedItems),
BlockedTags = user.GetPreference(PreferenceKind.BlockedTags),
AllowedTags = user.GetPreference(PreferenceKind.AllowedTags),
}
});
mock.Setup(m => m.UpdatePolicyAsync(It.IsAny<Guid>(), It.IsAny<UserPolicy>()))
.Returns((Guid id, UserPolicy policy) =>
{
var user = users.Find(u => u.Id == id)
?? throw new KeyNotFoundException($"No user with id {id}");
user.SetPermission(PermissionKind.IsAdministrator, policy.IsAdministrator);
user.SetPermission(PermissionKind.EnableAllFolders, policy.EnableAllFolders);
user.SetPreference(PreferenceKind.EnabledFolders, policy.EnabledFolders ?? []);
user.MaxParentalRatingScore = policy.MaxParentalRating;
user.MaxParentalRatingSubScore = policy.MaxParentalSubRating;
user.SetPreference(PreferenceKind.BlockUnratedItems, policy.BlockUnratedItems ?? []);
user.SetPreference(PreferenceKind.BlockedTags, policy.BlockedTags ?? []);
user.SetPreference(PreferenceKind.AllowedTags, policy.AllowedTags ?? []);
return Task.CompletedTask;
});
}
/// <summary>
/// Sets a user's content restrictions directly, the way the user editor would.
/// </summary>
/// <param name="user">The user to restrict.</param>
/// <param name="maxRating">The rating cap, or <c>null</c> for none.</param>
/// <param name="maxSubRating">The sub-score cap at the rating cap.</param>
/// <param name="blockUnrated">The unrated kinds to block.</param>
/// <param name="blockedTags">The tags that hide an item.</param>
/// <param name="allowedTags">The whitelist, or none for no whitelist.</param>
/// <returns>The same user, for chaining.</returns>
public static User Restrict(
this User user,
int? maxRating = null,
int? maxSubRating = null,
IEnumerable<UnratedItem>? blockUnrated = null,
IEnumerable<string>? blockedTags = null,
IEnumerable<string>? allowedTags = null)
{
user.MaxParentalRatingScore = maxRating;
user.MaxParentalRatingSubScore = maxSubRating;
user.SetPreference(PreferenceKind.BlockUnratedItems, (blockUnrated ?? []).ToArray());
user.SetPreference(PreferenceKind.BlockedTags, (blockedTags ?? []).ToArray());
user.SetPreference(PreferenceKind.AllowedTags, (allowedTags ?? []).ToArray());
return user;
}
}