RequestHelpers.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Enums;
  9. using Jellyfin.Database.Implementations.Entities;
  10. using Jellyfin.Database.Implementations.Enums;
  11. using Jellyfin.Extensions;
  12. using MediaBrowser.Common.Extensions;
  13. using MediaBrowser.Controller.Dto;
  14. using MediaBrowser.Controller.Entities;
  15. using MediaBrowser.Controller.Library;
  16. using MediaBrowser.Controller.Net;
  17. using MediaBrowser.Controller.Session;
  18. using MediaBrowser.Model.Dto;
  19. using MediaBrowser.Model.Querying;
  20. using Microsoft.AspNetCore.Http;
  21. namespace Jellyfin.Api.Helpers;
  22. /// <summary>
  23. /// Request Extensions.
  24. /// </summary>
  25. public static class RequestHelpers
  26. {
  27. /// <summary>
  28. /// Get Order By.
  29. /// </summary>
  30. /// <param name="sortBy">Sort By. Comma delimited string.</param>
  31. /// <param name="requestedSortOrder">Sort Order. Comma delimited string.</param>
  32. /// <returns>Order By.</returns>
  33. public static (ItemSortBy, SortOrder)[] GetOrderBy(IReadOnlyList<ItemSortBy> sortBy, IReadOnlyList<SortOrder> requestedSortOrder)
  34. {
  35. if (sortBy.Count == 0)
  36. {
  37. return Array.Empty<(ItemSortBy, SortOrder)>();
  38. }
  39. var result = new (ItemSortBy, SortOrder)[sortBy.Count];
  40. var i = 0;
  41. // Add elements which have a SortOrder specified
  42. for (; i < requestedSortOrder.Count; i++)
  43. {
  44. result[i] = (sortBy[i], requestedSortOrder[i]);
  45. }
  46. // Add remaining elements with the first specified SortOrder
  47. // or the default one if no SortOrders are specified
  48. var order = requestedSortOrder.Count > 0 ? requestedSortOrder[0] : SortOrder.Ascending;
  49. for (; i < sortBy.Count; i++)
  50. {
  51. result[i] = (sortBy[i], order);
  52. }
  53. return result;
  54. }
  55. /// <summary>
  56. /// Checks if the user can access a user.
  57. /// </summary>
  58. /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> for the current request.</param>
  59. /// <param name="userId">The user id.</param>
  60. /// <returns>A <see cref="bool"/> whether the user can access the user.</returns>
  61. internal static Guid GetUserId(ClaimsPrincipal claimsPrincipal, Guid? userId)
  62. {
  63. var authenticatedUserId = claimsPrincipal.GetUserId();
  64. // UserId not provided, fall back to authenticated user id.
  65. if (userId.IsNullOrEmpty())
  66. {
  67. return authenticatedUserId;
  68. }
  69. // User must be administrator to access another user.
  70. var isAdministrator = claimsPrincipal.IsInRole(UserRoles.Administrator);
  71. if (!userId.Value.Equals(authenticatedUserId) && !isAdministrator)
  72. {
  73. throw new SecurityException("Forbidden");
  74. }
  75. return userId.Value;
  76. }
  77. /// <summary>
  78. /// Checks if the user can update an entry.
  79. /// </summary>
  80. /// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> for the current request.</param>
  81. /// <param name="user">The user id.</param>
  82. /// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
  83. /// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
  84. internal static bool AssertCanUpdateUser(ClaimsPrincipal claimsPrincipal, User user, bool restrictUserPreferences)
  85. {
  86. var authenticatedUserId = claimsPrincipal.GetUserId();
  87. var isAdministrator = claimsPrincipal.IsInRole(UserRoles.Administrator);
  88. // If they're going to update the record of another user, they must be an administrator
  89. if (!user.Id.Equals(authenticatedUserId) && !isAdministrator)
  90. {
  91. return false;
  92. }
  93. // TODO the EnableUserPreferenceAccess policy does not seem to be used elsewhere
  94. if (!restrictUserPreferences || isAdministrator)
  95. {
  96. return true;
  97. }
  98. return user.EnableUserPreferenceAccess;
  99. }
  100. /// <summary>
  101. /// Get the session based on http request.
  102. /// </summary>
  103. /// <param name="sessionManager">The session manager.</param>
  104. /// <param name="userManager">The user manager.</param>
  105. /// <param name="httpContext">The http context.</param>
  106. /// <param name="userId">The optional userid.</param>
  107. /// <returns>The session.</returns>
  108. /// <exception cref="ResourceNotFoundException">Session not found.</exception>
  109. public static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null)
  110. {
  111. userId ??= httpContext.User.GetUserId();
  112. User? user = null;
  113. if (!userId.IsNullOrEmpty())
  114. {
  115. user = userManager.GetUserById(userId.Value);
  116. }
  117. var session = await sessionManager.LogSessionActivity(
  118. httpContext.User.GetClient(),
  119. httpContext.User.GetVersion(),
  120. httpContext.User.GetDeviceId(),
  121. httpContext.User.GetDevice(),
  122. httpContext.GetNormalizedRemoteIP().ToString(),
  123. user).ConfigureAwait(false);
  124. if (session is null)
  125. {
  126. throw new ResourceNotFoundException("Session not found.");
  127. }
  128. return session;
  129. }
  130. internal static async Task<string> GetSessionId(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext)
  131. {
  132. var session = await GetSession(sessionManager, userManager, httpContext).ConfigureAwait(false);
  133. return session.Id;
  134. }
  135. internal static QueryResult<BaseItemDto> CreateQueryResult(
  136. QueryResult<(BaseItem Item, ItemCounts ItemCounts)> result,
  137. DtoOptions dtoOptions,
  138. IDtoService dtoService,
  139. bool includeItemTypes,
  140. User? user)
  141. {
  142. var dtos = result.Items.Select(i =>
  143. {
  144. var (baseItem, counts) = i;
  145. var dto = dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  146. if (includeItemTypes)
  147. {
  148. dto.ChildCount = counts.ItemCount;
  149. dto.ProgramCount = counts.ProgramCount;
  150. dto.SeriesCount = counts.SeriesCount;
  151. dto.EpisodeCount = counts.EpisodeCount;
  152. dto.MovieCount = counts.MovieCount;
  153. dto.TrailerCount = counts.TrailerCount;
  154. dto.AlbumCount = counts.AlbumCount;
  155. dto.SongCount = counts.SongCount;
  156. dto.ArtistCount = counts.ArtistCount;
  157. }
  158. return dto;
  159. });
  160. return new QueryResult<BaseItemDto>(
  161. result.StartIndex,
  162. result.TotalRecordCount,
  163. dtos.ToArray());
  164. }
  165. }