代码重构:重新调整MongoDB连接字符串获取方法
This commit is contained in:
parent
12c53eeead
commit
7e1b8291c8
|
@ -26,14 +26,14 @@
|
||||||
"CorsItems": [
|
"CorsItems": [
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Name": "ba",
|
"Key": "ba",
|
||||||
"Url": "CacheList.axd",
|
"Url": "CacheList.axd",
|
||||||
"Desc": "后台管理数据缓存接口",
|
"Desc": "后台管理数据缓存接口",
|
||||||
"Self": true
|
"Self": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Name": "APP",
|
"Key": "APP",
|
||||||
"Url": "http://localhost:49185/CacheList.axd",
|
"Url": "http://localhost:49185/CacheList.axd",
|
||||||
"Desc": "测试系统",
|
"Desc": "测试系统",
|
||||||
"Self": false
|
"Self": false
|
||||||
|
|
|
@ -15,22 +15,21 @@
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
|
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
|
||||||
},
|
},
|
||||||
|
"MongoDB": "BootstrapAdmin",
|
||||||
"DB": [
|
"DB": [
|
||||||
{
|
{
|
||||||
"Enabled": true
|
"Enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Widget": "Bootstrap.DataAccess.MySQL",
|
"ProviderName": "MySql",
|
||||||
"DBProviderFactory": "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data",
|
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ba": "Server=10.211.55.2;Database=BA;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"
|
"ba": "Server=10.211.55.2;Database=BA;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Widget": "Bootstrap.DataAccess.SQLite",
|
"ProviderName": "Sqlite",
|
||||||
"DBProviderFactory": "Microsoft.Data.Sqlite.SqliteFactory, Microsoft.Data.Sqlite",
|
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ba": "Data Source=BootstrapAdmin.db;"
|
"ba": "Data Source=BootstrapAdmin.db;"
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,7 @@
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Widget": "Bootstrap.DataAccess.MongoDB",
|
"Widget": "Bootstrap.DataAccess.MongoDB",
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ba": "mongodb://10.211.55.2:27017;Data Source=BootstrapAdmin"
|
"ba": "mongodb://10.211.55.2:27017"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -63,14 +62,14 @@
|
||||||
"CorsItems": [
|
"CorsItems": [
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Name": "ba",
|
"Key": "ba",
|
||||||
"Url": "CacheList.axd",
|
"Url": "CacheList.axd",
|
||||||
"Desc": "后台管理数据缓存接口",
|
"Desc": "后台管理数据缓存接口",
|
||||||
"Self": true
|
"Self": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Enabled": true,
|
"Enabled": true,
|
||||||
"Name": "Pallet",
|
"Key": "Pallet",
|
||||||
"Url": "http://localhost/WebConsole/CacheList.axd",
|
"Url": "http://localhost/WebConsole/CacheList.axd",
|
||||||
"Desc": "托盘组垛系统",
|
"Desc": "托盘组垛系统",
|
||||||
"Self": false
|
"Self": false
|
||||||
|
|
|
@ -5,15 +5,13 @@ using MongoDB.Bson;
|
||||||
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization;
|
||||||
using MongoDB.Bson.Serialization.Serializers;
|
using MongoDB.Bson.Serialization.Serializers;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Bootstrap.DataAccess.MongoDB
|
namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class MongoDbAccessManager
|
internal static class DbManager
|
||||||
{
|
{
|
||||||
private static IMongoDatabase _db = null;
|
private static IMongoDatabase _db = null;
|
||||||
private static bool _register = false;
|
private static bool _register = false;
|
||||||
|
@ -122,14 +120,8 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
|
|
||||||
private static void InitDb()
|
private static void InitDb()
|
||||||
{
|
{
|
||||||
var connectString = DbAdapterManager.GetConnectionString("ba");
|
var client = new MongoClient(Longbow.Data.DbManager.GetConnectionString());
|
||||||
if (string.IsNullOrEmpty(connectString)) throw new InvalidOperationException("Please set the BA default value in configuration file.");
|
_db = client.GetDatabase(ConfigurationManager.AppSettings["MongoDB"]);
|
||||||
|
|
||||||
var seq = connectString.SpanSplit(";", StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
if (seq.Count != 2) throw new InvalidOperationException("ConnectionString invalid in configuration file. It should be mongodb://127.0.0.1:27017;Data Source=BootstrapAdmin");
|
|
||||||
|
|
||||||
var client = new MongoClient(seq[0]);
|
|
||||||
_db = client.GetDatabase(seq[1].SpanSplit("=", StringSplitOptions.RemoveEmptyEntries).LastOrDefault());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void InitClassMap()
|
private static void InitClassMap()
|
|
@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<BootstrapDict> RetrieveDicts() => MongoDbAccessManager.Dicts.Find(FilterDefinition<BootstrapDict>.Empty).ToList();
|
public override IEnumerable<BootstrapDict> RetrieveDicts() => DbManager.Dicts.Find(FilterDefinition<BootstrapDict>.Empty).ToList();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
@ -28,7 +28,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
list.Add(new DeleteOneModel<BootstrapDict>(Builders<BootstrapDict>.Filter.Eq(md => md.Id, id)));
|
list.Add(new DeleteOneModel<BootstrapDict>(Builders<BootstrapDict>.Filter.Eq(md => md.Id, id)));
|
||||||
}
|
}
|
||||||
MongoDbAccessManager.Dicts.BulkWrite(list);
|
DbManager.Dicts.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,12 +42,12 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
if (p.Id == "0")
|
if (p.Id == "0")
|
||||||
{
|
{
|
||||||
p.Id = null;
|
p.Id = null;
|
||||||
MongoDbAccessManager.Dicts.InsertOne(p);
|
DbManager.Dicts.InsertOne(p);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Dicts.UpdateOne(md => md.Id == p.Id, Builders<BootstrapDict>.Update.Set(md => md.Category, p.Category)
|
DbManager.Dicts.UpdateOne(md => md.Id == p.Id, Builders<BootstrapDict>.Update.Set(md => md.Category, p.Category)
|
||||||
.Set(md => md.Define, p.Define)
|
.Set(md => md.Define, p.Define)
|
||||||
.Set(md => md.Name, p.Name)
|
.Set(md => md.Name, p.Name)
|
||||||
.Set(md => md.Code, p.Code));
|
.Set(md => md.Code, p.Code));
|
||||||
|
@ -62,7 +62,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveSettings(BootstrapDict dict)
|
public override bool SaveSettings(BootstrapDict dict)
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Dicts.FindOneAndUpdate(md => md.Category == dict.Category && md.Name == dict.Name, Builders<BootstrapDict>.Update.Set(md => md.Code, dict.Code));
|
DbManager.Dicts.FindOneAndUpdate(md => md.Category == dict.Category && md.Name == dict.Name, Builders<BootstrapDict>.Update.Set(md => md.Code, dict.Code));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
System.Threading.Tasks.Task.Run(() =>
|
System.Threading.Tasks.Task.Run(() =>
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddDays(-7));
|
DbManager.Exceptions.DeleteMany(ex => ex.LogTime < DateTime.Now.AddDays(-7));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.Exceptions> Retrieves()
|
public override IEnumerable<DataAccess.Exceptions> Retrieves()
|
||||||
{
|
{
|
||||||
return MongoDbAccessManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList();
|
return DbManager.Exceptions.Find(ex => ex.LogTime >= DateTime.Now.AddDays(-7)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -47,7 +47,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
UserId = additionalInfo?["UserId"],
|
UserId = additionalInfo?["UserId"],
|
||||||
UserIp = additionalInfo?["UserIp"]
|
UserIp = additionalInfo?["UserIp"]
|
||||||
};
|
};
|
||||||
MongoDbAccessManager.Exceptions.InsertOne(excep);
|
DbManager.Exceptions.InsertOne(excep);
|
||||||
ClearExceptions();
|
ClearExceptions();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.Group> Retrieves()
|
public override IEnumerable<DataAccess.Group> Retrieves()
|
||||||
{
|
{
|
||||||
return MongoDbAccessManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
|
return DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -33,7 +33,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
if (p.Id == "0")
|
if (p.Id == "0")
|
||||||
{
|
{
|
||||||
p.Id = null;
|
p.Id = null;
|
||||||
MongoDbAccessManager.Groups.InsertOne(new Group() {
|
DbManager.Groups.InsertOne(new Group() {
|
||||||
GroupName = p.GroupName,
|
GroupName = p.GroupName,
|
||||||
Description = p.Description,
|
Description = p.Description,
|
||||||
Roles = new List<string>()
|
Roles = new List<string>()
|
||||||
|
@ -42,7 +42,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
list.Add(new DeleteOneModel<Group>(Builders<Group>.Filter.Eq(g => g.Id, id)));
|
list.Add(new DeleteOneModel<Group>(Builders<Group>.Filter.Eq(g => g.Id, id)));
|
||||||
}
|
}
|
||||||
MongoDbAccessManager.Groups.BulkWrite(list);
|
DbManager.Groups.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByUserId(string userId, IEnumerable<string> groupIds)
|
public override bool SaveByUserId(string userId, IEnumerable<string> groupIds)
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Groups, groupIds));
|
DbManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Groups, groupIds));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,23 +108,23 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByRoleId(string roleId, IEnumerable<string> groupIds)
|
public override bool SaveByRoleId(string roleId, IEnumerable<string> groupIds)
|
||||||
{
|
{
|
||||||
var groups = MongoDbAccessManager.Groups.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
var groups = DbManager.Groups.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
||||||
|
|
||||||
// Remove roles
|
// Remove roles
|
||||||
groups.ForEach(p =>
|
groups.ForEach(p =>
|
||||||
{
|
{
|
||||||
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
roles.Remove(roleId);
|
roles.Remove(roleId);
|
||||||
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
||||||
});
|
});
|
||||||
|
|
||||||
groups = MongoDbAccessManager.Groups.Find(md => groupIds.Contains(md.Id)).ToList();
|
groups = DbManager.Groups.Find(md => groupIds.Contains(md.Id)).ToList();
|
||||||
// Add roles
|
// Add roles
|
||||||
groups.ForEach(p =>
|
groups.ForEach(p =>
|
||||||
{
|
{
|
||||||
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
roles.Add(roleId);
|
roles.Add(roleId);
|
||||||
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,13 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.Log> Retrieves() => MongoDbAccessManager.Logs.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList();
|
public override IEnumerable<DataAccess.Log> Retrieves() => DbManager.Logs.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除日志信息
|
/// 删除日志信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value"></param>
|
/// <param name="value"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static void DeleteLogAsync() => System.Threading.Tasks.Task.Run(() => MongoDbAccessManager.Logs.DeleteMany(log => log.LogTime < DateTime.Now.AddDays(-7)));
|
private static void DeleteLogAsync() => System.Threading.Tasks.Task.Run(() => DbManager.Logs.DeleteMany(log => log.LogTime < DateTime.Now.AddDays(-7)));
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -28,7 +28,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
public override bool Save(DataAccess.Log log)
|
public override bool Save(DataAccess.Log log)
|
||||||
{
|
{
|
||||||
log.LogTime = DateTime.Now;
|
log.LogTime = DateTime.Now;
|
||||||
MongoDbAccessManager.Logs.InsertOne(log);
|
DbManager.Logs.InsertOne(log);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
|
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
|
||||||
|
|
||||||
var menus = MongoDbAccessManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
|
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
|
||||||
menus.ForEach(m =>
|
menus.ForEach(m =>
|
||||||
{
|
{
|
||||||
m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name;
|
m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name;
|
||||||
|
@ -38,7 +38,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
if (p.Id == "0")
|
if (p.Id == "0")
|
||||||
{
|
{
|
||||||
p.Id = null;
|
p.Id = null;
|
||||||
MongoDbAccessManager.Menus.InsertOne(p);
|
DbManager.Menus.InsertOne(p);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -52,7 +52,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
.Set(md => md.Target, p.Target)
|
.Set(md => md.Target, p.Target)
|
||||||
.Set(md => md.IsResource, p.IsResource)
|
.Set(md => md.IsResource, p.IsResource)
|
||||||
.Set(md => md.Application, p.Application);
|
.Set(md => md.Application, p.Application);
|
||||||
MongoDbAccessManager.Menus.UpdateOne(md => md.Id == p.Id, update);
|
DbManager.Menus.UpdateOne(md => md.Id == p.Id, update);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
list.Add(new DeleteOneModel<BootstrapMenu>(Builders<BootstrapMenu>.Filter.Eq(g => g.Id, id)));
|
list.Add(new DeleteOneModel<BootstrapMenu>(Builders<BootstrapMenu>.Filter.Eq(g => g.Id, id)));
|
||||||
}
|
}
|
||||||
MongoDbAccessManager.Menus.BulkWrite(list);
|
DbManager.Menus.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="roleId"></param>
|
/// <param name="roleId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<object> RetrieveMenusByRoleId(string roleId) => MongoDbAccessManager.Roles.Find(md => md.Id == roleId).FirstOrDefault().Menus.Select(m => new { Id = m });
|
public override IEnumerable<object> RetrieveMenusByRoleId(string roleId) => DbManager.Roles.Find(md => md.Id == roleId).FirstOrDefault().Menus.Select(m => new { Id = m });
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
@ -88,7 +88,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
|
public override bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Roles.FindOneAndUpdate(md => md.Id == roleId, Builders<Role>.Update.Set(md => md.Menus, menuIds));
|
DbManager.Roles.FindOneAndUpdate(md => md.Id == roleId, Builders<Role>.Update.Set(md => md.Menus, menuIds));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected override IEnumerable<DataAccess.Message> Retrieves(string userName)
|
protected override IEnumerable<DataAccess.Message> Retrieves(string userName)
|
||||||
{
|
{
|
||||||
var msg = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Message>("Messages");
|
var msg = DbManager.DBAccess.GetCollection<DataAccess.Message>("Messages");
|
||||||
return msg.Find(message => message.To == userName || message.From == userName).ToList();
|
return msg.Find(message => message.To == userName || message.From == userName).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.Role> Retrieves()
|
public override IEnumerable<DataAccess.Role> Retrieves()
|
||||||
{
|
{
|
||||||
return MongoDbAccessManager.Roles.Find(FilterDefinition<Role>.Empty).ToList();
|
return DbManager.Roles.Find(FilterDefinition<Role>.Empty).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -34,7 +34,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
if (p.Id == "0")
|
if (p.Id == "0")
|
||||||
{
|
{
|
||||||
p.Id = null;
|
p.Id = null;
|
||||||
MongoDbAccessManager.Roles.InsertOne(new Role()
|
DbManager.Roles.InsertOne(new Role()
|
||||||
{
|
{
|
||||||
RoleName = p.RoleName,
|
RoleName = p.RoleName,
|
||||||
Description = p.Description,
|
Description = p.Description,
|
||||||
|
@ -44,7 +44,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
|
DbManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
list.Add(new DeleteOneModel<Role>(Builders<Role>.Filter.Eq(g => g.Id, id)));
|
list.Add(new DeleteOneModel<Role>(Builders<Role>.Filter.Eq(g => g.Id, id)));
|
||||||
}
|
}
|
||||||
MongoDbAccessManager.Roles.BulkWrite(list);
|
DbManager.Roles.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByUserId(string userId, IEnumerable<string> roleIds)
|
public override bool SaveByUserId(string userId, IEnumerable<string> roleIds)
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
|
DbManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,24 +127,24 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SavaByMenuId(string menuId, IEnumerable<string> roleIds)
|
public override bool SavaByMenuId(string menuId, IEnumerable<string> roleIds)
|
||||||
{
|
{
|
||||||
var roles = MongoDbAccessManager.Roles.Find(md => md.Menus != null && md.Menus.Contains(menuId)).ToList();
|
var roles = DbManager.Roles.Find(md => md.Menus != null && md.Menus.Contains(menuId)).ToList();
|
||||||
|
|
||||||
// Remove roles
|
// Remove roles
|
||||||
roles.ForEach(p =>
|
roles.ForEach(p =>
|
||||||
{
|
{
|
||||||
var menus = p.Menus == null ? new List<string>() : p.Menus.ToList();
|
var menus = p.Menus == null ? new List<string>() : p.Menus.ToList();
|
||||||
menus.Remove(menuId);
|
menus.Remove(menuId);
|
||||||
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
DbManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
||||||
});
|
});
|
||||||
|
|
||||||
roles = MongoDbAccessManager.Roles.Find(md => roleIds.Contains(md.Id)).ToList();
|
roles = DbManager.Roles.Find(md => roleIds.Contains(md.Id)).ToList();
|
||||||
roles.ForEach(role =>
|
roles.ForEach(role =>
|
||||||
{
|
{
|
||||||
var menus = role.Menus == null ? new List<string>() : role.Menus.ToList();
|
var menus = role.Menus == null ? new List<string>() : role.Menus.ToList();
|
||||||
if (!menus.Contains(menuId))
|
if (!menus.Contains(menuId))
|
||||||
{
|
{
|
||||||
menus.Add(menuId);
|
menus.Add(menuId);
|
||||||
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == role.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
DbManager.Roles.UpdateOne(md => md.Id == role.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
|
@ -171,7 +171,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByGroupId(string groupId, IEnumerable<string> roleIds)
|
public override bool SaveByGroupId(string groupId, IEnumerable<string> roleIds)
|
||||||
{
|
{
|
||||||
MongoDbAccessManager.Groups.FindOneAndUpdate(u => u.Id == groupId, Builders<Group>.Update.Set(u => u.Roles, roleIds));
|
DbManager.Groups.FindOneAndUpdate(u => u.Id == groupId, Builders<Group>.Update.Set(u => u.Roles, roleIds));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +182,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<string> RetrieveRolesByUrl(string url)
|
public override IEnumerable<string> RetrieveRolesByUrl(string url)
|
||||||
{
|
{
|
||||||
var menu = MongoDbAccessManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
|
var menu = DbManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
|
||||||
var ret = RoleHelper.Retrieves().Cast<Role>().Where(md => md.Menus != null && md.Menus.Contains(menu.Id)).Select(m => m.RoleName).ToList();
|
var ret = RoleHelper.Retrieves().Cast<Role>().Where(md => md.Menus != null && md.Menus.Contains(menu.Id)).Select(m => m.RoleName).ToList();
|
||||||
if (!ret.Contains("Administrators")) ret.Add("Administrators");
|
if (!ret.Contains("Administrators")) ret.Add("Administrators");
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.Task> Retrieves()
|
public override IEnumerable<DataAccess.Task> Retrieves()
|
||||||
{
|
{
|
||||||
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Task>("Tasks");
|
var users = DbManager.DBAccess.GetCollection<DataAccess.Task>("Tasks");
|
||||||
return users.Find(FilterDefinition<DataAccess.Task>.Empty).SortByDescending(task => task.AssignTime).ToList();
|
return users.Find(FilterDefinition<DataAccess.Task>.Empty).SortByDescending(task => task.AssignTime).ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
.Include(u => u.DisplayName)
|
.Include(u => u.DisplayName)
|
||||||
.Include(u => u.Icon)
|
.Include(u => u.Icon)
|
||||||
.Include(u => u.Css);
|
.Include(u => u.Css);
|
||||||
return MongoDbAccessManager.Users.Find(user => user.UserName == userName).Project<DataAccess.User>(project).FirstOrDefault();
|
return DbManager.Users.Find(user => user.UserName == userName).Project<DataAccess.User>(project).FirstOrDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -47,7 +47,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false;
|
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false;
|
||||||
|
|
||||||
var u = MongoDbAccessManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
|
var u = DbManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
|
||||||
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
|
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
|
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => DbManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
|
@ -72,7 +72,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
.Include(u => u.Description)
|
.Include(u => u.Description)
|
||||||
.Include(u => u.Groups)
|
.Include(u => u.Groups)
|
||||||
.Include(u => u.Roles);
|
.Include(u => u.Roles);
|
||||||
return MongoDbAccessManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
return DbManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -87,7 +87,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
|
|
||||||
if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500);
|
if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500);
|
||||||
if (user.UserName.Length > 50) user.UserName = user.UserName.Substring(0, 50);
|
if (user.UserName.Length > 50) user.UserName = user.UserName.Substring(0, 50);
|
||||||
MongoDbAccessManager.Users.InsertOne(new User()
|
DbManager.Users.InsertOne(new User()
|
||||||
{
|
{
|
||||||
UserName = user.UserName,
|
UserName = user.UserName,
|
||||||
DisplayName = user.DisplayName,
|
DisplayName = user.DisplayName,
|
||||||
|
@ -116,7 +116,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
var passSalt = LgbCryptography.GenerateSalt();
|
var passSalt = LgbCryptography.GenerateSalt();
|
||||||
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
|
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
|
||||||
var update = Builders<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName);
|
var update = Builders<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName);
|
||||||
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == id, update);
|
DbManager.Users.FindOneAndUpdate(u => u.Id == id, update);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
var passSalt = LgbCryptography.GenerateSalt();
|
var passSalt = LgbCryptography.GenerateSalt();
|
||||||
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
|
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
|
||||||
var update = Builders<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt);
|
var update = Builders<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt);
|
||||||
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.UserName == UserName, update);
|
DbManager.Users.FindOneAndUpdate(u => u.UserName == UserName, update);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +152,7 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
{
|
{
|
||||||
list.Add(new DeleteOneModel<User>(Builders<User>.Filter.Eq(u => u.Id, id)));
|
list.Add(new DeleteOneModel<User>(Builders<User>.Filter.Eq(u => u.Id, id)));
|
||||||
}
|
}
|
||||||
MongoDbAccessManager.Users.BulkWrite(list);
|
DbManager.Users.BulkWrite(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,23 +176,23 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByRoleId(string roleId, IEnumerable<string> userIds)
|
public override bool SaveByRoleId(string roleId, IEnumerable<string> userIds)
|
||||||
{
|
{
|
||||||
var users = MongoDbAccessManager.Users.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
var users = DbManager.Users.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
||||||
|
|
||||||
// Remove roles
|
// Remove roles
|
||||||
users.ForEach(p =>
|
users.ForEach(p =>
|
||||||
{
|
{
|
||||||
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
roles.Remove(roleId);
|
roles.Remove(roleId);
|
||||||
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
||||||
});
|
});
|
||||||
|
|
||||||
users = MongoDbAccessManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
users = DbManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
||||||
// Add roles
|
// Add roles
|
||||||
users.ForEach(p =>
|
users.ForEach(p =>
|
||||||
{
|
{
|
||||||
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
||||||
roles.Add(roleId);
|
roles.Add(roleId);
|
||||||
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -217,23 +217,23 @@ namespace Bootstrap.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override bool SaveByGroupId(string groupId, IEnumerable<string> userIds)
|
public override bool SaveByGroupId(string groupId, IEnumerable<string> userIds)
|
||||||
{
|
{
|
||||||
var users = MongoDbAccessManager.Users.Find(md => md.Groups != null && md.Groups.Contains(groupId)).ToList();
|
var users = DbManager.Users.Find(md => md.Groups != null && md.Groups.Contains(groupId)).ToList();
|
||||||
|
|
||||||
// Remove roles
|
// Remove roles
|
||||||
users.ForEach(p =>
|
users.ForEach(p =>
|
||||||
{
|
{
|
||||||
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
||||||
groups.Remove(groupId);
|
groups.Remove(groupId);
|
||||||
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
||||||
});
|
});
|
||||||
|
|
||||||
users = MongoDbAccessManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
users = DbManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
||||||
// Add roles
|
// Add roles
|
||||||
users.ForEach(p =>
|
users.ForEach(p =>
|
||||||
{
|
{
|
||||||
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
||||||
groups.Add(groupId);
|
groups.Add(groupId);
|
||||||
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue