| | 1 | | using System.Threading.Tasks; |
| | 2 | | using Jellyfin.Api.Extensions; |
| | 3 | | using Jellyfin.Data.Enums; |
| | 4 | | using Jellyfin.Database.Implementations.Enums; |
| | 5 | | using MediaBrowser.Common.Extensions; |
| | 6 | | using MediaBrowser.Controller.Library; |
| | 7 | | using MediaBrowser.Controller.SyncPlay; |
| | 8 | | using Microsoft.AspNetCore.Authorization; |
| | 9 | |
|
| | 10 | | namespace 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> |
| 16 | 25 | | public SyncPlayAccessHandler( |
| 16 | 26 | | ISyncPlayManager syncPlayManager, |
| 16 | 27 | | IUserManager userManager) |
| | 28 | | { |
| 16 | 29 | | _syncPlayManager = syncPlayManager; |
| 16 | 30 | | _userManager = userManager; |
| 16 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <inheritdoc /> |
| | 34 | | protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, SyncPlayAccessRequirement re |
| | 35 | | { |
| 0 | 36 | | var userId = context.User.GetUserId(); |
| 0 | 37 | | var user = _userManager.GetUserById(userId); |
| 0 | 38 | | if (user is null) |
| | 39 | | { |
| 0 | 40 | | throw new ResourceNotFoundException(); |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | if (requirement.RequiredAccess == SyncPlayAccessRequirementType.HasAccess) |
| | 44 | | { |
| 0 | 45 | | if (user.SyncPlayAccess is SyncPlayUserAccessType.CreateAndJoinGroups or SyncPlayUserAccessType.JoinGrou |
| 0 | 46 | | || _syncPlayManager.IsUserActive(userId)) |
| | 47 | | { |
| 0 | 48 | | context.Succeed(requirement); |
| | 49 | | } |
| | 50 | | } |
| 0 | 51 | | else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.CreateGroup) |
| | 52 | | { |
| 0 | 53 | | if (user.SyncPlayAccess == SyncPlayUserAccessType.CreateAndJoinGroups) |
| | 54 | | { |
| 0 | 55 | | context.Succeed(requirement); |
| | 56 | | } |
| | 57 | | } |
| 0 | 58 | | else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.JoinGroup) |
| | 59 | | { |
| 0 | 60 | | if (user.SyncPlayAccess == SyncPlayUserAccessType.CreateAndJoinGroups |
| 0 | 61 | | || user.SyncPlayAccess == SyncPlayUserAccessType.JoinGroups) |
| | 62 | | { |
| 0 | 63 | | context.Succeed(requirement); |
| | 64 | | } |
| | 65 | | } |
| 0 | 66 | | else if (requirement.RequiredAccess == SyncPlayAccessRequirementType.IsInGroup) |
| | 67 | | { |
| 0 | 68 | | if (_syncPlayManager.IsUserActive(userId)) |
| | 69 | | { |
| 0 | 70 | | context.Succeed(requirement); |
| | 71 | | } |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | return Task.CompletedTask; |
| | 75 | | } |
| | 76 | | } |
| | 77 | | } |