增加功能:增加MongoDB数据库支持 #IS7V2

This commit is contained in:
Argo Zhang 2019-03-05 16:50:32 +08:00
parent 642b74edfc
commit 20a9ba8da7
8 changed files with 99 additions and 22 deletions

View File

@ -58,6 +58,6 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="user"></param> /// <param name="user"></param>
/// <returns></returns> /// <returns></returns>
[HttpPut] [HttpPut]
public bool Put([FromBody]ResetUser user) => UserHelper.ForgotPassword(user.UserName, user.DisplayName, user.Reason); public bool Put([FromBody]ResetUser user) => UserHelper.ForgotPassword(user);
} }
} }

View File

@ -127,6 +127,17 @@ namespace Bootstrap.DataAccess.MongoDB
return DBAccess.GetCollection<DataAccess.LoginUser>("LoginLogs"); return DBAccess.GetCollection<DataAccess.LoginUser>("LoginLogs");
} }
} }
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.ResetUser> ResetUsers
{
get
{
return DBAccess.GetCollection<DataAccess.ResetUser>("ResetUsers");
}
}
#endregion #endregion
private static void InitDb() private static void InitDb()
@ -240,6 +251,15 @@ namespace Bootstrap.DataAccess.MongoDB
md.IdMemberMap.SetIgnoreIfDefault(true); md.IdMemberMap.SetIgnoreIfDefault(true);
}); });
} }
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.ResetUser)))
{
BsonClassMap.RegisterClassMap<DataAccess.ResetUser>(md =>
{
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
} }
} }
} }

View File

@ -0,0 +1,33 @@
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
/// <summary>
///
/// </summary>
public class ResetUser : DataAccess.ResetUser
{
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override DataAccess.ResetUser RetrieveUserByUserName(string userName) => DbManager.ResetUsers.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).FirstOrDefault();
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override IEnumerable<KeyValuePair<DateTime, string>> RetrieveResetReasonsByUserName(string userName) => DbManager.ResetUsers.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).ToList().OrderByDescending(user => user.ResetTime).Select(user => new KeyValuePair<DateTime, string>(user.ResetTime, user.Reason));
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
public override void DeleteByUserName(string userName) => DbManager.ResetUsers.DeleteMany(User => User.UserName.ToLowerInvariant() == userName.ToLowerInvariant());
}
}

View File

@ -85,7 +85,8 @@ namespace Bootstrap.DataAccess.MongoDB
.Include(u => u.ApprovedBy) .Include(u => u.ApprovedBy)
.Include(u => u.Description) .Include(u => u.Description)
.Include(u => u.Groups) .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<User>(project).ToList(); return DbManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
} }
@ -112,8 +113,9 @@ namespace Bootstrap.DataAccess.MongoDB
ApprovedBy = user.ApprovedBy, ApprovedBy = user.ApprovedBy,
Roles = new List<string>(), Roles = new List<string>(),
Groups = new List<string>(), Groups = new List<string>(),
Icon = $"{DictHelper.RetrieveIconFolderPath()}default.jpg", Icon = "default.jpg",
Description = user.Description Description = user.Description,
IsReset = 0
}); });
return true; return true;
} }
@ -251,5 +253,37 @@ namespace Bootstrap.DataAccess.MongoDB
}); });
return true; return true;
} }
/// <summary>
///
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public override bool ForgotPassword(DataAccess.ResetUser user)
{
DbManager.Users.UpdateOne(md => md.UserName.ToLowerInvariant() == user.UserName.ToLowerInvariant(), Builders<User>.Update.Set(md => md.IsReset, 1));
user.ResetTime = DateTime.Now;
DbManager.ResetUsers.InsertOne(user);
return true;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
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<User>.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;
}
} }
} }

View File

@ -135,11 +135,8 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="user"></param>
/// <param name="displayName"></param> public static bool ForgotPassword(ResetUser user) => DbContextManager.Create<User>().ForgotPassword(user);
/// <param name="desc"></param>
/// <returns></returns>
public static bool ForgotPassword(string userName, string displayName, string desc) => DbContextManager.Create<User>().ForgotPassword(userName, displayName, desc);
/// <summary> /// <summary>
/// ///

View File

@ -42,8 +42,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public virtual bool Save() public virtual bool Save()
{ {
var db = DbManager.Create(); DbManager.Create().Save(this);
db.Save(this);
return true; return true;
} }

View File

@ -190,19 +190,11 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="user"></param>
/// <param name="displayName"></param>
/// <param name="desc"></param>
/// <returns></returns> /// <returns></returns>
public virtual bool ForgotPassword(string userName, string displayName, string desc) public virtual bool ForgotPassword(ResetUser user)
{ {
ResetUser user = new ResetUser() user.ResetTime = DateTime.Now;
{
UserName = userName,
DisplayName = displayName,
Reason = desc,
ResetTime = DateTime.Now
};
return user.Save(); return user.Save();
} }

View File

@ -12,6 +12,7 @@
"Icon": "default.jpg", "Icon": "default.jpg",
"Css": null, "Css": null,
"App": null, "App": null,
"IsReset": 0,
"Roles": [ "Roles": [
"5bd7cc105fa31256f77e4eb7" "5bd7cc105fa31256f77e4eb7"
], ],
@ -32,6 +33,7 @@
"Icon": "default.jpg", "Icon": "default.jpg",
"Css": null, "Css": null,
"App": null, "App": null,
"IsReset": 0,
"Groups": [ "Groups": [
], ],