예제 #1
0
        public async Task <AjaxResponse> SaveUserModel(UserInput model)
        {
            if (model.ID == null)
            {
                UserInfo modelInput = ObjectMapper.Map <UserInfo>(model);
                //查询范围包含已经逻辑删除的数据
                using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
                {
                    //唯一键冲突必须要先进行验证
                    var res = await _userInfoManager.FindByNameAsync(modelInput.UserCode);

                    if (res != null)
                    {
                        throw new UserFriendlyException("账号重复", "您输入的账号:" + model.UserCode + "重复!");
                    }
                }
                //新增用户设置默认图标
                modelInput.ImageUrl = "m";
                if (modelInput.Sex == "0")
                {
                    modelInput.ImageUrl = "w";
                }
                //新增用户对象(新增前默认验证UserCode的账号是否存在如果存在不做新增)
                await _userInfoManager.CreateAsync(modelInput);

                //EF 保存当前用户数据对象
                _unitOfWorkManager.Current.SaveChanges();
                //获取新增后的主键id
                model.ID = modelInput.Id;
                // 新增用户默认订阅所有通知
                await _sysNotificationAppService.UserSubscriptionNotificationInfoAll(modelInput.Id);
            }
            else
            {
                long id = Convert.ToInt64(model.ID);
                //更新数据时不对数据是否逻辑删除做判断
                using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
                {
                    //获取需要更新的数据
                    UserInfo data = _userInfoRepository.Get(id);
                    //映射需要修改的数据对象
                    UserInfo modelInput = ObjectMapper.Map(model, data);
                    //提交修改
                    await _userInfoManager.UpdateAsync(modelInput);
                }
                //清除缓存
                _cacheManagerExtens.RemoveUserInfoCache(id);
            }
            //用户扩展信息,如有扩展字段需要重写该部分
            bool isFlag = await _userInfoExtens.UpdateUserInfo(model);

            if (!isFlag)
            {
                throw new UserFriendlyException("用户扩展信息", "用户扩展信息保存失败!");
            }
            return(new AjaxResponse {
                Success = true
            });
        }
예제 #2
0
        public async Task <AjaxResponse> SaveUserModel(UserInput model)
        {
            if (model.ID == null)
            {
                UserInfo modelInput = model.MapTo <UserInfo>();
                //查询范围包含已经逻辑删除的数据
                using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
                {
                    //唯一键冲突必须要先进行验证
                    var res = await _userInfoManager.FindByNameAsync(modelInput.UserCode);

                    if (res != null)
                    {
                        return(new AjaxResponse {
                            Success = false, Error = new ErrorInfo("账号重复")
                        });
                    }
                }
                //新增用户设置默认图标
                modelInput.ImageUrl = "m";
                if (modelInput.Sex == "0")
                {
                    modelInput.ImageUrl = "w";
                }
                //新增用户对象
                await _userInfoManager.CreateAsync(modelInput);

                //获取新增后的主键id
                model.ID = modelInput.Id;
                //新增订阅通知
                var notificationInfo = await _sysNotificationAppService.GetNotificationInfoAllAsync();

                foreach (var item in notificationInfo)
                {
                    await _sysNotificationAppService.InsertSubscriptionAsync(new List <long>() { modelInput.Id }, item.NotificationName);
                }
            }
            else
            {
                long id = Convert.ToInt64(model.ID);
                //更新数据时不对数据是否逻辑删除做判断
                using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
                {
                    //获取需要更新的数据
                    UserInfo data = _userInfoRepository.Get(id);
                    //映射需要修改的数据对象
                    UserInfo modelInput = ObjectMapper.Map(model, data);

                    //提交修改
                    await _userInfoManager.UpdateAsync(modelInput);
                }
                //清除缓存
                _cacheManagerExtens.RemoveUserInfoCache(id);
            }

            bool isFlag = await _userInfoExtens.UpdateUserInfo(model);

            if (!isFlag)
            {
                return(new AjaxResponse {
                    Success = false, Error = new ErrorInfo("用户扩展信息保存失败!")
                });
            }
            return(new AjaxResponse {
                Success = true
            });
        }
