Ejemplo n.º 1
0
        public ActionResult Update(int userid, string RetypePassword, string[] AssignRoles)
        {
            if (userid <= 0)
            {
                throw new HttpRequestValidationException();
            }
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
            AccountModels           model = new AccountModels();
            user   _model      = _MemberShipProvider.GetUser(userid, false);
            string oldpassHash = _model.password;

            //giu lai username khong cho sua
            string username = _model.username;

            try
            {
                TryUpdateModel(_model);
                if (_model.username != username)
                {
                    throw new HttpRequestValidationException();
                }
                AssignRoles = AssignRoles ?? new string[] { };
                if (_model.password != RetypePassword)
                {
                    List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                    model.RetypePassword = _model.password = oldpassHash;
                    model.AllRoles       = lst.ToArray();
                    model.UserRoles      = _RoleProvider.GetRolesForUser(_model.username);
                    model.tmpUser        = _model;
                    Messages.AddErrorMessage("Nhập đúng mật khẩu của bạn.");
                    return(View("Edit", model));
                }
                if (RetypePassword != oldpassHash)
                {
                    _model.password = FormsAuthentication.HashPasswordForStoringInConfigFile(RetypePassword, "MD5");
                }
                _model.FailedPasswordAttemptCount = 0;
                _MemberShipProvider.UpdateUser(_model);
                _RoleProvider.UpdateUsersToRoles(_model.username, AssignRoles);
                Messages.AddFlashMessage("Cập nhật thông tin thành công.");

                logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "User - Update : " + string.Format("Update: {0} by {1}", _model.username, HttpContext.User.Identity.Name), "Update User Success ", LogType.Success, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);

                return(RedirectToAction("index"));
            }
            catch (Exception ex)
            {
                logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "User - Update ", "Update User Error " + ex, LogType.Error, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);

                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                model.RetypePassword = _model.password = oldpassHash;
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = _RoleProvider.GetRolesForUser(_model.username);
                model.tmpUser        = _model;
                Messages.AddErrorMessage("Có lỗi xảy ra, vui lòng thực hiện lại!");
                return(View("Edit", model));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Active(ActiveModels model, string captch)
        {
            if (string.IsNullOrWhiteSpace(captch))
            {
                model.ErrMessages = "Nhập đúng mã xác thực.";
                return(View(model));
            }
            bool cv = CaptchaController.IsValidCaptchaValue(captch);

            if (!cv)
            {
                model.ErrMessages = "Nhập đúng mã xác thực.";
                return(View(model));
            }
            try
            {
                if (string.IsNullOrWhiteSpace(model.username))
                {
                    model.ErrMessages = "Không tồn tại tài khoản trong hệ thống.";
                    return(View(model));
                }
                if (model.username != System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(model.code)))
                {
                    model.ErrMessages = "Không tồn tại tài khoản trong hệ thống.";
                    return(View(model));
                }
                Company currentComp = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
                if (_MemberShipProvider.GetUser(model.username, true) != null)
                {
                    model.ErrMessages = "Tài khoản đã được kích hoạt trước đó, liên hệ để được hỗ trợ.";
                    return(View("Active", model));
                }
                if (!model.password.Equals(model.comfirmpassword))
                {
                    model.ErrMessages = "Nhập đúng mật khẩu xác thực.";
                    return(View("Active", model));
                }
                string status = "";
                user   tmp    = _MemberShipProvider.CreateUser(model.username, model.password, currentComp.Email, null, null, true, null, currentComp.id.ToString(), out status);
                if (status != "Success" || tmp == null)
                {
                    model.ErrMessages = "Chưa kích hoạt được tài khoản, liên hệ để được hỗ trợ.";
                    return(View("Active", model));
                }
                IRBACRoleProvider _RoleProvider = IoC.Resolve <IRBACRoleProvider>();
                if (_RoleProvider.RoleExists("Admin"))
                {
                    _RoleProvider.UpdateUsersToRoles(tmp.userid, new string[] { "Admin" });
                }
                return(Redirect("/Account/Logon"));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                model.ErrMessages = "Chưa kích hoạt được tài khoản, liên hệ để được hỗ trợ.";
                return(View());
            }
        }
