Пример #1
0
        public ActionResult Reset(string username)
        {
            ResetModel mm = new ResetModel();

            try
            {
                IuserService _userService    = IoC.Resolve <IuserService>();
                Company      _currentCompany = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                user         model           = _userService.Query.Where(u => u.GroupName.Equals(_currentCompany.id.ToString()) && u.username == username).FirstOrDefault();
                if (model != null)
                {
                    string randompass = IdentityManagement.WebProviders.RBACMembershipProvider.CreateRandomPassword(8);
                    IService.IRegisterEmailService emailSrv      = FX.Core.IoC.Resolve <IService.IRegisterEmailService>();
                    Dictionary <string, string>    subjectParams = new Dictionary <string, string>(1);
                    subjectParams.Add("$subject", "");
                    Dictionary <string, string> bodyParams = new Dictionary <string, string>(3);
                    bodyParams.Add("$password", randompass);
                    bodyParams.Add("$site", FX.Utils.UrlUtil.GetSiteUrl());
                    emailSrv.ProcessEmail("*****@*****.**", model.email, "ResetPassword", subjectParams, bodyParams);
                    model.password = GeneratorPassword.EncodePassword(randompass, model.PasswordFormat, model.PasswordSalt);//FormsAuthentication.HashPasswordForStoringInConfigFile(randompass, "MD5");
                    model.LastPasswordChangedDate = DateTime.Now;
                    _userService.Save(model);
                    _userService.CommitChanges();
                    mm.lblErrorMessage = "Kiểm tra email để lấy mật khẩu của bạn.";
                    return(View("ResetPassword", mm));
                }
                mm.lblErrorMessage = "Tài khoản không tồn tại trên hệ thống";
                return(View("ResetPassword", mm));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                ResetModel model = new ResetModel();
                model.lblErrorMessage = "Tài khoản không tồn tại trên hệ thống";
                return(View("ResetPassword", mm));
            }
        }
Пример #2
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"));
        }
Пример #3
0
 public void Save(userDTO dto)
 {
     _service.Save(dto);
 }
Пример #4
0
        public ActionResult LogOn(LogOnModel _model, string captch)
        {
            if (string.IsNullOrWhiteSpace(captch))
            {
                _model.lblErrorMessage = "Nhập đúng mã xác thực.";
                _model.Password        = "";
                return(View(_model));
            }
            bool cv = CaptchaController.IsValidCaptchaValue(captch);

            if (!cv)
            {
                _model.lblErrorMessage = "Nhập đúng mã xác thực.";
                _model.Password        = "";
                return(View(_model));
            }
            log.Info("Login: "******"LogOn:" + HttpContext.User.Identity.Name + ", Date:" + DateTime.Now);
                        if (!string.IsNullOrWhiteSpace(_model.ReturnUrl) && Url.IsLocalUrl(_model.ReturnUrl))
                        {
                            return(Redirect(_model.ReturnUrl));
                        }
                        return(Redirect("/"));
                    }
                    else
                    {
                        IuserService userSrv  = IoC.Resolve <IuserService>();
                        var          currComp = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                        user         TempUser = userSrv.Query.Where(u => u.username == _model.UserName && u.GroupName.Equals(currComp.id.ToString())).FirstOrDefault();
                        if (TempUser != null)
                        {
                            if (TempUser.IsLockedOut)
                            {
                                _model.lblErrorMessage = "Tài khoản đã bị khóa.";
                            }
                            else
                            {
                                if (!_model.IsThread)
                                {
                                    if (TempUser.FailedPasswordAttemptCount > 0)
                                    {
                                        TempUser.FailedPasswordAttemptCount = 0;
                                        userSrv.Save(TempUser);
                                        userSrv.CommitChanges();
                                    }
                                    _model.lblErrorMessage = Resources.Message.User_MesWrongAccOrPass;
                                    _model.Password        = "";
                                    _model.IsThread        = true;
                                    return(View(_model));
                                }
                                if (TempUser.FailedPasswordAttemptCount == 4)
                                {
                                    TempUser.IsLockedOut = true;
                                }
                                TempUser.FailedPasswordAttemptCount++;
                                _model.lblErrorMessage = Resources.Message.User_MesWrongAccOrPass;
                                userSrv.Save(TempUser);
                                userSrv.CommitChanges();
                            }
                            _model.Password = "";
                            return(View(_model));
                        }
                        _model.lblErrorMessage = Resources.Message.User_MesWrongAccOrPass;
                        _model.Password        = "";
                        return(View(_model));
                    }
                }
                else
                {
                    _model.Password = "";
                    return(View("LogOn", _model));
                }
            }
            catch (Exception ex)
            {
                log.Error("Error", ex);
                _model.lblErrorMessage = Resources.Message.User_MesWrongAccOrPass;
                _model.Password        = "";
                return(View("LogOn", _model));
            }
        }