mirror of
https://github.com/bitwarden/server.git
synced 2026-04-10 09:41:16 -05:00
[PM-32066] - Add Org Ability View (#7194)
* Added sproc, view, repo methods, and tests
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -131,6 +131,19 @@ public class OrganizationRepository : Repository<Organization, Guid>, IOrganizat
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
@@ -146,6 +146,20 @@ public class OrganizationRepository : Repository<Core.AdminConsole.Entities.Orga
|
||||
}
|
||||
}
|
||||
|
||||
#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();
|
||||
|
||||
@@ -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
src/Sql/dbo/Views/OrganizationAbilityView.sql
Normal file
28
src/Sql/dbo/Views/OrganizationAbilityView.sql
Normal file
@@ -0,0 +1,28 @@
|
||||
CREATE 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]
|
||||
@@ -96,6 +96,7 @@ public static class OrganizationTestHelpers
|
||||
UseAutomaticUserConfirmation = true,
|
||||
UsePhishingBlocker = true,
|
||||
UseDisableSmAdsForUsers = true,
|
||||
UseMyItems = true,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -365,6 +365,57 @@ public class OrganizationRepositoryTests
|
||||
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,
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user