Skip to content

The Free Software Media System - Server Backend & API

Merge remote-tracking branch 'upstream/api-migration' into api-library

crobibero38788a1f1d3b
diff --cc Jellyfin.Api/Helpers/RequestHelpers.cs
index a57cf146f6,2ff40a8a5e..64723d1192
--- a/Jellyfin.Api/Helpers/RequestHelpers.cs
+++ b/Jellyfin.Api/Helpers/RequestHelpers.cs
@@@ -1,5 -1,8 +1,9 @@@
using System;
+using System.Linq;
+ using Jellyfin.Data.Enums;
+ using MediaBrowser.Controller.Net;
+ using MediaBrowser.Controller.Session;
+ using Microsoft.AspNetCore.Http;
namespace Jellyfin.Api.Helpers
{
@@@ -27,21 -30,48 +31,65 @@@
: value.Split(separator);
}
+ /// <summary>
+ /// Splits a comma delimited string and parses Guids.
+ /// </summary>
+ /// <param name="value">Input value.</param>
+ /// <returns>Parsed Guids.</returns>
+ public static Guid[] GetGuids(string value)
+ {
+ if (value == null)
+ {
+ return Array.Empty<Guid>();
+ }
+
+ return value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
+ .Select(i => new Guid(i))
+ .ToArray();
+ }
++
+ /// <summary>
+ /// Checks if the user can update an entry.
+ /// </summary>
+ /// <param name="authContext">Instance of the <see cref="IAuthorizationContext"/> interface.</param>
+ /// <param name="requestContext">The <see cref="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 bool AssertCanUpdateUser(IAuthorizationContext authContext, HttpRequest requestContext, Guid userId, bool restrictUserPreferences)
+ {
+ var auth = authContext.GetAuthorizationInfo(requestContext);
+
+ var authenticatedUser = auth.User;
+
+ // If they're going to update the record of another user, they must be an administrator
+ if ((!userId.Equals(auth.UserId) && !authenticatedUser.HasPermission(PermissionKind.IsAdministrator))
+ || (restrictUserPreferences && !authenticatedUser.EnableUserPreferenceAccess))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ internal static SessionInfo GetSession(ISessionManager sessionManager, IAuthorizationContext authContext, HttpRequest request)
+ {
+ var authorization = authContext.GetAuthorizationInfo(request);
+ var user = authorization.User;
+ var session = sessionManager.LogSessionActivity(
+ authorization.Client,
+ authorization.Version,
+ authorization.DeviceId,
+ authorization.Device,
+ request.HttpContext.Connection.RemoteIpAddress.ToString(),
+ user);
+
+ if (session == null)
+ {
+ throw new ArgumentException("Session not found.");
+ }
+
+ return session;
+ }
}
}