jellyfin/Jellyfin.Data/Entities/User.cs

356 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2020-05-12 19:10:35 -07:00
using System.Globalization;
using System.Linq;
2020-05-12 19:25:45 -07:00
using System.Text.Json.Serialization;
2020-05-12 19:10:35 -07:00
using Jellyfin.Data.Enums;
namespace Jellyfin.Data.Entities
{
2020-05-15 14:24:01 -07:00
/// <summary>
/// An entity representing a user.
/// </summary>
public partial class User : IHasPermissions, ISavingChanges
2020-05-02 14:56:05 -07:00
{
2020-05-12 19:10:35 -07: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 14:56:05 -07:00
/// <summary>
2020-05-15 14:24:01 -07:00
/// Initializes a new instance of the <see cref="User"/> class.
/// Public constructor with required data.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-05-15 14:24:01 -07:00
/// <param name="username">The username for the new user.</param>
/// <param name="authenticationProviderId">The authentication provider's Id</param>
public User(string username, string authenticationProviderId)
2020-05-02 14:56:05 -07:00
{
2020-05-12 19:10:35 -07:00
if (string.IsNullOrEmpty(username))
{
throw new ArgumentNullException(nameof(username));
}
if (string.IsNullOrEmpty(authenticationProviderId))
{
throw new ArgumentNullException(nameof(authenticationProviderId));
}
Username = username;
AuthenticationProviderId = authenticationProviderId;
Groups = new HashSet<Group>();
Permissions = new HashSet<Permission>();
ProviderMappings = new HashSet<ProviderMapping>();
Preferences = new HashSet<Preference>();
AccessSchedules = new HashSet<AccessSchedule>();
// Set default values
Id = Guid.NewGuid();
2020-05-15 14:24:01 -07:00
InvalidLoginAttemptCount = 0;
MustUpdatePassword = false;
2020-05-12 19:10:35 -07:00
DisplayMissingEpisodes = false;
DisplayCollectionsView = false;
HidePlayedInLatest = true;
RememberAudioSelections = true;
RememberSubtitleSelections = true;
EnableNextEpisodeAutoPlay = true;
EnableAutoLogin = false;
2020-05-15 14:24:01 -07:00
PlayDefaultAudioTrack = true;
SubtitleMode = SubtitlePlaybackMode.Default;
2020-05-02 14:56:05 -07:00
2020-05-15 14:24:01 -07:00
AddDefaultPermissions();
AddDefaultPreferences();
2020-05-02 14:56:05 -07:00
Init();
}
/// <summary>
2020-05-15 14:24:01 -07:00
/// Initializes a new instance of the <see cref="User"/> class.
/// Default constructor. Protected due to required properties, but present because EF needs it.
2020-05-02 14:56:05 -07:00
/// </summary>
2020-05-15 14:24:01 -07:00
protected User()
2020-05-02 14:56:05 -07:00
{
2020-05-15 14:24:01 -07:00
Groups = new HashSet<Group>();
Permissions = new HashSet<Permission>();
ProviderMappings = new HashSet<ProviderMapping>();
Preferences = new HashSet<Preference>();
AccessSchedules = new HashSet<AccessSchedule>();
AddDefaultPermissions();
AddDefaultPreferences();
Init();
2020-05-02 14:56:05 -07:00
}
/// <summary>
/// Static create function (for use in LINQ queries, etc.)
/// </summary>
2020-05-15 14:24:01 -07:00
/// <param name="username">The username for the created user.</param>
/// <param name="authenticationProviderId">The Id of the user's authentication provider.</param>
/// <returns>The created instance.</returns>
public static User Create(string username, string authenticationProviderId)
2020-05-02 14:56:05 -07:00
{
2020-05-15 14:24:01 -07:00
return new User(username, authenticationProviderId);
2020-05-02 14:56:05 -07:00
}
/*************************************************************************
* Properties
*************************************************************************/
/// <summary>
/// Identity, Indexed, Required
/// </summary>
[Key]
[Required]
2020-05-12 19:25:45 -07:00
[JsonIgnore]
public Guid Id { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
/// Required, Max length = 255
/// </summary>
[Required]
[MaxLength(255)]
[StringLength(255)]
public string Username { get; set; }
/// <summary>
/// Max length = 65535
/// </summary>
[MaxLength(65535)]
[StringLength(65535)]
public string Password { get; set; }
2020-05-12 19:10:35 -07:00
/// <summary>
/// Max length = 65535.
/// </summary>
[MaxLength(65535)]
[StringLength(65535)]
public string EasyPassword { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
/// Required
/// </summary>
[Required]
public bool MustUpdatePassword { get; set; }
/// <summary>
2020-05-12 19:10:35 -07:00
/// Max length = 255.
2020-05-02 14:56:05 -07:00
/// </summary>
[MaxLength(255)]
[StringLength(255)]
public string AudioLanguagePreference { get; set; }
/// <summary>
/// Required, Max length = 255
/// </summary>
[Required]
[MaxLength(255)]
[StringLength(255)]
public string AuthenticationProviderId { get; set; }
2020-05-12 19:10:35 -07:00
[Required]
[MaxLength(255)]
[StringLength(255)]
public string PasswordResetProviderId { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
/// Required
/// </summary>
[Required]
public int InvalidLoginAttemptCount { get; set; }
2020-05-12 19:10:35 -07:00
public DateTime LastActivityDate { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public DateTime LastLoginDate { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public int? LoginAttemptsBeforeLockout { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
2020-05-12 19:10:35 -07:00
/// Required.
2020-05-02 14:56:05 -07:00
/// </summary>
[Required]
2020-05-12 19:10:35 -07:00
public SubtitlePlaybackMode SubtitleMode { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
/// Required
/// </summary>
[Required]
public bool PlayDefaultAudioTrack { get; set; }
/// <summary>
2020-05-15 14:24:01 -07:00
/// Gets or sets the subtitle language preference.
2020-05-02 14:56:05 -07:00
/// Max length = 255
/// </summary>
[MaxLength(255)]
[StringLength(255)]
2020-05-12 19:10:35 -07:00
public string SubtitleLanguagePreference { get; set; }
[Required]
public bool DisplayMissingEpisodes { get; set; }
[Required]
public bool DisplayCollectionsView { get; set; }
[Required]
public bool EnableLocalPassword { get; set; }
[Required]
public bool HidePlayedInLatest { get; set; }
[Required]
public bool RememberAudioSelections { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
[Required]
public bool RememberSubtitleSelections { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
[Required]
public bool EnableNextEpisodeAutoPlay { get; set; }
[Required]
public bool EnableAutoLogin { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
[Required]
public bool EnableUserPreferenceAccess { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public int? MaxParentalAgeRating { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public int? RemoteClientBitrateLimit { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
/// <summary>
2020-05-15 14:24:01 -07:00
/// Gets or sets the internal id.
2020-05-12 19:10:35 -07: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>
[Required]
public long InternalId { get; set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public ImageInfo ProfileImage { get; set; }
2020-05-02 14:56:05 -07:00
/// <summary>
2020-05-15 14:24:01 -07:00
/// Gets or sets the row version.
/// Required, ConcurrenyToken.
2020-05-02 14:56:05 -07:00
/// </summary>
[ConcurrencyCheck]
[Required]
public uint RowVersion { get; set; }
public void OnSavingChanges()
{
RowVersion++;
}
/*************************************************************************
* Navigation properties
*************************************************************************/
2020-05-15 14:24:01 -07:00
[ForeignKey("Group_Groups_Guid")]
2020-05-12 19:10:35 -07:00
public ICollection<Group> Groups { get; protected set; }
2020-05-02 14:56:05 -07:00
2020-05-15 14:24:01 -07:00
[ForeignKey("Permission_Permissions_Guid")]
2020-05-12 19:10:35 -07:00
public ICollection<Permission> Permissions { get; protected set; }
2020-05-02 14:56:05 -07:00
[ForeignKey("ProviderMapping_ProviderMappings_Id")]
2020-05-12 19:10:35 -07:00
public ICollection<ProviderMapping> ProviderMappings { get; protected set; }
2020-05-02 14:56:05 -07:00
2020-05-15 14:24:01 -07:00
[ForeignKey("Preference_Preferences_Guid")]
2020-05-12 19:10:35 -07:00
public ICollection<Preference> Preferences { get; protected set; }
2020-05-02 14:56:05 -07:00
2020-05-12 19:10:35 -07:00
public ICollection<AccessSchedule> AccessSchedules { get; protected set; }
2020-05-15 14:24:01 -07:00
partial void Init();
2020-05-12 19:10:35 -07:00
public bool HasPermission(PermissionKind permission)
{
2020-05-15 14:24:01 -07:00
return Permissions.First(p => p.Kind == permission).Value;
2020-05-12 19:10:35 -07:00
}
public void SetPermission(PermissionKind kind, bool value)
{
var permissionObj = Permissions.First(p => p.Kind == kind);
permissionObj.Value = value;
}
public string[] GetPreference(PreferenceKind preference)
{
2020-05-15 14:24:01 -07:00
var val = Preferences
2020-05-12 19:10:35 -07:00
.Where(p => p.Kind == preference)
.Select(p => p.Value)
2020-05-15 14:24:01 -07:00
.First();
return Equals(val, string.Empty) ? Array.Empty<string>() : val.Split(Delimiter);
2020-05-12 19:10:35 -07:00
}
public void SetPreference(PreferenceKind preference, string[] values)
{
var pref = Preferences.First(p => p.Kind == preference);
pref.Value = string.Join(Delimiter.ToString(CultureInfo.InvariantCulture), values);
}
public bool IsParentalScheduleAllowed()
{
var schedules = this.AccessSchedules;
return schedules.Count == 0 || schedules.Any(i => IsParentalScheduleAllowed(i, DateTime.Now));
}
public bool IsFolderGrouped(Guid id)
{
return GetPreference(PreferenceKind.GroupedFolders).Any(i => new Guid(i) == id);
}
private bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
{
if (date.Kind != DateTimeKind.Utc)
{
throw new ArgumentException("Utc date expected");
}
var localTime = date.ToLocalTime();
return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek) &&
IsWithinTime(schedule, localTime);
}
private bool IsWithinTime(AccessSchedule schedule, DateTime localTime)
{
var hour = localTime.TimeOfDay.TotalHours;
return hour >= schedule.StartHour && hour <= schedule.EndHour;
}
2020-05-15 14:24:01 -07:00
// TODO: make these user configurable?
private void AddDefaultPermissions()
{
Permissions.Add(new Permission(PermissionKind.IsAdministrator, false));
Permissions.Add(new Permission(PermissionKind.IsDisabled, false));
Permissions.Add(new Permission(PermissionKind.IsHidden, false));
Permissions.Add(new Permission(PermissionKind.EnableAllChannels, false));
Permissions.Add(new Permission(PermissionKind.EnableAllDevices, true));
Permissions.Add(new Permission(PermissionKind.EnableAllFolders, false));
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));
}
private void AddDefaultPreferences()
{
foreach (var val in Enum.GetValues(typeof(PreferenceKind)).Cast<PreferenceKind>())
{
Preferences.Add(new Preference(val, string.Empty));
}
}
2020-05-02 14:56:05 -07:00
}
}