Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ODS-6189] Possible bug with 64 bit Education Organization ID look up #961

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,43 @@
using Microsoft.Data.SqlClient;
using EdFi.Ods.Common.Security.Authorization;
using EdFi.Ods.Common.Security.Claims;
using EdFi.Ods.Common.Models;
using EdFi.Common;
using EdFi.Common.Extensions;
using EdFi.Ods.Common.Conventions;
using System;
using System.Collections.Generic;

namespace EdFi.Ods.Api.Security.AuthorizationStrategies.Relationships.Filters;

public class SqlServerViewBasedSingleItemAuthorizationQuerySupport : IViewBasedSingleItemAuthorizationQuerySupport
{
private const int SqlServerParameterCountThreshold = 2000;

private readonly Type _edOrgSystemType;
private readonly Dictionary<Type, string> _structuredTypeBySystemType = new()
{
{typeof(int), "dbo.IntTable"},
{typeof(long), "dbo.BigIntTable"}
};

public SqlServerViewBasedSingleItemAuthorizationQuerySupport(IDomainModelProvider domainModelProvider)
{
Preconditions.ThrowIfNull(domainModelProvider, nameof(domainModelProvider));

var standardModelVersion = Version.Parse(
domainModelProvider
.GetDomainModel()
.Schemas
.Single(x => x.LogicalName.EqualsIgnoreCase(EdFiConventions.LogicalName))
.Version
);

_edOrgSystemType = standardModelVersion.Major < 5
? typeof(int)
: typeof(long);
}

public string GetItemExistenceCheckSql(ViewBasedAuthorizationFilterDefinition filterDefinition, AuthorizationFilterContext filterContext)
{
if (filterContext.ClaimEndpointValues.Length < SqlServerParameterCountThreshold)
Expand Down Expand Up @@ -47,16 +77,16 @@ public void ApplyClaimsParametersToCommand(DbCommand cmd, EdFiAuthorizationConte
var sqlParameter = cmd.CreateParameter() as SqlParameter;

DataTable dt = new();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Id", _edOrgSystemType);

foreach (int claimEdOrgId in claimEdOrgIds)
foreach (var claimEdOrgId in claimEdOrgIds)
{
dt.Rows.Add(claimEdOrgId);
}

sqlParameter!.ParameterName = "ClaimEducationOrganizationIds";
sqlParameter.SqlDbType = SqlDbType.Structured;
sqlParameter.TypeName = "dbo.IntTable";
sqlParameter.TypeName = _structuredTypeBySystemType[_edOrgSystemType];
sqlParameter.Value = dt;

cmd.Parameters.Add(sqlParameter);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- SPDX-License-Identifier: Apache-2.0
-- Licensed to the Ed-Fi Alliance under one or more agreements.
-- The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
-- See the LICENSE and NOTICES files in the project root for more information.

CREATE TYPE dbo.BigIntTable AS TABLE
(
Id BIGINT
)
Loading