2
0

RequestHelpers.cs 2.3 KB

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