User.cs 19 KB

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