public ActionResult Upsert(string id)
        {
            ViewUserInfoModel model = null;

            try
            {
                using (var rolRepository = new RoleRepository())
                {
                    ViewBag.LstRoles = new JavaScriptSerializer().Serialize(rolRepository.FindAll());
                }

                if (String.IsNullOrEmpty(id) == false)
                {
                    using (var repository = new UserRepository())
                    {
                        model = UserInfoDto.ToDto(repository.FindViewById(id));
                    }
                    ViewBag.IsNew = false;
                }
                else
                {
                    model = new ViewUserInfoModel
                    {
                        Id = String.Empty
                    };
                    ViewBag.IsNew = true;
                }
            }
            catch (Exception ex)
            {
                SharedLogger.LogError(ex, id);
            }
            return(View(model));
        }
Пример #2
0
        public PartialViewResult EditGender(string SaveAndChange, ViewUserInfoModel ob)
        {
            string uname = Session["UserName"].ToString();

            if (SaveAndChange == "Cancel")
            {
                ob.Type   = "Cancel";
                ob.Gender = db.UserRegistrations.Where(x => x.UserName == uname).Select(x => x.Gender).Single();
                return(PartialView("_PartialPageGenderEdit", ob));
            }
            else
            if (SaveAndChange == "Edit")
            {
                ob.Type = "Edit";
                var row = db.UserRegistrations.Where(x => x.UserName == uname).Single();
                ob.Gender = row.Gender;
                return(PartialView("_PartialPageGenderEdit", ob));
            }
            else
            {
                ob.Type = "Save";
                var row = db.UserRegistrations.Where(x => x.UserName == uname).Single();
                row.Gender = ob.Gender;
                db.SaveChanges();
                ob.Gender = db.UserRegistrations.Where(x => x.UserName == uname).Select(x => x.Gender).Single();
                return(PartialView("_PartialPageGenderEdit", ob));
            }
        }
Пример #3
0
        public PartialViewResult EditEmail(String SaveAndCancelEmail, ViewUserInfoModel ob)
        {
            string uname = Session["UserName"].ToString();

            if (SaveAndCancelEmail == "Save")
            {
                ob.Type = "Save";
                var row = db.UserRegistrations.Where(x => x.UserName == uname).Single();
                row.EmailId = ob.EmailId;
                db.SaveChanges();
                return(PartialView("_PartialPageEmailEdit", ob));
            }
            else if (SaveAndCancelEmail == "Edit")
            {
                ob.Type    = "Edit";
                ob.EmailId = db.UserRegistrations.Where(x => x.UserName == uname).Select(x => x.EmailId).Single();
                return(PartialView("_PartialPageEmailEdit", ob));
            }
            else
            {
                ob.Type    = "Cancel";
                ob.EmailId = db.UserRegistrations.Where(x => x.UserName == uname).Select(x => x.EmailId).Single();
                return(PartialView("_PartialPageEmailEdit", ob));
            }
        }
Пример #4
0
 public ActionResult YourAccount(string SaveAndCancel)
 {
     if (Session["UserName"] != null)
     {
         ViewUserInfoModel ob    = new ViewUserInfoModel();
         string            uname = Session["UserName"].ToString();
         var urinfo = (from x in db.UserRegistrations
                       where x.UserName == uname
                       select x).SingleOrDefault();
         ob.FirstName = urinfo.FirstName;
         ob.LastName  = urinfo.LastName;
         ob.EmailId   = urinfo.EmailId;
         if (urinfo.Gender == "M")
         {
             ob.Gender = "Male";
         }
         else
         {
             ob.Gender = "Female";
         }
         return(View(ob));
     }
     else
     {
         return(RedirectToAction("Login"));
     }
 }
Пример #5
0
 public void AddUser(ViewUserInfoModel model)
 {
     DbConn.UserDetail.Add(new UserDetail
     {
         Id          = model.Id,
         BirthDate   = model.BirthDateIn,
         FirstName   = model.FirstName,
         LastName    = model.LastName,
         IsObsolete  = false,
         InsDateTime = DateTime.Now
     });
     DbConn.SaveChanges();
 }
Пример #6
0
        public bool UpdateUser(ViewUserInfoModel model)
        {
            if (IsAlreadyUser(model.Id, model.UserName))
            {
                return(false);
            }

            var userOld       = DbConn.AspNetUsers.FirstOrDefault(e => e.Id == model.Id);
            var userDetailOld = DbConn.UserDetail.FirstOrDefault(e => e.Id == model.Id);

            if (userOld == null || userDetailOld == null)
            {
                return(false);
            }

            userOld.Email    = model.Email;
            userOld.UserName = model.UserName;

            userDetailOld.BirthDate  = model.BirthDateIn;
            userDetailOld.FirstName  = model.FirstName;
            userDetailOld.LastName   = model.LastName;
            userDetailOld.IsObsolete = model.IsObsoleteIn;

            if (userOld.AspNetRoles.Any(e => e.Id == model.RoleId) == false)
            {
                var newRole = DbConn.AspNetRoles.FirstOrDefault(e => e.Id == model.RoleId);

                if (newRole == null)
                {
                    return(false);
                }

                for (var i = userOld.AspNetRoles.Count - 1; i >= 0; i--)
                {
                    var role = userOld.AspNetRoles.ElementAt(i);
                    userOld.AspNetRoles.Remove(role);
                }

                userOld.AspNetRoles.Add(newRole);
                DbConn.SaveChanges();
            }

            DbConn.SaveChanges();
            return(true);
        }
