Beispiel #1
0
        /// <summary>
        /// GetRole
        /// </summary>
        /// <param name="mongoDb"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public static BsonDocument GetRole(MongoDatabase mongoDb, String RoleName)
        {
            EvalArgs doc = new EvalArgs();

            doc.Code = "db.getRole('" + RoleName + "',{showPrivileges:true})";
            return(mongoDb.Eval(doc).AsBsonDocument);
        }
        public void TestEvalWithMaxTime()
        {
            RequireServer.Check().Supports(Feature.Eval);
#pragma warning disable 618
            if (!DriverTestConfiguration.Client.Settings.Credentials.Any())
            {
                if (_primary.Supports(FeatureId.MaxTime))
                {
                    using (var failpoint = new FailPoint(FailPointName.MaxTimeAlwaysTimeout, _server, _primary))
                    {
                        if (failpoint.IsSupported())
                        {
                            failpoint.SetAlwaysOn();
                            var args = new EvalArgs
                            {
                                Code    = "return 0;",
                                MaxTime = TimeSpan.FromMilliseconds(1)
                            };
                            Assert.Throws <MongoExecutionTimeoutException>(() => _adminDatabase.Eval(args));
                        }
                    }
                }
            }
#pragma warning restore
        }
Beispiel #3
0
        /// <summary>
        ///     GetRole
        /// </summary>
        /// <param name="mongoDb"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public static BsonDocument GetRole(MongoDatabase mongoDb, string roleName)
        {
            var doc = new EvalArgs()
            {
                Code = "db.getRole('" + roleName + "',{showPrivileges:true})"
            };

            return(mongoDb.Eval(doc).AsBsonDocument);
        }
Beispiel #4
0
 public void LogEval(MongoDatabase database, EvalArgs evalArgs)
 {
     if (_writeToFile == null)
     {
         return;
     }
     _writeToFile.Write("{0:yyyy-MM-dd HH:mm:ss.ffffff} ", DateTime.Now);
     _writeToFile.Write("Eval : {0} ", database.zGetFullName());
     _writeToFile.Write(evalArgs.ToJson());
     _writeToFile.WriteLine();
 }
Beispiel #5
0
        /// <summary>
        ///     初始化副本(Mongo Shell)
        /// </summary>
        /// <returns></returns>
        public static CommandResult InitReplicaSet()
        {
            //使用local数据库发送 rs.initiate() 指令
            MongoDatabase mongoDb = RuntimeMongoDbContext.GetCurrentClient().GetServer().GetDatabase("local");
            var           args    = new EvalArgs {
                Code = "rs.initiate()"
            };
            var result = mongoDb.Eval(args);

            return(new CommandResult(result.AsBsonDocument));
        }
Beispiel #6
0
        /// <summary>
        ///     eval Javascript
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdEval_Click(object sender, EventArgs e)
        {
            var mongoDb = RuntimeMongoDbContext.GetCurrentDataBase();
            var js      = new BsonJavaScript(ctlEval.Context);
            var Params  = new List <BsonValue>();

            if (txtParm.Text != string.Empty)
            {
                foreach (var parm in txtParm.Text.Split(",".ToCharArray()))
                {
                    if (parm.StartsWith("'") & parm.EndsWith("'"))
                    {
                        Params.Add(parm.Trim("'".ToCharArray()));
                    }
                    else
                    {
                        try
                        {
                            var isNuberic = true;
                            for (var i = 0; i < parm.Length; i++)
                            {
                                if (!char.IsNumber(parm, i))
                                {
                                    isNuberic = false;
                                }
                            }
                            if (isNuberic)
                            {
                                Params.Add(Convert.ToInt16(parm));
                            }
                        }
                        catch (Exception ex)
                        {
                            Utility.ExceptionDeal(ex, "Exception", "Parameter Exception");
                        }
                    }
                }
            }
            try
            {
                var args = new EvalArgs {
                    Args = Params.ToArray(), Code = js
                };
                var result = mongoDb.Eval(args);
                MyMessageBox.ShowMessage("Result", "Result",
                                         result.ToJson(MongoHelper.JsonWriterSettings), true);
            }
            catch (Exception ex)
            {
                Utility.ExceptionDeal(ex, "Exception", "Result");
            }
        }
