public JsonResult Save([FromBody] OperatingAuthoritySaveModel model) { if (string.IsNullOrEmpty(model.RoleID)) { return(Json(new { Code = 300, Msg = "RoleID is not defined." })); } if (model.Authorities == null) { model.Authorities = new List <string>(); } var mongo = new MongoHelper(); // 移除旧权限 var filter = Builders <BsonDocument> .Filter.Eq("RoleID", model.RoleID); mongo.DeleteMany(Constant.OperatingAuthorityCollectionName, filter); // 添加新权限 if (model.Authorities.Count > 0) { var docs = new List <BsonDocument>(); foreach (var i in model.Authorities) { docs.Add(new BsonDocument { ["RoleID"] = model.RoleID, ["AuthorityID"] = i }); } mongo.InsertMany(Constant.OperatingAuthorityCollectionName, docs); } return(Json(new { Code = 200, Msg = "Saved successfully!" })); }
public JsonResult Initialize() { // 判断权限是否开启 var enableAuthority = ConfigurationManager.AppSettings["EnableAuthority"]; if (enableAuthority != "true") { return(Json(new { Code = 300, Msg = "Authority is not enabled.", })); } // 判断是否已经初始化 var mongo = new MongoHelper(); var doc = mongo.FindAll(Constant.ConfigCollectionName).FirstOrDefault(); if (doc != null && doc.Contains("Initialized") && doc["Initialized"].ToBoolean() == true) { return(Json(new { Code = 300, Msg = "System has already initialized." })); } var defaultRegisterRoleID = ObjectId.GenerateNewId(); if (doc == null) { doc = new BsonDocument { ["ID"] = ObjectId.GenerateNewId(), ["Initialized"] = true, ["DefaultRegisterRole"] = defaultRegisterRoleID }; mongo.InsertOne(Constant.ConfigCollectionName, doc); } else { var filter11 = Builders <BsonDocument> .Filter.Eq("_id", doc["_id"].AsObjectId); var update11 = Builders <BsonDocument> .Update.Set("Initialized", true); var update12 = Builders <BsonDocument> .Update.Set("DefaultRegisterRole", defaultRegisterRoleID); var update13 = Builders <BsonDocument> .Update.Combine(update11, update12); mongo.UpdateOne(Constant.ConfigCollectionName, filter11, update13); } // 初始化角色 var now = DateTime.Now; var filter1 = Builders <BsonDocument> .Filter.Eq("Name", "Administrator"); var filter2 = Builders <BsonDocument> .Filter.Eq("Name", "User"); var filter3 = Builders <BsonDocument> .Filter.Eq("Name", "Guest"); var filter = Builders <BsonDocument> .Filter.Or(filter1, filter2, filter3); mongo.DeleteMany(Constant.RoleCollectionName, filter); var adminRoleID = ObjectId.GenerateNewId(); // 管理员RoleID var role1 = new BsonDocument { ["ID"] = adminRoleID, ["Name"] = "Administrator", ["CreateTime"] = now, ["UpdateTime"] = now, ["Description"] = "Administrator", ["Status"] = 0, }; var role2 = new BsonDocument { ["ID"] = defaultRegisterRoleID, ["Name"] = "User", ["CreateTime"] = now, ["UpdateTime"] = now, ["Description"] = "Login User", ["Status"] = 0, }; var role3 = new BsonDocument { ["ID"] = ObjectId.GenerateNewId(), ["Name"] = "Guest", ["CreateTime"] = now, ["UpdateTime"] = now, ["Description"] = "No Login User", ["Status"] = 0, }; mongo.InsertMany(Constant.RoleCollectionName, new[] { role1, role2, role3 }); // 初始化用户 var password = "******"; var salt = DateTime.Now.ToString("yyyyMMddHHmmss"); var user = new BsonDocument { ["ID"] = ObjectId.GenerateNewId(), ["Username"] = "******", ["Password"] = MD5Helper.Encrypt(password + salt), ["Name"] = "Administrator", ["RoleID"] = adminRoleID.ToString(), ["Gender"] = 0, ["Phone"] = "", ["Email"] = "", ["QQ"] = "", ["CreateTime"] = now, ["UpdateTime"] = now, ["Salt"] = salt, ["Status"] = 0, }; mongo.InsertOne(Constant.UserCollectionName, user); return(Json(new { Code = 200, Msg = "Initialize successfully!" })); }
public async Task <bool> DeleteMany <T>(Expression <Func <T, bool> > findExpression) { return(await MongoHelper.DeleteMany(findExpression).ConfigureAwait(false)); }
public JsonResult Save([FromBody] OperatingAuthoritySaveModel model) { if (string.IsNullOrEmpty(model.RoleID)) { return(Json(new { Code = 300, Msg = "RoleID is not defined." })); } if (model.Authorities == null) { model.Authorities = new List <string>(); } // 获取角色 var roleID = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(model.RoleID) && !ObjectId.TryParse(model.RoleID, out roleID)) { return(Json(new { Code = 300, Msg = "ID is not allowed." })); } var mongo = new MongoHelper(); var filter = Builders <BsonDocument> .Filter.Eq("ID", roleID); var role = mongo.FindOne(Constant.RoleCollectionName, filter); if (role == null) { return(Json(new { Code = 300, Msg = "The role is not existed." })); } var roleName = role["Name"].ToString(); if (roleName == "Administrator") { return(Json(new { Code = 300, Msg = "Modifying admin rights is not allowed." })); } // 移除旧权限 filter = Builders <BsonDocument> .Filter.Eq("RoleID", model.RoleID); mongo.DeleteMany(Constant.OperatingAuthorityCollectionName, filter); // 添加新权限 if (model.Authorities.Count > 0) { var docs = new List <BsonDocument>(); foreach (var i in model.Authorities) { docs.Add(new BsonDocument { ["RoleID"] = model.RoleID, ["AuthorityID"] = i }); } mongo.InsertMany(Constant.OperatingAuthorityCollectionName, docs); } return(Json(new { Code = 200, Msg = "Saved successfully!" })); }
public void TestDeleteMany() { var count = _helper.DeleteMany <Employee>("T_Employee", (m) => m.Name == "Arima"); Assert.AreEqual(3, count); }