Ejemplo n.º 3
0
        public ActionResult SaveServiceRole(int userid, string RetypePassword)
        {
            if (userid <= 0)
            {
                throw new HttpRequestValidationException();
            }
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();
            Company currentCom = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            user    model      = _MemberShipProvider.GetUser(userid, false);

            if (HttpContext.User.Identity.Name == model.username)
            {
                Messages.AddErrorFlashMessage(Resources.Message.User_UMesCantEdit);
                return(RedirectToAction("ServiceRoleIndex"));
            }
            string oldpassHash = model.password;
            string username    = model.username;
            string email       = model.email;

            try
            {
                TryUpdateModel(model);
                model.username = username;
                if (model.password != RetypePassword)
                {
                    ViewData["RetypePassword"] = model.password;
                    Messages.AddErrorFlashMessage(Resources.Message.User_MesConfirmPass);
                    return(View("ServiceRoleEdit", model));
                }
                if (RetypePassword != oldpassHash)
                {
                    model.password = GeneratorPassword.EncodePassword(RetypePassword, model.PasswordFormat, model.PasswordSalt);//FormsAuthentication.HashPasswordForStoringInConfigFile(RetypePassword, "MD5");
                }
                model.FailedPasswordAttemptCount = 0;
                _MemberShipProvider.UpdateUser(model);
                _RoleProvider.UpdateUsersToRoles(model.username, new string[] { "ServiceRole" });
                Messages.AddFlashMessage(Resources.Message.User_UMesSuccess);
                log.Info("ServiceRoleUpdate by: " + HttpContext.User.Identity.Name + "Info-- tai khoan " + model.username);
                return(RedirectToAction("ServiceRoleIndex"));
            }
            catch (Exception ex)
            {
                ViewData["RetypePassword"] = model.password;
                log.Error("ServiceRoleUpdate-" + ex.Message);
                Messages.AddFlashException(ex);
                return(View("ServiceRoleEdit", model));
            }
        }
Ejemplo n.º 4
0
        public ActionResult CreateNewServiceRole(user _model, string RetypePassword)
        {
            if (string.IsNullOrWhiteSpace(_model.username))
            {
                Messages.AddErrorMessage("Cần nhập tên tài khoản người dùng.");
                ViewData["RetypePassword"] = _model.password;
                return(View("NewServiceRole", _model));
            }
            Company                 currentCom          = ((EInvoiceContext)FXContext.Current).CurrentCompany;
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();

            try
            {
                string status = "";
                if (!_model.password.Equals(RetypePassword))
                {
                    Messages.AddErrorMessage(Resources.Message.User_MesConfirmPass);
                    ViewData["RetypePassword"] = _model.password;
                    return(View("NewServiceRole", _model));
                }
                else
                {
                    _model.GroupName = currentCom.id.ToString();
                    _MemberShipProvider.CreateUser(_model.username, _model.password, _model.email, _model.PasswordQuestion, _model.PasswordAnswer, _model.IsApproved, _model.userid, _model.GroupName, out status);
                    if (status != "Success")
                    {
                        Messages.AddErrorMessage("Dữ liệu không hợp lệ hoặc tài khoản đã có trên hệ thống.");
                        ViewData["RetypePassword"] = _model.password;
                        return(View("NewServiceRole", _model));
                    }
                    string[] roleservice = new string[] { "ServiceRole" };
                    _RoleProvider.UpdateUsersToRoles(_model.username, roleservice);
                    Messages.AddFlashMessage(Resources.Message.User_IMesSuccess);
                    log.Info("CreateNewServiceRole by:" + HttpContext.User.Identity.Name + " Info-- tai khoan " + _model.username);
                    return(RedirectToAction("ServiceRoleIndex"));
                }
            }
            catch (Exception ex)
            {
                Messages.AddErrorMessage("Có lỗi xảy ra, vui lòng thực hiện lại.");
                log.Error("CreateNewServiceRole-" + ex.Message);
                ViewData["RetypePassword"] = _model.password;
                return(View("NewServiceRole", _model));
            }
        }
