2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Data;
|
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;
|
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;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
public OrganizationRepository(GlobalSettings globalSettings)
|
2018-10-09 17:21:12 -04:00
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2018-10-09 17:21:12 -04:00
|
|
|
|
public OrganizationRepository(string connectionString, string readOnlyConnectionString)
|
|
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2020-08-12 16:38:22 -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
|
|
|
|
|
|
|
|
|
|
public async Task<Organization> GetByLicenseKeyAsync(string licenseKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await connection.QueryAsync<Organization>(
|
|
|
|
|
|
"[dbo].[Organization_ReadByLicenseKey]",
|
|
|
|
|
|
new { LicenseKey = licenseKey },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return result.SingleOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<SelfHostedOrganizationDetails> GetSelfHostedOrganizationDetailsById(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
await using var connection = new SqlConnection(ConnectionString);
|
|
|
|
|
|
|
|
|
|
|
|
return await connection.QueryAsync<string>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadOwnerEmailAddressesById]",
|
|
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|