RequestHelpers.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. internal static async Task<SessionInfo> GetSession(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext, Guid? userId = null)
  101. {
  102. userId ??= httpContext.User.GetUserId();
  103. User? user = null;
  104. if (!userId.IsNullOrEmpty())
  105. {
  106. user = userManager.GetUserById(userId.Value);
  107. }
  108. var session = await sessionManager.LogSessionActivity(
  109. httpContext.User.GetClient(),
  110. httpContext.User.GetVersion(),
  111. httpContext.User.GetDeviceId(),
  112. httpContext.User.GetDevice(),
  113. httpContext.GetNormalizedRemoteIP().ToString(),
  114. user).ConfigureAwait(false);
  115. if (session is null)
  116. {
  117. throw new ResourceNotFoundException("Session not found.");
  118. }
  119. return session;
  120. }
  121. internal static async Task<string> GetSessionId(ISessionManager sessionManager, IUserManager userManager, HttpContext httpContext)
  122. {
  123. var session = await GetSession(sessionManager, userManager, httpContext).ConfigureAwait(false);
  124. return session.Id;
  125. }
  126. internal static QueryResult<BaseItemDto> CreateQueryResult(
  127. QueryResult<(BaseItem Item, ItemCounts ItemCounts)> result,
  128. DtoOptions dtoOptions,
  129. IDtoService dtoService,
  130. bool includeItemTypes,
  131. User? user)
  132. {
  133. var dtos = result.Items.Select(i =>
  134. {
  135. var (baseItem, counts) = i;
  136. var dto = dtoService.GetItemByNameDto(baseItem, dtoOptions, null, user);
  137. if (includeItemTypes)
  138. {
  139. dto.ChildCount = counts.ItemCount;
  140. dto.ProgramCount = counts.ProgramCount;
  141. dto.SeriesCount = counts.SeriesCount;
  142. dto.EpisodeCount = counts.EpisodeCount;
  143. dto.MovieCount = counts.MovieCount;
  144. dto.TrailerCount = counts.TrailerCount;
  145. dto.AlbumCount = counts.AlbumCount;
  146. dto.SongCount = counts.SongCount;
  147. dto.ArtistCount = counts.ArtistCount;
  148. }
  149. return dto;
  150. });
  151. return new QueryResult<BaseItemDto>(
  152. result.StartIndex,
  153. result.TotalRecordCount,
  154. dtos.ToArray());
  155. }
  156. }