RequestHelpers.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Claims;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Api.Constants;
  7. using Jellyfin.Api.Extensions;
  8. using Jellyfin.Data.Entities;
  9. using Jellyfin.Data.Enums;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Controller.Dto;
  12. using MediaBrowser.Controller.Entities;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.Session;
  15. using MediaBrowser.Model.Dto;
  16. using MediaBrowser.Model.Querying;
  17. using Microsoft.AspNetCore.Http;
  18. namespace Jellyfin.Api.Helpers;
  19. /// <summary>
  20. /// Request Extensions.
  21. /// </summary>
  22. public static class RequestHelpers
  23. {
  24. /// <summary>
  25. /// Get Order By.
  26. /// </summary>
  27. /// <param name="sortBy">Sort By. Comma delimited string.</param>
  28. /// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
  29. /// <returns>Order By.</returns>
  30. public static (string, SortOrder)[] GetOrderBy(IReadOnlyList<string> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
  31. {
  32. if (sortBy.Count == 0)
  33. {
  34. return Array.Empty<(string, SortOrder)>();
  35. }
  36. var result = new (string, SortOrder)[sortBy.Count];
  37. var i = 0;
  38. // Add elements which have a SortOrder specified
  39. for (; i < requestedSortOrder.Count; i++)
  40. {
  41. result[i] = (sortBy[i], requestedSortOrder[i]);
  42. }
  43. // Add remaining elements with the first specified SortOrder
  44. // or the default one if no SortOrders are specified
  45. var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
  46. for (; i < sortBy.Count; i++)
  47. {
  48. result[i] = (sortBy[i], order);
  49. }
  50. return result;
  51. }
  52. /// <summary>
  53. /// Checks if the user can update an entry.
  54. /// </summary>
  55. /// <param name="userManager">An instance of the <see cref="IUserManager"/> interface.</param>
  56. /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> for the current request.</param>
  57. /// <param name="userId">The user id.</param>
  58. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  59. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  60. internal static bool AssertCanUpdateUser(IUserManager userManager, ClaimsPrincipal claimsPrincipal, Guid userId, bool restrictUserPreferences)
  61. {
  62. var authenticatedUserId = claimsPrincipal.GetUserId();
  63. var isAdministrator = claimsPrincipal.IsInRole(UserRoles.Administrator);
  64. // If they're going to update the record of another user, they must be an administrator
  65. if (!userId.Equals(authenticatedUserId) && !isAdministrator)
  66. {
  67. return false;
  68. }
  69. // TODO the EnableUserPreferenceAccess policy does not seem to be used elsewhere
  70. if (!restrictUserPreferences || isAdministrator)
  71. {
  72. return true;
  73. }
  74. var user = userManager.GetUserById(userId);
  75. return user.EnableUserPreferenceAccess;
  76. }
  77. internal static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext)
  78. {
  79. var userId = httpContext.User.GetUserId();
  80. var user = userManager.GetUserById(userId);
  81. var session = await sessionManager.LogSessionActivity(
  82. httpContext.User.GetClient(),
  83. httpContext.User.GetVersion(),
  84. httpContext.User.GetDeviceId(),
  85. httpContext.User.GetDevice(),
  86. httpContext.GetNormalizedRemoteIp().ToString(),
  87. user).ConfigureAwait(false);
  88. if (session is null)
  89. {
  90. throw new ArgumentException("Session not found.");
  91. }
  92. return session;
  93. }
  94. internal static async Task<string> GetSessionId(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext)
  95. {
  96. var session = await GetSession(sessionManager, userManager, httpContext).ConfigureAwait(false);
  97. return session.Id;
  98. }
  99. internal static QueryResult<BaseItemDto> CreateQueryResult(
  100. QueryResult<(BaseItem Item, ItemCounts ItemCounts)> result,
  101. DtoOptions dtoOptions,
  102. IDtoService dtoService,
  103. bool includeItemTypes,
  104. User? user)
  105. {
  106. var dtos = result.Items.Select(i =>
  107. {
  108. var (baseItem, counts) = i;
  109. var dto = dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  110. if (includeItemTypes)
  111. {
  112. dto.ChildCount = counts.ItemCount;
  113. dto.ProgramCount = counts.ProgramCount;
  114. dto.SeriesCount = counts.SeriesCount;
  115. dto.EpisodeCount = counts.EpisodeCount;
  116. dto.MovieCount = counts.MovieCount;
  117. dto.TrailerCount = counts.TrailerCount;
  118. dto.AlbumCount = counts.AlbumCount;
  119. dto.SongCount = counts.SongCount;
  120. dto.ArtistCount = counts.ArtistCount;
  121. }
  122. return dto;
  123. });
  124. return new QueryResult<BaseItemDto>(
  125. result.StartIndex,
  126. result.TotalRecordCount,
  127. dtos.ToArray());
  128. }
  129. }