Skip to content
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 @@ -18,6 +18,7 @@ public interface IOrganizationRepository : IRepository<Organization, Guid>
Task<ICollection<Organization>> SearchAsync(string name, string userEmail, bool? paid, int skip, int take);
Task UpdateStorageAsync(Guid id);
Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync();
Task<OrganizationAbility?> GetAbilityAsync(Guid organizationId);
Task<Organization?> GetByLicenseKeyAsync(string licenseKey);
Task<SelfHostedOrganizationDetails?> GetSelfHostedOrganizationDetailsById(Guid id);
Task<ICollection<Organization>> SearchUnassignedToProviderAsync(string name, string ownerEmail, int skip, int take);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ public async Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync()
}
}

public async Task<OrganizationAbility?> GetAbilityAsync(Guid organizationId)
{
using (var connection = new SqlConnection(ConnectionString))
{
var result = await connection.QueryAsync<OrganizationAbility>(
"[dbo].[Organization_ReadAbilityById]",
new { Id = organizationId },
commandType: CommandType.StoredProcedure);

return result.SingleOrDefault();
}
}

public async Task<Organization?> GetByLicenseKeyAsync(string licenseKey)
{
using (var connection = new SqlConnection(ConnectionString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ public async Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync()
}
}

#nullable enable
public async Task<OrganizationAbility?> GetAbilityAsync(Guid organizationId)
{
using var scope = ServiceScopeFactory.CreateScope();

var dbContext = GetDatabaseContext(scope);

return await GetDbSet(dbContext)
.Where(e => e.Id == organizationId)
.Select(e => new OrganizationAbility(e))
.SingleOrDefaultAsync();
}
#nullable disable

public async Task<ICollection<Core.AdminConsole.Entities.Organization>> SearchUnassignedToProviderAsync(string name, string ownerEmail, int skip, int take)
{
using var scope = ServiceScopeFactory.CreateScope();
Expand Down
13 changes: 13 additions & 0 deletions src/Sql/dbo/Stored Procedures/Organization_ReadAbilityById.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE PROCEDURE [dbo].[Organization_ReadAbilityById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
*
FROM
[dbo].[OrganizationAbilityView]
WHERE
[Id] = @Id
END
28 changes: 28 additions & 0 deletions src/Sql/dbo/Views/OrganizationAbilityView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE VIEW [dbo].[OrganizationAbilityView]
Comment thread
jrmccannon marked this conversation as resolved.
AS
SELECT
[Id],
[UseEvents],
[Use2fa],
IIF([Use2fa] = 1 AND [TwoFactorProviders] IS NOT NULL AND [TwoFactorProviders] != '{}', 1, 0) AS [Using2fa],
Comment thread
jrmccannon marked this conversation as resolved.
[UsersGetPremium],
[Enabled],
[UseSso],
[UseKeyConnector],
[UseScim],
[UseResetPassword],
[UseCustomPermissions],
[UsePolicies],
[LimitCollectionCreation],
[LimitCollectionDeletion],
[LimitItemDeletion],
[AllowAdminAccessToAllCollectionItems],
[UseRiskInsights],
[UseOrganizationDomains],
[UseAdminSponsoredFamilies],
[UseAutomaticUserConfirmation],
[UseDisableSmAdsForUsers],
[UsePhishingBlocker],
[UseMyItems]
FROM
[dbo].[Organization]
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static Task<Organization> CreateTestOrganizationAsync(this IOrganizationR
UseAutomaticUserConfirmation = true,
UsePhishingBlocker = true,
UseDisableSmAdsForUsers = true,
UseMyItems = true,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
Assert.NotNull(updateResult);
Assert.Equal(organization.Id, updateResult.Id);
Assert.True(updateResult.SyncSeats);
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));

Check warning on line 214 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.IncrementSeatCountAsync_GivenOrganizationHasNotChangedSeatCountBefore_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 214 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.IncrementSeatCountAsync_GivenOrganizationHasNotChangedSeatCountBefore_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

// Annul
await sutRepository.DeleteAsync(organization);
Expand All @@ -236,7 +236,7 @@
Assert.NotNull(updateResult);
Assert.Equal(organization.Id, updateResult.Id);
Assert.True(updateResult.SyncSeats);
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));

Check warning on line 239 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.IncrementSeatCountAsync_GivenOrganizationHasChangedSeatCountBeforeAndRecordExists_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 239 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.IncrementSeatCountAsync_GivenOrganizationHasChangedSeatCountBeforeAndRecordExists_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

// Annul
await sutRepository.DeleteAsync(organization);
Expand All @@ -259,7 +259,7 @@
Assert.NotNull(updateResult);
Assert.Equal(organization.Id, updateResult.Id);
Assert.True(updateResult.SyncSeats);
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));

