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

95 lines
4.3 KiB
C#
Raw Normal View History

2017-09-20 12:00:33 -04:00
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Models.Api;
using Bit.Core.Services;
using Bit.Core.Repositories;
using Bit.Core.Enums;
2017-10-06 20:47:30 -04:00
using Bit.Core.Exceptions;
2017-11-22 09:53:14 -05:00
using System.Linq;
using Bit.Core.Models.Table;
using System.Collections.Generic;
2021-06-30 09:35:26 +02:00
using Bit.Core.Enums.Provider;
2018-06-11 14:25:53 -04:00
using Bit.Core.Models.Data;
using Bit.Core.Settings;
2017-09-20 12:00:33 -04:00
namespace Bit.Api.Controllers
{
[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;
private readonly ICollectionCipherRepository _collectionCipherRepository;
2017-09-20 12:00:33 -04:00
private readonly IOrganizationUserRepository _organizationUserRepository;
2021-06-30 09:35:26 +02:00
private readonly IProviderUserRepository _providerUserRepository;
2020-01-28 15:33:32 -05:00
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;
public SyncController(
IUserService userService,
IFolderRepository folderRepository,
ICipherRepository cipherRepository,
2017-11-22 09:53:14 -05:00
ICollectionRepository collectionRepository,
ICollectionCipherRepository collectionCipherRepository,
2017-09-20 12:00:33 -04:00
IOrganizationUserRepository organizationUserRepository,
2021-06-30 09:35:26 +02:00
IProviderUserRepository providerUserRepository,
2020-01-28 15:33:32 -05:00
IPolicyRepository policyRepository,
2020-11-18 13:55:50 -05:00
ISendRepository sendRepository,
2017-09-20 12:00:33 -04:00
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;
}
[HttpGet("")]
2020-11-18 13:55:50 -05:00
public async Task<SyncResponseModel> Get([FromQuery] bool excludeDomains = false)
2017-09-20 12:00:33 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-10-06 20:47:30 -04:00
{
throw new BadRequestException("User not found.");
}
2017-09-20 12:00:33 -04:00
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 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);
2017-11-22 09:53:14 -05:00
2018-06-11 14:25:53 -04:00
IEnumerable<CollectionDetails> collections = null;
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphersGroupDict = null;
2020-01-28 15:33:32 -05:00
IEnumerable<Policy> policies = null;
if (hasEnabledOrgs)
2017-11-22 09:53:14 -05:00
{
2018-06-11 14:25:53 -04:00
collections = await _collectionRepository.GetManyByUserIdAsync(user.Id);
2017-11-22 09:53:14 -05:00
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(user.Id);
collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
2020-01-28 15:33:32 -05:00
policies = await _policyRepository.GetManyByUserIdAsync(user.Id);
2017-11-22 09:53:14 -05:00
}
var userTwoFactorEnabled = await _userService.TwoFactorIsEnabledAsync(user);
2018-08-28 17:40:08 -04:00
var response = new SyncResponseModel(_globalSettings, user, userTwoFactorEnabled, organizationUserDetails,
2021-06-30 09:35:26 +02:00
providerUserDetails, folders, collections, ciphers, collectionCiphersGroupDict, excludeDomains,
policies, sends);
2017-09-20 12:00:33 -04:00
return response;
}
}
}