11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using System . Threading . Tasks ;
45using Bit . Admin . Models ;
56using Bit . Core . Entities ;
67using Bit . Core . Enums ;
78using Bit . Core . Models . Business ;
9+ using Bit . Core . Models . OrganizationConnectionConfigs ;
10+ using Bit . Core . OrganizationFeatures . OrganizationSponsorships . FamiliesForEnterprise . Interfaces ;
811using Bit . Core . Repositories ;
912using Bit . Core . Services ;
1013using Bit . Core . Settings ;
@@ -19,11 +22,14 @@ public class OrganizationsController : Controller
1922 {
2023 private readonly IOrganizationRepository _organizationRepository ;
2124 private readonly IOrganizationUserRepository _organizationUserRepository ;
25+ private readonly IOrganizationConnectionRepository _organizationConnectionRepository ;
26+ private readonly ISelfHostedSyncSponsorshipsCommand _syncSponsorshipsCommand ;
2227 private readonly ICipherRepository _cipherRepository ;
2328 private readonly ICollectionRepository _collectionRepository ;
2429 private readonly IGroupRepository _groupRepository ;
2530 private readonly IPolicyRepository _policyRepository ;
2631 private readonly IPaymentService _paymentService ;
32+ private readonly ILicensingService _licensingService ;
2733 private readonly IApplicationCacheService _applicationCacheService ;
2834 private readonly GlobalSettings _globalSettings ;
2935 private readonly IReferenceEventService _referenceEventService ;
@@ -32,23 +38,29 @@ public class OrganizationsController : Controller
3238 public OrganizationsController (
3339 IOrganizationRepository organizationRepository ,
3440 IOrganizationUserRepository organizationUserRepository ,
41+ IOrganizationConnectionRepository organizationConnectionRepository ,
42+ ISelfHostedSyncSponsorshipsCommand syncSponsorshipsCommand ,
3543 ICipherRepository cipherRepository ,
3644 ICollectionRepository collectionRepository ,
3745 IGroupRepository groupRepository ,
3846 IPolicyRepository policyRepository ,
3947 IPaymentService paymentService ,
48+ ILicensingService licensingService ,
4049 IApplicationCacheService applicationCacheService ,
4150 GlobalSettings globalSettings ,
4251 IReferenceEventService referenceEventService ,
4352 IUserService userService )
4453 {
4554 _organizationRepository = organizationRepository ;
4655 _organizationUserRepository = organizationUserRepository ;
56+ _organizationConnectionRepository = organizationConnectionRepository ;
57+ _syncSponsorshipsCommand = syncSponsorshipsCommand ;
4758 _cipherRepository = cipherRepository ;
4859 _collectionRepository = collectionRepository ;
4960 _groupRepository = groupRepository ;
5061 _policyRepository = policyRepository ;
5162 _paymentService = paymentService ;
63+ _licensingService = licensingService ;
5264 _applicationCacheService = applicationCacheService ;
5365 _globalSettings = globalSettings ;
5466 _referenceEventService = referenceEventService ;
@@ -104,7 +116,8 @@ public async Task<IActionResult> View(Guid id)
104116 policies = await _policyRepository . GetManyByOrganizationIdAsync ( id ) ;
105117 }
106118 var users = await _organizationUserRepository . GetManyDetailsByOrganizationAsync ( id ) ;
107- return View ( new OrganizationViewModel ( organization , users , ciphers , collections , groups , policies ) ) ;
119+ var billingSyncConnection = _globalSettings . EnableCloudCommunication ? await _organizationConnectionRepository . GetByOrganizationIdTypeAsync ( id , OrganizationConnectionType . CloudBillingSync ) : null ;
120+ return View ( new OrganizationViewModel ( organization , billingSyncConnection , users , ciphers , collections , groups , policies ) ) ;
108121 }
109122
110123 [ SelfHosted ( NotSelfHostedOnly = true ) ]
@@ -130,8 +143,9 @@ public async Task<IActionResult> Edit(Guid id)
130143 }
131144 var users = await _organizationUserRepository . GetManyDetailsByOrganizationAsync ( id ) ;
132145 var billingInfo = await _paymentService . GetBillingAsync ( organization ) ;
146+ var billingSyncConnection = _globalSettings . EnableCloudCommunication ? await _organizationConnectionRepository . GetByOrganizationIdTypeAsync ( id , OrganizationConnectionType . CloudBillingSync ) : null ;
133147 return View ( new OrganizationEditModel ( organization , users , ciphers , collections , groups , policies ,
134- billingInfo , _globalSettings ) ) ;
148+ billingInfo , billingSyncConnection , _globalSettings ) ) ;
135149 }
136150
137151 [ HttpPost ]
@@ -164,5 +178,40 @@ public async Task<IActionResult> Delete(Guid id)
164178
165179 return RedirectToAction ( "Index" ) ;
166180 }
181+
182+ public async Task < IActionResult > TriggerBillingSync ( Guid id )
183+ {
184+ var organization = await _organizationRepository . GetByIdAsync ( id ) ;
185+ if ( organization == null )
186+ {
187+ return RedirectToAction ( "Index" ) ;
188+ }
189+ var connection = ( await _organizationConnectionRepository . GetEnabledByOrganizationIdTypeAsync ( id , OrganizationConnectionType . CloudBillingSync ) ) . FirstOrDefault ( ) ;
190+ if ( connection != null )
191+ {
192+ try
193+ {
194+ var config = connection . GetConfig < BillingSyncConfig > ( ) ;
195+ await _syncSponsorshipsCommand . SyncOrganization ( id , config . CloudOrganizationId , connection ) ;
196+ TempData [ "ConnectionActivated" ] = id ;
197+ TempData [ "ConnectionError" ] = null ;
198+ }
199+ catch ( Exception ex )
200+ {
201+ TempData [ "ConnectionError" ] = ex . Message ;
202+ }
203+
204+ if ( _globalSettings . SelfHosted )
205+ {
206+ return RedirectToAction ( "View" , new { id } ) ;
207+ }
208+ else
209+ {
210+ return RedirectToAction ( "Edit" , new { id } ) ;
211+ }
212+ }
213+ return RedirectToAction ( "Index" ) ;
214+ }
215+
167216 }
168217}
0 commit comments