public void Update(UserDto model)
        {
            var entity = _userRepository.GetById(model.Id);
            entity = model.MapTo(entity);
            //删除之前的中间数据(改成真删)
            var oldUserRoleIds = _userRoleRepository.GetAll().Where(m => m.IsDeleted == false && m.UserId == entity.Id).Select(m => m.Id).ToList();
            _userRoleRepository.DeleteList(oldUserRoleIds);
            
            var newRoleIds = model.SelectedRoleIds.ToList();

            foreach (var newRoleId in newRoleIds)
            {
                var userRole = new UserRole()
                {
                    UserId = entity.Id,
                    RoleId = newRoleId,
                    CreateId = entity.CreateId,
                    CreateBy = entity.CreateBy,
                    CreateTime = DateTime.Now,
                    ModifyId = entity.ModifyId,
                    ModifyBy = entity.ModifyBy,
                    ModifyTime = DateTime.Now
                };
                _userRoleRepository.Insert(userRole);

            }
            _userRepository.Update(entity);
        }
        public void Create(UserDto model)
        {
            model.PwdErrorCount = 0;
            model.LoginCount = 0;
            model.RegisterTime = DateTime.Now;
            var entity = model.MapTo<User>();

            foreach (int roleId in model.SelectedRoleIds)
            {
                if (_roleRepository.GetAll().Any(t => t.Id == roleId))
                {
                    entity.UserRole.Add(
                        new UserRole()
                        {
                            UserId = entity.Id,
                            RoleId = roleId,
                            CreateId = entity.CreateId,
                            CreateBy = entity.CreateBy,
                            CreateTime = DateTime.Now,
                            ModifyId = entity.ModifyId,
                            ModifyBy = entity.ModifyBy,
                            ModifyTime = DateTime.Now
                        });
                }
            }

            _userRepository.Insert(entity);
        }
        private void InitButton(UserDto user, ResultExecutingContext filterContext)
        {
            var roleIds = user.UserRole.Select(t => t.RoleId);
            var controller = filterContext.RouteData.Values["controller"].ToString().ToLower();
            var buttonList = _roleService.GetViewButtons(new GetUserPermissionInput() { RoleIdList = roleIds.ToList(), Controller = controller });

            ((ViewResult)filterContext.Result).ViewBag.ButtonList = buttonList;
        }
        public ActionResult Login(UserDto model)
        {
            model.LastLoginTime = DateTime.Now;
            var result = _userService.Login(model);
            Session["CurrentUser"] = result.AppendData;
            Session.Timeout = 200000;

            return Json(result);
        }
 public bool IsAllowed(UserDto user, string controller, string action)
 {
     if (user.UserRole == null)
     {
         return false;
     }
     var roleIds = user.UserRole.Select(t => t.RoleId);
     var isHavaPermission = _roleService.IsHavaPermission(new GetUserPermissionInput() { RoleIdList = roleIds.ToList(), Controller = controller, Action = action });
     return isHavaPermission;
 }
 public JsonResult CreateOrUpdate(UserDto model)
 {
     if (model.Id == 0)
     {
         this.Create(model);
     }
     else
     {
         this.Update(model);
     }
     return Json(1, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Edit(int? id)
        {
            //所有角色
            ViewBag.roleList = _roleService.GetList();

            UserDto model = null;
            if (!id.HasValue)  //新建
            {
                model = new UserDto();
            }
            else  //编辑
            {
                model = _userService.GetById(id.Value);
                //已经选择的角色id集合
                ViewBag.SelectedRoleIds = model.UserRole.Where(m => !m.IsDeleted).Select(m => m.RoleId).ToList();
            }
            return View(model);
        }
 public JsonResult Update(UserDto model)
 {
     _userService.Update(model);
     return Json(1, JsonRequestBehavior.AllowGet);
 }
        //<summary>
         //用户登录
         //</summary>
        public OperationResult Login(UserDto model)
        {

            User user = _userRepository.GetAll().Where(m => m.LoginName == model.LoginName).FirstOrDefault();
            if (user == null)
            {
                return new OperationResult(OperationResultType.QueryNull, "指定账号的用户不存在。");
            }
            if (user.LoginPwd != model.LoginPwd)
            {
                return new OperationResult(OperationResultType.Warning, "登录密码不正确。");
            }
            return new OperationResult(OperationResultType.Success, "登录成功。", user.MapTo<UserDto>());
        }