46 lines
2.2 KiB
C#
46 lines
2.2 KiB
C#
// The following code is a derivative work of the code from the Jellyfin project,
|
|
// which is licensed GPLv2. This code therefore is also licensed under the terms
|
|
// of the GNU Public License, verison 2.
|
|
// https://github.com/jellyfin/jellyfin/blob/a60cb280a3d31ba19ffb3a94cf83ef300a7473b7/Jellyfin.Api/Helpers/RequestHelpers.cs#L63-L77
|
|
|
|
// Use of this relatively small snippet complies with fair use
|
|
// See https://www.gnu.org/licenses/gpl-faq.en.html#SourceCodeInDocumentation
|
|
// These helpers were not published within a Nuget package, so it was neccessary to re-implement.
|
|
|
|
namespace Jellyfin.Plugin.OIDC_Auth.Helpers;
|
|
|
|
/// <summary>
|
|
/// Request Extensions.
|
|
/// </summary>
|
|
public static class RequestHelpers {
|
|
/// <summary>
|
|
/// Checks if the user can update an entry.
|
|
/// </summary>
|
|
/// <param name="authContext">Instance of the <see cref="MediaBrowser.Controller.Net.IAuthorizationContext" /> interface.</param>
|
|
/// <param name="requestContext">The <see cref="Microsoft.AspNetCore.Http.HttpRequest" />.</param>
|
|
/// <param name="userId">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 async System.Threading.Tasks.Task<bool> AssertCanUpdateUser(
|
|
MediaBrowser.Controller.Net.IAuthorizationContext authContext,
|
|
Microsoft.AspNetCore.Http.HttpRequest requestContext,
|
|
System.Guid userId,
|
|
bool restrictUserPreferences
|
|
) {
|
|
MediaBrowser.Controller.Net.AuthorizationInfo auth =
|
|
await authContext.GetAuthorizationInfo(requestContext).ConfigureAwait(false);
|
|
|
|
Jellyfin.Database.Implementations.Entities.User authenticatedUser = auth.User;
|
|
|
|
// If they're going to update the record of another user, they must be an administrator
|
|
if (
|
|
(!userId.Equals(auth.UserId) && !Jellyfin.Data.UserEntityExtensions.HasPermission(authenticatedUser, Jellyfin.Database.Implementations.Enums.PermissionKind.IsAdministrator))
|
|
|| (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|