User.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.ComponentModel.DataAnnotations.Schema;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Text.Json.Serialization;
  9. using Jellyfin.Data.Enums;
  10. using Jellyfin.Data.Interfaces;
  11. namespace Jellyfin.Data.Entities
  12. {
  13. /// <summary>
  14. /// An entity representing a user.
  15. /// </summary>
  16. public partial class User : IHasPermissions, IHasConcurrencyToken
  17. {
  18. /// <summary>
  19. /// The values being delimited here are Guids, so commas work as they do not appear in Guids.
  20. /// </summary>
  21. private const char Delimiter = ',';
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="User"/> class.
  24. /// Public constructor with required data.
  25. /// </summary>
  26. /// <param name="username">The username for the new user.</param>
  27. /// <param name="authenticationProviderId">The Id of the user's authentication provider.</param>
  28. /// <param name="passwordResetProviderId">The Id of the user's password reset provider.</param>
  29. public User(string username, string authenticationProviderId, string passwordResetProviderId)
  30. {
  31. if (string.IsNullOrEmpty(username))
  32. {
  33. throw new ArgumentNullException(nameof(username));
  34. }
  35. if (string.IsNullOrEmpty(authenticationProviderId))
  36. {
  37. throw new ArgumentNullException(nameof(authenticationProviderId));
  38. }
  39. if (string.IsNullOrEmpty(passwordResetProviderId))
  40. {
  41. throw new ArgumentNullException(nameof(passwordResetProviderId));
  42. }
  43. Username = username;
  44. AuthenticationProviderId = authenticationProviderId;
  45. PasswordResetProviderId = passwordResetProviderId;
  46. AccessSchedules = new HashSet<AccessSchedule>();
  47. ItemDisplayPreferences = new HashSet<ItemDisplayPreferences>();
  48. // Groups = new HashSet<Group>();
  49. Permissions = new HashSet<Permission>();
  50. Preferences = new HashSet<Preference>();
  51. // ProviderMappings = new HashSet<ProviderMapping>();
  52. // Set default values
  53. Id = Guid.NewGuid();
  54. InvalidLoginAttemptCount = 0;
  55. EnableUserPreferenceAccess = true;
  56. MustUpdatePassword = false;
  57. DisplayMissingEpisodes = false;
  58. DisplayCollectionsView = false;
  59. HidePlayedInLatest = true;
  60. RememberAudioSelections = true;
  61. RememberSubtitleSelections = true;
  62. EnableNextEpisodeAutoPlay = true;
  63. EnableAutoLogin = false;
  64. PlayDefaultAudioTrack = true;
  65. SubtitleMode = SubtitlePlaybackMode.Default;
  66. SyncPlayAccess = SyncPlayAccess.CreateAndJoinGroups;
  67. AddDefaultPermissions();
  68. AddDefaultPreferences();
  69. Init();
  70. }
  71. /// <summary>
  72. /// Initializes a new instance of the <see cref="User"/> class.
  73. /// Default constructor. Protected due to required properties, but present because EF needs it.
  74. /// </summary>
  75. protected User()
  76. {
  77. Init();
  78. }
  79. /*************************************************************************
  80. * Properties
  81. *************************************************************************/
  82. /// <summary>
  83. /// Gets or sets the Id of the user.
  84. /// </summary>
  85. /// <remarks>
  86. /// Identity, Indexed, Required.
  87. /// </remarks>
  88. [Key]
  89. [Required]
  90. [JsonIgnore]
  91. public Guid Id { get; set; }
  92. /// <summary>
  93. /// Gets or sets the user's name.
  94. /// </summary>
  95. /// <remarks>
  96. /// Required, Max length = 255.
  97. /// </remarks>
  98. [Required]
  99. [MaxLength(255)]
  100. [StringLength(255)]
  101. public string Username { get; set; }
  102. /// <summary>
  103. /// Gets or sets the user's password, or <c>null</c> if none is set.
  104. /// </summary>
  105. /// <remarks>
  106. /// Max length = 65535.
  107. /// </remarks>
  108. [MaxLength(65535)]
  109. [StringLength(65535)]
  110. public string Password { get; set; }
  111. /// <summary>
  112. /// Gets or sets the user's easy password, or <c>null</c> if none is set.
  113. /// </summary>
  114. /// <remarks>
  115. /// Max length = 65535.
  116. /// </remarks>
  117. [MaxLength(65535)]
  118. [StringLength(65535)]
  119. public string EasyPassword { get; set; }
  120. /// <summary>
  121. /// Gets or sets a value indicating whether the user must update their password.
  122. /// </summary>
  123. /// <remarks>
  124. /// Required.
  125. /// </remarks>
  126. [Required]
  127. public bool MustUpdatePassword { get; set; }
  128. /// <summary>
  129. /// Gets or sets the audio language preference.
  130. /// </summary>
  131. /// <remarks>
  132. /// Max length = 255.
  133. /// </remarks>
  134. [MaxLength(255)]
  135. [StringLength(255)]
  136. public string AudioLanguagePreference { get; set; }
  137. /// <summary>
  138. /// Gets or sets the authentication provider id.
  139. /// </summary>
  140. /// <remarks>
  141. /// Required, Max length = 255.
  142. /// </remarks>
  143. [Required]
  144. [MaxLength(255)]
  145. [StringLength(255)]
  146. public string AuthenticationProviderId { get; set; }
  147. /// <summary>
  148. /// Gets or sets the password reset provider id.
  149. /// </summary>
  150. /// <remarks>
  151. /// Required, Max length = 255.
  152. /// </remarks>
  153. [Required]
  154. [MaxLength(255)]
  155. [StringLength(255)]
  156. public string PasswordResetProviderId { get; set; }
  157. /// <summary>
  158. /// Gets or sets the invalid login attempt count.
  159. /// </summary>
  160. /// <remarks>
  161. /// Required.
  162. /// </remarks>
  163. [Required]
  164. public int InvalidLoginAttemptCount { get; set; }
  165. /// <summary>
  166. /// Gets or sets the last activity date.
  167. /// </summary>
  168. public DateTime? LastActivityDate { get; set; }
  169. /// <summary>
  170. /// Gets or sets the last login date.
  171. /// </summary>
  172. public DateTime? LastLoginDate { get; set; }
  173. /// <summary>
  174. /// Gets or sets the number of login attempts the user can make before they are locked out.
  175. /// </summary>
  176. public int? LoginAttemptsBeforeLockout { get; set; }
  177. /// <summary>
  178. /// Gets or sets the subtitle mode.
  179. /// </summary>
  180. /// <remarks>
  181. /// Required.
  182. /// </remarks>
  183. [Required]
  184. public SubtitlePlaybackMode SubtitleMode { get; set; }
  185. /// <summary>
  186. /// Gets or sets a value indicating whether the default audio track should be played.
  187. /// </summary>
  188. /// <remarks>
  189. /// Required.
  190. /// </remarks>
  191. [Required]
  192. public bool PlayDefaultAudioTrack { get; set; }
  193. /// <summary>
  194. /// Gets or sets the subtitle language preference.
  195. /// </summary>
  196. /// <remarks>
  197. /// Max length = 255.
  198. /// </remarks>
  199. [MaxLength(255)]
  200. [StringLength(255)]
  201. public string SubtitleLanguagePreference { get; set; }
  202. /// <summary>
  203. /// Gets or sets a value indicating whether missing episodes should be displayed.
  204. /// </summary>
  205. /// <remarks>
  206. /// Required.
  207. /// </remarks>
  208. [Required]
  209. public bool DisplayMissingEpisodes { get; set; }
  210. /// <summary>
  211. /// Gets or sets a value indicating whether to display the collections view.
  212. /// </summary>
  213. /// <remarks>
  214. /// Required.
  215. /// </remarks>
  216. [Required]
  217. public bool DisplayCollectionsView { get; set; }
  218. /// <summary>
  219. /// Gets or sets a value indicating whether the user has a local password.
  220. /// </summary>
  221. /// <remarks>
  222. /// Required.
  223. /// </remarks>
  224. [Required]
  225. public bool EnableLocalPassword { get; set; }
  226. /// <summary>
  227. /// Gets or sets a value indicating whether the server should hide played content in "Latest".
  228. /// </summary>
  229. /// <remarks>
  230. /// Required.
  231. /// </remarks>
  232. [Required]
  233. public bool HidePlayedInLatest { get; set; }
  234. /// <summary>
  235. /// Gets or sets a value indicating whether to remember audio selections on played content.
  236. /// </summary>
  237. /// <remarks>
  238. /// Required.
  239. /// </remarks>
  240. [Required]
  241. public bool RememberAudioSelections { get; set; }
  242. /// <summary>
  243. /// Gets or sets a value indicating whether to remember subtitle selections on played content.
  244. /// </summary>
  245. /// <remarks>
  246. /// Required.
  247. /// </remarks>
  248. [Required]
  249. public bool RememberSubtitleSelections { get; set; }
  250. /// <summary>
  251. /// Gets or sets a value indicating whether to enable auto-play for the next episode.
  252. /// </summary>
  253. /// <remarks>
  254. /// Required.
  255. /// </remarks>
  256. [Required]
  257. public bool EnableNextEpisodeAutoPlay { get; set; }
  258. /// <summary>
  259. /// Gets or sets a value indicating whether the user should auto-login.
  260. /// </summary>
  261. /// <remarks>
  262. /// Required.
  263. /// </remarks>
  264. [Required]
  265. public bool EnableAutoLogin { get; set; }
  266. /// <summary>
  267. /// Gets or sets a value indicating whether the user can change their preferences.
  268. /// </summary>
  269. /// <remarks>
  270. /// Required.
  271. /// </remarks>
  272. [Required]
  273. public bool EnableUserPreferenceAccess { get; set; }
  274. /// <summary>
  275. /// Gets or sets the maximum parental age rating.
  276. /// </summary>
  277. public int? MaxParentalAgeRating { get; set; }
  278. /// <summary>
  279. /// Gets or sets the remote client bitrate limit.
  280. /// </summary>
  281. public int? RemoteClientBitrateLimit { get; set; }
  282. /// <summary>
  283. /// Gets or sets the internal id.
  284. /// This is a temporary stopgap for until the library db is migrated.
  285. /// This corresponds to the value of the index of this user in the library db.
  286. /// </summary>
  287. [Required]
  288. public long InternalId { get; set; }
  289. /// <summary>
  290. /// Gets or sets the user's profile image. Can be <c>null</c>.
  291. /// </summary>
  292. // [ForeignKey("UserId")]
  293. public virtual ImageInfo ProfileImage { get; set; }
  294. /// <summary>
  295. /// Gets or sets the user's display preferences.
  296. /// </summary>
  297. /// <remarks>
  298. /// Required.
  299. /// </remarks>
  300. [Required]
  301. public virtual DisplayPreferences DisplayPreferences { get; set; }
  302. [Required]
  303. public SyncPlayAccess SyncPlayAccess { get; set; }
  304. /// <summary>
  305. /// Gets or sets the row version.
  306. /// </summary>
  307. /// <remarks>
  308. /// Required, Concurrency Token.
  309. /// </remarks>
  310. [ConcurrencyCheck]
  311. [Required]
  312. public uint RowVersion { get; set; }
  313. /*************************************************************************
  314. * Navigation properties
  315. *************************************************************************/
  316. /// <summary>
  317. /// Gets or sets the list of access schedules this user has.
  318. /// </summary>
  319. public virtual ICollection<AccessSchedule> AccessSchedules { get; protected set; }
  320. /// <summary>
  321. /// Gets or sets the list of item display preferences.
  322. /// </summary>
  323. public virtual ICollection<ItemDisplayPreferences> ItemDisplayPreferences { get; protected set; }
  324. /*
  325. /// <summary>
  326. /// Gets or sets the list of groups this user is a member of.
  327. /// </summary>
  328. [ForeignKey("Group_Groups_Guid")]
  329. public virtual ICollection<Group> Groups { get; protected set; }
  330. */
  331. /// <summary>
  332. /// Gets or sets the list of permissions this user has.
  333. /// </summary>
  334. [ForeignKey("Permission_Permissions_Guid")]
  335. public virtual ICollection<Permission> Permissions { get; protected set; }
  336. /*
  337. /// <summary>
  338. /// Gets or sets the list of provider mappings this user has.
  339. /// </summary>
  340. [ForeignKey("ProviderMapping_ProviderMappings_Id")]
  341. public virtual ICollection<ProviderMapping> ProviderMappings { get; protected set; }
  342. */
  343. /// <summary>
  344. /// Gets or sets the list of preferences this user has.
  345. /// </summary>
  346. [ForeignKey("Preference_Preferences_Guid")]
  347. public virtual ICollection<Preference> Preferences { get; protected set; }
  348. /// <summary>
  349. /// Static create function (for use in LINQ queries, etc.)
  350. /// </summary>
  351. /// <param name="username">The username for the created user.</param>
  352. /// <param name="authenticationProviderId">The Id of the user's authentication provider.</param>
  353. /// <param name="passwordResetProviderId">The Id of the user's password reset provider.</param>
  354. /// <returns>The created instance.</returns>
  355. public static User Create(string username, string authenticationProviderId, string passwordResetProviderId)
  356. {
  357. return new User(username, authenticationProviderId, passwordResetProviderId);
  358. }
  359. /// <inheritdoc/>
  360. public void OnSavingChanges()
  361. {
  362. RowVersion++;
  363. }
  364. /// <summary>
  365. /// Checks whether the user has the specified permission.
  366. /// </summary>
  367. /// <param name="kind">The permission kind.</param>
  368. /// <returns><c>True</c> if the user has the specified permission.</returns>
  369. public bool HasPermission(PermissionKind kind)
  370. {
  371. return Permissions.First(p => p.Kind == kind).Value;
  372. }
  373. /// <summary>
  374. /// Sets the given permission kind to the provided value.
  375. /// </summary>
  376. /// <param name="kind">The permission kind.</param>
  377. /// <param name="value">The value to set.</param>
  378. public void SetPermission(PermissionKind kind, bool value)
  379. {
  380. Permissions.First(p => p.Kind == kind).Value = value;
  381. }
  382. /// <summary>
  383. /// Gets the user's preferences for the given preference kind.
  384. /// </summary>
  385. /// <param name="preference">The preference kind.</param>
  386. /// <returns>A string array containing the user's preferences.</returns>
  387. public string[] GetPreference(PreferenceKind preference)
  388. {
  389. var val = Preferences.First(p => p.Kind == preference).Value;
  390. return Equals(val, string.Empty) ? Array.Empty<string>() : val.Split(Delimiter);
  391. }
  392. /// <summary>
  393. /// Sets the specified preference to the given value.
  394. /// </summary>
  395. /// <param name="preference">The preference kind.</param>
  396. /// <param name="values">The values.</param>
  397. public void SetPreference(PreferenceKind preference, string[] values)
  398. {
  399. Preferences.First(p => p.Kind == preference).Value
  400. = string.Join(Delimiter.ToString(CultureInfo.InvariantCulture), values);
  401. }
  402. /// <summary>
  403. /// Checks whether this user is currently allowed to use the server.
  404. /// </summary>
  405. /// <returns><c>True</c> if the current time is within an access schedule, or there are no access schedules.</returns>
  406. public bool IsParentalScheduleAllowed()
  407. {
  408. return AccessSchedules.Count == 0
  409. || AccessSchedules.Any(i => IsParentalScheduleAllowed(i, DateTime.UtcNow));
  410. }
  411. /// <summary>
  412. /// Checks whether the provided folder is in this user's grouped folders.
  413. /// </summary>
  414. /// <param name="id">The Guid of the folder.</param>
  415. /// <returns><c>True</c> if the folder is in the user's grouped folders.</returns>
  416. public bool IsFolderGrouped(Guid id)
  417. {
  418. return GetPreference(PreferenceKind.GroupedFolders).Any(i => new Guid(i) == id);
  419. }
  420. private static bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  421. {
  422. var localTime = date.ToLocalTime();
  423. var hour = localTime.TimeOfDay.TotalHours;
  424. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek)
  425. && hour >= schedule.StartHour
  426. && hour <= schedule.EndHour;
  427. }
  428. // TODO: make these user configurable?
  429. private void AddDefaultPermissions()
  430. {
  431. Permissions.Add(new Permission(PermissionKind.IsAdministrator, false));
  432. Permissions.Add(new Permission(PermissionKind.IsDisabled, false));
  433. Permissions.Add(new Permission(PermissionKind.IsHidden, true));
  434. Permissions.Add(new Permission(PermissionKind.EnableAllChannels, true));
  435. Permissions.Add(new Permission(PermissionKind.EnableAllDevices, true));
  436. Permissions.Add(new Permission(PermissionKind.EnableAllFolders, true));
  437. Permissions.Add(new Permission(PermissionKind.EnableContentDeletion, false));
  438. Permissions.Add(new Permission(PermissionKind.EnableContentDownloading, true));
  439. Permissions.Add(new Permission(PermissionKind.EnableMediaConversion, true));
  440. Permissions.Add(new Permission(PermissionKind.EnableMediaPlayback, true));
  441. Permissions.Add(new Permission(PermissionKind.EnablePlaybackRemuxing, true));
  442. Permissions.Add(new Permission(PermissionKind.EnablePublicSharing, true));
  443. Permissions.Add(new Permission(PermissionKind.EnableRemoteAccess, true));
  444. Permissions.Add(new Permission(PermissionKind.EnableSyncTranscoding, true));
  445. Permissions.Add(new Permission(PermissionKind.EnableAudioPlaybackTranscoding, true));
  446. Permissions.Add(new Permission(PermissionKind.EnableLiveTvAccess, true));
  447. Permissions.Add(new Permission(PermissionKind.EnableLiveTvManagement, true));
  448. Permissions.Add(new Permission(PermissionKind.EnableSharedDeviceControl, true));
  449. Permissions.Add(new Permission(PermissionKind.EnableVideoPlaybackTranscoding, true));
  450. Permissions.Add(new Permission(PermissionKind.ForceRemoteSourceTranscoding, false));
  451. Permissions.Add(new Permission(PermissionKind.EnableRemoteControlOfOtherUsers, false));
  452. }
  453. private void AddDefaultPreferences()
  454. {
  455. foreach (var val in Enum.GetValues(typeof(PreferenceKind)).Cast<PreferenceKind>())
  456. {
  457. Preferences.Add(new Preference(val, string.Empty));
  458. }
  459. }
  460. partial void Init();
  461. }
  462. }