public async Task <ActionResult> Register(RegisterViewModel model)
        {
            var clientIP = Request.UserHostAddress.ToString();

            if (!IPCount.CheckIsAble(clientIP))
            {
                ModelState.AddModelError("UserName", "已经超过尝试次数");
            }
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = new SysUser {
                UserName = model.UserName, Email = CommonCodeGenerator.GenerateEmail(model.UserName),
            };

            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var iUnitOfWork = DependencyResolver.Current.GetService <IUnitOfWork>();
                if (!string.IsNullOrEmpty(model.DepartmentId))
                {
                    SysDepartmentSysUserService.Save(null,
                                                     new SysDepartmentSysUser {
                        SysDepartmentId = model.DepartmentId, SysUserId = user.Id
                    });
                }
                //前三个用户赋予管理员权限

                if (UserManager.Users.Count() < 3)
                {
                    await UserManager.AddToRoleAsync(user.Id, "系统管理员");
                }
                else
                {
                    await UserManager.AddToRoleAsync(user.Id, "运动员");
                }
                await iUnitOfWork.CommitAsync();

                TempData[Alerts.Success] = "注册成功,请您登陆";

                //await SignInManager.SignInAsync(user, false, true);

                // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                // 发送包含此链接的电子邮件
                //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                //await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">这里</a>来确认你的帐户");

                return(RedirectToAction("Index", "Index", new { area = "Platform" }));
            }
            AddErrors(result);


            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return(View(model));
        }
Ejemplo n.º 2
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.UpdatedDate = DateTimeLocal.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
                    });
                }

                if (!string.IsNullOrEmpty(collection.DepartmentId))
                {
                    _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.Email = CommonCodeGenerator.GenerateEmail(collection.UserName);
                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));
        }