2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Api.Models.Response;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
using Bit.Core.Enums.Provider;
|
2017-10-06 20:47:30 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
2018-06-11 14:25:53 -04:00
|
|
|
|
using Bit.Core.Models.Data;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-09-20 12:00:33 -04:00
|
|
|
|
[Route("sync")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class SyncController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IUserService _userService;
|
|
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
|
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
2017-11-22 09:53:14 -05:00
|
|
|
|
private readonly ICollectionRepository _collectionRepository;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
private readonly ICollectionCipherRepository _collectionCipherRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly IProviderUserRepository _providerUserRepository;
|
|
|
|
|
|
private readonly IPolicyRepository _policyRepository;
|
2020-11-18 13:55:50 -05:00
|
|
|
|
private readonly ISendRepository _sendRepository;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-09-20 12:00:33 -04:00
|
|
|
|
public SyncController(
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IFolderRepository folderRepository,
|
|
|
|
|
|
ICipherRepository cipherRepository,
|
|
|
|
|
|
ICollectionRepository collectionRepository,
|
|
|
|
|
|
ICollectionCipherRepository collectionCipherRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
|
|
|
|
IProviderUserRepository providerUserRepository,
|
2020-01-28 15:33:32 -05:00
|
|
|
|
IPolicyRepository policyRepository,
|
2017-09-20 12:00:33 -04:00
|
|
|
|
ISendRepository sendRepository,
|
|
|
|
|
|
GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_folderRepository = folderRepository;
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
2017-11-22 09:53:14 -05:00
|
|
|
|
_collectionRepository = collectionRepository;
|
|
|
|
|
|
_collectionCipherRepository = collectionCipherRepository;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
2021-06-30 09:35:26 +02:00
|
|
|
|
_providerUserRepository = providerUserRepository;
|
2020-01-28 15:33:32 -05:00
|
|
|
|
_policyRepository = policyRepository;
|
2020-11-18 13:55:50 -05:00
|
|
|
|
_sendRepository = sendRepository;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-09-20 12:00:33 -04:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<SyncResponseModel> Get([FromQuery] bool excludeDomains = false)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-09-20 12:00:33 -04:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("User not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id,
|
|
|
|
|
|
OrganizationUserStatusType.Confirmed);
|
2021-06-30 09:35:26 +02:00
|
|
|
|
var providerUserDetails = await _providerUserRepository.GetManyDetailsByUserAsync(user.Id,
|
|
|
|
|
|
ProviderUserStatusType.Confirmed);
|
2021-07-08 17:05:32 +02:00
|
|
|
|
var providerUserOrganizationDetails =
|
|
|
|
|
|
await _providerUserRepository.GetManyOrganizationDetailsByUserAsync(user.Id,
|
2021-06-30 09:35:26 +02:00
|
|
|
|
ProviderUserStatusType.Confirmed);
|
2018-04-24 12:48:43 -04:00
|
|
|
|
var hasEnabledOrgs = organizationUserDetails.Any(o => o.Enabled);
|
2017-09-20 12:00:33 -04:00
|
|
|
|
var folders = await _folderRepository.GetManyByUserIdAsync(user.Id);
|
2018-04-24 12:48:43 -04:00
|
|
|
|
var ciphers = await _cipherRepository.GetManyByUserIdAsync(user.Id, hasEnabledOrgs);
|
2020-11-18 13:55:50 -05:00
|
|
|
|
var sends = await _sendRepository.GetManyByUserIdAsync(user.Id);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2018-06-11 14:25:53 -04:00
|
|
|
|
IEnumerable<CollectionDetails> collections = null;
|
2017-09-20 12:00:33 -04:00
|
|
|
|
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null;
|
|
|
|
|
|
IEnumerable<Policy> policies = null;
|
|
|
|
|
|
if (hasEnabledOrgs)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-09-20 12:00:33 -04:00
|
|
|
|
collections = await _collectionRepository.GetManyByUserIdAsync(user.Id);
|
|
|
|
|
|
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id);
|
2017-11-24 09:28:38 -05:00
|
|
|
|
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
|
|
|
|
|
|
policies = await _policyRepository.GetManyByUserIdAsync(user.Id);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-09-20 12:00:33 -04:00
|
|
|
|
var userTwoFactorEnabled = await _userService.TwoFactorIsEnabledAsync(user);
|
2022-06-17 06:30:50 +10:00
|
|
|
|
var userHasPremiumFromOrganization = await _userService.HasPremiumFromOrganization(user);
|
|
|
|
|
|
var response = new SyncResponseModel(_globalSettings, user, userTwoFactorEnabled, userHasPremiumFromOrganization, organizationUserDetails,
|
2021-07-08 17:05:32 +02:00
|
|
|
|
providerUserDetails, providerUserOrganizationDetails, folders, collections, ciphers,
|
2018-12-19 10:47:53 -05:00
|
|
|
|
collectionCiphersGroupDict, excludeDomains, policies, sends);
|
2017-09-20 12:00:33 -04:00
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|