Exemplo n.º 1
0
        public ActionResult GridViewPartialUpdate(cms.User item)
        {
            var model = db.Users;

            if (ModelState.IsValid)
            {
                try
                {
                    //  if password is null then assign existing password from databse to empty field and save
                    if (item.TempPassword == null)
                    {
                        var modelItem = model.FirstOrDefault(it => it.Id == item.Id);

                        //assign existing password from databse to empty field
                        item.Password = modelItem.Password;


                        if (modelItem != null)
                        {
                            this.UpdateModel(modelItem);
                            db.SaveChanges();
                        }
                    }

                    else

                    // if password change is true then enrypt
                    {
                        // get password from the UI

                        var keyNew = item.TempPassword;

                        //  Encrypt the password from the UI
                        //	var password = PasswordHelper.Encrypt(item.TempPassword, keyNew);

                        // Assign the password to PasswordHash Field
                        //	item.Password = password;

                        //Empty TempPassword so it's not stored in the database
                        //	item.TempPassword = "";

                        var modelItem = model.FirstOrDefault(it => it.Id == item.Id);
                        if (modelItem != null)
                        {
                            this.UpdateModel(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
            {
                ViewData["EditError"] = "Please, correct all errors.";
            }
            return(PartialView("_GridViewPartial", model.ToList()));
        }
Exemplo n.º 2
0
        public ActionResult GridViewPartialAddNew(cms.User item)
        {
            var model = db.Users;

            if (ModelState.IsValid)
            {
                try
                {
                    //  check on the database if Username and email already exist
                    var currentUserToAddExists = db.Users.Where(c => c.UserName == item.UserName && c.Email == item.Email).Any();

                    // if username and email don't exist then save new entry
                    if (currentUserToAddExists == false)
                    {
                        if (string.IsNullOrWhiteSpace(item.TempPassword))
                        {
                            ViewData["EditError"] = "Password may not be Empty when Creating a User. ";
                        }


                        else
                        {
                            item.Id = Guid.NewGuid();

                            //  Assign the password to PasswordHash Field
                            item.Password = item.TempPassword;

                            //Empty TempPassword so it's not stored in the database
                            item.TempPassword = null;

                            model.Add(item);
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        ViewData["EditError"] = "User Already Exist. ";
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
            {
                ViewData["EditError"] = "Please, correct all errors.";
            }


            return(PartialView("_GridViewPartial", model.ToList()));
        }