RequestHelpers.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Linq;
  3. using Jellyfin.Data.Enums;
  4. using MediaBrowser.Controller.Net;
  5. using MediaBrowser.Controller.Session;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Querying;
  8. using Microsoft.AspNetCore.Http;
  9. namespace Jellyfin.Api.Helpers
  10. {
  11. /// <summary>
  12. /// Request Extensions.
  13. /// </summary>
  14. public static class RequestHelpers
  15. {
  16. /// <summary>
  17. /// Splits a string at a separating character into an array of substrings.
  18. /// </summary>
  19. /// <param name="value">The string to split.</param>
  20. /// <param name="separator">The char that separates the substrings.</param>
  21. /// <param name="removeEmpty">Option to remove empty substrings from the array.</param>
  22. /// <returns>An array of the substrings.</returns>
  23. internal static string[] Split(string? value, char separator, bool removeEmpty)
  24. {
  25. if (string.IsNullOrWhiteSpace(value))
  26. {
  27. return Array.Empty<string>();
  28. }
  29. return removeEmpty
  30. ? value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries)
  31. : value.Split(separator);
  32. }
  33. /// <summary>
  34. /// Checks if the user can update an entry.
  35. /// </summary>
  36. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  37. /// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
  38. /// <param name="userId">The user id.</param>
  39. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  40. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  41. internal static bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
  42. {
  43. var auth = authContext.GetAuthorizationInfo(requestContext);
  44. var authenticatedUser = auth.User;
  45. // If they're going to update the record of another user, they must be an administrator
  46. if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
  47. || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
  48. {
  49. return false;
  50. }
  51. return true;
  52. }
  53. internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
  54. {
  55. var authorization = authContext.GetAuthorizationInfo(request);
  56. var user = authorization.User;
  57. var session = sessionManager.LogSessionActivity(
  58. authorization.Client,
  59. authorization.Version,
  60. authorization.DeviceId,
  61. authorization.Device,
  62. request.HttpContext.Connection.RemoteIpAddress.ToString(),
  63. user);
  64. if (session == null)
  65. {
  66. throw new ArgumentException("Session not found.");
  67. }
  68. return session;
  69. }
  70. /// <summary>
  71. /// Get Guid array from string.
  72. /// </summary>
  73. /// <param name="value">String value.</param>
  74. /// <returns>Guid array.</returns>
  75. internal static Guid[] GetGuids(string? value)
  76. {
  77. if (value == null)
  78. {
  79. return Array.Empty<Guid>();
  80. }
  81. return Split(value, ',', true)
  82. .Select(i => new Guid(i))
  83. .ToArray();
  84. }
  85. /// <summary>
  86. /// Get orderby.
  87. /// </summary>
  88. /// <param name="sortBy">Sort by.</param>
  89. /// <param name="requestedSortOrder">Sort order.</param>
  90. /// <returns>Resulting order by.</returns>
  91. internal static ValueTuple<string, SortOrder>[] GetOrderBy(string? sortBy, string? requestedSortOrder)
  92. {
  93. if (string.IsNullOrEmpty(sortBy))
  94. {
  95. return Array.Empty<ValueTuple<string, SortOrder>>();
  96. }
  97. var vals = sortBy.Split(',');
  98. if (string.IsNullOrWhiteSpace(requestedSortOrder))
  99. {
  100. requestedSortOrder = "Ascending";
  101. }
  102. var sortOrders = requestedSortOrder.Split(',');
  103. var result = new ValueTuple<string, SortOrder>[vals.Length];
  104. for (var i = 0; i < vals.Length; i++)
  105. {
  106. var sortOrderIndex = sortOrders.Length > i ? i : 0;
  107. var sortOrderValue = sortOrders.Length > sortOrderIndex ? sortOrders[sortOrderIndex] : null;
  108. var sortOrder = string.Equals(sortOrderValue, "Descending", StringComparison.OrdinalIgnoreCase)
  109. ? SortOrder.Descending
  110. : SortOrder.Ascending;
  111. result[i] = new ValueTuple<string, SortOrder>(vals[i], sortOrder);
  112. }
  113. return result;
  114. }
  115. /// <summary>
  116. /// Gets the filters.
  117. /// </summary>
  118. /// <param name="filters">The filter string.</param>
  119. /// <returns>IEnumerable{ItemFilter}.</returns>
  120. internal static ItemFilter[] GetFilters(string filters)
  121. {
  122. return string.IsNullOrEmpty(filters)
  123. ? Array.Empty<ItemFilter>()
  124. : Split(filters, ',', true)
  125. .Select(v => Enum.Parse<ItemFilter>(v, true)).ToArray();
  126. }
  127. /// <summary>
  128. /// Gets the item fields.
  129. /// </summary>
  130. /// <param name="fields">The fields string.</param>
  131. /// <returns>IEnumerable{ItemFields}.</returns>
  132. internal static ItemFields[] GetItemFields(string? fields)
  133. {
  134. if (string.IsNullOrEmpty(fields))
  135. {
  136. return Array.Empty<ItemFields>();
  137. }
  138. return Split(fields, ',', true)
  139. .Select(v =>
  140. {
  141. if (Enum.TryParse(v, true, out ItemFields value))
  142. {
  143. return (ItemFields?)value;
  144. }
  145. return null;
  146. }).Where(i => i.HasValue)
  147. .Select(i => i!.Value)
  148. .ToArray();
  149. }
  150. }
  151. }