コード例 #1
0
        public ActionResult EditUser(string Id)
        {
            var model   = new UserEditVM();
            var context = new ApplicationDbContext();
            var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var userMgr = new UserManager <AppUser>(new UserStore <AppUser>(context));
            var list    = roleMgr.Roles.ToList();

            model.User = userMgr.FindById(Id);
            var oldRoleName = userMgr.GetRoles(Id).ToString();

            model.SelectedRoleID = oldRoleName;

            model.RoleList = list.Select(x => new SelectListItem()
            {
                Text     = x.Name,
                Value    = x.Id.ToString(),
                Selected = model.User.Roles.Any(s => s.UserId == model.User.Id)
            }).ToList();

            var role = roleMgr.FindById(model.User.Roles.FirstOrDefault(s => s.UserId == model.User.Id).RoleId);

            model.SelectedRoleID = role.Id;
            return(View(model));
        }
コード例 #2
0
        // GET: User/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var user = await context.Users
                       .FirstOrDefaultAsync(u => u.Id == id);

            if (user == null)
            {
                return(NotFound());
            }

            UserEditVM model = new UserEditVM
            {
                Id       = user.Id,
                Login    = user.Login,
                Password = user.Password,
                Role     = user.RoleId,
            };

            ViewData["Roles"] = CreateRolesDropDownList();

            return(View(model));
        }
コード例 #3
0
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            User user = await _userManager.FindByIdAsync(id);

            if (user == await _userManager.GetUserAsync(User))
            {
                return(NotFound());
            }
            if (user == null)
            {
                return(NotFound());
            }
            if (user.isDeleted == true)
            {
                return(NotFound());
            }

            ViewBag.ActiveRole = (await _userManager.GetRolesAsync(user))[0];

            UserEditVM userVM = new UserEditVM
            {
                Fullname = user.Fullname,
                Username = user.UserName,
                Email    = user.Email,
                Role     = (await _userManager.GetRolesAsync(user))[0],
                Course   = _db.Courses.FirstOrDefault(c => c.Id == user.CourseID),
                Courses  = _db.Courses
            };

            return(View(userVM));
        }
コード例 #4
0
        public ActionResult Edit(int?id)
        {
            UsersService userService = new UsersService();
            UserEditVM   model       = new UserEditVM();

            TryUpdateModel(model);

            User user;

            if (!id.HasValue)
            {
                user = new User();
            }
            else
            {
                user = userService.GetByID(id.Value);
                if (user == null)
                {
                    return(this.RedirectToAction(c => c.List(1)));
                }
            }


            Mapper.Map(user, model);

            return(View(model));
        }
コード例 #5
0
        public ActionResult Edit()
        {
            UsersService userService = new UsersService();
            UserEditVM   model       = new UserEditVM();

            TryUpdateModel(model);
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User u;

            if (model.ID != 0)
            {
                u = userService.GetByID(model.ID);
            }
            else
            {
                u = new User();
            }

            if (u == null)
            {
                return(this.RedirectToAction(c => c.List(1)));
            }

            Mapper.Map(model, u);

            userService.Save(u);
            return(RedirectToAction("List"));
        }
コード例 #6
0
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            IdentityUser user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                return(NotFound());
            }

            IList <string> existingUserRoles = (await _userManager.GetRolesAsync(user));
            string         userRole          = SecurityConstants.GuestRoleString;

            if (existingUserRoles.Count > 0)
            {
                userRole = existingUserRoles.ElementAt(0);
            }
            UserEditVM vm = new UserEditVM()
            {
                Email    = user.Email,
                Username = user.UserName,
                UserRole = userRole
            };

            ViewData["UserRole"] = new SelectList(SecurityConstants.GetRoles());
            return(View(vm));
        }
