Ejemplo n.º 1
0
        public async Task <IActionResult> AssignUserToRole(AssignUserToRoleViewModel model)
        {
            ApplicationUser user = await userManager.FindByIdAsync(model.UserId);

            ApplicationRole role = await roleManager.FindByIdAsync(model.RoleId);

            await userManager.AddToRoleAsync(user, role.Name);

            return(Ok());
        }
        public ActionResult AssignUserToRole(string id) //<==(The "id" has to come along with data, to identify the role I just clicked on to assign user to Roles)

        {
            var userdb = db.Users.Find(id); //<==(go to the database find users and then put in a list )


            var model = new AssignUserToRoleViewModel();

            model.Id            = id;
            model.Name          = userdb.FirstName;
            model.SelectedRoles = helper.ListUserRoles(id).ToArray(); //<==(give me a list of all users not in a role and then push them into an array)
            model.Roles         = new MultiSelectList(db.Roles, "Name", "Name", model.SelectedRoles);

            return(View(model));
        }
        //POST: Assign/UsersToRoles EDIT
        public ActionResult AssignUserToRole(AssignUserToRoleViewModel model)

        {
            var userdb = db.Users.Find(model.Id);

            foreach (var roles in db.Roles.Select(r => r.Name).ToList())
            {
                helper.RemoveUserFromRole(userdb.Id, roles);
            }
            foreach (var assinRoles in model.SelectedRoles)
            {
                helper.AddUserToRole(userdb.Id, assinRoles);
            }

            return(RedirectToAction("Roles", "Admin"));
        }