コード例 #1
0
        protected override async ValueTask HandleRequirementAsync(AuthorizationHandlerContext context, TeamLeaderRequirement requirement, DiscordMember member)
        {
            if (requirement.AllowAdmin && DiscordClientProvider.HasAdminRole(member))
            {
                context.Succeed(requirement);
                return;
            }

            if ((requirement.AllowRaidLeader && DiscordClientProvider.HasRaidLeaderRole(member)) ||
                (requirement.AllowLootMaster && DiscordClientProvider.HasLootMasterRole(member)) ||
                (requirement.AllowRecruiter && DiscordClientProvider.HasRecruiterRole(member)))
            {
                long?teamId = context.Resource switch
                {
                    long id => id,
                    TeamDto team => team.Id,
                    _ => null
                };

                if (teamId.HasValue)
                {
                    var discordId    = (long)member.Id;
                    var teamIdString = teamId.Value.ToString();

                    if (context.User.HasClaim(AppClaimTypes.RaidLeader, teamIdString) ||
                        await _context.UserClaims.AsNoTracking().CountAsync(claim => claim.UserId == discordId && claim.ClaimType == AppClaimTypes.RaidLeader && claim.ClaimValue == teamIdString) > 0)
                    {
                        context.Succeed(requirement);
                    }
                }
                else
                {
                    context.Succeed(requirement);
                }
            }
        }
    }
コード例 #2
0
    protected override async ValueTask HandleRequirementAsync(AuthorizationHandlerContext context, TeamLeaderRequirement requirement, DiscordMember member)
    {
        if (requirement.AllowAdmin && DiscordClientProvider.HasAdminRole(member))
        {
            context.Succeed(requirement);
            return;
        }

        if ((requirement.AllowRaidLeader && DiscordClientProvider.HasRaidLeaderRole(member)) ||
            (requirement.AllowLootMaster && DiscordClientProvider.HasLootMasterRole(member)) ||
            (requirement.AllowRecruiter && DiscordClientProvider.HasRecruiterRole(member)))
        {
            long?teamId = context.Resource switch
            {
                long id => id,
                TeamDto team => team.Id,
                RaidTeam team => team.Id,
                _ => null
            };

            if (teamId.HasValue)
            {
                var discordId = (long)member.Id;

                if (await _context.RaidTeamLeaders.AsNoTracking().CountAsync(rtl => rtl.UserId == discordId && rtl.RaidTeamId == teamId) > 0)
                {
                    context.Succeed(requirement);
                }
            }
            else
            {
                context.Succeed(requirement);
            }
        }
    }
}