jellyfin-server/Jellyfin.Data/Entities/User.cs

546 lines
20 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-12-11 22:00:43 +00:00
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
2020-05-13 02:25:45 +00:00
using System.Text.Json.Serialization;
2020-05-13 02:10:35 +00:00
using Jellyfin.Data.Enums;
using Jellyfin.Data.Interfaces;
namespace Jellyfin.Data.Entities
{
2020-05-15 21:24:01 +00:00
/// <summary>
/// An entity representing a user.
/// </summary>
2020-09-01 15:09:08 +00:00
public class User : IHasPermissions, IHasConcurrencyToken
2020-05-02 21:56:05 +00:00
{
2020-05-13 02:10:35 +00:00
/// <summary>
/// The values being delimited here are Guids, so commas work as they do not appear in Guids.
/// </summary>
private const char Delimiter = ',';
2020-05-02 21:56:05 +00:00
/// <summary>
2020-05-15 21:24:01 +00:00
/// Initializes a new instance of the <see cref="User"/> class.
/// Public constructor with required data.
2020-05-02 21:56:05 +00:00
/// </summary>
2020-05-15 21:24:01 +00:00
/// <param name="username">The username for the new user.</param>
/// <param name="authenticationProviderId">The Id of the user's authentication provider.</param>
/// <param name="passwordResetProviderId">The Id of the user's password reset provider.</param>
public User(string username, string authenticationProviderId, string passwordResetProviderId)
2020-05-02 21:56:05 +00:00
{
2020-05-13 02:10:35 +00:00
if (string.IsNullOrEmpty(username))
{
throw new ArgumentNullException(nameof(username));
}
if (string.IsNullOrEmpty(authenticationProviderId))
{
throw new ArgumentNullException(nameof(authenticationProviderId));
}
2020-05-27 15:30:53 +00:00
if (string.IsNullOrEmpty(passwordResetProviderId))
{
throw new ArgumentNullException(nameof(passwordResetProviderId));
}
2020-05-13 02:10:35 +00:00
Username = username;
AuthenticationProviderId = authenticationProviderId;
PasswordResetProviderId = passwordResetProviderId;
2020-05-13 02:10:35 +00:00
AccessSchedules = new HashSet<AccessSchedule>();
DisplayPreferences = new HashSet<DisplayPreferences>();
ItemDisplayPreferences = new HashSet<ItemDisplayPreferences>();
// Groups = new HashSet<Group>();
2020-05-13 02:10:35 +00:00
Permissions = new HashSet<Permission>();
Preferences = new HashSet<Preference>();
// ProviderMappings = new HashSet<ProviderMapping>();
2020-05-13 02:10:35 +00:00
// Set default values
Id = Guid.NewGuid();
2020-05-15 21:24:01 +00:00
InvalidLoginAttemptCount = 0;
2020-06-11 00:53:33 +00:00
EnableUserPreferenceAccess = true;
2020-05-15 21:24:01 +00:00
MustUpdatePassword = false;
2020-05-13 02:10:35 +00:00
DisplayMissingEpisodes = false;
DisplayCollectionsView = false;
HidePlayedInLatest = true;
RememberAudioSelections = true;
RememberSubtitleSelections = true;
EnableNextEpisodeAutoPlay = true;
EnableAutoLogin = false;
2020-05-15 21:24:01 +00:00
PlayDefaultAudioTrack = true;
SubtitleMode = SubtitlePlaybackMode.Default;
SyncPlayAccess = SyncPlayUserAccessType.CreateAndJoinGroups;
2020-05-02 21:56:05 +00:00
}
/// <summary>
/// Gets or sets the Id of the user.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Identity, Indexed, Required.
/// </remarks>
2020-05-13 02:25:45 +00:00
[JsonIgnore]
public Guid Id { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the user's name.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required, Max length = 255.
/// </remarks>
2020-05-02 21:56:05 +00:00
[MaxLength(255)]
[StringLength(255)]
public string Username { get; set; }
/// <summary>
/// Gets or sets the user's password, or <c>null</c> if none is set.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Max length = 65535.
/// </remarks>
2020-05-02 21:56:05 +00:00
[MaxLength(65535)]
[StringLength(65535)]
public string? Password { get; set; }
2020-05-02 21:56:05 +00:00
2020-05-13 02:10:35 +00:00
/// <summary>
/// Gets or sets the user's easy password, or <c>null</c> if none is set.
2020-05-13 02:10:35 +00:00
/// </summary>
/// <remarks>
/// Max length = 65535.
/// </remarks>
2020-05-13 02:10:35 +00:00
[MaxLength(65535)]
[StringLength(65535)]
public string? EasyPassword { get; set; }
2020-05-13 02:10:35 +00:00
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets a value indicating whether the user must update their password.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-02 21:56:05 +00:00
public bool MustUpdatePassword { get; set; }
/// <summary>
/// Gets or sets the audio language preference.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Max length = 255.
/// </remarks>
2020-05-02 21:56:05 +00:00
[MaxLength(255)]
[StringLength(255)]
public string? AudioLanguagePreference { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the authentication provider id.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required, Max length = 255.
/// </remarks>
2020-05-02 21:56:05 +00:00
[MaxLength(255)]
[StringLength(255)]
public string AuthenticationProviderId { get; set; }
/// <summary>
/// Gets or sets the password reset provider id.
/// </summary>
/// <remarks>
/// Required, Max length = 255.
/// </remarks>
2020-05-13 02:10:35 +00:00
[MaxLength(255)]
[StringLength(255)]
public string PasswordResetProviderId { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the invalid login attempt count.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-02 21:56:05 +00:00
public int InvalidLoginAttemptCount { get; set; }
/// <summary>
/// Gets or sets the last activity date.
/// </summary>
public DateTime? LastActivityDate { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the last login date.
/// </summary>
public DateTime? LastLoginDate { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the number of login attempts the user can make before they are locked out.
/// </summary>
2020-05-13 02:10:35 +00:00
public int? LoginAttemptsBeforeLockout { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the maximum number of active sessions the user can have at once.
/// </summary>
2020-10-04 17:25:07 +00:00
public int MaxActiveSessions { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the subtitle mode.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public SubtitlePlaybackMode SubtitleMode { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets a value indicating whether the default audio track should be played.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-02 21:56:05 +00:00
public bool PlayDefaultAudioTrack { get; set; }
/// <summary>
2020-05-15 21:24:01 +00:00
/// Gets or sets the subtitle language preference.
2020-05-02 21:56:05 +00:00
/// </summary>
/// <remarks>
/// Max length = 255.
/// </remarks>
2020-05-02 21:56:05 +00:00
[MaxLength(255)]
[StringLength(255)]
public string? SubtitleLanguagePreference { get; set; }
2020-05-13 02:10:35 +00:00
/// <summary>
/// Gets or sets a value indicating whether missing episodes should be displayed.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool DisplayMissingEpisodes { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to display the collections view.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool DisplayCollectionsView { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the user has a local password.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool EnableLocalPassword { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the server should hide played content in "Latest".
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool HidePlayedInLatest { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to remember audio selections on played content.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool RememberAudioSelections { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets a value indicating whether to remember subtitle selections on played content.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool RememberSubtitleSelections { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets a value indicating whether to enable auto-play for the next episode.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool EnableNextEpisodeAutoPlay { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the user should auto-login.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool EnableAutoLogin { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets a value indicating whether the user can change their preferences.
/// </summary>
/// <remarks>
/// Required.
/// </remarks>
2020-05-13 02:10:35 +00:00
public bool EnableUserPreferenceAccess { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the maximum parental age rating.
/// </summary>
2020-05-13 02:10:35 +00:00
public int? MaxParentalAgeRating { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the remote client bitrate limit.
/// </summary>
2020-05-13 02:10:35 +00:00
public int? RemoteClientBitrateLimit { get; set; }
2020-05-02 21:56:05 +00:00
2020-05-13 02:10:35 +00:00
/// <summary>
2020-05-15 21:24:01 +00:00
/// Gets or sets the internal id.
2020-05-13 02:10:35 +00:00
/// This is a temporary stopgap for until the library db is migrated.
/// This corresponds to the value of the index of this user in the library db.
/// </summary>
public long InternalId { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
/// Gets or sets the user's profile image. Can be <c>null</c>.
/// </summary>
2020-06-13 20:38:17 +00:00
// [ForeignKey("UserId")]
public virtual ImageInfo? ProfileImage { get; set; }
2020-05-02 21:56:05 +00:00
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the user's display preferences.
/// </summary>
2021-03-17 23:08:11 +00:00
public virtual ICollection<DisplayPreferences> DisplayPreferences { get; private set; }
2020-09-01 15:09:08 +00:00
/// <summary>
/// Gets or sets the level of sync play permissions this user has.
/// </summary>
public SyncPlayUserAccessType SyncPlayAccess { get; set; }
2020-05-27 00:52:05 +00:00
2021-03-17 23:08:11 +00:00
/// <inheritdoc />
2020-05-02 21:56:05 +00:00
[ConcurrencyCheck]
2021-03-17 23:08:11 +00:00
public uint RowVersion { get; private set; }
2020-05-02 21:56:05 +00:00
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of access schedules this user has.
/// </summary>
2021-03-17 23:08:11 +00:00
public virtual ICollection<AccessSchedule> AccessSchedules { get; private set; }
2020-07-01 01:44:41 +00:00
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of item display preferences.
2020-07-01 01:44:41 +00:00
/// </summary>
2021-03-17 23:08:11 +00:00
public virtual ICollection<ItemDisplayPreferences> ItemDisplayPreferences { get; private set; }
2020-07-01 01:44:41 +00:00
/*
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of groups this user is a member of.
/// </summary>
2021-03-17 23:08:11 +00:00
public virtual ICollection<Group> Groups { get; private set; }
*/
2020-05-02 21:56:05 +00:00
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of permissions this user has.
/// </summary>
2020-05-15 21:24:01 +00:00
[ForeignKey("Permission_Permissions_Guid")]
2021-03-17 23:08:11 +00:00
public virtual ICollection<Permission> Permissions { get; private set; }
2020-05-02 21:56:05 +00:00
/*
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of provider mappings this user has.
/// </summary>
2021-03-17 23:08:11 +00:00
public virtual ICollection<ProviderMapping> ProviderMappings { get; private set; }
*/
2020-05-02 21:56:05 +00:00
/// <summary>
2021-03-17 23:08:11 +00:00
/// Gets the list of preferences this user has.
/// </summary>
2020-05-15 21:24:01 +00:00
[ForeignKey("Preference_Preferences_Guid")]
2021-03-17 23:08:11 +00:00
public virtual ICollection<Preference> Preferences { get; private set; }
2020-05-02 21:56:05 +00:00
/// <inheritdoc/>
public void OnSavingChanges()
{
RowVersion++;
}
/// <summary>
/// Checks whether the user has the specified permission.
/// </summary>
2020-05-31 00:49:31 +00:00
/// <param name="kind">The permission kind.</param>
/// <returns><c>True</c> if the user has the specified permission.</returns>
2020-05-31 00:49:31 +00:00
public bool HasPermission(PermissionKind kind)
2020-05-13 02:10:35 +00:00
{
return Permissions.FirstOrDefault(p => p.Kind == kind)?.Value ?? false;
2020-05-13 02:10:35 +00:00
}
/// <summary>
/// Sets the given permission kind to the provided value.
/// </summary>
/// <param name="kind">The permission kind.</param>
/// <param name="value">The value to set.</param>
2020-05-13 02:10:35 +00:00
public void SetPermission(PermissionKind kind, bool value)
{
var currentPermission = Permissions.FirstOrDefault(p => p.Kind == kind);
if (currentPermission == null)
{
Permissions.Add(new Permission(kind, value));
}
else
{
currentPermission.Value = value;
}
2020-05-13 02:10:35 +00:00
}
/// <summary>
/// Gets the user's preferences for the given preference kind.
/// </summary>
/// <param name="preference">The preference kind.</param>
/// <returns>A string array containing the user's preferences.</returns>
2020-05-13 02:10:35 +00:00
public string[] GetPreference(PreferenceKind preference)
{
var val = Preferences.FirstOrDefault(p => p.Kind == preference)?.Value;
2020-05-15 21:24:01 +00:00
return string.IsNullOrEmpty(val) ? Array.Empty<string>() : val.Split(Delimiter);
2020-05-13 02:10:35 +00:00
}
2020-12-11 22:00:43 +00:00
/// <summary>
/// Gets the user's preferences for the given preference kind.
/// </summary>
/// <param name="preference">The preference kind.</param>
/// <typeparam name="T">Type of preference.</typeparam>
/// <returns>A {T} array containing the user's preference.</returns>
2020-12-13 15:15:26 +00:00
public T[] GetPreferenceValues<T>(PreferenceKind preference)
2020-12-11 22:00:43 +00:00
{
var val = Preferences.FirstOrDefault(p => p.Kind == preference)?.Value;
2020-12-11 22:00:43 +00:00
if (string.IsNullOrEmpty(val))
{
return Array.Empty<T>();
}
// Convert array of {string} to array of {T}
2020-12-11 22:00:43 +00:00
var converter = TypeDescriptor.GetConverter(typeof(T));
var stringValues = val.Split(Delimiter);
var convertedCount = 0;
2020-12-14 14:53:56 +00:00
var parsedValues = new T[stringValues.Length];
for (var i = 0; i < stringValues.Length; i++)
{
try
{
2020-12-14 14:53:56 +00:00
var parsedValue = converter.ConvertFromString(stringValues[i].Trim());
if (parsedValue != null)
{
2020-12-14 16:03:36 +00:00
parsedValues[convertedCount++] = (T)parsedValue;
2020-12-14 14:53:56 +00:00
}
}
catch (FormatException)
{
// Unable to convert value
}
}
2020-12-14 14:53:56 +00:00
return parsedValues[..convertedCount];
2020-12-11 22:00:43 +00:00
}
/// <summary>
/// Sets the specified preference to the given value.
/// </summary>
/// <param name="preference">The preference kind.</param>
/// <param name="values">The values.</param>
2020-05-13 02:10:35 +00:00
public void SetPreference(PreferenceKind preference, string[] values)
{
var value = string.Join(Delimiter, values);
var currentPreference = Preferences.FirstOrDefault(p => p.Kind == preference);
if (currentPreference == null)
{
Preferences.Add(new Preference(preference, value));
}
else
{
currentPreference.Value = value;
}
2020-12-11 22:00:43 +00:00
}
/// <summary>
/// Sets the specified preference to the given value.
/// </summary>
/// <param name="preference">The preference kind.</param>
/// <param name="values">The values.</param>
/// <typeparam name="T">The type of value.</typeparam>
public void SetPreference<T>(PreferenceKind preference, T[] values)
{
var value = string.Join(Delimiter, values);
var currentPreference = Preferences.FirstOrDefault(p => p.Kind == preference);
if (currentPreference == null)
{
Preferences.Add(new Preference(preference, value));
}
else
{
currentPreference.Value = value;
}
2020-05-13 02:10:35 +00:00
}
/// <summary>
/// Checks whether this user is currently allowed to use the server.
/// </summary>
/// <returns><c>True</c> if the current time is within an access schedule, or there are no access schedules.</returns>
2020-05-13 02:10:35 +00:00
public bool IsParentalScheduleAllowed()
{
2020-05-20 18:17:59 +00:00
return AccessSchedules.Count == 0
|| AccessSchedules.Any(i => IsParentalScheduleAllowed(i, DateTime.UtcNow));
2020-05-13 02:10:35 +00:00
}
/// <summary>
/// Checks whether the provided folder is in this user's grouped folders.
/// </summary>
/// <param name="id">The Guid of the folder.</param>
/// <returns><c>True</c> if the folder is in the user's grouped folders.</returns>
2020-05-13 02:10:35 +00:00
public bool IsFolderGrouped(Guid id)
{
2020-12-13 15:15:26 +00:00
return Array.IndexOf(GetPreferenceValues<Guid>(PreferenceKind.GroupedFolders), id) != -1;
2020-05-13 02:10:35 +00:00
}
2021-03-17 21:42:45 +00:00
/// <summary>
/// Initializes the default permissions for a user. Should only be called on user creation.
/// </summary>
2020-05-15 21:24:01 +00:00
// TODO: make these user configurable?
2021-03-17 21:42:45 +00:00
public void AddDefaultPermissions()
2020-05-15 21:24:01 +00:00
{
Permissions.Add(new Permission(PermissionKind.IsAdministrator, false));
Permissions.Add(new Permission(PermissionKind.IsDisabled, false));
Permissions.Add(new Permission(PermissionKind.IsHidden, true));
Permissions.Add(new Permission(PermissionKind.EnableAllChannels, true));
2020-05-15 21:24:01 +00:00
Permissions.Add(new Permission(PermissionKind.EnableAllDevices, true));
Permissions.Add(new Permission(PermissionKind.EnableAllFolders, true));
2020-05-15 21:24:01 +00:00
Permissions.Add(new Permission(PermissionKind.EnableContentDeletion, false));
Permissions.Add(new Permission(PermissionKind.EnableContentDownloading, true));
Permissions.Add(new Permission(PermissionKind.EnableMediaConversion, true));
Permissions.Add(new Permission(PermissionKind.EnableMediaPlayback, true));
Permissions.Add(new Permission(PermissionKind.EnablePlaybackRemuxing, true));
Permissions.Add(new Permission(PermissionKind.EnablePublicSharing, true));
Permissions.Add(new Permission(PermissionKind.EnableRemoteAccess, true));
Permissions.Add(new Permission(PermissionKind.EnableSyncTranscoding, true));
Permissions.Add(new Permission(PermissionKind.EnableAudioPlaybackTranscoding, true));
Permissions.Add(new Permission(PermissionKind.EnableLiveTvAccess, true));
Permissions.Add(new Permission(PermissionKind.EnableLiveTvManagement, true));
Permissions.Add(new Permission(PermissionKind.EnableSharedDeviceControl, true));
Permissions.Add(new Permission(PermissionKind.EnableVideoPlaybackTranscoding, true));
Permissions.Add(new Permission(PermissionKind.ForceRemoteSourceTranscoding, false));
Permissions.Add(new Permission(PermissionKind.EnableRemoteControlOfOtherUsers, false));
}
2021-03-17 21:42:45 +00:00
/// <summary>
/// Initializes the default preferences. Should only be called on user creation.
/// </summary>
public void AddDefaultPreferences()
2020-05-15 21:24:01 +00:00
{
foreach (var val in Enum.GetValues(typeof(PreferenceKind)).Cast<PreferenceKind>())
{
Preferences.Add(new Preference(val, string.Empty));
}
}
2021-03-17 21:42:45 +00:00
private static bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
{
var localTime = date.ToLocalTime();
var hour = localTime.TimeOfDay.TotalHours;
return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek)
&& hour >= schedule.StartHour
&& hour <= schedule.EndHour;
}
2020-05-02 21:56:05 +00:00
}
}