2018-10-30 13:07:29 +08:00
|
|
|
|
using Bootstrap.Security;
|
2018-11-02 10:25:33 +08:00
|
|
|
|
using Longbow.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Primitives;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.Serialization;
|
|
|
|
|
using MongoDB.Bson.Serialization.Serializers;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2019-01-15 12:52:09 +08:00
|
|
|
|
internal static class DbManager
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
|
|
|
|
private static IMongoDatabase _db = null;
|
|
|
|
|
private static bool _register = false;
|
2018-11-02 10:25:33 +08:00
|
|
|
|
private static readonly object _locker = new object();
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoDatabase DBAccess
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_db == null)
|
|
|
|
|
{
|
2018-11-02 10:25:33 +08:00
|
|
|
|
lock (_locker)
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
2018-11-02 10:25:33 +08:00
|
|
|
|
if (!_register)
|
|
|
|
|
{
|
|
|
|
|
_register = true;
|
|
|
|
|
ChangeToken.OnChange(() => ConfigurationManager.AppSettings.GetReloadToken(), () => _db = null);
|
|
|
|
|
InitClassMap();
|
|
|
|
|
}
|
|
|
|
|
InitDb();
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _db;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 09:51:28 +08:00
|
|
|
|
|
2018-11-02 10:25:33 +08:00
|
|
|
|
#region Collections
|
2018-10-30 19:01:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-30 22:11:27 +08:00
|
|
|
|
public static IMongoCollection<DataAccess.Log> Logs
|
2018-10-30 19:01:49 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-30 22:11:27 +08:00
|
|
|
|
return DBAccess.GetCollection<DataAccess.Log>("Logs");
|
2018-10-30 19:01:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 09:51:28 +08:00
|
|
|
|
|
2018-10-30 19:01:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-30 22:15:29 +08:00
|
|
|
|
public static IMongoCollection<DataAccess.Exceptions> Exceptions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<DataAccess.Exceptions>("Exceptions");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-30 23:01:36 +08:00
|
|
|
|
public static IMongoCollection<BootstrapDict> Dicts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<BootstrapDict>("Dicts");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 09:51:28 +08:00
|
|
|
|
|
2018-10-30 23:01:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-30 19:01:49 +08:00
|
|
|
|
public static IMongoCollection<User> Users
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<User>("Users");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-31 09:51:28 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 16:03:49 +08:00
|
|
|
|
public static IMongoCollection<Group> Groups
|
2018-10-31 09:51:28 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-31 16:03:49 +08:00
|
|
|
|
return DBAccess.GetCollection<Group>("Groups");
|
2018-10-31 09:51:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 11:15:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 14:13:37 +08:00
|
|
|
|
public static IMongoCollection<Role> Roles
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
return DBAccess.GetCollection<Role>("Roles");
|
2018-10-31 11:15:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 12:08:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoCollection<BootstrapMenu> Menus
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<BootstrapMenu>("Navigations");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-04 02:41:53 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoCollection<DataAccess.LoginUser> LoginUsers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<DataAccess.LoginUser>("LoginLogs");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-05 16:50:32 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoCollection<DataAccess.ResetUser> ResetUsers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<DataAccess.ResetUser>("ResetUsers");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-16 20:24:33 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoCollection<DataAccess.Trace> Traces
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<DataAccess.Trace>("Traces");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-14 15:08:46 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IMongoCollection<RejectUser> RejectUsers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return DBAccess.GetCollection<RejectUser>("RejectUsers");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-02 10:25:33 +08:00
|
|
|
|
#endregion
|
2018-10-31 12:08:57 +08:00
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
private static void InitDb()
|
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
var client = new MongoClient(Longbow.Data.DbManager.GetConnectionString());
|
|
|
|
|
_db = client.GetDatabase(ConfigurationManager.AppSettings["MongoDB"]);
|
2018-10-30 15:37:09 +08:00
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
2018-10-30 15:37:09 +08:00
|
|
|
|
private static void InitClassMap()
|
|
|
|
|
{
|
|
|
|
|
BsonSerializer.RegisterSerializer(DateTimeSerializer.LocalInstance);
|
2018-10-30 22:11:27 +08:00
|
|
|
|
|
2019-05-14 15:08:46 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(Dict)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<Dict>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapDict)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<BootstrapDict>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
2019-05-14 15:08:46 +08:00
|
|
|
|
md.AddKnownType(typeof(Dict));
|
2018-10-30 13:07:29 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.User)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.User>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
2018-10-30 17:02:36 +08:00
|
|
|
|
md.UnmapMember(user => user.Checked);
|
|
|
|
|
md.UnmapMember(user => user.Period);
|
|
|
|
|
md.UnmapMember(user => user.NewPassword);
|
|
|
|
|
md.UnmapMember(user => user.UserStatus);
|
2018-10-30 13:07:29 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapMenu)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<BootstrapMenu>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
2018-11-01 10:16:52 +08:00
|
|
|
|
md.UnmapMember(m => m.CategoryName);
|
|
|
|
|
md.UnmapMember(m => m.Active);
|
|
|
|
|
md.UnmapMember(m => m.ParentName);
|
|
|
|
|
md.UnmapMember(m => m.Menus);
|
2018-10-30 13:07:29 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Group)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Group>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
md.UnmapMember(group => group.Checked);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Role)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Role>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
md.UnmapMember(role => role.Checked);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Task)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Task>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Message)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Message>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Exceptions)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Exceptions>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
2018-10-31 21:29:42 +08:00
|
|
|
|
md.UnmapMember(ex => ex.Period);
|
2018-10-30 13:07:29 +08:00
|
|
|
|
});
|
2019-05-01 16:46:24 +08:00
|
|
|
|
}
|
2019-05-14 15:08:46 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Log)))
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
2019-05-14 15:08:46 +08:00
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Log>(md =>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
});
|
2019-05-01 16:46:24 +08:00
|
|
|
|
}
|
2019-05-14 15:08:46 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.Trace)))
|
2019-03-16 20:24:33 +08:00
|
|
|
|
{
|
2019-05-14 15:08:46 +08:00
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.Trace>(md =>
|
2019-05-01 16:46:24 +08:00
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
2019-05-14 15:08:46 +08:00
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
md.AddKnownType(typeof(DataAccess.Log));
|
2019-03-16 20:24:33 +08:00
|
|
|
|
});
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2019-03-04 02:41:53 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.LoginUser)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.LoginUser>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-03-05 16:50:32 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.ResetUser)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<DataAccess.ResetUser>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
});
|
2019-05-01 16:46:24 +08:00
|
|
|
|
}
|
2019-05-14 15:08:46 +08:00
|
|
|
|
if (!BsonClassMap.IsClassMapRegistered(typeof(RejectUser)))
|
|
|
|
|
{
|
|
|
|
|
BsonClassMap.RegisterClassMap<RejectUser>(md =>
|
|
|
|
|
{
|
|
|
|
|
md.AutoMap();
|
|
|
|
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
|
|
|
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|