/// <summary> /// Add A Custom User /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddRole_Click(object sender, EventArgs e) { Role r = new Role(); if (SystemManager.GetCurrentDataBase() != null) { r.database = SystemManager.GetCurrentDataBase().Name; } else { r.database = MongoDbHelper.DATABASE_NAME_ADMIN; } r.rolename = txtRolename.Text; r.privileges = new Role.privilege[PrivilegeList.Count]; for (int i = 0; i < PrivilegeList.Count; i++) { r.privileges[i] = PrivilegeList[i]; } r.roles = new Role.GrantRole[RoleList.Count]; for (int i = 0; i < RoleList.Count; i++) { r.roles[i] = RoleList[i]; } var result = Role.AddRole(SystemManager.GetCurrentDataBase(), r); if (result.IsBsonDocument) { MyMessageBox.ShowMessage("Error", "Add Role Error",MongoDbHelper.ConvertToString(result)); } else { MyMessageBox.ShowEasyMessage("Succeed", "Add Role OK"); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddRoleToDB_Click(object sender, EventArgs e) { Role r = new Role(); r.rolename = "tester" + System.DateTime.Now.ToString("yyyyMMddHHmmss"); r.database = "admin"; r.roles = new Role.GrantRole[1]; r.roles[0] = new Role.GrantRole() { db = "admin", mRole="read"}; r.privileges = new Role.privilege[1]; r.privileges[0] = new Role.privilege() { actions = new Common.Security.Action.ActionType[1] { Common.Security.Action.ActionType.DatabaseManagementActions_createCollection }, resource = new Resource() { CollectionName = "", DataBaseName = "admin", Type = Resource.ResourceType.DataBase } }; var doc = Role.AddRole(SystemManager.GetCurrentServer().GetDatabase("admin"), r); if (doc.IsBsonDocument) MongoDbHelper.FillDataToTreeView("myClusterwideAdmin", treeViewColumns1, doc.AsBsonDocument); }
/// <summary> /// 添加一个用户自定义角色 /// </summary> public static BsonValue AddRole(MongoDatabase mongoDb, Role role) { String RoleCommand = String.Empty; EvalArgs doc = new EvalArgs(); RoleCommand = "db.createRole(" + System.Environment.NewLine; RoleCommand += "{" + System.Environment.NewLine; RoleCommand += " role: '" + role.rolename + "'," + System.Environment.NewLine; //Roles RoleCommand += " roles:" + System.Environment.NewLine; RoleCommand += " ["; for (int i = 0; i < role.roles.Length; i++) { var singleroles = role.roles[i]; RoleCommand += "{ role: '" + singleroles.mRole + "', db: '" + singleroles.db + "' }" + ((i == role.roles.Length - 1) ? "" : ",") + System.Environment.NewLine; } RoleCommand += " ],"; //privileges RoleCommand += " privileges:" + System.Environment.NewLine; RoleCommand += " ["; for (int i = 0; i < role.privileges.Length; i++) { var singleprivileges = role.privileges[i]; RoleCommand += "{" + singleprivileges.resource.GetJsCode() + "," + Action.GetActionListJs(singleprivileges.actions) + "}" + ((i == role.privileges.Length - 1) ? "" : ",") + System.Environment.NewLine; } RoleCommand += " ],"; // RoleCommand += "}" + System.Environment.NewLine; RoleCommand += ")"; doc.Code = RoleCommand; BsonValue result; try { result = mongoDb.Eval(doc); } catch (MongoCommandException ex) { result = ex.CommandResult.Response; } return result; }