Ejemplo n.º 1
0
        public ActionResult Add(QuanLyNguoiDungModels models)
        {
            var error       = string.Empty;
            var currentUser = UserDataService.Getbykey(models.ID) ?? new user();
            var currentNd   = Nguoidung.Query.FirstOrDefault(x => x.TENDANGNHAP.ToUpper() == models.UserName.ToUpper()) ?? new NGUOIDUNG();
            var tempNgD     = ((EISContext)FXContext.Current).CurrentNguoidung;
            var check       = false;

            if (tempNgD.ISPQ == true)
            {
                if (models.VaiTro_ID == null)
                {
                    check = true;
                    error = error + "Vui lòng nhập vai trò người dùng!";
                }
                if (models.DonVi_ID == null)
                {
                    models.DonVi_ID = currentNd.DONVI_ID;
                }
                models.UserName    = currentUser.username;
                models.Password    = currentUser.password;
                models.IsApproved  = currentUser.IsApproved;
                models.IsLockedOut = currentUser.IsLockedOut;
                models.IsAdmin     = currentUser.ISADMIN;
                models.Email       = currentUser.email;

                models.NguoiDungId = currentNd.ID;
                models.Ten         = currentNd.TEN;
                models.SDT         = currentNd.PHONE;
                models.SoCMND      = currentNd.SOCMT;
                models.DiaChi      = currentNd.ADDRESS;
                models.IsPQ        = currentNd.ISPQ;
            }
            else
            {
                if (string.IsNullOrEmpty(models.Ten))
                {
                    check = true;
                    error = "Vui lòng nhập tên người dùng!";
                }
                else if (models.Email == null)
                {
                    check = true;
                    error = "Vui lòng nhập email!";
                }
                else if (models.DonVi_ID == null)
                {
                    check = true;
                    error = "Vui lòng nhập đơn vị!";
                }
                if (models.Password != null)
                {
                    if (models.Password.IndexOfAny(SpecialChars) == -1)
                    {
                        check = true;
                        error = "Password phải chứa ký tự đặc biệt!";
                    }
                    if (models.Password.IndexOfAny(UPPERCHAR) == -1)
                    {
                        check = true;
                        error = "Password phải chứa ký tự hoa!";
                    }
                    if (models.Password.IndexOfAny(NUMBER) == -1)
                    {
                        check = true;
                        error = "Password phải chứa số!";
                    }
                }
                else
                {
                    if (models.ID == 0)
                    {
                        check = true;
                        error = "Password không thể để trống!";
                    }
                }

                if (models.ID != 0)
                {
                    models.VaiTro_ID = currentNd.VAITRO;
                }
            }

            if (ModelState.IsValid && check == false)
            {
                var roles   = models.Roles == null ? new List <long>() : models.Roles.Split(',').Where(x => !String.IsNullOrEmpty(x)).Select(x => Convert.ToInt64(x)).ToList();
                var cosokcb = models.COSO_KCBID == null ? new List <long>() : models.COSO_KCBID.Split(',').Where(x => !String.IsNullOrEmpty(x)).Select(x => Convert.ToInt64(x)).ToList();
                var tempDf  = cosokcb.FirstOrDefault(x => x == currentNd.DF_COSOKCB_ID);
                var dfcskcb = tempDf != 0 ? currentNd.DF_COSOKCB_ID : null;
                // var redis = EIS.FEW.MvcApplication.redis;

                string password;
                if (tempNgD.ISPQ != true)
                {
                    password = !string.IsNullOrEmpty(models.Password)
                        ? FormsAuthentication.HashPasswordForStoringInConfigFile(models.Password, "MD5")
                        : currentUser.password;
                }
                else
                {
                    password = models.Password;
                }
                var user = new user
                {
                    userid          = models.ID,
                    username        = models.UserName,
                    password        = password,
                    PasswordSalt    = "MD5",
                    GroupName       = tempNgD.ISPQ != true ? System.Web.HttpContext.Current.User.Identity.Name : currentUser.GroupName,
                    email           = models.Email,
                    IsApproved      = models.IsApproved != null && (bool)(models.IsApproved),
                    IsLockedOut     = models.IsLockedOut != null && (bool)(models.IsLockedOut),
                    Roles           = RoleService.Query.Where(t => roles.Contains(t.roleid)).ToList(),
                    CreateDate      = DateTime.Now,
                    ISADMIN         = models.IsAdmin != null && (bool)(models.IsAdmin),
                    ApplicationList = ApplicationsService.Query.Where(x => x.AppID == 1).ToList()
                };
                var nguoiDung = new NGUOIDUNG
                {
                    ID            = models.NguoiDungId,
                    TEN           = models.Ten,
                    TENDANGNHAP   = models.UserName,
                    TRANGTHAI     = 1,
                    PHONE         = models.SDT,
                    SOCMT         = models.SoCMND,
                    ADDRESS       = models.DiaChi,
                    VAITRO        = models.VaiTro_ID,
                    DONVI_ID      = models.DonVi_ID == 0 ? null : models.DonVi_ID,
                    COSOKCBS      = DmcosokcbService.Query.Where(t => cosokcb.Contains(t.ID)).ToList(),
                    ISPQ          = models.IsPQ,
                    DF_COSOKCB_ID = dfcskcb,
                    DF_LOAITG     = currentNd.DF_LOAITG,
                    DF_NAM        = currentNd.DF_NAM,
                    DF_QUY        = currentNd.DF_QUY,
                    DF_THANG      = currentNd.DF_THANG
                };

                try
                {
                    UserDataService.BeginTran();
                    UserDataService.Clear();
                    UserDataService.Save(user);
                    // insert log
                    _iLogSystemService.CreateNew(HttpContext.User.Identity.Name, "Tạo mới người dùng", "Tạo mới người dùng thành công:" + user.username,
                                                 Helper.GetIPAddress.GetVisitorIPAddress(), HttpContext.Request.Browser.Browser);

                    Nguoidung.Save(nguoiDung);
                    UserDataService.CommitTran();

                    if (user == null || nguoiDung == null)
                    {
                        _iLogSystemService.CreateNew(HttpContext.User.Identity.Name, "Tạo mới người dùng", "Tạo mới hoặc sửa người dùng thất bại do không thể kết nối redis",
                                                     Helper.GetIPAddress.GetVisitorIPAddress(), HttpContext.Request.Browser.Browser);
                    }
                    else
                    {
                        //var bientam_nguoidung = new NGUOIDUNG();
                        //bientam_nguoidung.ADDRESS = nguoiDung.ADDRESS;
                        //bientam_nguoidung.CapImage = nguoiDung.CapImage;
                        //bientam_nguoidung.CapImageText = nguoiDung.CapImageText;
                        //bientam_nguoidung.CaptchaCodeText = nguoiDung.CaptchaCodeText;
                        //bientam_nguoidung.COSOKCB = nguoiDung.COSOKCB;
                        //bientam_nguoidung.COSOKCB.DM_DONVI.TINHTHANH = null;
                        //bientam_nguoidung.COSOKCB.DM_DONVI.DONVICHA = null;
                        //bientam_nguoidung.COSOKCBS = nguoiDung.COSOKCBS;
                        //foreach (var item in bientam_nguoidung.COSOKCBS)
                        //{
                        //    if (item.DM_DONVI != null)
                        //    {
                        //        item.DM_DONVI.DONVICHA = null;
                        //        item.DM_TINHTHANH = null;
                        //        item.DM_DONVIHANHCHINH = null;
                        //        item.DM_DONVI = null;
                        //        item.DM_QUANHUYEN = null;
                        //    }
                        //}

                        //bientam_nguoidung.DF_COSOKCB_ID = nguoiDung.DF_COSOKCB_ID;
                        //bientam_nguoidung.DF_LOAITG = nguoiDung.DF_LOAITG;
                        //bientam_nguoidung.DF_NAM = nguoiDung.DF_NAM;
                        //bientam_nguoidung.DF_QUY = nguoiDung.DF_QUY;
                        //bientam_nguoidung.DF_THANG = nguoiDung.DF_THANG;
                        //bientam_nguoidung.DONVI = nguoiDung.DONVI;
                        //bientam_nguoidung.DONVI_ID = nguoiDung.DONVI_ID;
                        //bientam_nguoidung.ID = nguoiDung.ID;
                        //bientam_nguoidung.ISPQ = nguoiDung.ISPQ;
                        //bientam_nguoidung.PHONE = nguoiDung.PHONE;
                        //bientam_nguoidung.SOCMT = nguoiDung.SOCMT;
                        //bientam_nguoidung.TEN = nguoiDung.TEN;
                        //bientam_nguoidung.TENDANGNHAP = nguoiDung.TENDANGNHAP;
                        //bientam_nguoidung.TRANGTHAI = nguoiDung.TRANGTHAI;
                        //bientam_nguoidung.VAITRO = nguoiDung.VAITRO;
                        string keyUser      = "******" + user.username;
                        string keyNguoidung = "NGUOIDUNG_" + nguoiDung.TENDANGNHAP;
                        // redis.PushNguoiDung(keyNguoidung, nguoiDung);
                        // redis.PushRedis<user>(keyUser, user);
                    }
                    //     redis.Close();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                    UserDataService.RolbackTran();
                    ViewBag.IsAdd   = models.ID == 0;
                    ViewBag.IsAdmin = tempNgD.ISPQ != true;
                    //    redis.Close();
                    return(View("TaoMoi_NguoiDungPartial", new QuanLyNguoiDungModels
                    {
                        ID = models.ID,
                        NguoiDungId = models.NguoiDungId,
                        UserName = models.UserName,
                        Password = models.Password,
                        PasswordRe = models.PasswordRe,
                        Email = models.Email,
                        IsApproved = models.IsApproved,
                        IsLockedOut = models.IsLockedOut,
                        IsAdmin = models.IsAdmin,
                        DonVi_ID = models.DonVi_ID,
                        VaiTro_ID = models.VaiTro_ID,
                        Ten = models.Ten,
                        COSO_KCBID = models.COSO_KCBID,
                        Roles = models.Roles,
                        IsPQ = models.IsPQ
                    }));
                }
            }
            else
            {
                ViewBag.IsAdd         = models.ID == 0;
                ViewBag.IsAdmin       = tempNgD.ISPQ != true;
                ViewData["EditError"] = error == string.Empty ? Resources.Localizing.MessageCommon : error;
                return(View("TaoMoi_NguoiDungPartial", new QuanLyNguoiDungModels
                {
                    ID = models.ID,
                    NguoiDungId = models.NguoiDungId,
                    UserName = models.UserName,
                    Password = models.Password,
                    PasswordRe = models.PasswordRe,
                    Email = models.Email,
                    IsApproved = models.IsApproved,
                    IsLockedOut = models.IsLockedOut,
                    IsAdmin = models.IsAdmin,
                    DonVi_ID = models.DonVi_ID,
                    VaiTro_ID = models.VaiTro_ID,
                    Ten = models.Ten,
                    COSO_KCBID = models.COSO_KCBID,
                    Roles = models.Roles,
                    IsPQ = models.IsPQ
                }));
            }
            return(RedirectToAction("Index", "QuanLyNguoiDung"));
        }