Exemple #1
0
        public ActionResult EditUser(Entities.SysUser model, string returnUrl = null)
        {
            ModelState.Remove("Id");
            ViewBag.ReturnUrl = Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("userIndex");
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            if (!String.IsNullOrEmpty(model.MobilePhone))
            {
                model.MobilePhone = StringUitls.toDBC(model.MobilePhone);
            }
            model.Name = model.Name.Trim();

            if (model.Id == Guid.Empty)
            {
                model.Id           = Guid.NewGuid();
                model.CreationTime = DateTime.Now;
                model.Salt         = EncryptorHelper.CreateSaltKey();
                model.Account      = StringUitls.toDBC(model.Account.Trim());
                model.Enabled      = true;
                model.IsAdmin      = false;
                model.Password     = EncryptorHelper.GetMD5(model.Account + model.Salt);
                model.Creator      = WorkContext.CurrentUser.Id;
                _sysUserService.insertSysUser(model);
            }
            else
            {
                model.ModifiedTime = DateTime.Now;
                model.Modifier     = WorkContext.CurrentUser.Id;
                _sysUserService.updateSysUser(model);
            }
            return(Redirect(ViewBag.ReturnUrl));
        }
Exemple #2
0
        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(NotValid());
            }
            (bool Status, string Message)res;
            var item = _mapper.Map <Entities.Sys_User>(SysUser);

            if (SysUser.Id != Guid.Empty)
            {
                res = _sysUserService.UpdateUser(SysUser, UserId);
            }
            else
            {
                item.Account      = item.Account.TrimSpace();
                item.Id           = CombGuid.NewGuid();
                item.CreationTime = DateTime.Now;
                item.Creator      = UserId;
                item.Salt         = EncryptorHelper.CreateSaltKey();
                item.Password     = (EncryptorHelper.GetMD5(item.Account + item.Salt));
                res = _sysUserService.AddUser(item);
            }
            AjaxData.Message = res.Message;
            AjaxData.Code    = res.Status ? 0 : 2001;
            if (res.Status)
            {
                _sysRoleService.SetUserRoles(item.Id, RoleIds, UserId);
            }
            return(Json(AjaxData));
        }
Exemple #3
0
        public IActionResult UserEdit(Sys_UserMapping SysUser, List <string> RoleIds)
        {
            (bool Status, string Message)res;
            var item = _mapper.Map <Entities.sys_user>(SysUser);

            if (!String.IsNullOrEmpty(SysUser.id))
            {
                res = _sysUserService.UpdateUser(SysUser, UserId);
            }
            else
            {
                item.account       = item.account.TrimSpace();
                item.id            = CombGuid.NewGuidAsString();
                item.creation_time = DateTime.Now;
                item.creator       = UserId;
                item.salt          = EncryptorHelper.CreateSaltKey();
                item.password      = (EncryptorHelper.GetMD5(item.account + item.salt));
                res = _sysUserService.AddUser(item);
            }
            AjaxData.Message = res.Message;
            AjaxData.Success = res.Status;
            if (res.Status)
            {
                _sysRoleService.SetUserRoles(item.id, RoleIds, UserId);
            }
            return(Json(AjaxData));
        }
Exemple #4
0
        public static void InitData(this IApplicationBuilder app)
        {
            #region 自动创建数据库
            // Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.SqlServer.Design
            //dotnet ef migrations add InitialEFDbContext -c EFDbContext -o Data/Migrations/DemoDB
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetRequiredService <EFDbContext>();

                /*
                 * System.Data.SqlClient.SqlException:“将 FOREIGN KEY 约束 'FK_SysPermission_SysRole_RoleId' 引入表 'SysPermission'
                 * 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
                 * 无法创建约束。请参阅前面的错误消息。
                 */
                context.Database.Migrate();
            }
            #endregion

            #region 初始化数据
            var _sysUserService = EnginContext.Current.Resolve <ISysUserService>();
            var _sysRoleService = EnginContext.Current.Resolve <ISysRoleService>();

            SysUser sysUser = new SysUser();
            if (!_sysUserService.ExistUser())
            {
                sysUser.Id           = Guid.NewGuid();
                sysUser.Account      = "admin";
                sysUser.Name         = "超级管理员";
                sysUser.Email        = "";
                sysUser.MobilePhone  = "";
                sysUser.Salt         = EncryptorHelper.CreateSaltKey();
                sysUser.Password     = EncryptorHelper.GetMD5(sysUser.Account + sysUser.Salt);
                sysUser.Enabled      = true;
                sysUser.IsAdmin      = true;
                sysUser.CreationTime = DateTime.Now;
                sysUser.LoginLock    = false;
                sysUser.IsDeleted    = false;
                _sysUserService.InsertSysUser(sysUser);
            }

            if (!_sysRoleService.ExistRole())
            {
                SysRole sysRole = new SysRole()
                {
                    Id           = Guid.NewGuid(),
                    Name         = "超级管理员",
                    Creator      = sysUser.Id,
                    CreationTime = DateTime.Now
                };
                _sysRoleService.InsertRole(sysRole);
            }
            #endregion
        }
