RequestHelpers.cs 5.6 KB

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