Skip to content

Commit

Permalink
[ODS-6100] Sandbox Admin needs a way to create vendors, applications …
Browse files Browse the repository at this point in the history
…and sanboxes (#980)
  • Loading branch information
semalaiappan authored Mar 4, 2024
1 parent 273cf61 commit 8a337b7
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Lib edFi.admin.dataaccess.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ concurrency:

env:
INFORMATIONAL_VERSION: "7.2"
BUILD_INCREMENTER: "-71"
BUILD_INCREMENTER: "-50"
CONFIGURATION: "Release"
AZURE_ARTIFACT_URL: "https://pkgs.dev.azure.com/ed-fi-alliance/Ed-Fi-Alliance-OSS/_packaging/EdFi/nuget/v3/index.json"
AZURE_ARTIFACT_NUGET_KEY: ${{ secrets.AZURE_ARTIFACTS_PERSONAL_ACCESS_TOKEN }}
Expand Down
19 changes: 19 additions & 0 deletions Application/EdFi.Admin.DataAccess/Models/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,24 @@ public ApplicationEducationOrganization CreateApplicationEducationOrganization(l
Application = this,
ApiClients = ApiClients
};

public static Application Create(string applicationName, long educationOrganizationId, Vendor vendor, string claimSetName, string operationalContextUri)
{
var application = new Application
{
ApplicationName = applicationName,
ClaimSetName = claimSetName,
OperationalContextUri = operationalContextUri,
Vendor =vendor
};

application.ApplicationEducationOrganizations.Add(
new ApplicationEducationOrganization
{
Application = application,
EducationOrganizationId = educationOrganizationId
});
return application;
}
}
}
3 changes: 2 additions & 1 deletion Application/EdFi.Admin.DataAccess/Models/Vendor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Application CreateApplication(string applicationName, string claimSetName

public static Vendor Create(string vendorName, IEnumerable<string> namespacePrefixes)
{
var vendor = new Vendor { VendorName = vendorName };
var vendor = new Vendor { VendorName = vendorName };

foreach (string namespacePrefix in namespacePrefixes)
{
Expand All @@ -58,6 +58,7 @@ public static Vendor Create(string vendorName, IEnumerable<string> namespacePref
});
}


return vendor;
}
}
Expand Down
128 changes: 125 additions & 3 deletions Application/EdFi.Admin.DataAccess/Repositories/ClientAppRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,117 @@ public void DeleteClient(string key)
}
}

public Application[] GetVendorApplications(int vendorId)
public IEnumerable<Vendor> GetVendors()
{
using (var context = _contextFactory.CreateContext())
{
return context.Applications.Where(a => a.Vendor.VendorId == vendorId)
.ToArray();
return context.Vendors.Include(u => u.VendorNamespacePrefixes).Include(a=>a.Applications)
.Include(b=>b.Users)
.ToList();
}
}

public IEnumerable<Application> GetApplications()
{
using (var context = _contextFactory.CreateContext())
{
return context.Applications.Include(u => u.ApplicationEducationOrganizations).Include(a=>a.Vendor).ToList();
}
}

public Vendor GetVendor(int vendorId)
{
using var context = _contextFactory.CreateContext();
return context.Vendors.FirstOrDefault(c => c.VendorId == vendorId);
}

public Application GetApplication(int applicationId)
{
using var context = _contextFactory.CreateContext();
var application = context.Applications
.Where(x => x.ApplicationId == applicationId)
.Include(x => x.ApplicationEducationOrganizations)
.FirstOrDefault();
return application;
}

public void DeleteApplication(int applicationId)
{
using var context = _contextFactory.CreateContext();
var application = context.Applications.Include(a => a.ApiClients).First(x => x.ApplicationId == applicationId);

var applicationEducationOrganizations = context.ApplicationEducationOrganizations.Include(x => x.Application)
.Where(x => x.Application.ApplicationId == application.ApplicationId);

foreach (var applicationEducationOrganization in applicationEducationOrganizations)
{
context.ApplicationEducationOrganizations.Remove(applicationEducationOrganization);
}

var apiClientKeysToDelete = application.ApiClients.Select(a => a.Key).ToList();

context.Applications.Remove(application);

context.SaveChanges();

foreach (var key in apiClientKeysToDelete)
{
DeleteClient(key);
}
}

public void DeleteVendor(int vendorId)
{
using (var context = _contextFactory.CreateContext())
{
var vendor = context.Vendors.First(x => x.VendorId == vendorId);

var applications = context.Applications.Include(c => c.Vendor)
.ThenInclude(c => c.VendorNamespacePrefixes)
.Include(c => c.ApplicationEducationOrganizations)
.Include(c => c.ApiClients)
.Where(x => x.Vendor.VendorId == vendor.VendorId);

List<string> apiClientKeysToDelete = new();

foreach (var application in applications)
{
apiClientKeysToDelete.AddRange(application.ApiClients.Select(a => a.Key).ToList());
context.ApplicationEducationOrganizations.RemoveRange(application.ApplicationEducationOrganizations);
context.Applications.Remove(application);
}

foreach (var user in vendor.Users)
{
context.Users.Remove(user);
}
context.Vendors.Remove(vendor);
context.SaveChanges();

foreach (var key in apiClientKeysToDelete)
{
DeleteClient(key);
}
}
}

