RequestHelpers.cs 3.0 KB

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