Ejemplo n.º 1
0
        public ActionResult Edit(string email)
        {
            var member = Membership.GetUser(email);

            if (member != null)
            {
                var user = new UserModel
                               {
                                   Email = member.Email,
                                   CreationDate = member.CreationDate,
                                   LastLoginDate = member.LastLoginDate,
                               };

                var profile = UserProfile.GetUserProfile(user.Email) ?? new UserProfile();

                user.FirstName = profile.FirstName;
                user.LastName = profile.LastName;

                PopulateAssignedRolesData(user);

                return View(user);
            }
            ViewBag.PossibleRoles = Roles.GetAllRoles();
            return View();
        }
Ejemplo n.º 2
0
        private void PopulateAssignedRolesData(UserModel user)
        {
            var allRoles = Roles.GetAllRoles();
            var userRoles = new HashSet<string>(Roles.GetRolesForUser(user.Email));
            var viewModel = allRoles.Select(role => new AssignedRole
            {
                RoleName = role,
                Assigned = userRoles.Contains(role)
            }).ToList();

            ViewBag.PossibleRoles = viewModel;
        }
Ejemplo n.º 3
0
        public ActionResult Index()
        {
            var members = Membership.GetAllUsers();

            var viewModel = new List<UserModel>();

            //Could really use automapper for this
            foreach (var m in members)
            {
                var member = m as MembershipUser;
                var user = new UserModel
                               {
                                   Email = member.Email,
                                   CreationDate = member.CreationDate,
                                   LastLoginDate = member.LastLoginDate,
                                   IsApproved = member.IsApproved
                               };

                viewModel.Add(user);
            }

            return View(viewModel);
        }
Ejemplo n.º 4
0
        private static void UpdateUserRoles(IEnumerable<string> selectedRoles, UserModel user)
        {
            var allRoles = Roles.GetAllRoles();

            if (selectedRoles == null)
            {
                Roles.RemoveUserFromRoles(user.Email, allRoles);
                return;
            }
            var selectedRolesHs = new HashSet<string>(selectedRoles);
            var userRoles = new HashSet<string>(Roles.GetRolesForUser(user.Email));

            foreach (var role in allRoles)
            {
                if (selectedRolesHs.Contains(role))
                {
                    if (!userRoles.Contains(role))
                    {
                        Roles.AddUserToRole(user.Email,role);
                    }
                }
                else
                {
                    if (userRoles.Contains(role))
                    {
                        Roles.RemoveUserFromRole(user.Email, role);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public ActionResult Edit(UserModel user,string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                var member = Membership.GetUser(user.Email);

                //Update User
                if (member != null)
                {
                    member.Email = user.Email;
                    Membership.UpdateUser(member);
                }

                UpdateUserRoles(selectedRoles, user);

                //Save profile
                var profile = UserProfile.GetUserProfile(user.Email);

                profile.FirstName = user.FirstName;
                profile.LastName = user.LastName;

                profile.Save();

                this.FlashInfo("Updated user");
                return RedirectToAction("Index", "Account");
            }
            // If we got this far, something failed, redisplay form
            PopulateAssignedRolesData(user);
            return View(user);
        }