Exemple #5
0
        public IActionResult Index()
        {
            var Salt     = EncryptorHelper.CreateSaltKey();
            var Password = EncryptorHelper.GetMD5("bobo" + Salt);

            string r = EncryptorHelper.GetMD5(Guid.NewGuid().ToString());

            HttpContext.Session.SetString(R_KEY, r);
            LoginModel loginModel = new LoginModel()
            {
                R = r
            };

            return(View(loginModel));
        }
        /// <summary>
        /// 重置密码(默认重置成和账号一样)
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="modifer">修改人Id</param>
        public void ResetPassword(Guid id, Guid modifer)
        {
            var user = _sysUserRepository.GetById(id);

            if (user != null)
            {
                if (string.IsNullOrEmpty(user.Salt))
                {
                    user.Salt = EncryptorHelper.CreateSaltKey();
                }
                user.Password     = EncryptorHelper.GetMD5(user.Account + user.Salt);
                user.ModifiedTime = DateTime.Now;
                user.Modifier     = modifer;
                _sysUserRepository.DbContext.SaveChanges();
            }
        }
Exemple #7
0
        public IActionResult register(Entities.RegisterModel model)
        {
            if (_sysUserService.existAccount(model.Account))
            {
                AjaxData.Status  = false;
                AjaxData.Message = "该账号已存在";
                return(Json(AjaxData));
            }

            if (_sysUserService.existEmail(model.Email))
            {
                AjaxData.Status  = false;
                AjaxData.Message = "该邮箱已被注册";
                return(Json(AjaxData));
            }

            if (_sysUserService.existMobilePhone(model.MobilePhone))
            {
                AjaxData.Status  = false;
                AjaxData.Message = "该手机号码已被注册";
                return(Json(AjaxData));
            }


            Entities.SysUser user = new Entities.SysUser();
            user.Account     = model.Account;
            user.Name        = model.Name;
            user.Email       = model.Email;
            user.MobilePhone = model.MobilePhone;

            user.Id           = Guid.NewGuid();
            user.CreationTime = DateTime.Now;
            user.Salt         = EncryptorHelper.CreateSaltKey();
            user.Account      = user.Account.Trim();
            user.Enabled      = true;
            user.IsAdmin      = false;
            user.Password     = EncryptorHelper.GetMD5(model.Password + user.Salt);
            user.Creator      = user.Id;
            _sysUserService.insertSysUser(user);

            AjaxData.Status  = true;
            AjaxData.Message = "注册成功";

            return(Json(AjaxData));
        }
Exemple #8
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new GeneralDbContext(serviceProvider.GetRequiredService <DbContextOptions <GeneralDbContext> >()))
            {
                if (context.SysUsers.Any())
                {
                    return;
                }
                var salt = EncryptorHelper.CreateSaltKey();

                context.SysUsers.AddRange(
                    new Entities.SysUser
                {
                    Id          = Guid.NewGuid(),
                    Account     = "41606217",
                    Name        = "吴宗锦",
                    Salt        = salt,
                    Password    = EncryptorHelper.GetMD5("15160296867" + salt),
                    IsAdmin     = true,
                    Email       = "*****@*****.**",
                    MobilePhone = "18229065977",


                    Sex              = "男",
                    Enabled          = true,
                    CreationTime     = DateTime.Now,
                    LoginFailedNum   = 0,
                    AllowLoginTime   = null,
                    LoginLock        = false,
                    LastLoginTime    = null,
                    LastIpAddress    = "",
                    LastActivityTime = DateTime.Now,
                    IsDeleted        = false,
                    DeletedTime      = null,
                    ModifiedTime     = null,
                    Modifier         = null,
                    Creator          = null,
                    Avatar           = new byte[0],
                }
                    );
                context.SaveChanges();
            }
        }
 public IActionResult EditUser(SysUser model, string sysRole, string returnUrl = null)
 {
     ViewBag.ReturnUrl   = Url.IsLocalUrl(returnUrl) ? returnUrl : Url.RouteUrl("userIndex");
     ViewBag.SysRoleList = _sysRoleService.GetAllRoles().Select(o => new SelectListItem()
     {
         Value = o.Id.ToString(), Text = o.Name, Selected = (o.Id.ToString() == sysRole)
     }).ToList();
     ModelState.Remove("Id");
     if (!ModelState.IsValid)
     {
         return(View(model));
     }
     if (!string.IsNullOrEmpty(model.MobilePhone))
     {
         model.MobilePhone = StringUitls.toDBC(model.MobilePhone);
     }
     model.Name = model.Name.Trim();
     if (model.Id == Guid.Empty)
     {
         model.Id           = Guid.NewGuid();
         model.CreationTime = DateTime.Now;
         model.Salt         = EncryptorHelper.CreateSaltKey();
         model.Account      = StringUitls.toDBC(model.Account);
         model.Enabled      = true;
         model.IsAdmin      = false;
         model.Password     = EncryptorHelper.GetMD5(model.Account + model.Salt);
         model.Creator      = _workContext.CurrentUser().Id;
         _sysUserService.InsertSysUser(model);
     }
     else
     {
         model.ModifiedTime = DateTime.Now;
         model.Modifier     = _workContext.CurrentUser().Id;
         _sysUserService.UpdateSysUser(model);
     }
     if (!string.IsNullOrEmpty(sysRole))
     {
         _sysUserRoleService.InsertOrUpdateSysUserRole(model.Id, new Guid(sysRole));
     }
     return(Redirect(ViewBag.ReturnUrl));
 }
Exemple #10
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // base.OnModelCreating(modelBuilder);
            var salt = EncryptorHelper.CreateSaltKey();

            modelBuilder.Entity <SysUser>().HasData(new SysUser()
            {
                Id               = Guid.NewGuid(),
                Account          = "admin",
                Name             = "超级管理员",
                Salt             = salt,
                Password         = EncryptorHelper.GetMD5("123456" + salt),
                Enabled          = true,
                IsAdmin          = true,
                CreationTime     = DateTime.Now,
                LoginFailedNum   = 0,
                LoginLock        = false,
                LastActivityTime = null,
                IsDeleted        = false
            });
        }