2
0

User.cs 18 KB

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