示例#1
0
        private PackageRegistration CreateOrGetPackageRegistration(User currentUser, PackageMetadata packageMetadata, bool isVerified)
        {
            var packageRegistration = FindPackageRegistrationById(packageMetadata.Id);

            if (packageRegistration != null && !PermissionsService.IsActionAllowed(packageRegistration, currentUser, PackageActions.UploadNewVersion))
            {
                throw new EntityException(Strings.PackageIdNotAvailable, packageMetadata.Id);
            }

            if (packageRegistration == null)
            {
                if (_packageNamingConflictValidator.IdConflictsWithExistingPackageTitle(packageMetadata.Id))
                {
                    throw new EntityException(Strings.NewRegistrationIdMatchesExistingPackageTitle, packageMetadata.Id);
                }

                packageRegistration = new PackageRegistration
                {
                    Id         = packageMetadata.Id,
                    IsVerified = isVerified
                };

                packageRegistration.Owners.Add(currentUser);

                _packageRegistrationRepository.InsertOnCommit(packageRegistration);
            }

            return(packageRegistration);
        }
示例#2
0
        public virtual ActionResult GetPackageOwners(string id, string version)
        {
            var package = _packageService.FindPackageByIdAndVersion(id, version);

            if (package == null)
            {
                return(Json(new { message = Strings.AddOwner_PackageNotFound }));
            }

            if (!PermissionsService.IsActionAllowed(package, HttpContext.User, PackageActions.ManagePackageOwners))
            {
                return(new HttpUnauthorizedResult());
            }

            var currentUser = GetCurrentUser();
            var packageRegistrationOwners  = package.PackageRegistration.Owners;
            var allMatchingNamespaceOwners = package
                                             .PackageRegistration
                                             .ReservedNamespaces
                                             .SelectMany(rn => rn.Owners)
                                             .Distinct();

            var packageAndReservedNamespaceOwners = packageRegistrationOwners.Intersect(allMatchingNamespaceOwners);
            var packageOwnersOnly = packageRegistrationOwners.Except(packageAndReservedNamespaceOwners);

            var owners =
                packageAndReservedNamespaceOwners
                .Select(u => new PackageOwnersResultViewModel(
                            u,
                            currentUser,
                            Url,
                            isPending: false,
                            isNamespaceOwner: true));

            var packageOwnersOnlyResultViewModel =
                packageOwnersOnly
                .Select(u => new PackageOwnersResultViewModel(
                            u,
                            currentUser,
                            Url,
                            isPending: false,
                            isNamespaceOwner: false));

            owners = owners.Union(packageOwnersOnlyResultViewModel);

            var pending =
                _packageOwnershipManagementService.GetPackageOwnershipRequests(package: package.PackageRegistration)
                .Select(r => new PackageOwnersResultViewModel(
                            r.NewOwner,
                            currentUser,
                            Url,
                            isPending: true,
                            isNamespaceOwner: false));

            var result = owners.Union(pending);

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
 public PackageOwnersResultViewModel(User user, User currentUser, UrlHelper url, bool isPending, bool isNamespaceOwner)
 {
     Name                              = user.Username;
     EmailAddress                      = user.EmailAddress;
     ProfileUrl                        = url.User(user, relativeUrl: false);
     ImageUrl                          = GravatarHelper.Url(user.EmailAddress, size: Constants.GravatarImageSize);
     GrantsCurrentUserAccess           = PermissionsService.IsActionAllowed(user, currentUser, AccountActions.ManagePackageOwnershipOnBehalfOf);
     IsCurrentUserMemberOfOrganization = PermissionsService.IsActionAllowed(user, currentUser, AccountActions.DisplayPrivateOrganization);
     Pending                           = isPending;
     IsNamespaceOwner                  = isNamespaceOwner;
 }
示例#4
0
        public virtual ActionResult Packages()
        {
            var user = GetCurrentUser();

            var owners = new List <ListPackageOwnerViewModel> {
                new ListPackageOwnerViewModel
                {
                    Username = "******"
                },
                new ListPackageOwnerViewModel(user)
                {
                    CanManagePackageOwners = PermissionsService.IsActionAllowed(
                        account: user,
                        currentUser: user,
                        action: PackageActions.ManagePackageOwners)
                }
            }.Concat(user.Organizations.Select(o => new ListPackageOwnerViewModel(o.Organization)
            {
                CanManagePackageOwners = PermissionsService.IsActionAllowed(
                    account: o.Organization,
                    currentUser: user,
                    action: PackageActions.ManagePackageOwners)
            }));

            var packages       = _packageService.FindPackagesByAnyMatchingOwner(user, includeUnlisted: true);
            var listedPackages = packages
                                 .Where(p => p.Listed)
                                 .Select(p => new ListPackageItemViewModel(p)).OrderBy(p => p.Id)
                                 .ToList();
            var unlistedPackages = packages
                                   .Where(p => !p.Listed)
                                   .Select(p => new ListPackageItemViewModel(p)).OrderBy(p => p.Id)
                                   .ToList();

            var incoming = _packageOwnerRequestService.GetPackageOwnershipRequests(newOwner: user);
            var outgoing = _packageOwnerRequestService.GetPackageOwnershipRequests(requestingOwner: user);

            var ownerRequests    = new OwnerRequestsViewModel(incoming, outgoing, user, _packageService);
            var reservedPrefixes = new ReservedNamespaceListViewModel(user.ReservedNamespaces);

            var model = new ManagePackagesViewModel
            {
                Owners             = owners,
                ListedPackages     = listedPackages,
                UnlistedPackages   = unlistedPackages,
                OwnerRequests      = ownerRequests,
                ReservedNamespaces = reservedPrefixes
            };

            return(View(model));
        }
        private bool TryGetManagePackageOwnerModel(string id, string username, out ManagePackageOwnerModel model)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException(nameof(username));
            }

            var package = _packageService.FindPackageRegistrationById(id);

            if (package == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_PackageNotFound);
                return(false);
            }
            if (!PermissionsService.IsActionAllowed(package, HttpContext.User, PackageActions.ManagePackageOwners))
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_NotPackageOwner);
                return(false);
            }

            var user = _userService.FindByUsername(username);

            if (user == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_OwnerNotFound);
                return(false);
            }
            if (!user.Confirmed)
            {
                model = new ManagePackageOwnerModel(
                    string.Format(CultureInfo.CurrentCulture, Strings.AddOwner_OwnerNotConfirmed, username));
                return(false);
            }

            var currentUser = _userService.FindByUsername(HttpContext.User.Identity.Name);

            if (currentUser == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_CurrentUserNotFound);
                return(false);
            }

            model = new ManagePackageOwnerModel(package, user, currentUser);
            return(true);
        }
