RequestHelpers.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Microsoft.AspNetCore.Http;
  8. namespace Jellyfin.Api.Helpers
  9. {
  10. /// <summary>
  11. /// Request Extensions.
  12. /// </summary>
  13. public static class RequestHelpers
  14. {
  15. /// <summary>
  16. /// Splits a string at a separating character into an array of substrings.
  17. /// </summary>
  18. /// <param name="value">The string to split.</param>
  19. /// <param name="separator">The char that separates the substrings.</param>
  20. /// <param name="removeEmpty">Option to remove empty substrings from the array.</param>
  21. /// <returns>An array of the substrings.</returns>
  22. internal static string[] Split(string value, char separator, bool removeEmpty)
  23. {
  24. if (string.IsNullOrWhiteSpace(value))
  25. {
  26. return Array.Empty<string>();
  27. }
  28. return removeEmpty
  29. ? value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries)
  30. : value.Split(separator);
  31. }
  32. /// <summary>
  33. /// Checks if the user can update an entry.
  34. /// </summary>
  35. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  36. /// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
  37. /// <param name="userId">The user id.</param>
  38. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  39. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  40. internal static bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
  41. {
  42. var auth = authContext.GetAuthorizationInfo(requestContext);
  43. var authenticatedUser = auth.User;
  44. // If they're going to update the record of another user, they must be an administrator
  45. if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
  46. || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
  47. {
  48. return false;
  49. }
  50. return true;
  51. }
  52. internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
  53. {
  54. var authorization = authContext.GetAuthorizationInfo(request);
  55. var user = authorization.User;
  56. var session = sessionManager.LogSessionActivity(
  57. authorization.Client,
  58. authorization.Version,
  59. authorization.DeviceId,
  60. authorization.Device,
  61. request.HttpContext.Connection.RemoteIpAddress.ToString(),
  62. user);
  63. if (session == null)
  64. {
  65. throw new ArgumentException("Session not found.");
  66. }
  67. return session;
  68. }
  69. /// <summary>
  70. /// Get Guid array from string.
  71. /// </summary>
  72. /// <param name="value">String value.</param>
  73. /// <returns>Guid array.</returns>
  74. internal static Guid[] GetGuids(string? value)
  75. {
  76. if (value == null)
  77. {
  78. return Array.Empty<Guid>();
  79. }
  80. return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  81. .Select(i => new Guid(i))
  82. .ToArray();
  83. }
  84. /// <summary>
  85. /// Get orderby.
  86. /// </summary>
  87. /// <param name="sortBy">Sort by.</param>
  88. /// <param name="requestedSortOrder">Sort order.</param>
  89. /// <returns>Resulting order by.</returns>
  90. internal static ValueTuple<string, SortOrder>[] GetOrderBy(string sortBy, string requestedSortOrder)
  91. {
  92. var val = sortBy;
  93. if (string.IsNullOrEmpty(val))
  94. {
  95. return Array.Empty<ValueTuple<string, SortOrder>>();
  96. }
  97. var vals = val.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. }
  116. }