public ListPackageItemRequiredSignerViewModel Setup(
     ListPackageItemRequiredSignerViewModel viewModel,
     Package package,
     User currentUser,
     bool wasAADLoginOrMultiFactorAuthenticated)
 {
     _listPackageItemViewModelFactory.Setup(viewModel, package, currentUser);
     return(SetupInternal(viewModel, package, currentUser, wasAADLoginOrMultiFactorAuthenticated));
 }
        public ListPackageItemRequiredSignerViewModel Create(
            Package package,
            User currentUser,
            bool wasAADLoginOrMultiFactorAuthenticated)
        {
            var viewModel = new ListPackageItemRequiredSignerViewModel();

            return(Setup(viewModel, package, currentUser, wasAADLoginOrMultiFactorAuthenticated));
        }
        private ListPackageItemRequiredSignerViewModel SetupInternal(
            ListPackageItemRequiredSignerViewModel viewModel,
            Package package,
            User currentUser,
            bool wasAADLoginOrMultiFactorAuthenticated)
        {
            if (currentUser == null)
            {
                throw new ArgumentNullException(nameof(currentUser));
            }

            var owners = package.PackageRegistration?.Owners ?? Enumerable.Empty <User>();

            if (owners.Any())
            {
                viewModel.ShowRequiredSigner = true;

                viewModel.CanEditRequiredSigner = CanEditRequiredSigner(package, currentUser, _securityPolicyService, owners);

                var requiredSigner = package.PackageRegistration?.RequiredSigners.FirstOrDefault();

                if (requiredSigner == null)
                {
                    if (owners.Count() == 1)
                    {
                        viewModel.RequiredSigner = GetSignerViewModel(owners.Single());
                    }
                    else
                    {
                        viewModel.RequiredSigner = AnySigner;
                    }
                }
                else
                {
                    viewModel.RequiredSigner = GetSignerViewModel(requiredSigner);
                }

                if (viewModel.CanEditRequiredSigner)
                {
                    if (owners.Count() == 1)
                    {
                        if (requiredSigner != null && requiredSigner != currentUser)
                        {
                            // Suppose users A and B own a package and user A is the required signer.
                            // Then suppose user A removes herself as package owner.
                            // User B must be able to change the required signer.
                            viewModel.AllSigners = new[] { viewModel.RequiredSigner, GetSignerViewModel(currentUser) };
                        }
                        else
                        {
                            viewModel.AllSigners            = new List <SignerViewModel>();
                            viewModel.CanEditRequiredSigner = false;
                            viewModel.ShowTextBox           = true;
                        }
                    }
                    else
                    {
                        viewModel.AllSigners = new[] { AnySigner }.Concat(owners.Select(owner => GetSignerViewModel(owner))).ToList();
                    }
                }
                else
                {
                    viewModel.AllSigners = new[] { viewModel.RequiredSigner };

                    var ownersWithRequiredSignerControl = owners.Where(
                        owner => _securityPolicyService.IsSubscribed(owner, ControlRequiredSignerPolicy.PolicyName));

                    if (owners.Count() == 1)
                    {
                        viewModel.ShowTextBox = true;
                    }
                    else
                    {
                        viewModel.UpdateRequiredSignerMessage(ownersWithRequiredSignerControl.Select(u => u.Username).ToList());
                    }
                }

                viewModel.CanEditRequiredSigner &= wasAADLoginOrMultiFactorAuthenticated;
            }

            viewModel.IsVulnerable = _packageVulnerabilitiesService.IsPackageVulnerable(package);

            return(viewModel);
        }