示例#6
0
        private bool TryGetManagePackageOwnerModel(string id, string username, bool isAddOwner, out ManagePackageOwnerModel model)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException(nameof(username));
            }

            var package = _packageService.FindPackageRegistrationById(id);

            if (package == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_PackageNotFound);
                return(false);
            }
            if (!PermissionsService.IsActionAllowed(package, HttpContext.User, PackageActions.ManagePackageOwners))
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_NotPackageOwner);
                return(false);
            }

            var user = _userService.FindByUsername(username);

            if (user == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_OwnerNotFound);
                return(false);
            }
            if (!user.Confirmed)
            {
                model = new ManagePackageOwnerModel(
                    string.Format(CultureInfo.CurrentCulture, Strings.AddOwner_OwnerNotConfirmed, username));
                return(false);
            }

            var isOwner =
                package.Owners.Any(o => o.MatchesUser(user)) ||
                _packageOwnershipManagementService.GetPackageOwnershipRequests(package: package, newOwner: user).Any();

            if (isAddOwner && isOwner)
            {
                model = new ManagePackageOwnerModel(
                    string.Format(CultureInfo.CurrentCulture, Strings.AddOwner_AlreadyOwner, username));
                return(false);
            }

            if (!isAddOwner && !isOwner)
            {
                model = new ManagePackageOwnerModel(
                    string.Format(CultureInfo.CurrentCulture, Strings.RemoveOwner_NotOwner, username));
                return(false);
            }

            var currentUser = _userService.FindByUsername(HttpContext.User.Identity.Name);

            if (currentUser == null)
            {
                model = new ManagePackageOwnerModel(Strings.AddOwner_CurrentUserNotFound);
                return(false);
            }

            model = new ManagePackageOwnerModel(package, user, currentUser);
            return(true);
        }
 public bool IsActionAllowed(IPrincipal principal, PermissionLevel actionPermissionLevel)
 {
     return(PermissionsService.IsActionAllowed(Owners, principal, actionPermissionLevel));
 }
        public virtual ActionResult GetPackageOwners(string id, string version)
        {
            var package = _packageService.FindPackageByIdAndVersion(id, version);

            if (package == null)
            {
                return(Json(new { message = Strings.AddOwner_PackageNotFound }));
            }

            if (!PermissionsService.IsActionAllowed(package, HttpContext.User, PackageActions.ManagePackageOwners))
            {
                return(new HttpUnauthorizedResult());
            }

            var currentUserName            = HttpContext.User.Identity.Name;
            var packageRegistrationOwners  = package.PackageRegistration.Owners;
            var allMatchingNamespaceOwners = package
                                             .PackageRegistration
                                             .ReservedNamespaces
                                             .SelectMany(rn => rn.Owners)
                                             .Distinct();

            var packageAndReservedNamespaceOwners = packageRegistrationOwners.Intersect(allMatchingNamespaceOwners);
            var packageOwnersOnly = packageRegistrationOwners.Except(packageAndReservedNamespaceOwners);

            var owners =
                packageAndReservedNamespaceOwners
                .Select(u => new PackageOwnersResultViewModel(
                            u.Username,
                            u.EmailAddress,
                            isCurrentUser: u.Username == currentUserName,
                            isPending: false,
                            isNamespaceOwner: true));

            var packageOwnersOnlyResultViewModel =
                packageOwnersOnly
                .Select(u => new PackageOwnersResultViewModel(
                            u.Username,
                            u.EmailAddress,
                            isCurrentUser: u.Username == currentUserName,
                            isPending: false,
                            isNamespaceOwner: false));

            owners = owners.Union(packageOwnersOnlyResultViewModel);

            var pending =
                _packageOwnershipManagementService.GetPackageOwnershipRequests(package: package.PackageRegistration)
                .Select(r => new PackageOwnersResultViewModel(
                            r.NewOwner.Username,
                            r.NewOwner.EmailAddress,
                            isCurrentUser: false,
                            isPending: true,
                            isNamespaceOwner: false));

            var result = owners.Union(pending).Select(o => new
            {
                name             = o.Name,
                profileUrl       = Url.User(o.Name, relativeUrl: false),
                imageUrl         = GravatarHelper.Url(o.EmailAddress, size: Constants.GravatarImageSize),
                current          = o.Current,
                pending          = o.Pending,
                isNamespaceOwner = o.IsNamespaceOwner
            });

            return(Json(result, JsonRequestBehavior.AllowGet));
        }