RequestHelpers.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// Checks if the user can update an entry.
  33. /// </summary>
  34. /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
  35. /// <param name="requestContext">The <see cref="HttpRequest"/>.</param>
  36. /// <param name="userId">The user id.</param>
  37. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  38. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  39. internal static bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
  40. {
  41. var auth = authContext.GetAuthorizationInfo(requestContext);
  42. var authenticatedUser = auth.User;
  43. // If they're going to update the record of another user, they must be an administrator
  44. if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
  45. || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
  46. {
  47. return false;
  48. }
  49. return true;
  50. }
  51. internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
  52. {
  53. var authorization = authContext.GetAuthorizationInfo(request);
  54. var user = authorization.User;
  55. var session = sessionManager.LogSessionActivity(
  56. authorization.Client,
  57. authorization.Version,
  58. authorization.DeviceId,
  59. authorization.Device,
  60. request.HttpContext.Connection.RemoteIpAddress.ToString(),
  61. user);
  62. if (session == null)
  63. {
  64. throw new ArgumentException("Session not found.");
  65. }
  66. return session;
  67. }
  68. /// <summary>
  69. /// Get Guid array from string.
  70. /// </summary>
  71. /// <param name="value">String value.</param>
  72. /// <returns>Guid array.</returns>
  73. internal static Guid[] GetGuids(string? value)
  74. {
  75. if (value == null)
  76. {
  77. return Array.Empty<Guid>();
  78. }
  79. return Split(value, ',', true)
  80. .Select(i => new Guid(i))
  81. .ToArray();
  82. }
  83. }
  84. }