Files
server/src/Api/Controllers/SyncController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
4.2 KiB
C#
Raw Normal View History

using Bit.Api.Models.Response;
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;
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);
var providerUserOrganizationDetails =
await _providerUserRepository.GetManyOrganizationDetailsByUserAsync(user.Id,
2021-06-30 09:35:26 +02:00
ProviderUserStatusType.Confirmed);
var hasEnabledOrgs = organizationUserDetails.Any(o => o.Enabled);
2017-09-20 12:00:33 -04:00
var folders = await _folderRepository.GetManyByUserIdAsync(user.Id);
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);
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
policies = await _policyRepository.GetManyByUserIdAsync(user.Id);
}
2022-08-29 16:06:55 -04:00
2017-09-20 12:00:33 -04:00
var userTwoFactorEnabled = await _userService.TwoFactorIsEnabledAsync(user);
var userHasPremiumFromOrganization = await _userService.HasPremiumFromOrganization(user);
var response = new SyncResponseModel(_globalSettings, user, userTwoFactorEnabled, userHasPremiumFromOrganization, organizationUserDetails,
providerUserDetails, providerUserOrganizationDetails, folders, collections, ciphers,
collectionCiphersGroupDict, excludeDomains, policies, sends);
2017-09-20 12:00:33 -04:00
return response;
}
}