1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

sync org policies to client devices

This commit is contained in:
Kyle Spearrin 2020-01-28 15:33:32 -05:00
parent 82be2ae06b
commit 725522128c
7 changed files with 63 additions and 2 deletions

View File

@ -25,6 +25,7 @@ namespace Bit.Api.Controllers
private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionCipherRepository _collectionCipherRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IPolicyRepository _policyRepository;
private readonly GlobalSettings _globalSettings;
public SyncController(
@ -34,6 +35,7 @@ namespace Bit.Api.Controllers
ICollectionRepository collectionRepository,
ICollectionCipherRepository collectionCipherRepository,
IOrganizationUserRepository organizationUserRepository,
IPolicyRepository policyRepository,
GlobalSettings globalSettings)
{
_userService = userService;
@ -42,6 +44,7 @@ namespace Bit.Api.Controllers
_collectionRepository = collectionRepository;
_collectionCipherRepository = collectionCipherRepository;
_organizationUserRepository = organizationUserRepository;
_policyRepository = policyRepository;
_globalSettings = globalSettings;
}
@ -62,16 +65,18 @@ namespace Bit.Api.Controllers
IEnumerable<CollectionDetails> collections = null;
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null;
IEnumerable<Policy> policies = null;
if(hasEnabledOrgs)
{
collections = await _collectionRepository.GetManyByUserIdAsync(user.Id);
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id);
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
policies = await _policyRepository.GetManyByUserIdAsync(user.Id);
}
var userTwoFactorEnabled = await _userService.TwoFactorIsEnabledAsync(user);
var response = new SyncResponseModel(_globalSettings, user, userTwoFactorEnabled, organizationUserDetails,
folders, collections, ciphers, collectionCiphersGroupDict, excludeDomains);
folders, collections, ciphers, collectionCiphersGroupDict, excludeDomains, policies);
return response;
}
}

View File

@ -18,7 +18,8 @@ namespace Bit.Core.Models.Api
IEnumerable<CollectionDetails> collections,
IEnumerable<CipherDetails> ciphers,
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersDict,
bool excludeDomains)
bool excludeDomains,
IEnumerable<Policy> policies)
: base("sync")
{
Profile = new ProfileResponseModel(user, organizationUserDetails, userTwoFactorEnabled);
@ -27,6 +28,7 @@ namespace Bit.Core.Models.Api
Collections = collections?.Select(
c => new CollectionDetailsResponseModel(c)) ?? new List<CollectionDetailsResponseModel>();
Domains = excludeDomains ? null : new DomainsResponseModel(user, false);
Policies = policies.Select(p => new PolicyResponseModel(p));
}
public ProfileResponseModel Profile { get; set; }
@ -34,5 +36,6 @@ namespace Bit.Core.Models.Api
public IEnumerable<CollectionDetailsResponseModel> Collections { get; set; }
public IEnumerable<CipherDetailsResponseModel> Ciphers { get; set; }
public DomainsResponseModel Domains { get; set; }
public IEnumerable<PolicyResponseModel> Policies { get; set; }
}
}

View File

@ -10,5 +10,6 @@ namespace Bit.Core.Repositories
{
Task<Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type);
Task<ICollection<Policy>> GetManyByOrganizationIdAsync(Guid organizationId);
Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId);
}
}

View File

@ -44,5 +44,18 @@ namespace Bit.Core.Repositories.SqlServer
return results.ToList();
}
}
public async Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId)
{
using(var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<Policy>(
$"[{Schema}].[{Table}_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
}
}

View File

@ -260,5 +260,6 @@
<Build Include="dbo\Stored Procedures\Policy_Update.sql" />
<Build Include="dbo\Views\PolicyView.sql" />
<Build Include="dbo\Stored Procedures\Policy_ReadByOrganizationIdType.sql" />
<Build Include="dbo\Stored Procedures\Policy_ReadByUserId.sql" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,15 @@
CREATE PROCEDURE [dbo].[Policy_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
P.*
FROM
[dbo].[PolicyView] P
INNER JOIN
[dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId]
WHERE
OU.[UserId] = @UserId
END

View File

@ -557,3 +557,26 @@ BEGIN
[Id] = @Id
END
GO
IF OBJECT_ID('[dbo].[Policy_ReadByUserId]') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[Policy_ReadByUserId]
END
GO
CREATE PROCEDURE [dbo].[Policy_ReadByUserId]
@UserId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
SELECT
P.*
FROM
[dbo].[PolicyView] P
INNER JOIN
[dbo].[OrganizationUser] OU ON P.[OrganizationId] = OU.[OrganizationId]
WHERE
OU.[UserId] = @UserId
END
GO