[ValidateAntiForgeryToken]//防伪标记 预防坏蛋攻击 public IActionResult UserChagePwd(string useroldpwd, string usernewpwd) { var response = new ResponseModel(); var userlogin = _userAccount.GetUserInfo(); if (!userlogin.UserPwd.Equals(MD5Encrypt.Get32MD5One(useroldpwd))) { response.RetMsg = "原密码不正确,请重新输入"; response.RetCode = StatesCode.failure; return(Ok(response)); } var user = _sysUserBLL.GetModels(t => t.UserId.Equals(userlogin.UserId)).SingleOrDefault(); if (user != null) { user.UserPwd = MD5Encrypt.Get32MD5One(usernewpwd); if (_sysUserBLL.Update(user)) { response.RetMsg = "密码修改成功"; response.RetCode = StatesCode.success; //密码修改成功 重新登录 _userAccount.Login(user); } else { response.RetMsg = "密码修改失败"; response.RetCode = StatesCode.failure; } } else { response.RetMsg = "账户不存在,请联系管理员!"; response.RetCode = StatesCode.failure; } return(Ok(response)); }
[ValidateAntiForgeryToken]//防伪标记 预防坏蛋攻击 public string UserChagePwd(string useroldpwd, string usernewpwd) { var response = new ResponseModel(); var userlogin = injection.GetT <UserAccount>().GetUserInfo(); if (!userlogin.UserPwd.Equals(MD5Encrypt.Get32MD5One(useroldpwd))) { response.Messages = "原密码不正确,请重新输入"; response.StateCode = StatesCode.failure; return(JsonHelper.ObjectToJson(response)); } var user = injection.GetT <ISysUserBLL>().GetModels(t => t.UserId.Equals(userlogin.UserId)).SingleOrDefault(); if (user != null) { user.UserPwd = MD5Encrypt.Get32MD5One(usernewpwd); if (injection.GetT <ISysUserBLL>().Update(user)) { response.Messages = "密码修改成功"; response.StateCode = StatesCode.success; //密码修改成功 重新登录 injection.GetT <UserAccount>().Login(user); } else { response.Messages = "密码修改失败"; response.StateCode = StatesCode.failure; } } else { response.Messages = "账户不存在,请联系管理员!"; response.StateCode = StatesCode.failure; } return(JsonHelper.ObjectToJson(response)); }
/// <summary> /// 初始化数据库 /// </summary> /// <param name="service"></param> /// <returns></returns> public static bool InitDB(IServiceProvider service) { using (var serviceScope = service.CreateScope()) { var context = serviceScope.ServiceProvider.GetService <ZhouLiContext>(); var userAdmin = context.SysUser.Where(t => t.UserName.Equals("zhouli")).SingleOrDefault(); if (userAdmin == null) { using (var tran = context.Database.BeginTransaction()) { try { #region 初始化字典表 //初始化字典表---------------------------------------------------------开始 //添加权限类型 context.DictAuthorityType.AddRange(new DictAuthorityType { AuthorityTypeName = "菜单权限", AuthorityTypeId = Guid.NewGuid().ToString() }); //初始化字典表---------------------------------------------------------结束 #endregion #region 初始化权限管理数据 #region 添加用户 //初始化用户(添加超级管理员:zhouli) var entityUser = context.SysUser.Add(new SysUser { UserAvatar = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1952694735,1793739098&fm=27&gp=0.jpg", UserName = "******", UserPwd = MD5Encrypt.Get32MD5One("123456"), UserEmail = "*****@*****.**", UserPhone = "17783042962", UserQq = "1021907330", UserWx = "17783042962", UserNikeName = "感谢有梦", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), Note = "系统初始化自动添加的", UserId = Guid.NewGuid().ToString() }); //这里不对超级管理员做任何授权,因为超级管理没有任何限制 ////添加角色 //var entityRole = context.SysRole.Add(new SysRole //{ // RoleName = "超级管理员", // Note = "系统初始化自动添加的" //}); ////添加用户角色关联表 //context.SysUrRelated.Add(new SysUrRelated //{ // UserId = entityUser.Entity.UserId, // RoleId = entityRole.Entity.RoleId //}); ////添加用户组 //var entityUserGroup = context.SysUserGroup.Add(new SysUserGroup //{ // UserGroupName = "超级管理员组", // Note = "系统初始化自动添加的" //}); ////添加用户组与用户关联表 //context.SysUuRelated.Add(new SysUuRelated //{ // UserId = entityUser.Entity.UserId, // UserGroupId = entityUserGroup.Entity.UserGroupId //}); ////添加角色与用户组关联表 //context.SysUgrRelated.Add(new SysUgrRelated //{ // UserGroupId = entityUserGroup.Entity.UserGroupId, // RoleId = entityRole.Entity.RoleId //}); //添加菜单,权限,权限菜单关联表(因为权限表与权限菜单关联表、权限菜单关联表与菜单表都是一对一的关系,也就是每添加一个菜单,就得同时往这三个表中各插入一条记录) #endregion #region 添加系统菜单 //----------------------------系统管理菜单begin var entityMenu = context.SysMenu.Add(new SysMenu { MenuName = "系统管理", MenuSort = 1, MenuIcon = "layui-icon-set", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), ParentMenuId = Guid.Empty.ToString(), MenuId = Guid.NewGuid().ToString() }); var entityAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityMenu.Entity.MenuId, AuthorityId = entityAuthority.Entity.AuthorityId }); //----------------------------系统管理菜单end //----------------------------菜单管理菜单begin var entityMenu2 = context.SysMenu.Add(new SysMenu { MenuName = "菜单管理", MenuUrl = "/System/Menu/Index", MenuIcon = "layui-icon-align-left", ParentMenuId = entityMenu.Entity.MenuId, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), MenuId = Guid.NewGuid().ToString() }); var entityAuthority2 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityMenu2.Entity.MenuId, AuthorityId = entityAuthority2.Entity.AuthorityId }); //----------------------------菜单管理菜单end //----------------------------角色管理菜单begin var entityMenu3 = context.SysMenu.Add(new SysMenu { MenuName = "角色管理", MenuUrl = "/System/Role/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuIcon = "layui-icon-prev-circle", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), MenuId = Guid.NewGuid().ToString() }); var entityAuthority3 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityMenu3.Entity.MenuId, AuthorityId = entityAuthority3.Entity.AuthorityId }); //----------------------------角色管理菜单end //----------------------------用户管理菜单begin var entityMenu4 = context.SysMenu.Add(new SysMenu { MenuName = "用户管理", MenuUrl = "/System/User/Index", MenuIcon = "layui-icon-username", ParentMenuId = entityMenu.Entity.MenuId, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), MenuId = Guid.NewGuid().ToString() }); var entityAuthority4 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityMenu4.Entity.MenuId, AuthorityId = entityAuthority4.Entity.AuthorityId }); //----------------------------用户管理菜单end //----------------------------用户组菜单begin var entityMenu5 = context.SysMenu.Add(new SysMenu { MenuName = "用户组管理", MenuUrl = "/System/UserGroup/Index", MenuIcon = "layui-icon-user", ParentMenuId = entityMenu.Entity.MenuId, CreateUserId = Guid.Empty.ToString(), CreateTime = DateTime.Now, MenuId = Guid.NewGuid().ToString() }); var entityAuthority5 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityMenu5.Entity.MenuId, AuthorityId = entityAuthority5.Entity.AuthorityId }); //----------------------------用户组菜单end #endregion #endregion #region 初始化博客管理 //----------------------------博客管理菜单begin var entityBlog = context.SysMenu.Add(new SysMenu { MenuName = "博客管理", MenuSort = 1, MenuIcon = "layui-icon-templeate-1", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), ParentMenuId = Guid.Empty.ToString(), MenuId = Guid.NewGuid().ToString() }); var entityBlogAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityBlog.Entity.MenuId, AuthorityId = entityBlogAuthority.Entity.AuthorityId }); //----------------------------博客管理菜单end //----------------------------博客友情链接菜单begin var entityBlogYqlj = context.SysMenu.Add(new SysMenu { MenuName = "友情链接", MenuSort = 1, MenuIcon = "layui-icon-share", CreateTime = DateTime.Now, MenuUrl = "/Blog/FriendshipLink/Index", CreateUserId = Guid.Empty.ToString(), ParentMenuId = entityBlog.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityBlogYqljAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityBlogYqlj.Entity.MenuId, AuthorityId = entityBlogYqljAuthority.Entity.AuthorityId }); //----------------------------博客友情链接菜单end //----------------------------博客文章菜单begin var entityBlogWz = context.SysMenu.Add(new SysMenu { MenuName = "文章管理", MenuSort = 1, MenuIcon = "layui-icon-read", MenuUrl = "/Blog/BlogArticle/Index", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), ParentMenuId = entityBlog.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityBlogWzAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityBlogWz.Entity.MenuId, AuthorityId = entityBlogWzAuthority.Entity.AuthorityId }); //----------------------------博客文章菜单end //----------------------------博客标签菜单begin var entityBlogBq = context.SysMenu.Add(new SysMenu { MenuName = "标签管理", MenuSort = 1, MenuIcon = "layui-icon-note", MenuUrl = "/Blog/BlogLable/Index", CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), ParentMenuId = entityBlog.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityBlogBqAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, CreateTime = DateTime.Now, CreateUserId = Guid.Empty.ToString(), AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { AmRelatedId = Guid.NewGuid().ToString(), MenuId = entityBlogBq.Entity.MenuId, AuthorityId = entityBlogBqAuthority.Entity.AuthorityId }); //----------------------------博客标签菜单end #endregion context.SaveChanges(); tran.Commit(); } catch (Exception e) { tran.Rollback(); throw e; } } } } return(true); }
/// <summary> /// 初始化数据库 /// </summary> /// <param name="service"></param> /// <returns></returns> public static bool InitDB(IServiceProvider service) { using (var serviceScope = service.CreateScope()) { var context = serviceScope.ServiceProvider.GetService <MyEFContext>(); var userAdmin = context.SysUser.Where(t => t.UserName.Equals("admin")).SingleOrDefault(); if (userAdmin == null) { using (var tran = context.Database.BeginTransaction()) { try { #region 初始化权限管理数据 #region 添加用户 //初始化用户(添加超级管理员:zhouli) var entityUser = context.SysUser.Add(new SysUser { UserName = "******", UserNikeName = "admin", UserPwd = MD5Encrypt.Get32MD5One("123456"), Note = "系统初始化自动添加的" }); //这里不对超级管理员做任何授权,因为超级管理没有任何限制 ////添加角色 //var entityRole = context.SysRole.Add(new SysRole //{ // RoleName = "超级管理员", // Note = "系统初始化自动添加的" //}); ////添加用户角色关联表 //context.SysUrRelated.Add(new SysUrRelated //{ // UserId = entityUser.Entity.UserId, // RoleId = entityRole.Entity.RoleId //}); ////添加用户组 //var entityUserGroup = context.SysUserGroup.Add(new SysUserGroup //{ // UserGroupName = "超级管理员组", // Note = "系统初始化自动添加的" //}); ////添加用户组与用户关联表 //context.SysUuRelated.Add(new SysUuRelated //{ // UserId = entityUser.Entity.UserId, // UserGroupId = entityUserGroup.Entity.UserGroupId //}); ////添加角色与用户组关联表 //context.SysUgrRelated.Add(new SysUgrRelated //{ // UserGroupId = entityUserGroup.Entity.UserGroupId, // RoleId = entityRole.Entity.RoleId //}); //添加菜单,权限,权限菜单关联表(因为权限表与权限菜单关联表、权限菜单关联表与菜单表都是一对一的关系,也就是每添加一个菜单,就得同时往这三个表中各插入一条记录) #endregion #region 添加系统菜单 //----------------------------系统管理菜单begin var entityMenu = context.SysMenu.Add(new SysMenu { MenuName = "系统管理", MenuSort = 1, MenuId = Guid.NewGuid().ToString() }); var entityAuthority = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu.Entity.MenuId, AuthorityId = entityAuthority.Entity.AuthorityId }); //----------------------------系统管理菜单end //----------------------------权限管理菜单begin var entityMenu1 = context.SysMenu.Add(new SysMenu { MenuName = "角色授权", MenuUrl = "/System/Authority/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityAuthority1 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu1.Entity.MenuId, AuthorityId = entityAuthority1.Entity.AuthorityId }); //----------------------------权限管理菜单end //----------------------------菜单管理菜单begin var entityMenu2 = context.SysMenu.Add(new SysMenu { MenuName = "菜单管理", MenuUrl = "/System/Menu/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityAuthority2 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu2.Entity.MenuId, AuthorityId = entityAuthority2.Entity.AuthorityId }); //----------------------------菜单管理菜单end //----------------------------角色管理菜单begin var entityMenu3 = context.SysMenu.Add(new SysMenu { MenuName = "角色管理", MenuUrl = "/System/Role/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityAuthority3 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu3.Entity.MenuId, AuthorityId = entityAuthority3.Entity.AuthorityId }); //----------------------------角色管理菜单end //----------------------------用户管理菜单begin var entityMenu4 = context.SysMenu.Add(new SysMenu { MenuName = "用户管理", MenuUrl = "/System/User/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityAuthority4 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu4.Entity.MenuId, AuthorityId = entityAuthority4.Entity.AuthorityId }); //----------------------------用户管理菜单end //----------------------------用户组菜单begin var entityMenu5 = context.SysMenu.Add(new SysMenu { MenuName = "用户组管理", MenuUrl = "/System/UserGroup/Index", ParentMenuId = entityMenu.Entity.MenuId, MenuId = Guid.NewGuid().ToString() }); var entityAuthority5 = context.SysAuthority.Add(new SysAuthority { AuthorityType = 1, AuthorityId = Guid.NewGuid().ToString() }); context.SysAmRelated.Add(new SysAmRelated { MenuId = entityMenu5.Entity.MenuId, AuthorityId = entityAuthority5.Entity.AuthorityId }); //----------------------------用户组菜单end #endregion #endregion context.SaveChanges(); tran.Commit(); } catch (Exception) { tran.Rollback(); } } } } return(true); }
/// <summary> /// 添加/编辑用户 /// </summary> /// <param name="userDto"></param> /// <param name="userId">当前登录用户id</param> /// <returns></returns> public MessageModel AddorEditUser(SysUserDto userDto, string userId) { var messageModel = new MessageModel(); var user = Mapper.Map <SysUser>(userDto); int intcount = usersDAL.GetCount(t => t.UserName.Equals(user.UserName)); //添加 if (string.IsNullOrEmpty(user.UserId)) { if (intcount == 0) { user.UserId = Guid.NewGuid().ToString(); //创建人id user.CreateUserId = userId; //默认密码 user.UserPwd = MD5Encrypt.Get32MD5One("123456"); user.CreateTime = DateTime.Now; //user.EditTime = DateTime.Now; //添加用户 if (Add(user)) { messageModel.Message = "添加成功"; } else { messageModel.Message = "添加失败"; messageModel.Result = false; } } else { messageModel.Message = "用户名已经被注册"; messageModel.Result = false; } } //修改 else { var user_edit = GetModels(t => t.UserId.Equals(user.UserId)).SingleOrDefault(); user_edit.UserName = user.UserName; user_edit.UserNikeName = user.UserNikeName; user_edit.UserSex = user.UserSex; user_edit.UserBirthday = user.UserBirthday; user_edit.UserEmail = user.UserEmail; user_edit.UserQq = user.UserQq; user_edit.UserWx = user.UserWx; user_edit.UserPhone = user.UserPhone; user_edit.UserGroupId = user.UserGroupId; user_edit.Note = user.Note; user_edit.EditTime = DateTime.Now; user_edit.UasSub = user.UasSub; user_edit.UserStatus = user.UserStatus; if (Update(user_edit)) { messageModel.Message = "修改成功"; } else { messageModel.Message = "修改失败"; } } return(messageModel); }