Пример #1
0
        public IActionResult Register(RegisterModel model)
        {
            model.isAny = _SY_UserService.GetAll().Result.Any();
            if (model.isAny)
            {
                return(RedirectToAction("Index", "Login"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //Kiểm tra mật khẩu
            if (model.Password != model.RePassword)
            {
                ModelState.AddModelError("", "Mật khẩu không khớp");
                return(View(model));
            }

            var salat = Guid.NewGuid().ToString();

            var obj = new SY_User()
            {
                Id            = Guid.NewGuid().ToString(),
                Name          = model.Name,
                Username      = model.Username,
                Active        = true,
                Avatar        = "",
                Password      = CryptoHelper.EncryptPass_User(model.Password, salat),
                PasswordSalat = salat,
                isAdmin       = true
            };

            var result = _SY_UserService.Create(obj).Result;

            if (result.isSuccess)
            {
                return(RedirectToAction("Index", "Login"));
            }
            else
            {
                ModelState.AddModelError("", result.Message);
                return(View(model));
            }
        }
Пример #2
0
        public async Task <IActionResult> Create(SY_User_Submit model, bool SaveAndCountinue = false)
        {
            model.Data_Role = await _SY_RoleService.GetAllActiveOrder();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //
            var existed = await _SY_UserService.GetByUsername(model.Username);

            if (existed != null)
            {
                ModelState.AddModelError("Username", "Tài khoản tồn tại");
                return(View(model));
            }

            if (string.IsNullOrWhiteSpace(model.Password))
            {
                model.Password = "******";
            }
            else
            {
                if (model.Password != model.RePassword)
                {
                    ModelState.AddModelError("RePassword", "Mật khẩu không khớp");
                    return(View(model));
                }
            }

            var obj = new SY_User()
            {
                Active        = model.Active,
                Id            = Guid.NewGuid().ToString(),
                Password      = model.Password,
                PasswordSalat = Guid.NewGuid().ToString(),
                Name          = model.Name,
                Username      = model.Username,
                isAdmin       = model.isAdmin,
                Phone         = model.Phone
            };

            if (!string.IsNullOrWhiteSpace(model.RoleIds))
            {
                var ks = model.RoleIds.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                model.Roles = new List <string>();
                foreach (var item in ks)
                {
                    model.Roles.Add(item);
                }

                foreach (var item in model.Roles)
                {
                    var t = new SY_Map_User_Role()
                    {
                        Id     = Guid.NewGuid().ToString(),
                        RoleId = item,
                        UserId = obj.Id
                    };

                    await _SY_RoleService.CreateMap(t);
                }
            }

            //Mã hóa pass
            obj.Password = CryptoHelper.EncryptPass_User(obj.Password, obj.PasswordSalat);

            //Thực hiện thêm mới
            var result = await _SY_UserService.Create(obj);

            if (result.isSuccess)
            {
                if (SaveAndCountinue)
                {
                    TempData["Success"] = "Thêm mới thành công";
                    return(RedirectToAction("Create"));
                }

                return(RedirectToAction("Index"));
            }
            else
            {
                ModelState.AddModelError("", result.Message);
                return(View(obj));
            }
        }