User.cs 19 KB

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