mirror of
https://github.com/bitwarden/server.git
synced 2025-05-21 11:34:31 -05:00
Use the correct policy enum type (#2045)
Explicit conversion from non-generics to generics doesn't seem to work here, so I moved to using the read methods already on base Policy.
This commit is contained in:
parent
65ac9a527f
commit
9ff82ab176
@ -182,10 +182,10 @@ namespace Bit.Api.Controllers
|
|||||||
throw new UnauthorizedAccessException();
|
throw new UnauthorizedAccessException();
|
||||||
}
|
}
|
||||||
|
|
||||||
var masterPasswordPolicy = await _policyRepository.GetByOrganizationIdTypeAsync<ResetPasswordDataModel>(orgId, PolicyType.MasterPassword);
|
var masterPasswordPolicy = await _policyRepository.GetByOrganizationIdTypeAsync(orgId, PolicyType.ResetPassword);
|
||||||
var useMasterPasswordPolicy = masterPasswordPolicy != null &&
|
var useMasterPasswordPolicy = masterPasswordPolicy != null &&
|
||||||
masterPasswordPolicy.Enabled &&
|
masterPasswordPolicy.Enabled &&
|
||||||
masterPasswordPolicy.DataModel.AutoEnrollEnabled;
|
masterPasswordPolicy.GetDataModel<ResetPasswordDataModel>().AutoEnrollEnabled;
|
||||||
|
|
||||||
if (useMasterPasswordPolicy &&
|
if (useMasterPasswordPolicy &&
|
||||||
string.IsNullOrWhiteSpace(model.ResetPasswordKey))
|
string.IsNullOrWhiteSpace(model.ResetPasswordKey))
|
||||||
|
@ -30,13 +30,4 @@ namespace Bit.Core.Entities
|
|||||||
Data = CoreHelpers.ClassToJsonData(dataModel);
|
Data = CoreHelpers.ClassToJsonData(dataModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Policy<T> : Policy where T : IPolicyDataModel, new()
|
|
||||||
{
|
|
||||||
public T DataModel
|
|
||||||
{
|
|
||||||
get => GetDataModel<T>();
|
|
||||||
set => SetDataModel(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ namespace Bit.Core.Repositories
|
|||||||
public interface IPolicyRepository : IRepository<Policy, Guid>
|
public interface IPolicyRepository : IRepository<Policy, Guid>
|
||||||
{
|
{
|
||||||
Task<Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type);
|
Task<Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type);
|
||||||
Task<Policy<T>> GetByOrganizationIdTypeAsync<T>(Guid organizationId, PolicyType type) where T : IPolicyDataModel, new();
|
|
||||||
Task<ICollection<Policy>> GetManyByOrganizationIdAsync(Guid organizationId);
|
Task<ICollection<Policy>> GetManyByOrganizationIdAsync(Guid organizationId);
|
||||||
Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId);
|
Task<ICollection<Policy>> GetManyByUserIdAsync(Guid userId);
|
||||||
Task<ICollection<Policy>> GetManyByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
Task<ICollection<Policy>> GetManyByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
||||||
|
@ -23,8 +23,6 @@ namespace Bit.Infrastructure.Dapper.Repositories
|
|||||||
: base(connectionString, readOnlyConnectionString)
|
: base(connectionString, readOnlyConnectionString)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
public async Task<Policy<T>> GetByOrganizationIdTypeAsync<T>(Guid organizationId, PolicyType type) where T : IPolicyDataModel, new() =>
|
|
||||||
(Policy<T>)await GetByOrganizationIdTypeAsync(organizationId, type);
|
|
||||||
public async Task<Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
public async Task<Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
||||||
{
|
{
|
||||||
using (var connection = new SqlConnection(ConnectionString))
|
using (var connection = new SqlConnection(ConnectionString))
|
||||||
|
@ -19,8 +19,6 @@ namespace Bit.Infrastructure.EntityFramework.Repositories
|
|||||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Policies)
|
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Policies)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
public async Task<Core.Entities.Policy<T>> GetByOrganizationIdTypeAsync<T>(Guid organizationId, PolicyType type) where T : IPolicyDataModel, new() =>
|
|
||||||
(Core.Entities.Policy<T>)await GetByOrganizationIdTypeAsync(organizationId, type);
|
|
||||||
public async Task<Core.Entities.Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
public async Task<Core.Entities.Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
||||||
{
|
{
|
||||||
using (var scope = ServiceScopeFactory.CreateScope())
|
using (var scope = ServiceScopeFactory.CreateScope())
|
||||||
|
@ -7,6 +7,7 @@ using Bit.Core.Entities;
|
|||||||
using Bit.Core.Models.Data.Organizations.Policies;
|
using Bit.Core.Models.Data.Organizations.Policies;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
using Bit.Test.Common.AutoFixture;
|
using Bit.Test.Common.AutoFixture;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
@ -48,14 +49,14 @@ namespace Bit.Api.Test.Controllers
|
|||||||
public async Task Accept_RequireMasterPasswordReset(Guid orgId, Guid orgUserId,
|
public async Task Accept_RequireMasterPasswordReset(Guid orgId, Guid orgUserId,
|
||||||
OrganizationUserAcceptRequestModel model, User user, SutProvider<OrganizationUsersController> sutProvider)
|
OrganizationUserAcceptRequestModel model, User user, SutProvider<OrganizationUsersController> sutProvider)
|
||||||
{
|
{
|
||||||
var policy = new Policy<ResetPasswordDataModel>
|
var policy = new Policy
|
||||||
{
|
{
|
||||||
Enabled = true,
|
Enabled = true,
|
||||||
DataModel = new ResetPasswordDataModel { AutoEnrollEnabled = true, },
|
Data = CoreHelpers.ClassToJsonData(new ResetPasswordDataModel { AutoEnrollEnabled = true, }),
|
||||||
};
|
};
|
||||||
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(user);
|
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(user);
|
||||||
sutProvider.GetDependency<IPolicyRepository>().GetByOrganizationIdTypeAsync<ResetPasswordDataModel>(orgId,
|
sutProvider.GetDependency<IPolicyRepository>().GetByOrganizationIdTypeAsync(orgId,
|
||||||
Core.Enums.PolicyType.MasterPassword).Returns(policy);
|
Core.Enums.PolicyType.ResetPassword).Returns(policy);
|
||||||
|
|
||||||
await sutProvider.Sut.Accept(orgId, orgUserId, model);
|
await sutProvider.Sut.Accept(orgId, orgUserId, model);
|
||||||
|
|
||||||
|
@ -30,16 +30,6 @@ namespace Bit.Core.Test.AutoFixture.PolicyFixtures
|
|||||||
.With(o => o.OrganizationId, Guid.NewGuid())
|
.With(o => o.OrganizationId, Guid.NewGuid())
|
||||||
.With(o => o.Type, Type)
|
.With(o => o.Type, Type)
|
||||||
.With(o => o.Enabled, true));
|
.With(o => o.Enabled, true));
|
||||||
fixture.Customize<Policy<ResetPasswordDataModel>>(composer => composer
|
|
||||||
.With(o => o.Data, JsonSerializer.Serialize(fixture.Create<ResetPasswordDataModel>()))
|
|
||||||
.With(o => o.OrganizationId, Guid.NewGuid())
|
|
||||||
.With(o => o.Type, Type)
|
|
||||||
.With(o => o.Enabled, true));
|
|
||||||
fixture.Customize<Policy<SendOptionsPolicyData>>(composer => composer
|
|
||||||
.With(o => o.Data, JsonSerializer.Serialize(fixture.Create<ResetPasswordDataModel>()))
|
|
||||||
.With(o => o.OrganizationId, Guid.NewGuid())
|
|
||||||
.With(o => o.Type, Type)
|
|
||||||
.With(o => o.Enabled, true));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ namespace Bit.Core.Test.Services
|
|||||||
|
|
||||||
[Theory, BitAutoData]
|
[Theory, BitAutoData]
|
||||||
public async Task SaveAsync_NewPolicy_Created(
|
public async Task SaveAsync_NewPolicy_Created(
|
||||||
[PolicyFixtures.Policy(PolicyType.MasterPassword)] Policy policy, SutProvider<PolicyService> sutProvider)
|
[PolicyFixtures.Policy(PolicyType.ResetPassword)] Policy policy, SutProvider<PolicyService> sutProvider)
|
||||||
{
|
{
|
||||||
policy.Id = default;
|
policy.Id = default;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user