using Bootstrap.Security; using Longbow.Security.Cryptography; using MongoDB.Driver; using System; using System.Collections.Generic; namespace Bootstrap.DataAccess.MongoDB { /// /// /// public class User : DataAccess.User { /// /// /// /// /// public override BootstrapUser RetrieveUserByUserName(string userName) { var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); return users.Find(user => user.UserName == userName).FirstOrDefault(); } /// /// /// /// /// /// public override bool Authenticate(string userName, string password) { if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false; var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); var u = users.Find(user => user.UserName == userName).FirstOrDefault(); return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt); } /// /// /// /// public override IEnumerable RetrieveNewUsers() { var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); return users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList(); } /// /// /// /// public override IEnumerable RetrieveUsers() { var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); return users.Find(user => user.ApprovedTime != DateTime.MinValue).ToList(); } /// /// /// /// /// public override bool SaveUser(DataAccess.User user) { if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500); if (user.UserName.Length > 50) user.UserName = user.UserName.Substring(0, 50); user.Id = null; user.PassSalt = LgbCryptography.GenerateSalt(); user.Password = LgbCryptography.ComputeHash(user.Password, user.PassSalt); user.RegisterTime = DateTime.Now; user.ApprovedTime = DateTime.Now; user.Icon = $"{DictHelper.RetrieveIconFolderPath().Code}default.jpg"; var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); users.InsertOne(user); return true; } /// /// /// /// /// /// /// public override bool UpdateUser(string id, string password, string displayName) { var passSalt = LgbCryptography.GenerateSalt(); var newPassword = LgbCryptography.ComputeHash(password, passSalt); var update = Builders.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName); var users = MongoDbAccessManager.DBAccess.GetCollection("Users"); users.FindOneAndUpdate(u => u.Id == id, update); return true; } } }