public Application CreateOrGetApplication(int vendorId, string applicationName, long educationOrganizationId,string claimSetName= "Ed-Fi Sandbox", string operationalContextUri = "uri://ed-fi-api-host.org")
{
using (var context = _contextFactory.CreateContext())
{
var application = context.Applications.SingleOrDefault(v => v.ApplicationName == applicationName);

if (application == null)
{
var vendor = context.Vendors.SingleOrDefault(v => v.VendorId == vendorId);
application = Application.Create(applicationName, educationOrganizationId, vendor, claimSetName, operationalContextUri);
}
context.Applications.Update(application);
context.SaveChanges();
return application;
}
}

public void AddApiClientToUserWithVendorApplication(int userId, ApiClient client)
{
using (var context = _contextFactory.CreateContext())
Expand Down Expand Up @@ -489,7 +591,27 @@ public Vendor CreateOrGetVendor(string userEmail, string userName, IEnumerable<s
vendor = Vendor.Create(vendorName, namespacePrefixes);
context.SaveChanges();
}
return vendor;
}
}

public Vendor CreateOrGetVendor(string vendorName, IEnumerable<string> namespacePrefixes, string contactName, string contactEmailAddress)
{
using (var context = _contextFactory.CreateContext())
{
var vendor = context.Vendors.SingleOrDefault(v => v.VendorName == vendorName);

if (vendor == null)
{
vendor = Vendor.Create(vendorName, namespacePrefixes);
}

var user = new User { FullName = contactName, Email = contactEmailAddress,Vendor=vendor };

vendor.Users.Add(user);

context.Vendors.Add(vendor);
context.SaveChanges();
return vendor;
}
}
Expand Down
19 changes: 17 additions & 2 deletions Application/EdFi.Admin.DataAccess/Repositories/IClientAppRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public interface IClientAppRepo

void SetDefaultVendorOnUserFromEmailAndName(string userEmail, string userName);

Application[] GetVendorApplications(int vendorId);

void AddApiClientToUserWithVendorApplication(int userId, ApiClient client);

ApiClient SetupDefaultSandboxClient(string name, SandboxType sandboxType, string key, string secret, int userId,
Expand All @@ -49,6 +47,23 @@ void SetupKeySecret(string name, SandboxType sandboxType, string key, string sec

Vendor CreateOrGetVendor(string userEmail, string userName, IEnumerable<string> namespacePrefixes);

Vendor CreateOrGetVendor(string vendorName, IEnumerable<string> namespacePrefixes, string contactName, string contactEmailAddress);


Application CreateOrGetApplication(int vendorId, string applicationName, long educationOrganizationId, string claimSetName, string operationalContextUri);

Vendor GetVendor(int vendorId);

Application GetApplication(int applicationId);

void DeleteApplication(int applicationId);

void DeleteVendor(int vendorId);

IEnumerable<Vendor> GetVendors();

IEnumerable<Application> GetApplications();

Application CreateApplicationForVendor(int vendorId, string applicationName, string claimSetName);

ApiClient CreateApiClient(int userId, string name, string key, string secret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Application FindOrCreateUpdatedDefaultSandboxApplication(int vendorId, Sa
var vendor = context.Vendors
.Where(x => x.VendorId == vendorId)
.Include(x => x.Applications).ThenInclude(x => x.ApplicationEducationOrganizations)
.Single();
.FirstOrDefault();

var defaultAppName = _configuration.GetSection("DefaultApplicationName").Value ?? "Default Sandbox Application";
var applicationName = defaultAppName + " " + sandboxType;
Expand Down
2 changes: 1 addition & 1 deletion Application/EdFi.Ods.Api/EdFi.Ods.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.56" />
<PackageReference Include="EdFi.Suite3.Security.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Common" Version="7.2.4" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Application/EdFi.Ods.Sandbox/EdFi.Ods.Sandbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.56" />
<PackageReference Include="EdFi.Suite3.Common" Version="7.2.4" />
</ItemGroup>
</Otherwise>
Expand Down
2 changes: 1 addition & 1 deletion Application/EdFi.Ods.Tests/EdFi.Ods.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.56" />
<PackageReference Include="EdFi.Suite3.Common" Version="7.2.4" />
</ItemGroup>
</Otherwise>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.56" />
<PackageReference Include="EdFi.Suite3.Common" Version="7.2.4" />
</ItemGroup>
</Otherwise>
</Choose>
<Target Name="CopyDirectory" AfterTargets="AfterBuild" Condition=" '$(StandardVersion)' == '5.0.0' ">
<Target Name="CopyDirectory" AfterTargets="AfterBuild" Condition=" '$(StandardVersion)' == '5.0.0' ">
<ItemGroup>
<MySourceDirectory Include="$(MSBuildProjectDirectory)\5.0.0\**\*" />
<MyDestinationDirectory Include="$(MSBuildProjectDirectory)\" />
</ItemGroup>
<Copy SourceFiles="@(MySourceDirectory)" DestinationFiles="@(MySourceDirectory->'$(MSBuildProjectDirectory)\%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<Target Name="CopyDirectory" AfterTargets="AfterBuild" Condition=" '$(StandardVersion)' == '4.0.0' ">
<Target Name="CopyDirectory" AfterTargets="AfterBuild" Condition=" '$(StandardVersion)' == '4.0.0' ">
<ItemGroup>
<MySourceDirectory Include="$(MSBuildProjectDirectory)\4.0.0\**\*" />
<MyDestinationDirectory Include="$(MSBuildProjectDirectory)\" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.3" />
<PackageReference Include="EdFi.Suite3.Admin.DataAccess" Version="7.2.56" />
<PackageReference Include="EdFi.Suite3.Common" Version="7.2.4" />
</ItemGroup>
</Otherwise>
Expand Down

0 comments on commit 8a337b7

Please sign in to comment.