コード例 #1
0
    public static object GetUsernames()
    {
        List <string> usernames = new List <string>();

        foreach (User user in MembershipManager.GetAllUsers())
        {
            Person person = PersonManager.GetPersonByUserId(user.Id);
            if (person != null)
            {
                usernames.Add(person.FirstName + " " + person.LastName + " (" + user.Name + ")");
            }
        }

        return(usernames);
    }
        protected override Func <IEnumerable <System.Web.Security.MembershipUser> > ActWithResult(MembershipManager classUnderTest)
        {
            int totalUsers;

            GetDependency <MembershipProvider>()
            .GetAllUsers(Arg.Any <int>(), Arg.Any <int>(), out totalUsers)
            .Returns(x =>
            {
                var user1 = Substitute.For <System.Web.Security.MembershipUser>();
                user1.UserName.Returns("Foo");

                var user2 = Substitute.For <System.Web.Security.MembershipUser>();
                user2.UserName.Returns("Bar");

                totalUsers = int.MaxValue;

                return(new MembershipUserCollection {
                    user1, user2
                });
            });

            return(() => classUnderTest.GetAllUsers());
        }
コード例 #3
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            JavaScriptSerializer             serializer = new JavaScriptSerializer();
            IList <SiteAccessDetail>         allSites   = serializer.Deserialize <List <SiteAccessDetail> >(listAllSitesDetails.Value);
            IList <PersonRegionAccessDetail> allRegions = serializer.Deserialize <List <PersonRegionAccessDetail> >(listRegionAccessDetails.Value);
            List <PersonType> allGroups = serializer.Deserialize <List <PersonType> >(listGroupsDetails.Value);

            //save personal details
            Person editedPerson = ctrlPD.GetPersonDetails();
            // Problem is that ctrlPD gives you a new person, not necessarily a real existing person.
            Person aPerson = PersonManager.GetPersonById(editedPerson.Id);
            if (aPerson == null)
            {
                // Find a person using their first name and last name.
                IList <Person> persons = PersonManager.SearchPersonsByFirstName(editedPerson.FirstName);
                if (persons.Count == 1)
                {
                    aPerson = persons[0];
                }
                else if (persons.Count > 1)
                {
                    IList <Person> personsLastName = PersonManager.SearchPersonsByLastName(editedPerson.LastName);
                    var            matches         = from x in personsLastName where persons.FirstOrDefault(y => y.Id.Equals(x.Id)) != null select x;
                    if (matches.Count() == 1)
                    {
                        aPerson = matches.First();
                    }
                    // Can't find em.
                }
            }
            if (aPerson == null)
            {
                throw new InvalidOperationException("There is no valid user to operate on");
            }

            editedPerson.Address = PersonManager.GetPersonById(editedPerson.Id).Address;
            IList <User> users = MembershipManager.GetAllUsers();
            User         user  = null;
            foreach (User u in users)
            {
                if (u.Person != null)
                {
                    if (u.Person.Id.Equals(editedPerson.Id))
                    {
                        user = u;
                    }
                }
            }

            string password = ctrlPD.GetPassword();

            if (!string.IsNullOrEmpty(password))
            {
                user.ChangePassword(password, true);
            }

            user.Name = ctrlPD.GetUserId();

            MembershipManager.UpdateUser(user);
            PersonManager.UpdatePerson(editedPerson);

            //save regions if changed
            if (allRegions != null)
            {
                IList <Region>       allRegionsList = SiteManager.GetAllRegions();
                IEnumerable <string> allRegionIds   = from st in allRegionsList select st.Id;

                IList <string> currentRegionIds = new List <string>();

                foreach (PersonRegionAccessDetail item in allRegions)
                {
                    Region tempRegion = SiteManager.GetRegionById(item.LocationId);

                    if (tempRegion != null)
                    {
                        PersonRegion PR = new PersonRegion();
                        PR = PersonManager.GetPersonRegionByPersonAndRegion(editedPerson, tempRegion);
                        //if PS is null that means a new site is being added for the person.
                        if (PR != null)
                        {
                            PR.IsView          = item.IsView;
                            PR.IsAdministrator = item.IsAdmin;
                            PR.IsManager       = item.IsManager;
                            PersonManager.UpdatePersonRegion(PR);
                        }
                        else
                        {
                            PersonManager.AddPersonToRegion(editedPerson.Id, tempRegion.Id, item.IsAdmin, item.IsManager);
                        }
                    }
                    currentRegionIds.Add(item.LocationId);
                }
                //remove regions
                foreach (string regionId in allRegionIds)
                {
                    if (!currentRegionIds.Contains(regionId))
                    {
                        PersonManager.DeletePersonFromRegion(editedPerson.Id, regionId);
                    }
                }
            }
            // save sites if changed
            if (allSites != null)
            {
                IList <Site> personSites = SiteManager.GetSitesByUser(user.Name, true);

                IEnumerable <string> siteIds = from st in personSites select st.Id;

                IList <string> sites = new List <string>();
                foreach (SiteAccessDetail item in allSites)
                {
                    Site tempSite = SiteManager.GetSiteById(item.LocationId);

                    if (tempSite != null)
                    {
                        PersonSite PS = new PersonSite();
                        PS = PersonManager.GetPersonSiteByPersonAndSite(editedPerson, tempSite, true);
                        //if PS is null that means a new site is being added for the person.
                        if (PS != null)
                        {
                            PS.IsAdministrator = item.IsAdmin;
                            PS.IsAssigned      = item.IsView;
                            PS.IsDefault       = item.IsPrimary;
                            PS.IsManager       = item.IsManager;
                            PersonManager.UpdatePersonSite(PS);
                        }
                        else
                        {
                            PersonManager.AddPersonToSite(editedPerson.Id, item.LocationId, item.IsAdmin, item.IsManager, item.IsView, item.IsPrimary);
                        }
                    }
                    sites.Add(item.LocationId);
                }
                //remove sites
                foreach (string siteId in siteIds)
                {
                    if (!sites.Contains(siteId))
                    {
                        PersonManager.DeletePersonFromSite(editedPerson.Id, siteId);
                    }
                }
            }

            // save groups
            foreach (PersonType pType in personTypes)
            {
                if (allGroups.Exists(x => x.Name.Equals(pType.Name)))
                {
                    bool isPersonType = PersonManager.IsPersonInPersonType(editedPerson, pType);

                    if (!isPersonType)
                    {
                        PersonManager.AddPersonToPersonType(editedPerson.Id, pType.Id);
                    }
                }
                else
                {
                    bool isPersonType = PersonManager.IsPersonInPersonType(editedPerson, pType);

                    if (isPersonType)
                    {
                        PersonManager.DeletePersonFromPersonType(editedPerson.Id, pType.Id);
                    }
                }
            }

            //show feedback!
            string   usernameLink = "<a href=../Directory/PersonDetails.aspx?id=" + editedPerson.Id + ">" + user.Name + "</a>";
            Feedback feedback     = new Feedback(BusiBlocksConstants.Blocks.Documents.BlockName, user.GetType().Name, Feedback.Actions.Saved, usernameLink);
            Session["feedback"] = feedback;
            Navigation.Admin_SearchUsers().Redirect(this);
        }
    }