User.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #pragma warning disable CA2227
  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 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. }
  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. }
  77. /// <summary>
  78. /// Gets or sets the Id of the user.
  79. /// </summary>
  80. /// <remarks>
  81. /// Identity, Indexed, Required.
  82. /// </remarks>
  83. [JsonIgnore]
  84. public Guid Id { get; set; }
  85. /// <summary>
  86. /// Gets or sets the user's name.
  87. /// </summary>
  88. /// <remarks>
  89. /// Required, Max length = 255.
  90. /// </remarks>
  91. [Required]
  92. [MaxLength(255)]
  93. [StringLength(255)]
  94. public string Username { get; set; }
  95. /// <summary>
  96. /// Gets or sets the user's password, or <c>null</c> if none is set.
  97. /// </summary>
  98. /// <remarks>
  99. /// Max length = 65535.
  100. /// </remarks>
  101. [MaxLength(65535)]
  102. [StringLength(65535)]
  103. public string Password { get; set; }
  104. /// <summary>
  105. /// Gets or sets the user's easy password, or <c>null</c> if none is set.
  106. /// </summary>
  107. /// <remarks>
  108. /// Max length = 65535.
  109. /// </remarks>
  110. [MaxLength(65535)]
  111. [StringLength(65535)]
  112. public string EasyPassword { get; set; }
  113. /// <summary>
  114. /// Gets or sets a value indicating whether the user must update their password.
  115. /// </summary>
  116. /// <remarks>
  117. /// Required.
  118. /// </remarks>
  119. public bool MustUpdatePassword { get; set; }
  120. /// <summary>
  121. /// Gets or sets the audio language preference.
  122. /// </summary>
  123. /// <remarks>
  124. /// Max length = 255.
  125. /// </remarks>
  126. [MaxLength(255)]
  127. [StringLength(255)]
  128. public string AudioLanguagePreference { get; set; }
  129. /// <summary>
  130. /// Gets or sets the authentication provider id.
  131. /// </summary>
  132. /// <remarks>
  133. /// Required, Max length = 255.
  134. /// </remarks>
  135. [Required]
  136. [MaxLength(255)]
  137. [StringLength(255)]
  138. public string AuthenticationProviderId { get; set; }
  139. /// <summary>
  140. /// Gets or sets the password reset provider id.
  141. /// </summary>
  142. /// <remarks>
  143. /// Required, Max length = 255.
  144. /// </remarks>
  145. [Required]
  146. [MaxLength(255)]
  147. [StringLength(255)]
  148. public string PasswordResetProviderId { get; set; }
  149. /// <summary>
  150. /// Gets or sets the invalid login attempt count.
  151. /// </summary>
  152. /// <remarks>
  153. /// Required.
  154. /// </remarks>
  155. public int InvalidLoginAttemptCount { get; set; }
  156. /// <summary>
  157. /// Gets or sets the last activity date.
  158. /// </summary>
  159. public DateTime? LastActivityDate { get; set; }
  160. /// <summary>
  161. /// Gets or sets the last login date.
  162. /// </summary>
  163. public DateTime? LastLoginDate { get; set; }
  164. /// <summary>
  165. /// Gets or sets the number of login attempts the user can make before they are locked out.
  166. /// </summary>
  167. public int? LoginAttemptsBeforeLockout { get; set; }
  168. /// <summary>
  169. /// Gets or sets the subtitle mode.
  170. /// </summary>
  171. /// <remarks>
  172. /// Required.
  173. /// </remarks>
  174. public SubtitlePlaybackMode SubtitleMode { get; set; }
  175. /// <summary>
  176. /// Gets or sets a value indicating whether the default audio track should be played.
  177. /// </summary>
  178. /// <remarks>
  179. /// Required.
  180. /// </remarks>
  181. public bool PlayDefaultAudioTrack { get; set; }
  182. /// <summary>
  183. /// Gets or sets the subtitle language preference.
  184. /// </summary>
  185. /// <remarks>
  186. /// Max length = 255.
  187. /// </remarks>
  188. [MaxLength(255)]
  189. [StringLength(255)]
  190. public string SubtitleLanguagePreference { get; set; }
  191. /// <summary>
  192. /// Gets or sets a value indicating whether missing episodes should be displayed.
  193. /// </summary>
  194. /// <remarks>
  195. /// Required.
  196. /// </remarks>
  197. public bool DisplayMissingEpisodes { get; set; }
  198. /// <summary>
  199. /// Gets or sets a value indicating whether to display the collections view.
  200. /// </summary>
  201. /// <remarks>
  202. /// Required.
  203. /// </remarks>
  204. public bool DisplayCollectionsView { get; set; }
  205. /// <summary>
  206. /// Gets or sets a value indicating whether the user has a local password.
  207. /// </summary>
  208. /// <remarks>
  209. /// Required.
  210. /// </remarks>
  211. public bool EnableLocalPassword { get; set; }
  212. /// <summary>
  213. /// Gets or sets a value indicating whether the server should hide played content in "Latest".
  214. /// </summary>
  215. /// <remarks>
  216. /// Required.
  217. /// </remarks>
  218. public bool HidePlayedInLatest { get; set; }
  219. /// <summary>
  220. /// Gets or sets a value indicating whether to remember audio selections on played content.
  221. /// </summary>
  222. /// <remarks>
  223. /// Required.
  224. /// </remarks>
  225. public bool RememberAudioSelections { get; set; }
  226. /// <summary>
  227. /// Gets or sets a value indicating whether to remember subtitle selections on played content.
  228. /// </summary>
  229. /// <remarks>
  230. /// Required.
  231. /// </remarks>
  232. public bool RememberSubtitleSelections { get; set; }
  233. /// <summary>
  234. /// Gets or sets a value indicating whether to enable auto-play for the next episode.
  235. /// </summary>
  236. /// <remarks>
  237. /// Required.
  238. /// </remarks>
  239. public bool EnableNextEpisodeAutoPlay { get; set; }
  240. /// <summary>
  241. /// Gets or sets a value indicating whether the user should auto-login.
  242. /// </summary>
  243. /// <remarks>
  244. /// Required.
  245. /// </remarks>
  246. public bool EnableAutoLogin { get; set; }
  247. /// <summary>
  248. /// Gets or sets a value indicating whether the user can change their preferences.
  249. /// </summary>
  250. /// <remarks>
  251. /// Required.
  252. /// </remarks>
  253. public bool EnableUserPreferenceAccess { get; set; }
  254. /// <summary>
  255. /// Gets or sets the maximum parental age rating.
  256. /// </summary>
  257. public int? MaxParentalAgeRating { get; set; }
  258. /// <summary>
  259. /// Gets or sets the remote client bitrate limit.
  260. /// </summary>
  261. public int? RemoteClientBitrateLimit { get; set; }
  262. /// <summary>
  263. /// Gets or sets the internal id.
  264. /// This is a temporary stopgap for until the library db is migrated.
  265. /// This corresponds to the value of the index of this user in the library db.
  266. /// </summary>
  267. public long InternalId { get; set; }
  268. /// <summary>
  269. /// Gets or sets the user's profile image. Can be <c>null</c>.
  270. /// </summary>
  271. // [ForeignKey("UserId")]
  272. public virtual ImageInfo ProfileImage { get; set; }
  273. /// <summary>
  274. /// Gets or sets the user's display preferences.
  275. /// </summary>
  276. /// <remarks>
  277. /// Required.
  278. /// </remarks>
  279. [Required]
  280. public virtual DisplayPreferences DisplayPreferences { get; set; }
  281. /// <summary>
  282. /// Gets or sets the level of sync play permissions this user has.
  283. /// </summary>
  284. public SyncPlayAccess SyncPlayAccess { get; set; }
  285. /// <summary>
  286. /// Gets or sets the row version.
  287. /// </summary>
  288. /// <remarks>
  289. /// Required, Concurrency Token.
  290. /// </remarks>
  291. [ConcurrencyCheck]
  292. public uint RowVersion { get; set; }
  293. /// <summary>
  294. /// Gets or sets the list of access schedules this user has.
  295. /// </summary>
  296. public virtual ICollection<AccessSchedule> AccessSchedules { get; protected set; }
  297. /// <summary>
  298. /// Gets or sets the list of item display preferences.
  299. /// </summary>
  300. public virtual ICollection<ItemDisplayPreferences> ItemDisplayPreferences { get; protected set; }
  301. /*
  302. /// <summary>
  303. /// Gets or sets the list of groups this user is a member of.
  304. /// </summary>
  305. [ForeignKey("Group_Groups_Guid")]
  306. public virtual ICollection<Group> Groups { get; protected set; }
  307. */
  308. /// <summary>
  309. /// Gets or sets the list of permissions this user has.
  310. /// </summary>
  311. [ForeignKey("Permission_Permissions_Guid")]
  312. public virtual ICollection<Permission> Permissions { get; protected set; }
  313. /*
  314. /// <summary>
  315. /// Gets or sets the list of provider mappings this user has.
  316. /// </summary>
  317. [ForeignKey("ProviderMapping_ProviderMappings_Id")]
  318. public virtual ICollection<ProviderMapping> ProviderMappings { get; protected set; }
  319. */
  320. /// <summary>
  321. /// Gets or sets the list of preferences this user has.
  322. /// </summary>
  323. [ForeignKey("Preference_Preferences_Guid")]
  324. public virtual ICollection<Preference> Preferences { get; protected set; }
  325. /// <inheritdoc/>
  326. public void OnSavingChanges()
  327. {
  328. RowVersion++;
  329. }
  330. /// <summary>
  331. /// Checks whether the user has the specified permission.
  332. /// </summary>
  333. /// <param name="kind">The permission kind.</param>
  334. /// <returns><c>True</c> if the user has the specified permission.</returns>
  335. public bool HasPermission(PermissionKind kind)
  336. {
  337. return Permissions.First(p => p.Kind == kind).Value;
  338. }
  339. /// <summary>
  340. /// Sets the given permission kind to the provided value.
  341. /// </summary>
  342. /// <param name="kind">The permission kind.</param>
  343. /// <param name="value">The value to set.</param>
  344. public void SetPermission(PermissionKind kind, bool value)
  345. {
  346. Permissions.First(p => p.Kind == kind).Value = value;
  347. }
  348. /// <summary>
  349. /// Gets the user's preferences for the given preference kind.
  350. /// </summary>
  351. /// <param name="preference">The preference kind.</param>
  352. /// <returns>A string array containing the user's preferences.</returns>
  353. public string[] GetPreference(PreferenceKind preference)
  354. {
  355. var val = Preferences.First(p => p.Kind == preference).Value;
  356. return Equals(val, string.Empty) ? Array.Empty<string>() : val.Split(Delimiter);
  357. }
  358. /// <summary>
  359. /// Sets the specified preference to the given value.
  360. /// </summary>
  361. /// <param name="preference">The preference kind.</param>
  362. /// <param name="values">The values.</param>
  363. public void SetPreference(PreferenceKind preference, string[] values)
  364. {
  365. Preferences.First(p => p.Kind == preference).Value
  366. = string.Join(Delimiter.ToString(CultureInfo.InvariantCulture), values);
  367. }
  368. /// <summary>
  369. /// Checks whether this user is currently allowed to use the server.
  370. /// </summary>
  371. /// <returns><c>True</c> if the current time is within an access schedule, or there are no access schedules.</returns>
  372. public bool IsParentalScheduleAllowed()
  373. {
  374. return AccessSchedules.Count == 0
  375. || AccessSchedules.Any(i => IsParentalScheduleAllowed(i, DateTime.UtcNow));
  376. }
  377. /// <summary>
  378. /// Checks whether the provided folder is in this user's grouped folders.
  379. /// </summary>
  380. /// <param name="id">The Guid of the folder.</param>
  381. /// <returns><c>True</c> if the folder is in the user's grouped folders.</returns>
  382. public bool IsFolderGrouped(Guid id)
  383. {
  384. return GetPreference(PreferenceKind.GroupedFolders).Any(i => new Guid(i) == id);
  385. }
  386. private static bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  387. {
  388. var localTime = date.ToLocalTime();
  389. var hour = localTime.TimeOfDay.TotalHours;
  390. return DayOfWeekHelper.GetDaysOfWeek(schedule.DayOfWeek).Contains(localTime.DayOfWeek)
  391. && hour >= schedule.StartHour
  392. && hour <= schedule.EndHour;
  393. }
  394. // TODO: make these user configurable?
  395. private void AddDefaultPermissions()
  396. {
  397. Permissions.Add(new Permission(PermissionKind.IsAdministrator, false));
  398. Permissions.Add(new Permission(PermissionKind.IsDisabled, false));
  399. Permissions.Add(new Permission(PermissionKind.IsHidden, true));
  400. Permissions.Add(new Permission(PermissionKind.EnableAllChannels, true));
  401. Permissions.Add(new Permission(PermissionKind.EnableAllDevices, true));
  402. Permissions.Add(new Permission(PermissionKind.EnableAllFolders, true));
  403. Permissions.Add(new Permission(PermissionKind.EnableContentDeletion, false));
  404. Permissions.Add(new Permission(PermissionKind.EnableContentDownloading, true));
  405. Permissions.Add(new Permission(PermissionKind.EnableMediaConversion, true));
  406. Permissions.Add(new Permission(PermissionKind.EnableMediaPlayback, true));
  407. Permissions.Add(new Permission(PermissionKind.EnablePlaybackRemuxing, true));
  408. Permissions.Add(new Permission(PermissionKind.EnablePublicSharing, true));
  409. Permissions.Add(new Permission(PermissionKind.EnableRemoteAccess, true));
  410. Permissions.Add(new Permission(PermissionKind.EnableSyncTranscoding, true));
  411. Permissions.Add(new Permission(PermissionKind.EnableAudioPlaybackTranscoding, true));
  412. Permissions.Add(new Permission(PermissionKind.EnableLiveTvAccess, true));
  413. Permissions.Add(new Permission(PermissionKind.EnableLiveTvManagement, true));
  414. Permissions.Add(new Permission(PermissionKind.EnableSharedDeviceControl, true));
  415. Permissions.Add(new Permission(PermissionKind.EnableVideoPlaybackTranscoding, true));
  416. Permissions.Add(new Permission(PermissionKind.ForceRemoteSourceTranscoding, false));
  417. Permissions.Add(new Permission(PermissionKind.EnableRemoteControlOfOtherUsers, false));
  418. }
  419. private void AddDefaultPreferences()
  420. {
  421. foreach (var val in Enum.GetValues(typeof(PreferenceKind)).Cast<PreferenceKind>())
  422. {
  423. Preferences.Add(new Preference(val, string.Empty));
  424. }
  425. }
  426. }
  427. }