UserEntityExtensions.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using Jellyfin.Database.Implementations.Entities;
  5. using Jellyfin.Database.Implementations.Enums;
  6. using Jellyfin.Database.Implementations.Interfaces;
  7. namespace Jellyfin.Data;
  8. /// <summary>
  9. /// Contains extension methods for manipulation of <see cref="User"/> entities.
  10. /// </summary>
  11. public static class UserEntityExtensions
  12. {
  13. /// <summary>
  14. /// The values being delimited here are Guids, so commas work as they do not appear in Guids.
  15. /// </summary>
  16. private const char Delimiter = ',';
  17. /// <summary>
  18. /// Checks whether the user has the specified permission.
  19. /// </summary>
  20. /// <param name="entity">The entity to update.</param>
  21. /// <param name="kind">The permission kind.</param>
  22. /// <returns><c>True</c> if the user has the specified permission.</returns>
  23. public static bool HasPermission(this IHasPermissions entity, PermissionKind kind)
  24. {
  25. return entity.Permissions.FirstOrDefault(p => p.Kind == kind)?.Value ?? false;
  26. }
  27. /// <summary>
  28. /// Sets the given permission kind to the provided value.
  29. /// </summary>
  30. /// <param name="entity">The entity to update.</param>
  31. /// <param name="kind">The permission kind.</param>
  32. /// <param name="value">The value to set.</param>
  33. public static void SetPermission(this IHasPermissions entity, PermissionKind kind, bool value)
  34. {
  35. var currentPermission = entity.Permissions.FirstOrDefault(p => p.Kind == kind);
  36. if (currentPermission is null)
  37. {
  38. entity.Permissions.Add(new Permission(kind, value));
  39. }
  40. else
  41. {
  42. currentPermission.Value = value;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets the user's preferences for the given preference kind.
  47. /// </summary>
  48. /// <param name="entity">The entity to update.</param>
  49. /// <param name="preference">The preference kind.</param>
  50. /// <returns>A string array containing the user's preferences.</returns>
  51. public static string[] GetPreference(this User entity, PreferenceKind preference)
  52. {
  53. var val = entity.Preferences.FirstOrDefault(p => p.Kind == preference)?.Value;
  54. return string.IsNullOrEmpty(val) ? Array.Empty<string>() : val.Split(Delimiter);
  55. }
  56. /// <summary>
  57. /// Gets the user's preferences for the given preference kind.
  58. /// </summary>
  59. /// <param name="entity">The entity to update.</param>
  60. /// <param name="preference">The preference kind.</param>
  61. /// <typeparam name="T">Type of preference.</typeparam>
  62. /// <returns>A {T} array containing the user's preference.</returns>
  63. public static T[] GetPreferenceValues<T>(this User entity, PreferenceKind preference)
  64. {
  65. var val = entity.Preferences.FirstOrDefault(p => p.Kind == preference)?.Value;
  66. if (string.IsNullOrEmpty(val))
  67. {
  68. return Array.Empty<T>();
  69. }
  70. // Convert array of {string} to array of {T}
  71. var converter = TypeDescriptor.GetConverter(typeof(T));
  72. var stringValues = val.Split(Delimiter);
  73. var convertedCount = 0;
  74. var parsedValues = new T[stringValues.Length];
  75. for (var i = 0; i < stringValues.Length; i++)
  76. {
  77. try
  78. {
  79. var parsedValue = converter.ConvertFromString(stringValues[i].Trim());
  80. if (parsedValue is not null)
  81. {
  82. parsedValues[convertedCount++] = (T)parsedValue;
  83. }
  84. }
  85. catch (FormatException)
  86. {
  87. // Unable to convert value
  88. }
  89. }
  90. return parsedValues[..convertedCount];
  91. }
  92. /// <summary>
  93. /// Sets the specified preference to the given value.
  94. /// </summary>
  95. /// <param name="entity">The entity to update.</param>
  96. /// <param name="preference">The preference kind.</param>
  97. /// <param name="values">The values.</param>
  98. public static void SetPreference(this User entity, PreferenceKind preference, string[] values)
  99. {
  100. var value = string.Join(Delimiter, values);
  101. var currentPreference = entity.Preferences.FirstOrDefault(p => p.Kind == preference);
  102. if (currentPreference is null)
  103. {
  104. entity.Preferences.Add(new Preference(preference, value));
  105. }
  106. else
  107. {
  108. currentPreference.Value = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Sets the specified preference to the given value.
  113. /// </summary>
  114. /// <param name="entity">The entity to update.</param>
  115. /// <param name="preference">The preference kind.</param>
  116. /// <param name="values">The values.</param>
  117. /// <typeparam name="T">The type of value.</typeparam>
  118. public static void SetPreference<T>(this User entity, PreferenceKind preference, T[] values)
  119. {
  120. var value = string.Join(Delimiter, values);
  121. var currentPreference = entity.Preferences.FirstOrDefault(p => p.Kind == preference);
  122. if (currentPreference is null)
  123. {
  124. entity.Preferences.Add(new Preference(preference, value));
  125. }
  126. else
  127. {
  128. currentPreference.Value = value;
  129. }
  130. }
  131. /// <summary>
  132. /// Checks whether this user is currently allowed to use the server.
  133. /// </summary>
  134. /// <param name="entity">The entity to update.</param>
  135. /// <returns><c>True</c> if the current time is within an access schedule, or there are no access schedules.</returns>
  136. public static bool IsParentalScheduleAllowed(this User entity)
  137. {
  138. return entity.AccessSchedules.Count == 0
  139. || entity.AccessSchedules.Any(i => IsParentalScheduleAllowed(i, DateTime.UtcNow));
  140. }
  141. /// <summary>
  142. /// Checks whether the provided folder is in this user's grouped folders.
  143. /// </summary>
  144. /// <param name="entity">The entity to update.</param>
  145. /// <param name="id">The Guid of the folder.</param>
  146. /// <returns><c>True</c> if the folder is in the user's grouped folders.</returns>
  147. public static bool IsFolderGrouped(this User entity, Guid id)
  148. {
  149. return Array.IndexOf(GetPreferenceValues<Guid>(entity, PreferenceKind.GroupedFolders), id) != -1;
  150. }
  151. /// <summary>
  152. /// Initializes the default permissions for a user. Should only be called on user creation.
  153. /// </summary>
  154. /// <param name="entity">The entity to update.</param>
  155. // TODO: make these user configurable?
  156. public static void AddDefaultPermissions(this User entity)
  157. {
  158. entity.Permissions.Add(new Permission(PermissionKind.IsAdministrator, false));
  159. entity.Permissions.Add(new Permission(PermissionKind.IsDisabled, false));
  160. entity.Permissions.Add(new Permission(PermissionKind.IsHidden, true));
  161. entity.Permissions.Add(new Permission(PermissionKind.EnableAllChannels, true));
  162. entity.Permissions.Add(new Permission(PermissionKind.EnableAllDevices, true));
  163. entity.Permissions.Add(new Permission(PermissionKind.EnableAllFolders, true));
  164. entity.Permissions.Add(new Permission(PermissionKind.EnableContentDeletion, false));
  165. entity.Permissions.Add(new Permission(PermissionKind.EnableContentDownloading, true));
  166. entity.Permissions.Add(new Permission(PermissionKind.EnableMediaConversion, true));
  167. entity.Permissions.Add(new Permission(PermissionKind.EnableMediaPlayback, true));
  168. entity.Permissions.Add(new Permission(PermissionKind.EnablePlaybackRemuxing, true));
  169. entity.Permissions.Add(new Permission(PermissionKind.EnablePublicSharing, true));
  170. entity.Permissions.Add(new Permission(PermissionKind.EnableRemoteAccess, true));
  171. entity.Permissions.Add(new Permission(PermissionKind.EnableSyncTranscoding, true));
  172. entity.Permissions.Add(new Permission(PermissionKind.EnableAudioPlaybackTranscoding, true));
  173. entity.Permissions.Add(new Permission(PermissionKind.EnableLiveTvAccess, true));
  174. entity.Permissions.Add(new Permission(PermissionKind.EnableLiveTvManagement, true));
  175. entity.Permissions.Add(new Permission(PermissionKind.EnableSharedDeviceControl, true));
  176. entity.Permissions.Add(new Permission(PermissionKind.EnableVideoPlaybackTranscoding, true));
  177. entity.Permissions.Add(new Permission(PermissionKind.ForceRemoteSourceTranscoding, false));
  178. entity.Permissions.Add(new Permission(PermissionKind.EnableRemoteControlOfOtherUsers, false));
  179. entity.Permissions.Add(new Permission(PermissionKind.EnableCollectionManagement, false));
  180. entity.Permissions.Add(new Permission(PermissionKind.EnableSubtitleManagement, false));
  181. entity.Permissions.Add(new Permission(PermissionKind.EnableLyricManagement, false));
  182. }
  183. /// <summary>
  184. /// Initializes the default preferences. Should only be called on user creation.
  185. /// </summary>
  186. /// <param name="entity">The entity to update.</param>
  187. public static void AddDefaultPreferences(this User entity)
  188. {
  189. foreach (var val in Enum.GetValues<PreferenceKind>())
  190. {
  191. entity.Preferences.Add(new Preference(val, string.Empty));
  192. }
  193. }
  194. private static bool IsParentalScheduleAllowed(AccessSchedule schedule, DateTime date)
  195. {
  196. var localTime = date.ToLocalTime();
  197. var hour = localTime.TimeOfDay.TotalHours;
  198. var currentDayOfWeek = localTime.DayOfWeek;
  199. return schedule.DayOfWeek.Contains(currentDayOfWeek)
  200. && hour >= schedule.StartHour
  201. && hour <= schedule.EndHour;
  202. }
  203. }