From 20a9ba8da757986db5b6d11b2fb32241c09f7580 Mon Sep 17 00:00:00 2001 From: Argo Zhang <5196060@qq.com> Date: Tue, 5 Mar 2019 16:50:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0MongoDB=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20#IS7V2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/RegisterController.cs | 2 +- Bootstrap.DataAccess.MongoDB/DbManager.cs | 20 ++++++++++ Bootstrap.DataAccess.MongoDB/ResetUser.cs | 33 +++++++++++++++ Bootstrap.DataAccess.MongoDB/User.cs | 40 +++++++++++++++++-- Bootstrap.DataAccess/Helper/UserHelper.cs | 7 +--- Bootstrap.DataAccess/ResetUsers.cs | 3 +- Bootstrap.DataAccess/User.cs | 14 ++----- .../MongoDB/BootstrapAdmin.Users.json | 2 + 8 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 Bootstrap.DataAccess.MongoDB/ResetUser.cs diff --git a/Bootstrap.Admin/Controllers/Api/RegisterController.cs b/Bootstrap.Admin/Controllers/Api/RegisterController.cs index 1ff40ca5..c040f9ce 100644 --- a/Bootstrap.Admin/Controllers/Api/RegisterController.cs +++ b/Bootstrap.Admin/Controllers/Api/RegisterController.cs @@ -58,6 +58,6 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPut] - public bool Put([FromBody]ResetUser user) => UserHelper.ForgotPassword(user.UserName, user.DisplayName, user.Reason); + public bool Put([FromBody]ResetUser user) => UserHelper.ForgotPassword(user); } } diff --git a/Bootstrap.DataAccess.MongoDB/DbManager.cs b/Bootstrap.DataAccess.MongoDB/DbManager.cs index 75f6b98c..ac3aa42d 100644 --- a/Bootstrap.DataAccess.MongoDB/DbManager.cs +++ b/Bootstrap.DataAccess.MongoDB/DbManager.cs @@ -127,6 +127,17 @@ namespace Bootstrap.DataAccess.MongoDB return DBAccess.GetCollection("LoginLogs"); } } + + /// + /// + /// + public static IMongoCollection ResetUsers + { + get + { + return DBAccess.GetCollection("ResetUsers"); + } + } #endregion private static void InitDb() @@ -240,6 +251,15 @@ namespace Bootstrap.DataAccess.MongoDB md.IdMemberMap.SetIgnoreIfDefault(true); }); } + if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.ResetUser))) + { + BsonClassMap.RegisterClassMap(md => + { + md.AutoMap(); + md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId)); + md.IdMemberMap.SetIgnoreIfDefault(true); + }); + } } } } diff --git a/Bootstrap.DataAccess.MongoDB/ResetUser.cs b/Bootstrap.DataAccess.MongoDB/ResetUser.cs new file mode 100644 index 00000000..919c7321 --- /dev/null +++ b/Bootstrap.DataAccess.MongoDB/ResetUser.cs @@ -0,0 +1,33 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Bootstrap.DataAccess.MongoDB +{ + /// + /// + /// + public class ResetUser : DataAccess.ResetUser + { + /// + /// + /// + /// + /// + public override DataAccess.ResetUser RetrieveUserByUserName(string userName) => DbManager.ResetUsers.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).FirstOrDefault(); + + /// + /// + /// + /// + /// + public override IEnumerable> RetrieveResetReasonsByUserName(string userName) => DbManager.ResetUsers.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).ToList().OrderByDescending(user => user.ResetTime).Select(user => new KeyValuePair(user.ResetTime, user.Reason)); + + /// + /// + /// + /// + public override void DeleteByUserName(string userName) => DbManager.ResetUsers.DeleteMany(User => User.UserName.ToLowerInvariant() == userName.ToLowerInvariant()); + } +} diff --git a/Bootstrap.DataAccess.MongoDB/User.cs b/Bootstrap.DataAccess.MongoDB/User.cs index fe5e14f3..9489da1c 100644 --- a/Bootstrap.DataAccess.MongoDB/User.cs +++ b/Bootstrap.DataAccess.MongoDB/User.cs @@ -85,7 +85,8 @@ namespace Bootstrap.DataAccess.MongoDB .Include(u => u.ApprovedBy) .Include(u => u.Description) .Include(u => u.Groups) - .Include(u => u.Roles); + .Include(u => u.Roles) + .Include(u => u.IsReset); return DbManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project(project).ToList(); } @@ -112,8 +113,9 @@ namespace Bootstrap.DataAccess.MongoDB ApprovedBy = user.ApprovedBy, Roles = new List(), Groups = new List(), - Icon = $"{DictHelper.RetrieveIconFolderPath()}default.jpg", - Description = user.Description + Icon = "default.jpg", + Description = user.Description, + IsReset = 0 }); return true; } @@ -251,5 +253,37 @@ namespace Bootstrap.DataAccess.MongoDB }); return true; } + + /// + /// + /// + /// + /// + public override bool ForgotPassword(DataAccess.ResetUser user) + { + DbManager.Users.UpdateOne(md => md.UserName.ToLowerInvariant() == user.UserName.ToLowerInvariant(), Builders.Update.Set(md => md.IsReset, 1)); + user.ResetTime = DateTime.Now; + DbManager.ResetUsers.InsertOne(user); + return true; + } + + /// + /// + /// + /// + /// + /// + public override bool ResetPassword(string userName, string password) + { + var ret = false; + var resetUser = UserHelper.RetrieveResetUserByUserName(userName); + if (resetUser == null) return ret; + + var passSalt = LgbCryptography.GenerateSalt(); + var newPassword = LgbCryptography.ComputeHash(password, passSalt); + DbManager.Users.UpdateOne(User => User.UserName.ToLowerInvariant() == userName.ToLowerInvariant(), Builders.Update.Set(md => md.Password, newPassword).Set(md => md.PassSalt, passSalt).Set(md => md.IsReset, 0)); + DbManager.ResetUsers.DeleteMany(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()); + return true; + } } } diff --git a/Bootstrap.DataAccess/Helper/UserHelper.cs b/Bootstrap.DataAccess/Helper/UserHelper.cs index 0cee2cb0..d1856096 100644 --- a/Bootstrap.DataAccess/Helper/UserHelper.cs +++ b/Bootstrap.DataAccess/Helper/UserHelper.cs @@ -135,11 +135,8 @@ namespace Bootstrap.DataAccess /// /// /// - /// - /// - /// - /// - public static bool ForgotPassword(string userName, string displayName, string desc) => DbContextManager.Create().ForgotPassword(userName, displayName, desc); + /// + public static bool ForgotPassword(ResetUser user) => DbContextManager.Create().ForgotPassword(user); /// /// diff --git a/Bootstrap.DataAccess/ResetUsers.cs b/Bootstrap.DataAccess/ResetUsers.cs index 9a76cc7d..9fbd15d4 100644 --- a/Bootstrap.DataAccess/ResetUsers.cs +++ b/Bootstrap.DataAccess/ResetUsers.cs @@ -42,8 +42,7 @@ namespace Bootstrap.DataAccess /// public virtual bool Save() { - var db = DbManager.Create(); - db.Save(this); + DbManager.Create().Save(this); return true; } diff --git a/Bootstrap.DataAccess/User.cs b/Bootstrap.DataAccess/User.cs index 039af467..cb4413c0 100644 --- a/Bootstrap.DataAccess/User.cs +++ b/Bootstrap.DataAccess/User.cs @@ -190,19 +190,11 @@ namespace Bootstrap.DataAccess /// /// /// - /// - /// - /// + /// /// - public virtual bool ForgotPassword(string userName, string displayName, string desc) + public virtual bool ForgotPassword(ResetUser user) { - ResetUser user = new ResetUser() - { - UserName = userName, - DisplayName = displayName, - Reason = desc, - ResetTime = DateTime.Now - }; + user.ResetTime = DateTime.Now; return user.Save(); } diff --git a/DatabaseScripts/MongoDB/BootstrapAdmin.Users.json b/DatabaseScripts/MongoDB/BootstrapAdmin.Users.json index eb8ff07f..18d416ff 100644 --- a/DatabaseScripts/MongoDB/BootstrapAdmin.Users.json +++ b/DatabaseScripts/MongoDB/BootstrapAdmin.Users.json @@ -12,6 +12,7 @@ "Icon": "default.jpg", "Css": null, "App": null, + "IsReset": 0, "Roles": [ "5bd7cc105fa31256f77e4eb7" ], @@ -32,6 +33,7 @@ "Icon": "default.jpg", "Css": null, "App": null, + "IsReset": 0, "Groups": [ ],