6
6
using Bit . Core . AdminConsole . Entities ;
7
7
using Bit . Core . AdminConsole . Enums ;
8
8
using Bit . Core . AdminConsole . OrganizationFeatures . OrganizationDomains . Interfaces ;
9
+ using Bit . Core . AdminConsole . OrganizationFeatures . Policies ;
9
10
using Bit . Core . AdminConsole . Repositories ;
10
- using Bit . Core . AdminConsole . Services ;
11
11
using Bit . Core . Auth . Models . Business . Tokenables ;
12
12
using Bit . Core . Context ;
13
13
using Bit . Core . Enums ;
@@ -28,7 +28,6 @@ namespace Bit.Api.AdminConsole.Controllers;
28
28
public class PoliciesController : Controller
29
29
{
30
30
private readonly IPolicyRepository _policyRepository ;
31
- private readonly IPolicyService _policyService ;
32
31
private readonly IOrganizationUserRepository _organizationUserRepository ;
33
32
private readonly IUserService _userService ;
34
33
private readonly ICurrentContext _currentContext ;
@@ -37,21 +36,21 @@ public class PoliciesController : Controller
37
36
private readonly IDataProtectorTokenFactory < OrgUserInviteTokenable > _orgUserInviteTokenDataFactory ;
38
37
private readonly IFeatureService _featureService ;
39
38
private readonly IOrganizationHasVerifiedDomainsQuery _organizationHasVerifiedDomainsQuery ;
39
+ private readonly ISavePolicyCommand _savePolicyCommand ;
40
40
41
41
public PoliciesController (
42
42
IPolicyRepository policyRepository ,
43
- IPolicyService policyService ,
44
43
IOrganizationUserRepository organizationUserRepository ,
45
44
IUserService userService ,
46
45
ICurrentContext currentContext ,
47
46
GlobalSettings globalSettings ,
48
47
IDataProtectionProvider dataProtectionProvider ,
49
48
IDataProtectorTokenFactory < OrgUserInviteTokenable > orgUserInviteTokenDataFactory ,
50
49
IFeatureService featureService ,
51
- IOrganizationHasVerifiedDomainsQuery organizationHasVerifiedDomainsQuery )
50
+ IOrganizationHasVerifiedDomainsQuery organizationHasVerifiedDomainsQuery ,
51
+ ISavePolicyCommand savePolicyCommand )
52
52
{
53
53
_policyRepository = policyRepository ;
54
- _policyService = policyService ;
55
54
_organizationUserRepository = organizationUserRepository ;
56
55
_userService = userService ;
57
56
_currentContext = currentContext ;
@@ -62,6 +61,7 @@ public PoliciesController(
62
61
_orgUserInviteTokenDataFactory = orgUserInviteTokenDataFactory ;
63
62
_featureService = featureService ;
64
63
_organizationHasVerifiedDomainsQuery = organizationHasVerifiedDomainsQuery ;
64
+ _savePolicyCommand = savePolicyCommand ;
65
65
}
66
66
67
67
[ HttpGet ( "{type}" ) ]
@@ -178,25 +178,20 @@ public async Task<PolicyResponseModel> GetMasterPasswordPolicy(Guid orgId)
178
178
}
179
179
180
180
[ HttpPut ( "{type}" ) ]
181
- public async Task < PolicyResponseModel > Put ( string orgId , int type , [ FromBody ] PolicyRequestModel model )
181
+ public async Task < PolicyResponseModel > Put ( Guid orgId , PolicyType type , [ FromBody ] PolicyRequestModel model )
182
182
{
183
- var orgIdGuid = new Guid ( orgId ) ;
184
- if ( ! await _currentContext . ManagePolicies ( orgIdGuid ) )
183
+ if ( ! await _currentContext . ManagePolicies ( orgId ) )
185
184
{
186
185
throw new NotFoundException ( ) ;
187
186
}
188
- var policy = await _policyRepository . GetByOrganizationIdTypeAsync ( new Guid ( orgId ) , ( PolicyType ) type ) ;
189
- if ( policy == null )
190
- {
191
- policy = model . ToPolicy ( orgIdGuid ) ;
192
- }
193
- else
187
+
188
+ if ( type != model . Type )
194
189
{
195
- policy = model . ToPolicy ( policy ) ;
190
+ throw new BadRequestException ( "Mismatched policy type" ) ;
196
191
}
197
192
198
- var userId = _userService . GetProperUserId ( User ) ;
199
- await _policyService . SaveAsync ( policy , userId ) ;
193
+ var policyUpdate = await model . ToPolicyUpdateAsync ( orgId , _currentContext ) ;
194
+ var policy = await _savePolicyCommand . SaveAsync ( policyUpdate ) ;
200
195
return new PolicyResponseModel ( policy ) ;
201
196
}
202
197
}
0 commit comments