Пример #1
0
        public void add(addUserModel model, [FromServices] IOptions <WebSetting> settings)
        {
            if (ModelState.IsValid)
            {
                var u = _context.PlayUser.SingleOrDefault(t => t.Username == model.username);
                if (u != null)
                {
                    return;           //已经存在用户不让注册
                }
                var newUser = new PlayUser();
                newUser.Username = model.username;
                newUser.Name     = model.name;
                newUser.Password = settings.Value.DefultPassword;
                newUser.Lv       = model.lv;
                newUser.JoinDate = DateTime.Now;
                _context.PlayUser.Add(newUser);
                ////为新增用户添加adminSpace
                //if(settings.Value.DefultUseAdminSpace)
                //{
                //    //ef core 目前不支持 LazyLoading ,只能自己查询
                //    var adminUser = _context.PlayUser.First(t => t.Lv == 99);
                //    var sps = _context.TestSpace.Where(t => t.UserId == adminUser.Id);

                //    foreach (var s in sps)
                //    {
                //        TestSpaceMap tsm = new TestSpaceMap();
                //        tsm.UserId = newUser.Id;
                //        tsm.SpaceId = s.Id;
                //        _context.TestSpaceMap.Add(tsm);
                //    }
                //}

                _context.SaveChanges();
            }
            else
            {
                Response.StatusCode = 400;
            }
        }
Пример #2
0
        public IActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var u = _context.PlayUser.FirstOrDefault(t => t.Username == model.UserName);
                if (u != null)
                {
                    ModelState.AddModelError("", "帐号已存在!");
                    return(View(model));
                }
                var newUser = new PlayUser();
                newUser.Username = model.UserName;
                newUser.Name     = model.Name;
                newUser.Password = model.Password;
                newUser.Lv       = 1;
                newUser.JoinDate = DateTime.Now;
                _context.PlayUser.Add(newUser);
                _context.SaveChanges();
                return(RedirectToAction("Login"));
            }

            return(View(model));
        }