2019-05-10 20:22:35 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Models.View;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class SharePageViewModel : BaseViewModel
|
|
|
|
|
{
|
|
|
|
|
private readonly IDeviceActionService _deviceActionService;
|
|
|
|
|
private readonly ICipherService _cipherService;
|
|
|
|
|
private readonly ICollectionService _collectionService;
|
2022-02-23 20:40:17 +03:00
|
|
|
|
private readonly IOrganizationService _organizationService;
|
2019-05-10 20:22:35 +03:00
|
|
|
|
private readonly IPlatformUtilsService _platformUtilsService;
|
|
|
|
|
private CipherView _cipher;
|
|
|
|
|
private int _organizationSelectedIndex;
|
|
|
|
|
private bool _hasCollections;
|
|
|
|
|
private bool _hasOrganizations;
|
|
|
|
|
private List<Core.Models.View.CollectionView> _writeableCollections;
|
|
|
|
|
|
|
|
|
|
public SharePageViewModel()
|
|
|
|
|
{
|
|
|
|
|
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
|
|
|
|
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
|
2022-02-23 20:40:17 +03:00
|
|
|
|
_organizationService = ServiceContainer.Resolve<IOrganizationService>("organizationService");
|
2019-05-10 20:22:35 +03:00
|
|
|
|
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
|
|
|
|
|
_collectionService = ServiceContainer.Resolve<ICollectionService>("collectionService");
|
|
|
|
|
Collections = new ExtendedObservableCollection<CollectionViewModel>();
|
|
|
|
|
OrganizationOptions = new List<KeyValuePair<string, string>>();
|
2021-08-04 22:46:28 +03:00
|
|
|
|
PageTitle = AppResources.MoveToOrganization;
|
2019-05-10 20:22:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CipherId { get; set; }
|
|
|
|
|
public string OrganizationId { get; set; }
|
|
|
|
|
public List<KeyValuePair<string, string>> OrganizationOptions { get; set; }
|
|
|
|
|
public ExtendedObservableCollection<CollectionViewModel> Collections { get; set; }
|
|
|
|
|
public int OrganizationSelectedIndex
|
|
|
|
|
{
|
|
|
|
|
get => _organizationSelectedIndex;
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (SetProperty(ref _organizationSelectedIndex, value))
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
OrganizationChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public bool HasCollections
|
|
|
|
|
{
|
|
|
|
|
get => _hasCollections;
|
|
|
|
|
set => SetProperty(ref _hasCollections, value);
|
|
|
|
|
}
|
|
|
|
|
public bool HasOrganizations
|
|
|
|
|
{
|
|
|
|
|
get => _hasOrganizations;
|
|
|
|
|
set => SetProperty(ref _hasOrganizations, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task LoadAsync()
|
|
|
|
|
{
|
|
|
|
|
var allCollections = await _collectionService.GetAllDecryptedAsync();
|
|
|
|
|
_writeableCollections = allCollections.Where(c => !c.ReadOnly).ToList();
|
|
|
|
|
|
2022-02-23 20:40:17 +03:00
|
|
|
|
var orgs = await _organizationService.GetAllAsync();
|
2019-05-10 20:22:35 +03:00
|
|
|
|
OrganizationOptions = orgs.OrderBy(o => o.Name)
|
|
|
|
|
.Where(o => o.Enabled && o.Status == OrganizationUserStatusType.Confirmed)
|
|
|
|
|
.Select(o => new KeyValuePair<string, string>(o.Name, o.Id)).ToList();
|
|
|
|
|
HasOrganizations = OrganizationOptions.Any();
|
|
|
|
|
|
|
|
|
|
var cipherDomain = await _cipherService.GetAsync(CipherId);
|
|
|
|
|
_cipher = await cipherDomain.DecryptAsync();
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (OrganizationId == null && OrganizationOptions.Any())
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
OrganizationId = OrganizationOptions.First().Value;
|
|
|
|
|
}
|
|
|
|
|
OrganizationSelectedIndex = string.IsNullOrWhiteSpace(OrganizationId) ? 0 :
|
|
|
|
|
OrganizationOptions.FindIndex(k => k.Value == OrganizationId);
|
|
|
|
|
FilterCollections();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> SubmitAsync()
|
|
|
|
|
{
|
2019-06-27 00:50:57 +03:00
|
|
|
|
var selectedCollectionIds = Collections?.Where(c => c.Checked).Select(c => c.Collection.Id);
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (!selectedCollectionIds?.Any() ?? true)
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, AppResources.SelectOneCollection,
|
|
|
|
|
AppResources.Ok);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (Xamarin.Essentials.Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.None)
|
2019-06-04 05:43:52 +03:00
|
|
|
|
{
|
|
|
|
|
await _platformUtilsService.ShowDialogAsync(AppResources.InternetConnectionRequiredMessage,
|
|
|
|
|
AppResources.InternetConnectionRequiredTitle);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-05-10 20:22:35 +03:00
|
|
|
|
|
|
|
|
|
var cipherDomain = await _cipherService.GetAsync(CipherId);
|
|
|
|
|
var cipherView = await cipherDomain.DecryptAsync();
|
|
|
|
|
|
2019-06-27 00:50:57 +03:00
|
|
|
|
var checkedCollectionIds = new HashSet<string>(selectedCollectionIds);
|
2019-05-10 20:22:35 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
|
|
|
|
|
await _cipherService.ShareWithServerAsync(cipherView, OrganizationId, checkedCollectionIds);
|
|
|
|
|
await _deviceActionService.HideLoadingAsync();
|
2021-08-04 22:46:28 +03:00
|
|
|
|
var movedItemToOrgText = string.Format(AppResources.MovedItemToOrg, cipherView.Name,
|
2022-02-23 20:40:17 +03:00
|
|
|
|
(await _organizationService.GetAsync(OrganizationId)).Name);
|
2021-08-04 22:46:28 +03:00
|
|
|
|
_platformUtilsService.ShowToast("success", null, movedItemToOrgText);
|
2019-05-10 20:22:35 +03:00
|
|
|
|
await Page.Navigation.PopModalAsync();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
catch (ApiException e)
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
await _deviceActionService.HideLoadingAsync();
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (e?.Error != null)
|
2019-10-22 23:37:40 +03:00
|
|
|
|
{
|
|
|
|
|
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
|
|
|
|
|
AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2019-05-10 20:22:35 +03:00
|
|
|
|
}
|
2020-03-28 16:16:28 +03:00
|
|
|
|
catch (System.Exception e)
|
2019-05-11 06:43:35 +03:00
|
|
|
|
{
|
|
|
|
|
await _deviceActionService.HideLoadingAsync();
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (e.Message != null)
|
2019-10-22 23:37:40 +03:00
|
|
|
|
{
|
|
|
|
|
await _platformUtilsService.ShowDialogAsync(e.Message, AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2019-05-11 06:43:35 +03:00
|
|
|
|
}
|
2019-05-10 20:22:35 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OrganizationChanged()
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (OrganizationSelectedIndex > -1)
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
OrganizationId = OrganizationOptions[OrganizationSelectedIndex].Value;
|
|
|
|
|
FilterCollections();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FilterCollections()
|
|
|
|
|
{
|
2020-03-28 16:16:28 +03:00
|
|
|
|
if (OrganizationId == null || !_writeableCollections.Any())
|
2019-05-10 20:22:35 +03:00
|
|
|
|
{
|
|
|
|
|
Collections.ResetWithRange(new List<CollectionViewModel>());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var cols = _writeableCollections.Where(c => c.OrganizationId == OrganizationId)
|
|
|
|
|
.Select(c => new CollectionViewModel { Collection = c }).ToList();
|
|
|
|
|
Collections.ResetWithRange(cols);
|
|
|
|
|
}
|
|
|
|
|
HasCollections = Collections.Any();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|