コード例 #7
0
        public async Task <IActionResult> Edit(UserEditVM model)
        {
            if (!ModelState.IsValid)
            {
                ViewData["Roles"] = CreateRolesDropDownList();

                return(View(model));
            }

            User userToEdit = await context.Users.FirstOrDefaultAsync(u => u.Id == model.Id);

            if (userToEdit == null)
            {
                return(NotFound());
            }

            userToEdit.Login = model.Login;

            if (!string.IsNullOrEmpty(model.Password))
            {
                userToEdit.Password = model.Password;
            }

            userToEdit.RoleId = model.Role;

            await context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
コード例 #8
0
ファイル: AdminController.cs プロジェクト: Lam6969/EMS
        public ActionResult EditUser(UserEditVM model)
        {
            var u = db.Users.Find(model.Id);

            if (model == null)
            {
                return(RedirectToAction("Index", "Admin"));
            }
            if (ModelState.IsValid)
            {
                u.name       = model.name;
                u.contact_no = model.contact_no;
                u.email      = model.email;
                u.password   = model.password;
                u.role       = model.role.ToString();
                if (model.Photo != null)
                {
                    DeletePhoto(u.photo);
                    u.photo = SavePhoto(model.Photo);
                }
                db.SaveChanges();

                TempData["info"] = "User record updated successfully";
                return(RedirectToAction("DisplayUser", "Admin"));
            }
            return(View(model));
        }
コード例 #9
0
        public ActionResult ChangeRole(UserEditVM model)
        {
            if (model == null)
            {
                new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            try
            {
                logic.ChangeRole(model.Login, model.Role);
                logger.Info("Role was changed", "Presentation layer, class UserController, method ActionResult ChangeRole(UserEditVM)");

                return(RedirectToAction("Index"));
            }
            catch (System.Exception e)
            {
                if (e is DalException)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                }
                else
                {
                    logger.Warning("BL", e);
                    return(RedirectToAction("Index", "Common"));
                }
            }
        }
コード例 #10
0
ファイル: UserController.cs プロジェクト: gogocode/DL
        public ActionResult Edit(int?id)
        {
            if (id == null && Session["Id"] != null)
            {
                id = int.Parse(Session["Id"].ToString());
            }

            UserService _userService = new UserService();
            User        user         = _userService.GetUserById(id.Value);

            if (user == null)
            {
                return(HttpNotFound());
            }

            Mapper.CreateMap <User, UserEditVM>();
            UserEditVM userVM = Mapper.Map <UserEditVM>(user);

            //SupperUser不可更改員工密碼
            if (Session["Account"].ToString() == "9999")
            {
                TempData["IsSupperUser"] = true;
            }

            userVM.ConfirmPassword = user.UserPassword;
            userVM.OriginPassword  = user.UserPassword;

            return(View(userVM));
        }
コード例 #11
0
        public ActionResult Edit(UserEditVM model)
        {
            if (model == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            try
            {
                this.logic.ChangeRole(model.Login, model.Role);
            }
            catch (System.Exception e)
            {
                if (e is DalException)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
                }
                else
                {
                    logger.Warning("BL", e);
                    return(RedirectToAction("Index", "Common"));
                }
            }

            return(RedirectToAction("Index", "User"));
        }
コード例 #12
0
        public async Task <IActionResult> Edit(UserEditVM model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByIdAsync(model.Id.ToString());

                if (user == null)
                {
                    return(NotFound());
                }

                if (model.UserName != user.UserName || model.LastName != user.LastName || model.FirstName != user.FirstName)
                {
                    user.Id        = model.Id;
                    user.LastName  = model.LastName;
                    user.FirstName = model.FirstName;
                    user.UserName  = model.UserName;
                    user.Email     = model.Email;

                    var setResult = await _userManager.UpdateAsync(user);

                    if (!setResult.Succeeded)
                    {
                        throw new ApplicationException($"Unexpected error occurred setting profile for user with ID '{user.Id}'.");
                    }
                }

                StatusMessage = "Your profile has been updated";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
コード例 #13
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var user = await _userManager.FindByIdAsync(id.ToString());

            if (user == null)
            {
                return(NotFound());
            }

            var model = new UserEditVM()
            {
                Id            = user.Id,
                UserName      = user.UserName,
                LastName      = user.LastName,
                FirstName     = user.FirstName,
                Email         = user.Email,
                StatusMessage = StatusMessage,
            };

            return(View(model));
        }
コード例 #14
0
        public IActionResult Edit(UserEditVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var repo = new UsersRepository();
            var user = repo.GetById(model.ID);

            // create
            if (user == null)
            {
                user = new User();
            }

            user.Username  = model.Username;
            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            user.Password  = model.Password;
            user.Email     = model.Email;

            repo.Save(user);

            return(RedirectToAction("List"));
        }
コード例 #15
0
        public async Task <IActionResult> Index(UserEditVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            user.Id        = model.Id;
            user.Email     = model.Email;
            user.LastName  = model.LastName;
            user.FirstName = model.FirstName;
            user.UserName  = model.UserName;

            var setResult = await _userManager.UpdateAsync(user);

            if (!setResult.Succeeded)
            {
                ViewBag.Message = "Error";
                return(View());
            }

            //StatusMessage = "Your profile has been updated";
            return(View());
        }
コード例 #16
0
ファイル: UserController.cs プロジェクト: gogocode/DL
        public ActionResult Edit(UserEditVM userVM)
        {
            UserService _userService = new UserService();

            if (!ModelState.IsValid)
            {
                return(View(userVM));
            }

            //MD5無法解密,故用此判斷是否更改過密碼
            //無更改過密碼,已經為MD5密碼
            if (userVM.UserPassword.Equals(userVM.OriginPassword))
            {
            }
            else//更改過密碼,需加密為MD5
            {
                userVM.UserPassword = MD5Encoder.Encrypt(userVM.UserPassword);
            }

            Mapper.CreateMap <UserEditVM, UserEditSV>();
            UserEditSV userEditSV = Mapper.Map <UserEditSV>(userVM);

            userEditSV.UpdateDate = System.DateTime.Now;
            userEditSV.UpdateId   = Session["Account"].ToString();

            _userService.ModifyUser(userEditSV);

            if (Session["Account"].Equals("9999"))
            {
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("EditSuccess"));
        }
コード例 #17
0
        public ActionResult Edit(int id)
        {
            UserDBEntities db         = new UserDBEntities();
            UserTable      user       = db.UserTables.Where(u => u.UserID == id).Single();
            UserEditVM     userEditVM = new UserEditVM(user);

            return(View("Edit", userEditVM));
        }
コード例 #18
0
        public ActionResult EditUser(UserEditVM data)
        {
            var     roleName = _repo.AdminFindRoleById(data.RoleId).Name;
            AppUser user     = data.User;

            user.Profile = data.UserProfile;
            _repo.AdminEditUser(user, roleName);
            return(RedirectToAction("Users"));
        }
コード例 #19
0
        public ActionResult EditUser(string id)
        {
            UserEditVM data = new UserEditVM();

            data.User        = _repo.AdminGetUserByGuid(id);
            data.UserProfile = _repo.GetProfileById(data.User.ProfileId);
            data.Roles       = _repo.AdminGetRoles();
            return(View(data));
        }
コード例 #20
0
        public async Task <ActionResult> EditUser(UserEditVM model)
        {
            if (ModelState.IsValid)
            {
                var context = new ApplicationDbContext();
                var userMgr = new UserManager <AppUser>(new UserStore <AppUser>(context));
                var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

                try
                {
                    var oldUser = userMgr.FindById(model.User.Id);

                    oldUser.FirstName = model.User.FirstName;
                    oldUser.LastName  = model.User.LastName;
                    oldUser.Email     = model.User.Email;
                    oldUser.UserName  = model.User.Email;

                    var result = await userMgr.UpdateAsync(oldUser);

                    var role    = roleMgr.FindById(model.SelectedRoleID.ToString());
                    var oldRole = roleMgr.FindById(oldUser.Roles.FirstOrDefault(s => s.UserId == oldUser.Id).RoleId);
                    if (result.Succeeded)
                    {
                        result = userMgr.RemoveFromRole(oldUser.Id, oldRole.Name);
                        result = userMgr.AddToRole(oldUser.Id, role.Name);

                        if (!string.IsNullOrEmpty(model.Password))
                        {
                            result = userMgr.RemovePassword(oldUser.Id);
                            result = userMgr.AddPassword(oldUser.Id, model.Password);
                        }
                    }
                    return(RedirectToAction("Users", new { id = oldUser.Id }));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var context = new ApplicationDbContext();

                var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                var list    = roleMgr.Roles.ToList();

                model.RoleList = list.Select(x => new SelectListItem()
                {
                    Text  = x.Name,
                    Value = x.Id
                });

                return(View(model));
            }
        }
コード例 #21
0
        public async Task <IActionResult> Edit()
        {
            //Get currently logged user
            var currentLoggedUser = await _userManager.GetUserAsync(User);

            var userGameAccounts = await unitOfWork.GameAccounts.GetAll(gameAccount => gameAccount.UserId == currentLoggedUser.Id);

            var userEditVM = new UserEditVM(currentLoggedUser, userGameAccounts.ToList());

            return(View("Edit", userEditVM));
        }
コード例 #22
0
        public IActionResult EditUser(long ID_USER)
        {
            if (!User.Identity.IsAuthenticated)// если неавторизован то редирект на авторизацию
            {
                Uri location = new Uri($"{Request.Scheme}://{Request.Host}/Account/Login");
                return(Redirect(location.AbsoluteUri));
            }

            UserEditVM VM = new UserEditVM(ID_USER);


            return(View(VM));
        }
コード例 #23
0
        public IActionResult EditUser(UserEditVM VM)
        {
            if (!User.Identity.IsAuthenticated)// если неавторизован то редирект на авторизацию
            {
                Uri location = new Uri($"{Request.Scheme}://{Request.Host}/Account/Login");
                return(Redirect(location.AbsoluteUri));
            }

            VM.user.Update();
            Uri locat = new Uri($"{Request.Scheme}://{Request.Host}/Home/Users");

            return(Redirect(locat.AbsoluteUri));
        }
コード例 #24
0
        public ActionResult Edit(UserEditVM user)
        {
            UserDBEntities db           = new UserDBEntities();
            UserTable      userToUpdate = db.UserTables.Where(u => u.Username == user.Username).Single();

            userToUpdate.Username  = user.Username;
            userToUpdate.Password  = user.Password;
            userToUpdate.IsAdmin   = user.IsAdmin;
            userToUpdate.Age       = user.Age;
            userToUpdate.FirstName = user.FirstName;
            userToUpdate.LastName  = user.LastName;
            db.SaveChanges();
            return(Customers());
        }
コード例 #25
0
        internal User Edit(UserEditVM model)
        {
            User user = Mapper.Map <User>(model);

            if (model.Photo != null)
            {
                WebImage img = new WebImage(model.Photo.InputStream);
                img.Resize(imageWidth, imageHeight);
                user.Photo             = new PictureData();
                user.Photo.Data        = img.GetBytes();
                user.Photo.ContentType = "image/" + img.ImageFormat;
            }
            return(userLogic.Update(user));
        }
コード例 #26
0
        public async Task <IActionResult> Index()
        {
            var user = await _userManager.GetUserAsync(User);

            var model = new UserEditVM()
            {
                Id        = user.Id,
                Email     = user.Email,
                UserName  = user.UserName,
                FirstName = user.FirstName,
                LastName  = user.LastName
            };

            return(View(model));
        }
コード例 #27
0
ファイル: UserController.cs プロジェクト: yousuf360/urdu-mag
        // GET: User
        public ActionResult EditProfile()
        {
            if (Session["User"] == null)
            {
                return(RedirectToAction("Index", "User"));
            }
            int UserId = ((Urdu_Magazine.Models.User)Session["User"]).id;
            var user   = db.Users.Find(UserId);

            UserEditVM userEdit = new UserEditVM
            {
                full_name = user.full_name,
            };

            ViewBag.user = user;
            return(View(userEdit));
        }
コード例 #28
0
 public ActionResult Edit(UserEditVM model)
 {
     ViewBag.Breadcrumb = new Breadcrumb("user", "edit", model.Name);
     if (ModelState.IsValid)
     {
         var result = userDm.Edit(model);
         if (result == null)
         {
             ModelState.AddModelError("", "Ошибка при сохранении");
             return(View());
         }
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View());
     }
 }
コード例 #29
0
 public IHttpActionResult Put(int id, [FromBody] UserEditVM user)
 {
     if (ModelState.IsValid)
     {
         if (userDm.GetUserByID(id) == null)
         {
             return(NotFound());
         }
         else
         {
             userDm.Edit(user);
             return(Ok());
         }
     }
     else
     {
         return(BadRequest("User not valid"));
     }
 }
コード例 #30
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Edit(UserEditVM model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                user.Update(
                    model.FirstName, model.LastName, model.NickName,
                    model.Description, model.Country, model.City);

                if (model.AvatarImage != null)
                {
                    user.UpdateAvatar(await _helperService.SaveAvatar(model.AvatarImage, user.Avatar));
                }

                await unitOfWork.Save();
            }

            return(RedirectToAction("Edit", model));
        }