using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; namespace DentalDecks.Server.Models { [BsonIgnoreExtraElements] public class AppUser { public ObjectId _id { get; set; } public string name; public string emailAddress; public string mobileNumber; public string password; internal static dynamic GetRegisteredCustomers() { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var usersCol = db.GetCollection("users"); var customers = usersCol.Find(new BsonDocument()).ToList(); return customers; } public string[] apps; [BsonIgnore] public bool successful; [BsonIgnore] public string message; internal static bool IsResetPasswordLinkExpired(string userId) { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); var filter = Builders.Filter.Eq("_id", ObjectId.Parse(userId)) & Builders.Filter.Exists("resetPasswordLinkValidity"); var user = col.Find(filter).SingleOrDefault(); if (user == null) return true; else { DateTime expiryDateTime = user["resetPasswordLinkValidity"].ToUniversalTime(); DateTime currentDateTime = DateTime.UtcNow; if (currentDateTime > expiryDateTime) return true; else return false; } } internal static void UpdatePassword(string userId, string password) { if (!IsResetPasswordLinkExpired(userId)) { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); var result = col.UpdateOne(Builders.Filter.Eq("_id", ObjectId.Parse(userId)), Builders.Update .Set("password", password) .Set("lastPasswordChangeDate", DateTime.UtcNow) .Unset("resetPasswordLinkValidity")); } } public static dynamic Authenticate(string applicationName, string username, string password) { try { var emailFilter = Builders.Filter.Eq("emailAddress", username) & Builders.Filter.Eq("password", password); var passwordFilter = Builders.Filter.Eq("mobileNumber", username) & Builders.Filter.Eq("password", password); var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); dynamic user = col.Find(emailFilter).SingleOrDefault(); if (user == null) user = col.Find(passwordFilter).SingleOrDefault(); user.successful = true; return user; } catch (Exception ex) { return null; } } public bool isRegistered() { return false; } public static bool isRegistered(string emailAddress, string mobileNumber) { FilterDefinition[] filters = { Builders.Filter.Eq("emailAddress", emailAddress), Builders.Filter.Eq("mobileNumber", mobileNumber) }; var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); List list; list = col.Find(Builders.Filter.Or(filters)).ToList(); if (list.Count > 0) return true; else return false; } public static bool isRegistered(string emailAddress) { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); List list; list = col.Find(Builders.Filter.Eq("emailAddress", emailAddress)).ToList(); if (list.Count > 0) return true; else return false; } public static dynamic GetUser(string userId) { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); dynamic userDetails = col.Find(Builders.Filter.Eq("_id", ObjectId.Parse(userId))).SingleOrDefault(); return userDetails; } public static AppUser Create(JObject userObject) { AppUser user = userObject.ToObject(); if (isRegistered(user.emailAddress, user.mobileNumber)) { throw new Exception("Our records indicate that you are already registered."); } else { var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); user.name = user.name.ToTitleCase(); col.InsertOne(user); user.successful = true; user.message = "User created successfully."; return user; } } public static string SetPasswordResetExpiry(string username) { if (!isRegistered(username)) { throw new Exception("This email address is not registered with us."); } var client = new MongoClient(); var db = client.GetDatabase(ConfigurationManager.AppSettings["database"]); var col = db.GetCollection("users"); var result = col.UpdateOne(Builders.Filter.Eq("emailAddress", username), Builders.Update.Set("resetPasswordLinkValidity", DateTime.UtcNow.AddDays(1).ToUniversalTime())); var user = col.Find(Builders.Filter.Eq("emailAddress", username)).SingleOrDefault(); return user._id.ToString(); } } }