Exemplo n.º 1
0
        public async Task <APIResult <bool> > SaveUserInfo(UserInfoModels model)
        {
            if (ModelState.IsValid)
            {
                var user = _iSysUserService.GetById(_iUserInfo.UserId);
                user.FullName       = model.FullName;
                user.Birthday       = model.Birthday;
                user.Sex            = model.Sex;
                user.SportGrade     = model.SportGrade;
                user.TrainId        = model.TrainId;
                user.Start4Training = model.Start4Training;

                //todo 保存教练
                if (!string.IsNullOrEmpty(model.DepartmentId))
                {
                    _iSysDepartmentSysUserService.Delete(a => a.SysUserId == model.DepartmentId);

                    _iSysDepartmentSysUserService.Save(null,
                                                       new SysDepartmentSysUser {
                        SysDepartmentId = model.DepartmentId, SysUserId = user.Id
                    });
                }

                _iSysUserService.Save(user.Id, user);
                await _iUnitOfWork.CommitAsync();

                return(new APIResult <bool>(true));
            }
            return(new APIResult <bool>(false, 100, "操作失败"));
        }
Exemplo n.º 2
0
        public ActionResult Edit(Guid?id, SysUser _sysUser)
        {
            Debug.WriteLine("a");

            if (!ModelState.IsValid)
            {
                Edit(id);
                return(View(_sysUser));
            }

            if (id.HasValue)
            {
                //清除原有部门数据
                _sysDepartmentSysUserService.Delete(a => a.SysUserId.Equals(id.Value));
                //清除原有数据
                _sysRoleSysUserService.Delete(a => a.SysUserId.Equals(id.Value));
            }

            _sysUserService.Save(id, _sysUser);

            if (_sysUser.SysDepartmentsId != null)
            {
                foreach (Guid sysDepartmentId in _sysUser.SysDepartmentsId)
                {
                    _sysDepartmentSysUserService.Save(null, new SysDepartmentSysUser
                    {
                        SysUserId       = _sysUser.Id,
                        SysDepartmentId = sysDepartmentId
                    });
                }
            }

            if (_sysUser.SysRolesId != null)
            {
                foreach (Guid sysRoleId in _sysUser.SysRolesId)
                {
                    _sysRoleSysUserService.Save(null, new SysRoleSysUser
                    {
                        SysUserId = _sysUser.Id,
                        SysRoleId = sysRoleId
                    });
                }
            }

            _unitOfWork.Commit();

            return(RedirectToAction("Index"));
        }
        public ActionResult Edit(Guid?id, SysUser collection)
        {
            if (!ModelState.IsValid)
            {
                Edit(id);
                return(View(collection));
            }

            if (id.HasValue)
            {
                //清除原有部门数据
                _sysDepartmentSysUserService.Delete(a => a.SysUserId.Equals(id.Value));
                //清除原有数据
                _sysRoleSysUserService.Delete(a => a.SysUserId.Equals(id.Value));
            }

            _sysUserService.Save(id, collection);

            if (collection.SysDepartmentsId != null)
            {
                foreach (var sysDepartmentId in collection.SysDepartmentsId)
                {
                    _sysDepartmentSysUserService.Save(null, new SysDepartmentSysUser
                    {
                        SysUserId       = collection.Id,
                        SysDepartmentId = sysDepartmentId
                    });
                }
            }

            if (collection.SysRolesId != null)
            {
                foreach (var sysRoleId in collection.SysRolesId)
                {
                    _sysRoleSysUserService.Save(null, new SysRoleSysUser
                    {
                        SysUserId = collection.Id,
                        SysRoleId = sysRoleId
                    });
                }
            }

            _unitOfWork.Commit();

            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Edit(string id, SysUserEditModel collection)
        {
            if (!ModelState.IsValid)
            {
                Edit(id);
                ViewBag.SysEnterprisesId =
                    new MultiSelectList(
                        _iSysEnterpriseService.GetAll(
                            a => a.SysEnterpriseSysUsers.Any(b => b.SysUserId == _iUserInfo.UserId)), "Id",
                        "EnterpriseName", collection.SysEnterprisesId);
                ViewBag.SysRolesId = new MultiSelectList(_sysRoleService.GetAll(), "Id", "RoleName",
                                                         collection.SysRolesId);

                ViewBag.DepartmentId = _iDepartmentService.GetAll().ToSystemIdSelectList(collection.DepartmentId);
                return(View(collection));
            }

            var config = new MapperConfiguration(a => a.CreateMap <SysUserEditModel, SysUser>());

            //config.AssertConfigurationIsValid();

            var mapper = config.CreateMapper();

            if (!string.IsNullOrEmpty(id))
            {
                //更新用户信息
                var item = _sysUserService.GetById(id);

                item.UpdatedDateTime = DateTimeOffset.Now;

                mapper.Map(collection, item);

                //处理角色
                //item.Roles.Clear();


                foreach (var role in _sysRoleService.GetAll().ToList())
                {
                    await UserManager.RemoveFromRoleAsync(item.Id, role.Name);
                }

                foreach (var roleId in collection.SysRolesId)
                {
                    item.Roles.Add(new IdentityUserRole {
                        RoleId = roleId, UserId = item.Id
                    });
                }

                _iSysDepartmentSysUserService.Delete(
                    a => a.SysUserId == item.Id && a.SysDepartment.EnterpriseId == _iUserInfo.EnterpriseId);

                _iSysDepartmentSysUserService.Save(null,
                                                   new SysDepartmentSysUser {
                    SysDepartmentId = collection.DepartmentId, SysUserId = item.Id
                });

                //处理关联企业
                //限制编辑自己的关联企业
                if (item.Id != _iUserInfo.UserId)
                {
                    foreach (
                        var ent in _iSysEnterpriseSysUserService.GetAll(a => a.SysUserId == _iUserInfo.UserId).ToList())
                    {
                        _iSysEnterpriseSysUserService.Delete(
                            a => a.SysEnterpriseId == ent.SysEnterpriseId && a.SysUserId == item.Id);
                    }

                    foreach (var entId in collection.SysEnterprisesId)
                    {
                        item.SysEnterpriseSysUsers.Add(new SysEnterpriseSysUser
                        {
                            SysEnterpriseId = entId,
                            SysUserId       = item.Id
                        });
                    }
                }

                await _unitOfWork.CommitAsync();

                if (!string.IsNullOrEmpty(collection.Password))
                {
                    UserManager.RemovePassword(id);
                    var re = UserManager.AddPassword(id, collection.Password);

                    if (!re.Succeeded)
                    {
                        foreach (var error in re.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
                }
            }
            else
            {
                collection.Id = Guid.NewGuid().ToString();
                var item = mapper.Map <SysUserEditModel, SysUser>(collection);

                item.SysDepartmentSysUsers.Add(new SysDepartmentSysUser
                {
                    SysUserId       = item.Id,
                    SysDepartmentId = collection.DepartmentId
                });

                foreach (var roleId in collection.SysRolesId)
                {
                    item.Roles.Add(new IdentityUserRole {
                        RoleId = roleId, UserId = item.Id
                    });
                }

                foreach (var entId in collection.SysEnterprisesId)
                {
                    item.SysEnterpriseSysUsers.Add(new SysEnterpriseSysUser
                    {
                        SysEnterpriseId = entId,
                        SysUserId       = item.Id
                    });
                }

                //创建用户
                var re = await UserManager.CreateAsync(item, collection.Password);

                if (!re.Succeeded)
                {
                    foreach (var error in re.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                Edit(id);
                ViewBag.SysRolesId = new MultiSelectList(_sysRoleService.GetAll(), "Id", "Name", collection.SysRolesId);

                ViewBag.DepartmentId = _iDepartmentService.GetAll().ToSystemIdSelectList(collection.DepartmentId);
                return(View(collection));
            }

            return(new EditSuccessResult(id));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> EditAsync(string id, SysUserEditModel collection)
        {
            if (!ModelState.IsValid)
            {
                await EditAsync(id);

                ViewBag.SysRolesId = new MultiSelectList(_sysRoleService.Roles.Select(a => new { a.Id, a.Name }), "Id", "Name", collection.SysRolesId);

                ViewBag.DepartmentId = new MultiSelectList(_iDepartmentService.GetAll().Select(a => new { id = a.Id, Name = a.SystemId + a.Name }), "Id", "Name", collection.DepartmentId);

                return(View(collection));
            }

            if (string.IsNullOrEmpty(id))
            {
                //新建用户

                var config = new MapperConfiguration(a => a.CreateMap <SysUserEditModel, IdentityUser>());

                var user = config.CreateMapper().Map <IdentityUser>(collection);

                var identityResult = await _userManager.CreateAsync(user, collection.Password);

                if (!identityResult.Succeeded)
                {
                    foreach (var error in identityResult.Errors)
                    {
                        throw new Exception(error.Description);
                    }
                }

                id = user.Id;
            }
            else
            {
                //更新用户
                var user = await _userManager.FindByIdAsync(id);

                var config = new MapperConfiguration(a => a.CreateMap <SysUserEditModel, IdentityUser>());

                config.CreateMapper().Map(collection, user);

                var identityResult = await _userManager.UpdateAsync(user);

                if (!identityResult.Succeeded)
                {
                    foreach (var error in identityResult.Errors)
                    {
                        throw new Exception(error.Description);
                    }
                }

                if (!string.IsNullOrEmpty(collection.Password))
                {
                    var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                    identityResult = await _userManager.ResetPasswordAsync(user, token, collection.Password);

                    if (!identityResult.Succeeded)
                    {
                        foreach (var error in identityResult.Errors)
                        {
                            throw new Exception(error.Description);
                        }
                    }
                }
            }

            //部门
            await _iSysDepartmentSysUserService.Delete(a => a.SysUserId == id);

            foreach (var DepartmentId in collection.DepartmentId)
            {
                await _iSysDepartmentSysUserService.SaveAsync(null, new SysDepartmentSysUser { SysDepartmentId = DepartmentId, SysUserId = id });
            }

            //删除角色
            await _iSysUserRoleService.Delete(a => a.UserId.Equals(id) && !collection.SysRolesId.Any(b => b == a.RoleId));

            //添加角色
            foreach (var s in collection.SysRolesId.Where(a => !_iSysUserRoleService.GetAll(b => b.UserId.Equals(id)).Any(c => c.RoleId == a)))
            {
                await _iSysUserRoleService.AddAsync(new IdentityUserRole <string>() { RoleId = s, UserId = id });
            }

            await _unitOfWork.CommitAsync();

            return(new EditSuccessResult(id));
        }