Exemple #1
0
        public async Task <IActionResult> Add(AccViewModel model)
        {
            var result = await userService.Add(model);

            if (result == null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            return(RedirectToAction("Index", new { errorMessage = result }));
        }
Exemple #2
0
        public async Task <IActionResult> EditUser(AccViewModel model)
        {
            var result = await userService.EditUser(model);

            if (result == null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            return(RedirectToAction("Index", new { errorMessage = result, errorId = model.UserV2.Id }));
        }
Exemple #3
0
        public async Task <string> Add(AccViewModel model)
        {
            try
            {
                string errorMessage = null;
                var    user         = new IdentityUserV2
                {
                    Email    = model.RegisterEmail,
                    UserName = model.RegisterName,
                    NickName = model.UserV2.NickName,
                    City     = model.UserV2.City,
                    Position = model.UserV2.Position
                };
                string[] deptInfo = model.Department.Split(',');
                user.Department = deptInfo[1];
                user.DeptCode   = deptInfo[0].Trim(',');
                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    logger.LogWarning("添加用户{LogType}{CustomProperty}", "Operate", user.UserName);
                    if (user.DeptCode != "0")
                    {
                        int deptId = deptManager.GetDeptId(user.DeptCode);
                        var ids    = await deptManager.GetRolesIdAsync(deptId);

                        foreach (var id in ids)
                        {
                            context.UserRoles.Add(new IdentityUserRole <string> {
                                UserId = user.Id, RoleId = id
                            });
                        }
                        context.SaveChanges();
                    }
                    return(errorMessage);
                }
                foreach (var error in result.Errors)
                {
                    errorMessage += error.Description + "\r\n";
                }
                logger.LogWarning("添加用户失败{LogType}{CustomProperty}", "Operate", errorMessage);
                return(errorMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #4
0
 public AccViewModel Index()
 {
     try
     {
         var          users       = userManager.Users.ToList();
         var          departments = context.Departments.ToList();
         AccViewModel model       = new AccViewModel(departments)
         {
             Users = users
         };
         logger.LogWarning("读取账户");
         return(model);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemple #5
0
        public async Task <string> EditUser(AccViewModel model)
        {
            try
            {
                string errorMessage = null;
                var    user         = await userManager.FindByIdAsync(model.UserV2.Id);

                if (user == null)
                {
                    errorMessage = "您操作的用户已不存在";
                    return(errorMessage);
                }
                string   oldDeptCode = user.DeptCode;
                string[] deptInfo    = model.Department.Split(',');
                user.Department = deptInfo[1];
                user.DeptCode   = deptInfo[0].Trim(',');
                user.Position   = model.UserV2.Position;
                user.NickName   = model.UserV2.NickName;
                user.City       = model.UserV2.City;
                var result = await userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    if (oldDeptCode != "0")
                    {
                        int deptId = deptManager.GetDeptId(oldDeptCode);
                        var ids    = await deptManager.GetRolesIdAsync(deptId);

                        foreach (var id in ids)
                        {
                            var ur = context.UserRoles.FirstOrDefault(ur => ur.RoleId == id && ur.UserId == user.Id);
                            if (ur != null)
                            {
                                context.UserRoles.Remove(new IdentityUserRole <string> {
                                    UserId = user.Id, RoleId = id
                                });
                            }
                        }
                    }
                    if (user.DeptCode != "0")
                    {
                        int deptId = deptManager.GetDeptId(user.DeptCode);
                        var ids    = await deptManager.GetRolesIdAsync(deptId);

                        foreach (var id in ids)
                        {
                            var ur = context.UserRoles.FirstOrDefault(ur => ur.RoleId == id && ur.UserId == user.Id);
                            if (ur == null)
                            {
                                context.UserRoles.Add(new IdentityUserRole <string> {
                                    UserId = user.Id, RoleId = id
                                });
                            }
                        }
                    }
                    context.SaveChanges();
                    logger.LogWarning("修改用户信息{LogType}{CustomProperty}", "Operate", JsonConvert.SerializeObject(model));
                    return(errorMessage);
                }
                foreach (var error in result.Errors)
                {
                    errorMessage += error.Description + ";";
                }
                logger.LogWarning("修改用户信息失败{LogType}{CustomProperty}", "Operate", JsonConvert.SerializeObject(errorMessage));
                return(errorMessage);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }