RequestHelpers.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Querying;
  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 Split(value, ',', true)
  81. .Select(i => new Guid(i))
  82. .ToArray();
  83. }
  84. /// <summary>
  85. /// Gets the filters.
  86. /// </summary>
  87. /// <param name="filters">The filter string.</param>
  88. /// <returns>IEnumerable{ItemFilter}.</returns>
  89. internal static ItemFilter[] GetFilters(string filters)
  90. {
  91. return string.IsNullOrEmpty(filters)
  92. ? Array.Empty<ItemFilter>()
  93. : Split(filters, ',', true)
  94. .Select(v => Enum.Parse<ItemFilter>(v, true)).ToArray();
  95. }
  96. }
  97. }