Check warning on line 262 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.GetOrganizationsForSubscriptionSyncAsync_GivenOrganizationHasChangedSeatCount_WhenGettingOrgsToUpdate_ThenReturnsOrgSubscriptionUpdate(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

Check warning on line 262 in test/Infrastructure.IntegrationTest/AdminConsole/Repositories/OrganizationRepositoryTests.cs

View workflow job for this annotation

GitHub Actions / Run tests

The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'OrganizationRepositoryTests.GetOrganizationsForSubscriptionSyncAsync_GivenOrganizationHasChangedSeatCount_WhenGettingOrgsToUpdate_ThenReturnsOrgSubscriptionUpdate(IOrganizationRepository)' with a call to 'DateTime.ToString(string, IFormatProvider)'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1305)

// Annul
await sutRepository.DeleteAsync(organization);
Expand Down Expand Up @@ -365,6 +365,57 @@
Assert.Null(orgUserAfter.UserId);
}

[Theory, DatabaseData]
public async Task GetAbilityAsync_WithExistingOrganization_ReturnsCorrectAbility(
IOrganizationRepository organizationRepository)
{
// Arrange
var organization = await organizationRepository.CreateTestOrganizationAsync();

// Act
var result = await organizationRepository.GetAbilityAsync(organization.Id);

// Assert
Assert.NotNull(result);
Assert.Equal(organization.Id, result.Id);
Assert.Equal(organization.UseEvents, result.UseEvents);
Assert.Equal(organization.Use2fa, result.Use2fa);
Assert.Equal(organization.Use2fa && organization.TwoFactorProviders != null && organization.TwoFactorProviders != "{}", result.Using2fa);
Assert.Equal(organization.UsersGetPremium, result.UsersGetPremium);
Assert.Equal(organization.Enabled, result.Enabled);
Assert.Equal(organization.UseSso, result.UseSso);
Assert.Equal(organization.UseKeyConnector, result.UseKeyConnector);
Assert.Equal(organization.UseScim, result.UseScim);
Assert.Equal(organization.UseResetPassword, result.UseResetPassword);
Assert.Equal(organization.UseCustomPermissions, result.UseCustomPermissions);
Assert.Equal(organization.UsePolicies, result.UsePolicies);
Assert.Equal(organization.LimitCollectionCreation, result.LimitCollectionCreation);
Assert.Equal(organization.LimitCollectionDeletion, result.LimitCollectionDeletion);
Assert.Equal(organization.LimitItemDeletion, result.LimitItemDeletion);
Assert.Equal(organization.AllowAdminAccessToAllCollectionItems, result.AllowAdminAccessToAllCollectionItems);
Assert.Equal(organization.UseRiskInsights, result.UseRiskInsights);
Assert.Equal(organization.UseOrganizationDomains, result.UseOrganizationDomains);
Assert.Equal(organization.UseAdminSponsoredFamilies, result.UseAdminSponsoredFamilies);
Assert.Equal(organization.UseAutomaticUserConfirmation, result.UseAutomaticUserConfirmation);
Assert.Equal(organization.UseDisableSmAdsForUsers, result.UseDisableSmAdsForUsers);
Assert.Equal(organization.UsePhishingBlocker, result.UsePhishingBlocker);
Assert.Equal(organization.UseMyItems, result.UseMyItems);

// Clean up
await organizationRepository.DeleteAsync(organization);
}

[Theory, DatabaseData]
public async Task GetAbilityAsync_WithNonExistentOrganization_ReturnsNull(
IOrganizationRepository organizationRepository)
{
// Act
var result = await organizationRepository.GetAbilityAsync(Guid.NewGuid());

// Assert
Assert.Null(result);
}

private static async Task<(User user, Organization organization, OrganizationUser organizationUser)>
CreatePendingOrganizationWithUserAsync(
IUserRepository userRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE OR ALTER VIEW [dbo].[OrganizationAbilityView]
AS
SELECT
[Id],
[UseEvents],
[Use2fa],
IIF([Use2fa] = 1 AND [TwoFactorProviders] IS NOT NULL AND [TwoFactorProviders] != '{}', 1, 0) AS [Using2fa],
[UsersGetPremium],
[Enabled],
[UseSso],
[UseKeyConnector],
[UseScim],
[UseResetPassword],
[UseCustomPermissions],
[UsePolicies],
[LimitCollectionCreation],
[LimitCollectionDeletion],
[LimitItemDeletion],
[AllowAdminAccessToAllCollectionItems],
[UseRiskInsights],
[UseOrganizationDomains],
[UseAdminSponsoredFamilies],
[UseAutomaticUserConfirmation],
[UseDisableSmAdsForUsers],
[UsePhishingBlocker],
[UseMyItems]
FROM
[dbo].[Organization]
GO

CREATE OR ALTER PROCEDURE [dbo].[Organization_ReadAbilityById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
*
FROM
[dbo].[OrganizationAbilityView]
WHERE
[Id] = @Id
END
GO
Loading