Beispiel #7
0
        //public static bool ResultToGrid = false;
        //public static bool ResultToText = false;

        public static void Eval(string code, string databaseName = "admin", string server = null)
        {
            MongoDatabase database = MongoCommand.GetDatabase(server, databaseName);
            //if (code.StartsWith("{") && code.EndsWith("}"))
            //    code = "db.runCommand( " + code + " )";
            //EvalArgs evalArgs = new EvalArgs { Code = new BsonJavaScript(code) };
            EvalArgs evalArgs = code.zToEvalArgs();

            Trace.WriteLine("Eval : {0} {1}", database.zGetFullName(), evalArgs.ToJson());
            // obsolete : 'MongoDB.Driver.MongoDatabase.Eval(MongoDB.Bson.BsonJavaScript, params object[])' is obsolete: 'Use the overload of Eval that has an EvalArgs parameter instead.'
            // BsonValue Eval(BsonJavaScript code, params object[] args);
            BsonValue result = database.zEval(evalArgs);

            Trace.WriteLine(result.zToJson());
        }
Beispiel #8
0
        /// <summary>
        ///     添加一个用户自定义角色
        /// </summary>
        public static BsonValue AddRole(MongoDatabase mongoDb, Role role)
        {
            var roleCommand = string.Empty;
            var doc         = new EvalArgs();

            roleCommand  = "db.createRole(" + Environment.NewLine;
            roleCommand += "{" + Environment.NewLine;
            roleCommand += "    role: '" + role.Rolename + "'," + Environment.NewLine;
            //Roles
            roleCommand += "    roles:" + Environment.NewLine;
            roleCommand += "    [";
            for (var i = 0; i < role.Roles.Length; i++)
            {
                var singleroles = role.Roles[i];
                roleCommand += "{ role: '" + singleroles.MRole + "', db: '" + singleroles.Db + "' }" +
                               (i == role.Roles.Length - 1 ? "" : ",") + Environment.NewLine;
            }
            roleCommand += "     ],";
            //privileges
            roleCommand += "    privileges:" + Environment.NewLine;
            roleCommand += "    [";
            for (var i = 0; i < role.Privileges.Length; i++)
            {
                var singleprivileges = role.Privileges[i];
                roleCommand += "{" + singleprivileges.Resource.GetJsCode() + "," +
                               MongoDbAction.GetActionListJs(singleprivileges.Actions) + "}" +
                               (i == role.Privileges.Length - 1 ? "" : ",") + Environment.NewLine;
            }
            roleCommand += "     ],";
            //
            roleCommand += "}" + Environment.NewLine;
            roleCommand += ")";
            doc.Code     = roleCommand;
            BsonValue result;

            try
            {
                result = mongoDb.Eval(doc);
            }
            catch (MongoCommandException ex)
            {
                result = ex.Result;
            }
            return(result);
        }
Beispiel #9
0
        /// <summary>
        ///     执行js
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butEval_Click(object sender, EventArgs e)
        {
            var mongoDb = RuntimeMongoDbContext.GetCurrentDataBase();
            var js      = new BsonJavaScript(txtParameter.Text);

            try
            {
                var args = new EvalArgs {
                    Code = js
                };
                var result = mongoDb.Eval(args);
                txtEvalJavaScript.Text = ToJson(result);
            }
            catch (Exception ex)
            {
                txtEvalJavaScript.Text = ex.Message;
            }
        }
Beispiel #10
0
        /// <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);
        }
Beispiel #11
0
 public void TestEvalWithMaxTime()
 {
     if (_primary.Supports(FeatureId.MaxTime))
     {
         using (var failpoint = new FailPoint(FailPointName.MaxTimeAlwaysTimeout, _server, _primary))
         {
             if (failpoint.IsSupported())
             {
                 failpoint.SetAlwaysOn();
                 var args = new EvalArgs
                 {
                     Code    = "return 0;",
                     MaxTime = TimeSpan.FromMilliseconds(1)
                 };
                 Assert.Throws <ExecutionTimeoutException>(() => _database.Eval(args));
             }
         }
     }
 }
Beispiel #12
0
 public static BsonValue zEval(this MongoDatabase database, EvalArgs evalArgs)
 {
     MongoLog.CurrentMongoLog.LogEval(database, evalArgs);
     return(MongoLog.CurrentMongoLog.ExecuteAndLogResult(() => database.Eval(evalArgs)));
 }