Esempio n. 1
0
        public ActionResult AddUser(UserViewModel model)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(model.LoginName)
                    || String.IsNullOrWhiteSpace(model.Password)
                    || String.IsNullOrWhiteSpace(model.PasswordConfirm)
                    || String.IsNullOrWhiteSpace(model.Name)
                    || String.IsNullOrWhiteSpace(model.City))
                {
                    throw new Exception("打 * 的为必填字段,请正确填写完毕再提交。");
                }

                if(!model.Password.Equals(model.PasswordConfirm))
                {
                    throw new Exception("两次输入的密码不一致,请重新输入。");
                }

                var bizUser = model.ToUserModel();
                UserBusiness ub = new UserBusiness();
                ub.AddUser(bizUser);

                ModelState.AddModelError("", "新用户添加成功。");
                return View("New", new UserViewModel());
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("",ex.Message);
                return View("New", new UserViewModel());
            }
        }
Esempio n. 2
0
        public ActionResult ChangePassword(ChangePasswordViewModel model)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(model.Password)
                    || String.IsNullOrWhiteSpace(model.NewPassword)
                    || String.IsNullOrWhiteSpace(model.PasswordConfirm))
                {
                    throw new Exception("打 * 的为必填字段,请正确填写完毕再提交。");
                }

                if (!model.NewPassword.Equals(model.PasswordConfirm))
                {
                    throw new Exception("两次输入的密码不一致,请重新输入。");
                }

                UserBusiness ub = new UserBusiness();
                ub.ChangePassword(SessionManager.User.LoginName, model.Password, model.NewPassword);

                ModelState.AddModelError("", "密码修改成功!");
                return View("Password", model);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View("Password", model);
            }
        }
Esempio n. 3
0
        public LoginResponse Login([FromBody]LoginRequest user)
        {
            if (user == null)
            {
                throw new BadRequestException();
            }

            var response = new LoginResponse();

            UserBusiness ub = new UserBusiness();
            UserModel userModel = null;
            bool isLogin = ub.Login(user.UserName, user.Password, out userModel, false, Context.ClientIP);

            if (!isLogin)
            {
                throw new UnauthorizedException("非法的用户名或密码!");
            }
            else
            {
                response.Token = userModel.Token;
                response.IsAdministrator = userModel.UserType == 0;
                // add login record
                //LoginRecordRepository recordRep = new LoginRecordRepository();
                //LoginRecord loginRecord = new LoginRecord
                //{
                //    IsLoginFromClient = 1,
                //    LoginIP = Context.ClientIP,
                //    UserName = user.UserName
                //};
                //recordRep.Add(loginRecord);
            }

            return response;
        }
Esempio n. 4
0
        public ActionResult Index(LoginRequest loginModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if(String.IsNullOrEmpty(loginModel.UserName) || String.IsNullOrEmpty(loginModel.Password))
                    {
                        ModelState.AddModelError("", "请输入用户名和密码!");
                        return View(loginModel);
                    }

                    UserBusiness userBiz = new UserBusiness();
                    UserModel userModel = null;
                    bool isLoginSuccess = userBiz.Login(loginModel.UserName, loginModel.Password, out userModel,true, Context.ClientIP);

                    if (isLoginSuccess)
                    {
                        SessionManager.IsLogin = true;
                        SessionManager.User = userModel;
                        string redirectUrl = Request.QueryString["returnUrl"];
                        if (!String.IsNullOrWhiteSpace(redirectUrl))
                        {
                            return Redirect(redirectUrl);
                        }
                        else
                        {
                            return RedirectToAction("MyInfo", "User");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "用户名或密码不正确!");
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(loginModel);
        }
Esempio n. 5
0
        public ChangePasswordResponse Login([FromBody]ChangePasswordRequest request,string userid)
        {
            if (request == null)
            {
                throw new BadRequestException();
            }

            var response = new ChangePasswordResponse();
            UserBusiness ub = new UserBusiness();

            try
            {
                if ("Administrator".Equals(userid, StringComparison.OrdinalIgnoreCase))
                {
                    response.IsSuccess = false;
                    response.Message = "不能修改Administrator密码。";
                    return response;
                }

                var user = ub.GetUser(userid);

                // 2 means 客户端用户
                if (user.UserType == 2)
                {
                    response.IsSuccess = false;
                    response.Message = "终端用户没有权限修改密码。";
                    return response;
                }

                ub.ChangePassword(userid, request.OldPassword, request.NewPassword,true);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }

            return response;
        }
Esempio n. 6
0
        public AjaxResponse DeleteUser(string userid)
        {
            var response = new AjaxResponse();

            try
            {
                if ("Administrator".Equals(userid, StringComparison.OrdinalIgnoreCase))
                {
                    response.IsSuccess = false;
                    response.Message = "Administrator账号不能被删除。";
                    return response;
                }

                UserBusiness ub = new UserBusiness();

                var user = ub.GetUser(Context.LoginName);

                // 2 means 客户端用户
                if (user.UserType == 2)
                {
                    response.IsSuccess = false;
                    response.Message = "终端用户没有权限进行此操作。";
                    return response;
                }

                ub.DeleteUser(userid);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }

            return response;
        }
Esempio n. 7
0
        public AjaxResponse Update([FromBody]UpdateUserRequest request, string userid)
        {
            if (request == null)
            {
                throw new BadRequestException();
            }

            var response = new AjaxResponse();
            UserBusiness ub = new UserBusiness();

            try
            {
                if ("Administrator".Equals(userid, StringComparison.OrdinalIgnoreCase))
                {
                    response.IsSuccess = false;
                    response.Message = "不能修改Administrator密码。";
                    return response;
                }
                var user = ub.GetUser(userid);

                // 2 means 客户端用户
                if (user.UserType == 2)
                {
                    response.IsSuccess = false;
                    response.Message = "终端用户没有权限修改密码。";
                    return response;
                }

                UserRepository ur = new UserRepository();
                User dbUser = new Repositories.User();
                dbUser.UserName = userid;
                dbUser.FullName = request.Name;
                dbUser.Phone = request.Phone;
                dbUser.City = request.City;
                dbUser.Description = request.Description;

                ur.UpdateUser(dbUser);
                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                response.IsSuccess = false;
                response.Message = ex.Message;
            }

            return response;
        }