RequestHelpers.cs 4.1 KB

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