示例#1
0
      //GET: Admin/RolesModels/Edit/5
      public async Task <IActionResult> Edit(string id)
      {
          if (id == null)
          {
              return(NotFound());
          }

          ApplicationUser userInfo = await _userManager.FindByIdAsync(id);

          IList <string> currentRoles = await _userManager.GetRolesAsync(userInfo);

          List <SelectListItem> roleSelector = new List <SelectListItem>();
          var roles = await _roleManager.Roles.ToListAsync();

          roleSelector = SharedMethods.SharedMethods.RolesListToSelectListAsync(roleSelector, roles);

          adminUserEditViewModel uservm = new adminUserEditViewModel {
              Roles        = roleSelector,
              CurrentRoles = currentRoles,
              UserName     = userInfo.UserName,
              GivenName    = userInfo.GivenName,
              FamilyName   = userInfo.FamilyName,
              DonNille     = userInfo.DonNille,
              Id           = userInfo.Id,
              Cantons      = new Teamm(),
              Canton       = userInfo.Canton
              , Email      = userInfo.Email,
          };

          if (userInfo == null)
          {
              return(NotFound());
          }
          return(View(uservm));
      }
示例#2
0
        public async Task <IActionResult> Edit(string id, [Bind("ID,Email,Username,Roles")] adminUserEditViewModel adminUserEdit, IFormCollection form)
        {
            if (id != adminUserEdit.ID)
            {
                return(NotFound());
            }
            ;
            var CurrentUser = await _context.Users.SingleOrDefaultAsync(m => m.Id == id);


            if (adminUserEdit != null && ModelState.IsValid)
            {
                try
                { //optimize
                    //remove existing roles
                    // TODO Check and skip over roles that exist already and are selected by editing user.
                    var yes = await _context.UserRoles.Where(x => x.UserId == id).ToListAsync();

                    foreach (var roleEntry in yes)
                    {
                        var ff = _context.UserRoles.Remove(roleEntry);
                    }

                    foreach (var roles in form["Roles"])
                    {
                        await _userManager.AddToRoleAsync(CurrentUser, roles);
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserExists(adminUserEdit.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(adminUserEdit));
        }
示例#3
0
      public async Task <IActionResult> Edit(IFormCollection form, string id, [Bind("Id,CurrentRoles,Email,Canton,GivenName,FamilyName,DonNille")] adminUserEditViewModel input)
      {
          Debug.WriteLine(form["Roles"]);
          if (id != input.Id)
          {
              return(NotFound());
          }

          if (ModelState.IsValid)
          {
              try
              {
                  var user = await _userManager.FindByIdAsync(id);

                  user.Id         = input.Id;
                  user.Email      = input.Email;
                  user.Canton     = input.Canton;
                  user.GivenName  = input.GivenName;
                  user.FamilyName = input.FamilyName;
                  user.DonNille   = input.DonNille;

                  _context.Update(user);

                  _context.UserRoles.RemoveRange(_context.UserRoles.Where(x => x.UserId == id));
                  await _context.SaveChangesAsync();

                  await _userManager.AddToRolesAsync(user, form["Roles"]);
              }
              catch (DbUpdateConcurrencyException)
              {
                  if (!UserExists(input.Id))
                  {
                      return(NotFound());
                  }
                  else
                  {
                      throw;
                  }
              }
              return(RedirectToAction(nameof(Index)));
          }
          return(View(input));
      }
示例#4
0
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            //get all roles names. No ID needed because roles manager uses name
            var Rolesz =
                (from ro in _context.Roles
                 select  new { RoleName = ro.Name });
            List <SelectListItem> RolesList = new List <SelectListItem>();

            //populate rolesslist
            foreach (var item in Rolesz)
            {
                RolesList.Add(new SelectListItem {
                    Value = item.RoleName, Text = item.RoleName
                });
            }
            // get info of selected user, perhaps should rename field.
            var CurrentUser = await _context.Users.SingleOrDefaultAsync(m => m.Id == id);

            if (CurrentUser == null)
            {
                return(NotFound());
            }
            //create edit view model and map fields

            adminUserEditViewModel EditViewModel = new adminUserEditViewModel()
            {
                Email    = CurrentUser.Email,
                Username = CurrentUser.UserName,
                ID       = CurrentUser.Id,
                Roles    = RolesList
            };

            return(View(EditViewModel));
        }