예제 #3
0
        public async Task <ActionResult> AddUser(AddUserModel model)
        {
            ViewBag.MenuItem = CurrentMenuItem;

            ArrayList errors = new ArrayList();

            if (ModelState.IsValid)
            {
                // Validation
                if (String.IsNullOrEmpty(model.UserName.Trim()))
                {
                    errors.Add("User Name is mandatory");
                }
                if (model.SelectedRoles == null)
                {
                    errors.Add("At least one role must be selected");
                }
                if (model.SelectedFacilities == null)
                {
                    errors.Add("At least one facility must be selected");
                }
                if (unitOfWork.Repository <User>().Queryable().Any(u => u.UserName == model.UserName))
                {
                    errors.Add("A user with the specified username already exists.");
                }

                if (errors.Count == 0)
                {
                    var user = new UserInfo()
                    {
                        FirstName = model.FirstName, LastName = model.LastName, UserName = model.UserName, Email = model.Email, Password = model.Password
                    };
                    IdentityResult result = await userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        var roleIdsInt64 = model.SelectedRoles.ConvertAll <Int64>(Convert.ToInt64).ToArray();

                        var roles = unitOfWork.Repository <Role>().Queryable()
                                    .Where(r => roleIdsInt64.Contains(r.Id))
                                    .Select(rk => rk.Key)
                                    .ToArray();

                        IdentityResult roleResult = await userManager.AddToRolesAsync(user.Id, roles);

                        if (roleResult.Succeeded)
                        {
                            var utemp            = unitOfWork.Repository <User>().Queryable().SingleOrDefault(u => u.Id == user.Id);
                            var facilityIdsInt64 = model.SelectedFacilities.ConvertAll <Int64>(Convert.ToInt64).ToArray();

                            ArrayList deleteCollection = new ArrayList();
                            foreach (UserFacility uf in utemp.Facilities)
                            {
                                if (!facilityIdsInt64.Contains(uf.Facility.Id))
                                {
                                    // remove
                                    deleteCollection.Add(uf);
                                }
                            }
                            ArrayList addCollection = new ArrayList();
                            foreach (Int64 id in facilityIdsInt64)
                            {
                                if (!utemp.Facilities.Any(uf => uf.Id == id))
                                {
                                    // add
                                    addCollection.Add(id);
                                }
                            }

                            foreach (UserFacility uf in deleteCollection)
                            {
                                utemp.Facilities.Remove(uf);
                            }
                            foreach (Int64 id in addCollection)
                            {
                                var uf = new UserFacility()
                                {
                                    Facility = unitOfWork.Repository <Facility>().Queryable().SingleOrDefault(f => f.Id == id), User = utemp
                                };
                                utemp.Facilities.Add(uf);
                            }
                            utemp.Active = true;
                            unitOfWork.Repository <User>().Update(utemp);
                            unitOfWork.Complete();

                            HttpCookie cookie = new HttpCookie("PopUpMessage");
                            cookie.Value = "User record added successfully";
                            Response.Cookies.Add(cookie);

                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            AddErrors(roleResult);
                        }
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
                else // if (errors.Count == 0)
                {
                    AddErrors(errors);
                }
            }

            ViewBag.Roles = unitOfWork.Repository <Role>().Queryable().ToList().Select(r => new SelectListItem {
                Value = r.Id.ToString(), Text = r.Name
            }).ToList();

            ViewBag.Facilities = unitOfWork.Repository <Facility>().Queryable().OrderBy(f => f.FacilityName).ToList().Select(f => new SelectListItem {
                Value = f.Id.ToString(), Text = f.FacilityName
            }).ToList();

            return(View(model));
        }