// GET: Users public async Task <IActionResult> Index(string sortOrder, string currentFilter, string searchString, int?pageNumber) { ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParam"] = string.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["LastNameSortParam"] = sortOrder == "lastname" ? "lastname_desc" : "lastname"; ViewData["EmailSortParam"] = sortOrder == "email" ? "email_desc" : "email"; ViewData["AddressSortParam"] = sortOrder == "address" ? "address_desc" : "address"; ViewData["ShopSortParam"] = sortOrder == "shop" ? "shop_desc" : "shop"; if (searchString != null) { pageNumber = 1; } else { searchString = currentFilter; } ViewData["CurrentFilter"] = searchString; var users = await _bll.AppUsers.AllAsync(sortOrder, searchString); var allUsersAndRoles = new List <UserCreateViewModel>(); foreach (var user in users) { var tempUser = DAL.App.EF.Mappers.AppUserMapper.MapFromDAL(AppUserMapper.MapFromBLL(user)); allUsersAndRoles.Add(new UserCreateViewModel() { AppUser = user, AppUserRoles = await _userManager.GetRolesAsync(tempUser) }); } return(View(PaginatedList <UserCreateViewModel> .Create(allUsersAndRoles, pageNumber ?? 1, 10, null))); }
public async Task <IActionResult> Edit(int id, UserCreateViewModel vm) { if (id != vm.AppUser.Id) { return(NotFound()); } var roles = await _userManager.GetRolesAsync(DAL.App.EF.Mappers.AppUserMapper.MapFromDAL(AppUserMapper.MapFromBLL(vm.AppUser))); var user = await _userManager.FindByIdAsync(id.ToString()); if (id != User.GetUserId()) { await _userManager.RemoveFromRolesAsync(user, roles.ToArray()); if (vm.SelectedRoles != null && id != User.GetUserId()) { foreach (var role in vm.SelectedRoles) { await _userManager.AddToRoleAsync(user, role); } } } user.FirstName = vm.AppUser.FirstName; user.LastName = vm.AppUser.LastName; user.ShopId = vm.AppUser.ShopId; user.Aadress = vm.AppUser.Aadress; if (ModelState.IsValid) { await _userManager.UpdateAsync(user); await _bll.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } vm.ShopsSelectList = new SelectList(await _bll.Shops.AllAsync(), nameof(Shop.Id), nameof(Shop.ShopName), vm.AppUser.ShopId); vm.RolesSelectList = new SelectList(await _roleManager.Roles.ToListAsync(), nameof(AppRole.Name), nameof(AppRole.Name)); PreSelect(vm.AppUserRoles, vm.RolesSelectList); return(View()); }
public async Task <ActionResult <PublicApi.v1.DTO.AppUser> > GetSingleUser() { var user = await _bll.AppUsers.GetUserInfo(User.GetUserId()); return(AppUserMapper.MapFromBLL(user)); }