Ejemplo n.º 5
0
        public ActionResult Create(user _model, string RetypePassword, string[] AssignRoles)
        {
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();
            IuserService            _userService        = IoC.Resolve <IuserService>();
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
            AccountModels           model = new AccountModels();

            if (string.IsNullOrWhiteSpace(_model.username))
            {
                Messages.AddErrorMessage("Cần nhập những thông tin bắt buộc.");
                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                model.RetypePassword = _model.password = "";
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = AssignRoles ?? new string[] { };
                model.tmpUser        = _model;
                return(View("New", model));
            }
            try
            {
                string status = "";
                AssignRoles = AssignRoles ?? new string[] { };
                if (!_model.password.Equals(RetypePassword))
                {
                    Messages.AddErrorMessage("Nhập đúng mật khẩu của bạn.");
                    List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                    model.RetypePassword = _model.password = "";
                    model.AllRoles       = lst.ToArray();
                    model.UserRoles      = new string[] { };
                    model.tmpUser        = _model;
                    return(View("New", model));
                }
                else
                {
                    _MemberShipProvider.CreateUser(_model.username, _model.password, _model.email, _model.PasswordQuestion, _model.PasswordAnswer, _model.IsApproved, null, out status);
                    if (status != "Success")
                    {
                        List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                        model.RetypePassword = _model.password = "";
                        model.AllRoles       = lst.ToArray();
                        model.UserRoles      = new string[] { };
                        model.tmpUser        = _model;
                        Messages.AddErrorMessage("Tài khoản đã có trên hệ thống hoặc dữ liệu không hợp lệ.");
                        return(View("New", model));
                    }
                    if (AssignRoles == null)
                    {
                        Messages.AddFlashMessage("Bạn tạo tài khoản thành công nhưng chưa phân quyền!");
                        return(RedirectToAction("index"));
                    }
                    _RoleProvider.UpdateUsersToRoles(_model.username, AssignRoles);
                    Messages.AddFlashMessage("Tạo tài khoản thành công.");

                    logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "User - Create : " + string.Format("Create: {0} by {1}", _model.username, HttpContext.User.Identity.Name), "Create User Success ", LogType.Success, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);

                    return(RedirectToAction("index"));
                }
            }
            catch (Exception ex)
            {
                logSrv.CreateNew(FXContext.Current.CurrentUser.userid, "User - Create ", "Create User Error: " + ex, LogType.Error, HttpContext.Request.UserHostAddress, HttpContext.Request.Browser.Browser);

                Messages.AddErrorMessage("Chưa tạo được người dùng.");
                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                model.RetypePassword = _model.password = "";
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = new string[] { };
                model.tmpUser        = _model;
                return(View("New", model));
            }
        }
Ejemplo n.º 6
0
        public ActionResult New(user temp, string RetypePassword, string[] UserRoles, string fullname)
        {
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();

            if (string.IsNullOrWhiteSpace(temp.username))
            {
                AccountModel model = new AccountModel();
                Messages.AddErrorMessage("Cần nhập những thông tin bắt buộc.");
                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                if (lst.IndexOf("ServiceRole") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("ServiceRole"));
                }
                if (lst.IndexOf("Root") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("Root"));
                }
                model.RetypePassword = temp.password = "";
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = UserRoles ?? new string[] { };
                model.UserTmp        = temp;
                return(View("Create", model));
            }
            try
            {
                if (temp.password != RetypePassword)
                {
                    AccountModel model = new AccountModel();
                    Messages.AddErrorMessage(Resources.Message.User_MesConfirmPass);
                    List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                    if (lst.IndexOf("ServiceRole") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("ServiceRole"));
                    }
                    if (lst.IndexOf("Root") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("Root"));
                    }
                    model.RetypePassword = temp.password = "";
                    model.AllRoles       = lst.ToArray();
                    model.UserRoles      = UserRoles ?? new string[] { };
                    model.UserTmp        = temp;
                    return(View("Create", model));
                }
                //Tao tai khoan
                string  status     = "";
                Company currentCom = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                user    u          = _MemberShipProvider.CreateUser(temp.username, temp.password, temp.email, null, null, temp.IsApproved, null, currentCom.id.ToString(), out status);
                if (status != "Success")
                {
                    AccountModel model = new AccountModel();
                    Messages.AddErrorMessage("Tài khoản đã có trên hệ thống hoặc dữ liệu không hợp lệ.");
                    List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                    if (lst.IndexOf("ServiceRole") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("ServiceRole"));
                    }
                    if (lst.IndexOf("Root") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("Root"));
                    }
                    model.RetypePassword = temp.password = "";
                    model.AllRoles       = lst.ToArray();
                    model.UserRoles      = UserRoles ?? new string[] { };
                    model.UserTmp        = temp;
                    return(View("Create", model));
                }
                _RoleProvider.UpdateUsersToRoles(u.userid, UserRoles);
                Messages.AddFlashMessage(Resources.Message.User_UMesSuccess);
                log.Info("Create Account:" + HttpContext.User.Identity.Name + ", Date: " + DateTime.Now);

                Company       currentComp = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                IStaffService _staSrv     = IoC.Resolve <IStaffService>();
                Staff         newStaff    = new Staff
                {
                    FullName    = fullname,
                    AccountName = u.username,
                    ComID       = currentComp.id,
                    Email       = u.email
                };

                _staSrv.CreateNew(newStaff);
                _staSrv.CommitChanges();

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                log.Error("Create Error:", ex);
                AccountModel model = new AccountModel();
                Messages.AddErrorMessage("Tài khoản đã có trên hệ thống hoặc dữ liệu không hợp lệ.");
                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                if (lst.IndexOf("ServiceRole") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("ServiceRole"));
                }
                if (lst.IndexOf("Root") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("Root"));
                }
                model.RetypePassword = temp.password = "";
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = new string[] { };
                model.UserTmp        = temp;
                return(View("Create", model));
            }
        }