Пример #7
0
 public PartialViewResult EditName(string SaveAndCancel, ViewUserInfoModel ob)
 {
     if (SaveAndCancel == "Edit")
     {
         string uname  = Session["UserName"].ToString();
         var    urinfo = (from x in db.UserRegistrations
                          where x.UserName == uname
                          select x).SingleOrDefault();
         ob.FirstName = urinfo.FirstName;
         ob.LastName  = urinfo.LastName;
         ob.Type      = "Edit";
         return(PartialView("_EditName", ob));
     }
     else if (SaveAndCancel == "Cancel")
     {
         string uname  = Session["UserName"].ToString();
         var    urinfo = (from x in db.UserRegistrations
                          where x.UserName == uname
                          select x).SingleOrDefault();
         ob.FirstName = urinfo.FirstName;
         ob.LastName  = urinfo.LastName;
         ob.Type      = "Cancel";
         return(PartialView("_EditName", ob));
     }
     else if (SaveAndCancel == "Save")
     {
         string uname  = Session["UserName"].ToString();
         var    urinfo = (from x in db.UserRegistrations
                          where x.UserName == uname
                          select x).SingleOrDefault();
         urinfo.FirstName = ob.FirstName;
         urinfo.LastName  = ob.LastName;
         db.SaveChanges();
         ob.Type = "Save";
         return(PartialView("_EditName", ob));
     }
     else
     {
         return(PartialView("_EditName", ob));
     }
 }
        public async Task <ActionResult> DoUpsert(ViewUserInfoModel model)
        {
            try
            {
                if (ModelState.IsValid == false)
                {
                    return(Json(new ResponseMessageModel
                    {
                        HasError = true,
                        Title = ResShared.TITLE_REGISTER_FAILED,
                        Message = ResShared.ERROR_INVALID_MODEL
                    }));
                }

                using (var repository = new UserRepository())
                {
                    if (String.IsNullOrWhiteSpace(model.Id))
                    {
                        //Verificar que el usuario no exista
                        if (repository.IsAlreadyUser(model.UserName))
                        {
                            return(Json(new ResponseMessageModel
                            {
                                HasError = true,
                                Title = ResShared.TITLE_REGISTER_FAILED,
                                Message = "El usuario ya existe en el sistema"
                            }));
                        }
                        var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext()));
                        var role        = await roleManager.FindByIdAsync(model.RoleId);

                        if (role == null)
                        {
                            return(Json(new ResponseMessageModel
                            {
                                HasError = true,
                                Title = ResShared.TITLE_REGISTER_FAILED,
                                Message = "El perfil no existe en el sistema"
                            }));
                        }

                        var user = new ApplicationUser {
                            UserName = model.UserName, Email = model.Email, PhoneNumber = model.PhoneNumber
                        };
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (!result.Succeeded)
                        {
                            return(Json(new ResponseMessageModel
                            {
                                HasError = true,
                                Title = ResShared.TITLE_REGISTER_FAILED,
                                Message = String.Join(", ", result.Errors.ToArray())
                            }));
                        }

                        await UserManager.AddToRoleAsync(user.Id, role.Name);

                        model.Id = user.Id;
                        repository.AddUser(model);
                    }
                    else
                    {
                        if (repository.UpdateUser(model) == false)
                        {
                            return(Json(new ResponseMessageModel
                            {
                                HasError = true,
                                Title = ResShared.TITLE_REGISTER_FAILED,
                                Message = "Usuario no se encuentra en el sistema o ya existe un registro con el mismo usuario"
                            }));
                        }
                    }
                }

                return(Json(new ResponseMessageModel
                {
                    HasError = false,
                    Title = ResShared.TITLE_REGISTER_SUCCESS,
                    Message = ResShared.INFO_REGISTER_SAVED
                }));
            }
            catch (Exception ex)
            {
                SharedLogger.LogError(ex, model.UserName, model.FirstName, model.LastName);
                return(Json(new ResponseMessageModel
                {
                    HasError = true,
                    Title = ResShared.TITLE_REGISTER_FAILED,
                    Message = ResShared.ERROR_UNKOWN
                }));
            }
        }