示例#1
0
            private Task <JsonResult> InvokeAddMember(
                OrganizationsController controller,
                Organization account,
                string memberName         = defaultMemberName,
                bool isAdmin              = false,
                EntityException exception = null)
            {
                // Arrange
                controller.SetCurrentUser(GetCurrentUser(controller));

                var userService = GetMock <IUserService>();

                userService.Setup(u => u.FindByUsername(account.Username))
                .Returns(account as User);
                var setup = userService.Setup(u => u.AddMembershipRequestAsync(It.IsAny <Organization>(), memberName, isAdmin));

                if (exception != null)
                {
                    setup.Throws(exception);
                }
                else
                {
                    var request = new MembershipRequest
                    {
                        Organization      = account,
                        NewMember         = new User(memberName),
                        IsAdmin           = isAdmin,
                        ConfirmationToken = "token"
                    };
                    setup.Returns(Task.FromResult(request)).Verifiable();
                }

                // Act
                return(controller.AddMember(account.Username, memberName, isAdmin));
            }
示例#2
0
        private OrganizationMemberViewModel ToOrganizationMemberViewModel(MembershipRequest membershipRequest)
        {
            var avatarUrl = Url.Avatar(
                membershipRequest.NewMember,
                _features.IsGravatarProxyEnabled(),
                GalleryConstants.GravatarElementSize);

            return(new OrganizationMemberViewModel(membershipRequest, avatarUrl));
        }
        public OrganizationMemberViewModel(MembershipRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var member = request.NewMember;

            if (member == null)
            {
                throw new ArgumentNullException(nameof(request.NewMember));
            }

            Username    = member.Username;
            IsAdmin     = request.IsAdmin;
            Pending     = true;
            GravatarUrl = GravatarHelper.Url(member.EmailAddress, Constants.GravatarElementSize);
        }
示例#4
0
        public async Task <MembershipRequest> AddMembershipRequestAsync(Organization organization, string memberName, bool isAdmin)
        {
            organization = organization ?? throw new ArgumentNullException(nameof(organization));

            var membership = FindMembershipByUsername(organization, memberName);

            if (membership != null)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture,
                                                        Strings.AddMember_AlreadyAMember, memberName));
            }

            var request = FindMembershipRequestByUsername(organization, memberName);

            if (request != null)
            {
                // If there is already an existing request, return it.
                // If the existing request grants collaborator but we are trying to create a request that grants admin, update the request to grant admin.
                request.IsAdmin = isAdmin || request.IsAdmin;
                await EntitiesContext.SaveChangesAsync();

                return(request);
            }

            if (Regex.IsMatch(memberName, GalleryConstants.EmailValidationRegex, RegexOptions.None, GalleryConstants.EmailValidationRegexTimeout))
            {
                throw new EntityException(Strings.AddMember_NameIsEmail);
            }

            var member = FindByUsername(memberName);

            if (member == null)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture,
                                                        Strings.AddMember_UserNotFound, memberName));
            }

            if (!member.Confirmed)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture,
                                                        Strings.AddMember_UserNotConfirmed, memberName));
            }

            if (member is Organization)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture,
                                                        Strings.AddMember_UserIsOrganization, memberName));
            }

            // Ensure that the new member meets the AAD tenant policy for this organization.
            var policyResult = await SecurityPolicyService.EvaluateOrganizationPoliciesAsync(
                SecurityPolicyAction.JoinOrganization, organization, member);

            if (policyResult != SecurityPolicyResult.SuccessResult)
            {
                throw new EntityException(policyResult.ErrorMessage);
            }

            request = new MembershipRequest()
            {
                Organization      = organization,
                NewMember         = member,
                IsAdmin           = isAdmin,
                ConfirmationToken = Crypto.GenerateToken(),
                RequestDate       = DateTime.UtcNow,
            };
            organization.MemberRequests.Add(request);

            await EntitiesContext.SaveChangesAsync();

            return(request);
        }
 public OrganizationMemberViewModel(MembershipRequest request)
     : this(request?.NewMember)
 {
     IsAdmin = request.IsAdmin;
     Pending = true;
 }
 public ManageOrganizationsItemViewModel(MembershipRequest request, IPackageService packageService)
     : this(request.Organization, request.IsAdmin, packageService)
 {
     IsPendingRequest  = true;
     ConfirmationToken = request.ConfirmationToken;
 }
示例#7
0
 public OrganizationMemberViewModel(MembershipRequest request, string gravatarUrl)
     : this(request?.NewMember, gravatarUrl)
 {
     IsAdmin = request.IsAdmin;
     Pending = true;
 }