Ejemplo n.º 7
0
        public ActionResult Update(int id, string RetypePassword, string[] UserRoles, string fullname)
        {
            if (id <= 0)
            {
                throw new HttpRequestValidationException();
            }
            IRBACMembershipProvider _MemberShipProvider = IoC.Resolve <IRBACMembershipProvider>();
            IRBACRoleProvider       _RoleProvider       = IoC.Resolve <IRBACRoleProvider>();
            user Ouser = _MemberShipProvider.GetUser(id, false);

            if (HttpContext.User.Identity.Name == Ouser.username)
            {
                Messages.AddErrorFlashMessage(Resources.Message.User_UMesCantEdit);
                return(RedirectToAction("index"));
            }
            //lay doi tuong tai khoan cu
            string       OldPassword = Ouser.password;
            string       Oldusername = Ouser.username;
            AccountModel model       = new AccountModel();

            try
            {
                TryUpdateModel <user>(Ouser);
                if (Ouser.password != RetypePassword)
                {
                    Messages.AddErrorMessage(Resources.Message.User_MesConfirmPass);
                    List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                    if (lst.IndexOf("ServiceRole") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("ServiceRole"));
                    }
                    if (lst.IndexOf("Root") >= 0)
                    {
                        lst.RemoveAt(lst.IndexOf("Root"));
                    }
                    model.RetypePassword = Ouser.password = OldPassword;
                    model.AllRoles       = lst.ToArray();
                    model.UserRoles      = _RoleProvider.GetRolesForUser(Ouser.userid);
                    model.UserTmp        = Ouser;
                    return(View("Edit", model));
                }
                if (Ouser.password != OldPassword)
                {
                    Ouser.PasswordSalt = GeneratorPassword.GenerateSalt();
                    Ouser.password     = GeneratorPassword.EncodePassword(Ouser.password, Ouser.PasswordFormat, Ouser.PasswordSalt);//FormsAuthentication.HashPasswordForStoringInConfigFile(RetypePassword, "MD5");
                }
                Ouser.FailedPasswordAttemptCount = 0;
                //update lai tai khoan
                _MemberShipProvider.UpdateUser(Ouser);
                model.UserRoles = UserRoles ?? new string[] { };
                _RoleProvider.UpdateUsersToRoles(Ouser.userid, model.UserRoles);
                Messages.AddFlashMessage(Resources.Message.User_UMesSuccess);
                log.Info("Update Account:" + HttpContext.User.Identity.Name + ", Date: " + DateTime.Now);

                Company       currentComp = ((EInvoiceContext)FXContext.Current).CurrentCompany;
                IStaffService _staSrv     = IoC.Resolve <IStaffService>();
                Staff         sta         = _staSrv.SearchByAccountName(Ouser.username, currentComp.id);
                sta.FullName = fullname;
                _staSrv.UpdateStaff(sta);

                return(RedirectToAction("index"));
            }
            catch (Exception ex)
            {
                log.Error("Error Update:", ex);
                Messages.AddErrorMessage("Có lỗi xảy ra, vui lòng thực hiện lại!");
                List <String> lst = new List <string>(_RoleProvider.GetAllRoles());
                if (lst.IndexOf("ServiceRole") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("ServiceRole"));
                }
                if (lst.IndexOf("Root") >= 0)
                {
                    lst.RemoveAt(lst.IndexOf("Root"));
                }
                model.RetypePassword = Ouser.password = OldPassword;
                model.AllRoles       = lst.ToArray();
                model.UserRoles      = _RoleProvider.GetRolesForUser(Ouser.userid);
                model.UserTmp        = Ouser;
                return(View("Edit", model));
            }
        }