예제 #1
0
        void UsersController_RecordCreated(object sender, CrudEventArgs e)
        {
            var user = e.Item as User;

            WebSecurity.CreateAccount(user.Username, _dataConfig.Value.DefaultUserPassword);
            _roles.AddUserToRole(user.Username, RoleNames.DefaultUsers);
        }
        public ActionResult Edit(UserEdit user, string[] userRoleCheck)
        {
            if (ModelState.IsValid)
            {
                if (webSecurity.CurrentUser.Identity.Name.Equals(user.username) || webSecurity.CurrentUser.IsInRole("Administrator"))
                {
                    User userLink = Request <User> .GetAuthItemById(user.ID, "A", "B");

                    userLink.realName = user.realName;
                    Request <User> .UpdateAuthItem(Request <User> .GetAuthItemById(user.ID, "A", "B"), userLink, "A", "B");


                    if (webSecurity.CurrentUser.IsInRole("Administrator"))
                    {
                        List <String> rolesList    = roles.GetAllRoles().ToList();
                        List <string> usersRoles   = roles.GetRolesForUser(user.username).ToList();
                        List <string> userNewRoles = userRoleCheck.ToList();
                        foreach (string role in rolesList)
                        {
                            if (usersRoles.Contains(role))
                            {
                                if (!userNewRoles.Contains(role))
                                {
                                    roles.RemoveUserFromRole(user.username, role);
                                }
                            }
                            else
                            {
                                if (userNewRoles.Contains(role))
                                {
                                    roles.AddUserToRole(user.username, role);
                                }
                            }
                        }
                        return(RedirectToAction("Index"));
                    }
                    return(RedirectToAction("Manage", "Accounts"));
                }
            }
            user = new UserEdit(Request <User> .GetAuthItemById(user.ID, "A", "B"));
            if (webSecurity.CurrentUser.IsInRole("Administrator"))
            {
                ViewBag.AllRoles  = roles.GetAllRoles().ToList();
                ViewBag.UserRoles = roles.GetRolesForUser(user.username).ToList();
            }
            return(View(user));
        }
예제 #3
0
        public ActionResult Edit(User user, string[] userRoleCheck)
        {
            if (ModelState.IsValid)
            {
                if (webSecurity.CurrentUser.Identity.Name.Equals(user.username) || webSecurity.CurrentUser.IsInRole("Administrator"))
                {
                    users.UpdateValues(users.Find(user.ID), user);
                    users.SaveChanges();

                    if (webSecurity.CurrentUser.IsInRole("Administrator"))
                    {
                        List <String> rolesList    = roles.GetAllRoles().ToList();
                        List <string> usersRoles   = roles.GetRolesForUser(user.username).ToList();
                        List <string> userNewRoles = userRoleCheck.ToList();
                        foreach (string role in rolesList)
                        {
                            if (usersRoles.Contains(role))
                            {
                                if (!userNewRoles.Contains(role))
                                {
                                    roles.RemoveUserFromRole(user.username, role);
                                }
                            }
                            else
                            {
                                if (userNewRoles.Contains(role))
                                {
                                    roles.AddUserToRole(user.username, role);
                                }
                            }
                        }
                        return(RedirectToAction("Index"));
                    }
                    return(RedirectToAction("Manage", "Accounts"));
                }
            }
            return(View(user));
        }