2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Data;
|
2023-11-23 07:07:37 +10:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2025-02-04 09:02:18 -05:00
|
|
|
|
using Bit.Core.AdminConsole.Enums.Provider;
|
2023-04-14 13:25:56 -04:00
|
|
|
|
using Bit.Core.Auth.Entities;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2022-05-10 17:12:09 -04:00
|
|
|
|
using Bit.Core.Models.Data.Organizations;
|
2025-06-11 14:03:45 +01:00
|
|
|
|
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
using Dapper;
|
2023-01-10 15:58:41 -05:00
|
|
|
|
using Microsoft.Data.SqlClient;
|
2024-01-12 10:38:47 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
2024-08-15 20:47:21 -04:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Repositories;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
public class OrganizationRepository : Repository<Organization, Guid>, IOrganizationRepository
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
2024-01-12 10:38:47 -05:00
|
|
|
|
private readonly ILogger<OrganizationRepository> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public OrganizationRepository(
|
|
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
ILogger<OrganizationRepository> logger)
|
2024-08-15 20:47:21 -04:00
|
|
|
|
: base(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2024-01-12 10:38:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
2024-08-15 20:47:21 -04:00
|
|
|
|
public async Task<Organization?> GetByIdentifierAsync(string identifier)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-08-12 16:38:22 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_ReadByIdentifier]",
|
2020-08-28 12:14:23 -04:00
|
|
|
|
new { Identifier = identifier },
|
2020-08-12 16:38:22 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.SingleOrDefault();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-08-12 16:38:22 -04:00
|
|
|
|
|
2017-08-22 15:27:29 -04:00
|
|
|
|
public async Task<ICollection<Organization>> GetManyByEnabledAsync()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-08-17 00:12:11 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Organization>(
|
2017-08-22 15:27:29 -04:00
|
|
|
|
"[dbo].[Organization_ReadByEnabled]",
|
2017-08-17 00:12:11 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-08-17 00:12:11 -04:00
|
|
|
|
|
2017-03-02 00:15:05 -05:00
|
|
|
|
public async Task<ICollection<Organization>> GetManyByUserIdAsync(Guid userId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Organization>(
|
2017-03-02 21:51:03 -05:00
|
|
|
|
"[dbo].[Organization_ReadByUserId]",
|
2017-03-02 00:15:05 -05:00
|
|
|
|
new { UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-07-10 16:21:18 -04:00
|
|
|
|
|
2018-03-21 17:41:14 -04:00
|
|
|
|
public async Task<ICollection<Organization>> SearchAsync(string name, string userEmail, bool? paid,
|
|
|
|
|
|
int skip, int take)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ReadOnlyConnectionString))
|
2018-03-21 17:41:14 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_Search]",
|
|
|
|
|
|
new { Name = name, UserEmail = userEmail, Paid = paid, Skip = skip, Take = take },
|
2018-10-11 22:21:59 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 120);
|
2018-03-21 17:41:14 -04:00
|
|
|
|
|
2018-03-22 21:10:10 -04:00
|
|
|
|
return results.ToList();
|
2018-03-21 17:41:14 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-03-21 17:41:14 -04:00
|
|
|
|
|
2017-07-10 22:08:52 -04:00
|
|
|
|
public async Task UpdateStorageAsync(Guid id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-07-10 16:21:18 -04:00
|
|
|
|
{
|
|
|
|
|
|
await connection.ExecuteAsync(
|
|
|
|
|
|
"[dbo].[Organization_UpdateStorage]",
|
2017-07-10 22:08:52 -04:00
|
|
|
|
new { Id = id },
|
2017-07-10 16:21:18 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 180);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-12-19 16:02:39 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-12-19 16:02:39 -05:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<OrganizationAbility>(
|
|
|
|
|
|
"[dbo].[Organization_ReadAbilities]",
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|
2023-02-24 07:54:19 +10:00
|
|
|
|
|
2024-08-15 20:47:21 -04:00
|
|
|
|
public async Task<Organization?> GetByLicenseKeyAsync(string licenseKey)
|
2023-02-24 07:54:19 +10:00
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_ReadByLicenseKey]",
|
|
|
|
|
|
new { LicenseKey = licenseKey },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return result.SingleOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-15 20:47:21 -04:00
|
|
|
|
public async Task<SelfHostedOrganizationDetails?> GetSelfHostedOrganizationDetailsById(Guid id)
|
2023-02-24 07:54:19 +10:00
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryMultipleAsync(
|
|
|
|
|
|
"[dbo].[Organization_ReadSelfHostedDetailsById]",
|
|
|
|
|
|
new { Id = id },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
var selfHostOrganization = await result.ReadSingleOrDefaultAsync<SelfHostedOrganizationDetails>();
|
|
|
|
|
|
if (selfHostOrganization == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
selfHostOrganization.OccupiedSeatCount = await result.ReadSingleAsync<int>();
|
|
|
|
|
|
selfHostOrganization.CollectionCount = await result.ReadSingleAsync<int>();
|
|
|
|
|
|
selfHostOrganization.GroupCount = await result.ReadSingleAsync<int>();
|
|
|
|
|
|
selfHostOrganization.OrganizationUsers = await result.ReadAsync<OrganizationUser>();
|
|
|
|
|
|
selfHostOrganization.Policies = await result.ReadAsync<Policy>();
|
|
|
|
|
|
selfHostOrganization.SsoConfig = await result.ReadFirstOrDefaultAsync<SsoConfig>();
|
|
|
|
|
|
selfHostOrganization.ScimConnections = await result.ReadAsync<OrganizationConnection>();
|
|
|
|
|
|
|
|
|
|
|
|
return selfHostOrganization;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-14 11:13:16 +01:00
|
|
|
|
|
|
|
|
|
|
public async Task<ICollection<Organization>> SearchUnassignedToProviderAsync(string name, string ownerEmail, int skip, int take)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ReadOnlyConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_UnassignedToProviderSearch]",
|
|
|
|
|
|
new { Name = name, OwnerEmail = ownerEmail, Skip = skip, Take = take },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure,
|
|
|
|
|
|
commandTimeout: 120);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-23 13:46:29 -04:00
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<string>> GetOwnerEmailAddressesById(Guid organizationId)
|
|
|
|
|
|
{
|
2024-01-12 10:38:47 -05:00
|
|
|
|
_logger.LogInformation("AC-1758: Executing GetOwnerEmailAddressesById (Dapper)");
|
|
|
|
|
|
|
2023-10-23 13:46:29 -04:00
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
return await connection.QueryAsync<string>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadOwnerEmailAddressesById]",
|
|
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
2024-09-11 11:29:57 +01:00
|
|
|
|
|
2024-10-17 16:06:32 +01:00
|
|
|
|
public async Task<ICollection<Organization>> GetByVerifiedUserEmailDomainAsync(Guid userId)
|
2024-09-11 11:29:57 +01:00
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_ReadByClaimedUserEmailDomain]",
|
|
|
|
|
|
new { UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
2024-10-17 16:06:32 +01:00
|
|
|
|
return result.ToList();
|
2024-09-11 11:29:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-04 09:02:18 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task<ICollection<Organization>> GetAddableToProviderByUserIdAsync(
|
|
|
|
|
|
Guid userId,
|
|
|
|
|
|
ProviderType providerType)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadAddableToProviderByUserId]",
|
|
|
|
|
|
new { UserId = userId, ProviderType = providerType },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return result.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-31 08:33:57 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task<ICollection<Organization>> GetManyByIdsAsync(IEnumerable<Guid> ids)
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
return (await connection.QueryAsync<Organization>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadManyByIds]",
|
|
|
|
|
|
new { OrganizationIds = ids.ToGuidIdArrayTVP() },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure))
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
2025-06-11 14:03:45 +01:00
|
|
|
|
|
|
|
|
|
|
public async Task<OrganizationSeatCounts> GetOccupiedSeatCountByOrganizationIdAsync(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryAsync<OrganizationSeatCounts>(
|
|
|
|
|
|
"[dbo].[Organization_ReadOccupiedSeatCountByOrganizationId]",
|
|
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return result.SingleOrDefault() ?? new OrganizationSeatCounts();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-31 07:54:51 -05:00
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<Organization>> GetOrganizationsForSubscriptionSyncAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
return await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_GetOrganizationsForSubscriptionSync]",
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateSuccessfulOrganizationSyncStatusAsync(IEnumerable<Guid> successfulOrganizations, DateTime syncDate)
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
await connection.ExecuteAsync("[dbo].[Organization_UpdateSubscriptionStatus]",
|
|
|
|
|
|
new
|
|
|
|
|
|
{
|
|
|
|
|
|
SuccessfulOrganizations = successfulOrganizations.ToGuidIdArrayTVP(),
|
|
|
|
|
|
SyncDate = syncDate
|
|
|
|
|
|
},
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task IncrementSeatCountAsync(Guid organizationId, int increaseAmount, DateTime requestDate)
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
await connection.ExecuteAsync("[dbo].[Organization_IncrementSeatCount]",
|
|
|
|
|
|
new { OrganizationId = organizationId, SeatsToAdd = increaseAmount, RequestDate = requestDate },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|