< Summary - Jellyfin

Information
Class: Jellyfin.Api.Auth.SyncPlayAccessPolicy.SyncPlayAccessHandler
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Auth/SyncPlayAccessPolicy/SyncPlayAccessHandler.cs
Line coverage
24%
Covered lines: 6
Uncovered lines: 19
Coverable lines: 25
Total lines: 77
Line coverage: 24%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleRequirementAsync(...)0%600240%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Auth/SyncPlayAccessPolicy/SyncPlayAccessHandler.cs

#LineLine coverage
 1using System.Threading.Tasks;
 2using Jellyfin.Api.Extensions;
 3using Jellyfin.Data.Enums;
 4using Jellyfin.Database.Implementations.Enums;
 5using MediaBrowser.Common.Extensions;
 6using MediaBrowser.Controller.Library;
 7using MediaBrowser.Controller.SyncPlay;
 8using Microsoft.AspNetCore.Authorization;
 9
 10namespace Jellyfin.Api.Auth.SyncPlayAccessPolicy
 11{
 12    /// <summary>
 13    /// Default authorization handler.
 14    /// </summary>
 15    public class SyncPlayAccessHandler : AuthorizationHandler<SyncPlayAccessRequirement>
 16    {
 17        private readonly ISyncPlayManager _syncPlayManager;
 18        private readonly IUserManager _userManager;
 19
 20        /// <summary>
 21        /// Initializes a new instance of the <see cref="SyncPlayAccessHandler"/> class.
 22        /// </summary>
 23        /// <param name="syncPlayManager">Instance of the <see cref="ISyncPlayManager"/> interface.</param>
 24        /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param>
 1625        public SyncPlayAccessHandler(
 1626            ISyncPlayManager syncPlayManager,
 1627            IUserManager userManager)
 28        {
 1629            _syncPlayManager = syncPlayManager;
 1630            _userManager = userManager;
 1631        }
 32
 33        /// <inheritdoc />
 34        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, SyncPlayAccessRequirement re
 35        {
 036            var userId = context.User.GetUserId();
 037            var user = _userManager.GetUserById(userId);
 038            if (user is null)
 39            {
 040                throw new ResourceNotFoundException();
 41            }
 42
 043            if (requirement.RequiredAccess == SyncPlayAccessRequirementType.HasAccess)
 44            {
 045                if (user.SyncPlayAccess is SyncPlayUserAccessType.CreateAndJoinGroups or SyncPlayUserAccessType.JoinGrou
 046                    || _syncPlayManager.IsUserActive(userId))
 47                {
 048                    context.Succeed(requirement);
 49                }
 50            }
 051            else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.CreateGroup)
 52            {
 053                if (user.SyncPlayAccess == SyncPlayUserAccessType.CreateAndJoinGroups)
 54                {
 055                    context.Succeed(requirement);
 56                }
 57            }
 058            else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.JoinGroup)
 59            {
 060                if (user.SyncPlayAccess == SyncPlayUserAccessType.CreateAndJoinGroups
 061                    || user.SyncPlayAccess == SyncPlayUserAccessType.JoinGroups)
 62                {
 063                    context.Succeed(requirement);
 64                }
 65            }
 066            else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.IsInGroup)
 67            {
 068                if (_syncPlayManager.IsUserActive(userId))
 69                {
 070                    context.Succeed(requirement);
 71                }
 72            }
 73
 074            return Task.CompletedTask;
 75        }
 76    }
 77}