Merge pull request #11665 from Bond-009/getuserbyid

This commit is contained in:
Cody Robibero 2024-08-28 12:39:48 -06:00 committed by GitHub
commit 8c3f3c503b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 75 additions and 98 deletions

View File

@ -1530,7 +1530,7 @@ namespace Emby.Server.Implementations.Library
{
var userViews = UserViewManager.GetUserViews(new UserViewQuery
{
UserId = user.Id,
User = user,
IncludeHidden = true,
IncludeExternalContent = allowExternalContent
});

View File

@ -16,7 +16,6 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Channels;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Library;
using MediaBrowser.Model.Querying;
@ -27,17 +26,15 @@ namespace Emby.Server.Implementations.Library
{
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localizationManager;
private readonly IUserManager _userManager;
private readonly IChannelManager _channelManager;
private readonly ILiveTvManager _liveTvManager;
private readonly IServerConfigurationManager _config;
public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IUserManager userManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerConfigurationManager config)
public UserViewManager(ILibraryManager libraryManager, ILocalizationManager localizationManager, IChannelManager channelManager, ILiveTvManager liveTvManager, IServerConfigurationManager config)
{
_libraryManager = libraryManager;
_localizationManager = localizationManager;
_userManager = userManager;
_channelManager = channelManager;
_liveTvManager = liveTvManager;
_config = config;
@ -45,11 +42,7 @@ namespace Emby.Server.Implementations.Library
public Folder[] GetUserViews(UserViewQuery query)
{
var user = _userManager.GetUserById(query.UserId);
if (user is null)
{
throw new ArgumentException("User id specified in the query does not exist.", nameof(query));
}
var user = query.User;
var folders = _libraryManager.GetUserRootFolder()
.GetChildren(user, true)
@ -125,14 +118,14 @@ namespace Emby.Server.Implementations.Library
{
var channelResult = _channelManager.GetChannelsInternalAsync(new ChannelQuery
{
UserId = query.UserId
UserId = user.Id
}).GetAwaiter().GetResult();
var channels = channelResult.Items;
list.AddRange(channels);
if (_liveTvManager.GetEnabledUsers().Select(i => i.Id).Contains(query.UserId))
if (_liveTvManager.GetEnabledUsers().Select(i => i.Id).Contains(user.Id))
{
list.Add(_liveTvManager.GetInternalLiveTvFolder(CancellationToken.None));
}
@ -207,9 +200,7 @@ namespace Emby.Server.Implementations.Library
public List<Tuple<BaseItem, List<BaseItem>>> GetLatestItems(LatestItemsQuery request, DtoOptions options)
{
var user = _userManager.GetUserById(request.UserId);
var libraryItems = GetItemsForLatestItems(user, request, options);
var libraryItems = GetItemsForLatestItems(request.User, request, options);
var list = new List<Tuple<BaseItem, List<BaseItem>>>();

View File

@ -19,14 +19,12 @@ namespace Emby.Server.Implementations.TV
{
public class TVSeriesManager : ITVSeriesManager
{
private readonly IUserManager _userManager;
private readonly IUserDataManager _userDataManager;
private readonly ILibraryManager _libraryManager;
private readonly IServerConfigurationManager _configurationManager;
public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
public TVSeriesManager(IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager configurationManager)
{
_userManager = userManager;
_userDataManager = userDataManager;
_libraryManager = libraryManager;
_configurationManager = configurationManager;
@ -34,12 +32,7 @@ namespace Emby.Server.Implementations.TV
public QueryResult<BaseItem> GetNextUp(NextUpQuery query, DtoOptions options)
{
var user = _userManager.GetUserById(query.UserId);
if (user is null)
{
throw new ArgumentException("User not found");
}
var user = query.User;
string? presentationUniqueKey = null;
if (!query.SeriesId.IsNullOrEmpty())
@ -83,12 +76,7 @@ namespace Emby.Server.Implementations.TV
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, BaseItem[] parentsFolders, DtoOptions options)
{
var user = _userManager.GetUserById(request.UserId);
if (user is null)
{
throw new ArgumentException("User not found");
}
var user = request.User;
string? presentationUniqueKey = null;
int? limit = null;

View File

@ -109,7 +109,7 @@ public class ImageController : BaseJellyfinApiController
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(_userManager, HttpContext.User, requestUserId, true))
if (!RequestHelpers.AssertCanUpdateUser(HttpContext.User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to update the image.");
}
@ -203,13 +203,18 @@ public class ImageController : BaseJellyfinApiController
[FromQuery] Guid? userId)
{
var requestUserId = RequestHelpers.GetUserId(User, userId);
if (!RequestHelpers.AssertCanUpdateUser(_userManager, HttpContext.User, requestUserId, true))
var user = _userManager.GetUserById(requestUserId);
if (user is null)
{
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(HttpContext.User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to delete the image.");
}
var user = _userManager.GetUserById(requestUserId);
if (user?.ProfileImage is null)
if (user.ProfileImage is null)
{
return NoContent();
}

View File

@ -972,12 +972,17 @@ public class ItemsController : BaseJellyfinApiController
[FromRoute, Required] Guid itemId)
{
var requestUserId = RequestHelpers.GetUserId(User, userId);
if (!RequestHelpers.AssertCanUpdateUser(_userManager, User, requestUserId, true))
var user = _userManager.GetUserById(requestUserId);
if (user is null)
{
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to view this item user data.");
}
var user = _userManager.GetUserById(requestUserId) ?? throw new ResourceNotFoundException();
var item = _libraryManager.GetItemById<BaseItem>(itemId, user);
if (item is null)
{
@ -1023,12 +1028,17 @@ public class ItemsController : BaseJellyfinApiController
[FromBody, Required] UpdateUserItemDataDto userDataDto)
{
var requestUserId = RequestHelpers.GetUserId(User, userId);
if (!RequestHelpers.AssertCanUpdateUser(_userManager, User, requestUserId, true))
var user = _userManager.GetUserById(requestUserId);
if (user is null)
{
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to update this item user data.");
}
var user = _userManager.GetUserById(requestUserId) ?? throw new ResourceNotFoundException();
var item = _libraryManager.GetItemById<BaseItem>(itemId, user);
if (item is null)
{

View File

@ -90,7 +90,12 @@ public class TvShowsController : BaseJellyfinApiController
[FromQuery] bool enableResumable = true,
[FromQuery] bool enableRewatching = false)
{
userId = RequestHelpers.GetUserId(User, userId);
var user = _userManager.GetUserById(RequestHelpers.GetUserId(User, userId));
if (user is null)
{
return NotFound();
}
var options = new DtoOptions { Fields = fields }
.AddClientFields(User)
.AddAdditionalDtoOptions(enableImages, enableUserData, imageTypeLimit, enableImageTypes);
@ -102,7 +107,7 @@ public class TvShowsController : BaseJellyfinApiController
ParentId = parentId,
SeriesId = seriesId,
StartIndex = startIndex,
UserId = userId.Value,
User = user,
EnableTotalRecordCount = enableTotalRecordCount,
DisableFirstEpisode = disableFirstEpisode,
NextUpDateCutoff = nextUpDateCutoff ?? DateTime.MinValue,
@ -111,10 +116,6 @@ public class TvShowsController : BaseJellyfinApiController
},
options);
var user = userId.IsNullOrEmpty()
? null
: _userManager.GetUserById(userId.Value);
var returnItems = _dtoService.GetBaseItemDtos(result.Items, options, user);
return new QueryResult<BaseItemDto>(

View File

@ -274,16 +274,15 @@ public class UserController : BaseJellyfinApiController
[FromBody, Required] UpdateUserPassword request)
{
var requestUserId = userId ?? User.GetUserId();
if (!RequestHelpers.AssertCanUpdateUser(_userManager, User, requestUserId, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to update the password.");
}
var user = _userManager.GetUserById(requestUserId);
if (user is null)
{
return NotFound("User not found");
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User is not allowed to update the password.");
}
if (request.ResetPassword)
@ -386,7 +385,7 @@ public class UserController : BaseJellyfinApiController
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(_userManager, User, requestUserId, true))
if (!RequestHelpers.AssertCanUpdateUser(User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User update not allowed.");
}
@ -396,7 +395,7 @@ public class UserController : BaseJellyfinApiController
await _userManager.RenameUser(user, updateUser.Name).ConfigureAwait(false);
}
await _userManager.UpdateConfigurationAsync(user.Id, updateUser.Configuration).ConfigureAwait(false);
await _userManager.UpdateConfigurationAsync(requestUserId, updateUser.Configuration).ConfigureAwait(false);
return NoContent();
}
@ -495,7 +494,13 @@ public class UserController : BaseJellyfinApiController
[FromBody, Required] UserConfiguration userConfig)
{
var requestUserId = userId ?? User.GetUserId();
if (!RequestHelpers.AssertCanUpdateUser(_userManager, User, requestUserId, true))
var user = _userManager.GetUserById(requestUserId);
if (user is null)
{
return NotFound();
}
if (!RequestHelpers.AssertCanUpdateUser(User, user, true))
{
return StatusCode(StatusCodes.Status403Forbidden, "User configuration update not allowed");
}

View File

@ -560,7 +560,7 @@ public class UserLibraryController : BaseJellyfinApiController
IsPlayed = isPlayed,
Limit = limit,
ParentId = parentId ?? Guid.Empty,
UserId = requestUserId,
User = user,
},
dtoOptions);

View File

@ -8,6 +8,7 @@ using Jellyfin.Api.Helpers;
using Jellyfin.Api.ModelBinders;
using Jellyfin.Api.Models.UserViewDtos;
using Jellyfin.Data.Enums;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
@ -69,8 +70,9 @@ public class UserViewsController : BaseJellyfinApiController
[FromQuery] bool includeHidden = false)
{
userId = RequestHelpers.GetUserId(User, userId);
var user = _userManager.GetUserById(userId.Value) ?? throw new ResourceNotFoundException();
var query = new UserViewQuery { UserId = userId.Value, IncludeHidden = includeHidden };
var query = new UserViewQuery { User = user, IncludeHidden = includeHidden };
if (includeExternalContent.HasValue)
{
@ -87,8 +89,6 @@ public class UserViewsController : BaseJellyfinApiController
var dtoOptions = new DtoOptions().AddClientFields(User);
dtoOptions.Fields = [..dtoOptions.Fields, ItemFields.PrimaryImageAspectRatio, ItemFields.DisplayPreferencesId];
var user = _userManager.GetUserById(userId.Value);
var dtos = Array.ConvertAll(folders, i => _dtoService.GetBaseItemDto(i, dtoOptions, user));
return new QueryResult<BaseItemDto>(dtos);

View File

@ -86,18 +86,17 @@ public static class RequestHelpers
/// <summary>
/// Checks if the user can update an entry.
/// </summary>
/// <param name="userManager">An instance of the <see cref="IUserManager"/> interface.</param>
/// <param name="claimsPrincipal">The <see cref="ClaimsPrincipal"/> for the current request.</param>
/// <param name="userId">The user id.</param>
/// <param name="user">The user id.</param>
/// <param name="restrictUserPreferences">Whether to restrict the user preferences.</param>
/// <returns>A <see cref="bool"/> whether the user can update the entry.</returns>
internal static bool AssertCanUpdateUser(IUserManager userManager, ClaimsPrincipal claimsPrincipal, Guid userId, bool restrictUserPreferences)
internal static bool AssertCanUpdateUser(ClaimsPrincipal claimsPrincipal, User user, bool restrictUserPreferences)
{
var authenticatedUserId = claimsPrincipal.GetUserId();
var isAdministrator = claimsPrincipal.IsInRole(UserRoles.Administrator);
// If they're going to update the record of another user, they must be an administrator
if (!userId.Equals(authenticatedUserId) && !isAdministrator)
if (!user.Id.Equals(authenticatedUserId) && !isAdministrator)
{
return false;
}
@ -108,12 +107,6 @@ public static class RequestHelpers
return true;
}
var user = userManager.GetUserById(userId);
if (user is null)
{
throw new ResourceNotFoundException();
}
return user.EnableUserPreferenceAccess;
}

View File

@ -1844,7 +1844,7 @@ namespace MediaBrowser.Controller.Entities
data.LastPlayedDate = datePlayed ?? data.LastPlayedDate ?? DateTime.UtcNow;
data.Played = true;
UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
UserDataManager.SaveUserData(user, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
}
/// <summary>
@ -1866,7 +1866,7 @@ namespace MediaBrowser.Controller.Entities
data.LastPlayedDate = null;
data.Played = false;
UserDataManager.SaveUserData(user.Id, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
UserDataManager.SaveUserData(user, this, data, UserDataSaveReason.TogglePlayed, CancellationToken.None);
}
/// <summary>

View File

@ -76,7 +76,7 @@ namespace MediaBrowser.Controller.Entities
var result = UserViewManager.GetUserViews(new UserViewQuery
{
UserId = query.User.Id,
User = query.User,
PresetViews = query.PresetViews
});

View File

@ -337,7 +337,7 @@ namespace MediaBrowser.Controller.Entities
{
Limit = query.Limit,
StartIndex = query.StartIndex,
UserId = query.User.Id
User = query.User
},
parentFolders,
query.DtoOptions);

View File

@ -1,6 +1,7 @@
#pragma warning disable CS1591
using System;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
namespace MediaBrowser.Model.Library
@ -14,10 +15,10 @@ namespace MediaBrowser.Model.Library
}
/// <summary>
/// Gets or sets the user identifier.
/// Gets or sets the user.
/// </summary>
/// <value>The user identifier.</value>
public Guid UserId { get; set; }
/// <value>The user.</value>
public required User User { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [include external content].

View File

@ -2,6 +2,7 @@
#pragma warning disable CS1591
using System;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using MediaBrowser.Model.Entities;
@ -18,7 +19,7 @@ namespace MediaBrowser.Model.Querying
/// Gets or sets the user to localize search results for.
/// </summary>
/// <value>The user id.</value>
public Guid UserId { get; set; }
public User User { get; set; }
/// <summary>
/// Gets or sets the parent id.

View File

@ -1,7 +1,7 @@
#nullable disable
#pragma warning disable CS1591
using System;
using Jellyfin.Data.Entities;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Model.Querying
@ -19,10 +19,10 @@ namespace MediaBrowser.Model.Querying
}
/// <summary>
/// Gets or sets the user id.
/// Gets or sets the user.
/// </summary>
/// <value>The user id.</value>
public Guid UserId { get; set; }
/// <value>The user.</value>
public required User User { get; set; }
/// <summary>
/// Gets or sets the parent identifier.
@ -48,24 +48,6 @@ namespace MediaBrowser.Model.Querying
/// <value>The limit.</value>
public int? Limit { get; set; }
/// <summary>
/// gets or sets the fields to return within the items, in addition to basic information.
/// </summary>
/// <value>The fields.</value>
public ItemFields[] Fields { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [enable images].
/// </summary>
/// <value><c>null</c> if [enable images] contains no value, <c>true</c> if [enable images]; otherwise, <c>false</c>.</value>
public bool? EnableImages { get; set; }
/// <summary>
/// Gets or sets the image type limit.
/// </summary>
/// <value>The image type limit.</value>
public int? ImageTypeLimit { get; set; }
/// <summary>
/// Gets or sets the enable image types.
/// </summary>