User.cs 18 KB

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