Beispiel #1
0
        public async Task <ActionResult> Edit(string id, string name, string email, string[] roles, string[] categories)
        {
            email = (email ?? "").ToLower();
            name  = name.ToStringFarsi();
            //if (string.IsNullOrWhiteSpace(name))
            //    TempData["MSG"] = "لطفا نام و نام خانوادگی را وارد کنید";

            if (string.IsNullOrWhiteSpace(email))
            {
                TempData["MSG"] = "لطفا پست الکترونیک را وارد کنید";
            }


            else
            {
                //string q = "{update:'users',updates:[{q:{_id:ObjectId('"+id+ "')},u:{$set:{name:'" + name+"',email:'"+email+"',roles:[" + string.Join(",", roles.Select(r=>"'"+r+"'"))  +"]}}}]}";
                string q = "{update:'users',updates:[{q:{_id:ObjectId('" + id + "')},u:{$set:{email:'" + email + "',roles:" + (roles ?? new string[] { }).toJSON() + ",categories:" + (categories ?? new string[] { }).toJSON() + "}}}]}";
                await NoSql.Instance.RunCommandAsync <BsonDocument>(q);

                return(RedirectToAction("Manage"));
            }
            ViewBag.name  = name;
            ViewBag.email = email;
            ViewBag.roles = await UC.AllRolesAsync();

            ViewBag.categories = await UC.AllCategoriesAsync();

            return(View());
        }
Beispiel #2
0
        public async Task <ActionResult> New()
        {
            await UC.InitializeAsync(this);

            ViewBag.roles = await UC.AllRolesAsync();

            ViewBag.categories = await UC.AllCategoriesAsync();

            ViewBag.active_users = "active";
            return(View());
        }
Beispiel #3
0
        public async Task <ActionResult> Edit(string id)
        {
            await UC.InitializeAsync(this);

            string q     = "{aggregate:'users',pipeline:[{$match:{_id:ObjectId('" + id + "')}},{$limit:1}]}";
            var    model = await NoSql.Instance.RunCommandAsync <BsonDocument>(q);

            ViewBag.roles = await UC.AllRolesAsync();

            ViewBag.categories = await UC.AllCategoriesAsync();

            ViewBag.active_users = "active";
            return(View(model.GetValue("result")[0]));
        }
Beispiel #4
0
        public async Task <ActionResult> New(string name, string uname, string password, string email, string[] roles, string[] categories)
        {
            await UC.InitializeAsync(this);

            uname = (uname ?? "").ToLower();
            email = (email ?? "").ToLower();
            name  = name.ToStringFarsi();
            if (string.IsNullOrWhiteSpace(name))
            {
                TempData["MSG"] = "لطفا نام و نام خانوادگی را وارد کنید";
            }
            else if (string.IsNullOrWhiteSpace(uname))
            {
                TempData["MSG"] = "لطفا نام کاربری را وارد کنید";
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                TempData["MSG"] = "لطفا رمزعبور را وارد کنید";
            }
            else if (string.IsNullOrWhiteSpace(email))
            {
                TempData["MSG"] = "لطفا پست الکترونیک را وارد کنید";
            }
            else if (roles == null)
            {
                TempData["MSG"] = "لطفا حدافل یک نقش  را انتخاب کنید";
            }
            else if (categories == null)
            {
                TempData["MSG"] = "لطفا حدافل یک سرویس  را انتخاب کنید";
            }
            else if ((await NoSql.Instance.RunCommandAsync <BsonDocument>("{aggregate:'users',pipeline:[{$match:{uname:'" + uname + "'}},{$limit:1}]}")).GetValue("result").AsBsonArray.Count > 0)
            {
                TempData["MSG"] = "این نام کاربری هم اکنون در سیستم موجود است";
            }
            else if ((await NoSql.Instance.RunCommandAsync <BsonDocument>("{aggregate:'users',pipeline:[{$match:{name:'" + name + "'}},{$limit:1}]}")).GetValue("result").AsBsonArray.Count > 0)
            {
                TempData["MSG"] = "کاربری با این نام و نام خانوادگی هم اکنون در سیستم موجود است";
            }
            else
            {
                var collection = NoSql.Instance.GetCollection <BsonDocument>("users");
                var bdoc       = new BsonDocument {
                    { "name", name }, { "uname", uname }, { "password", password }, { "email", email }, { "status", "فعال" }, { "createdat", DateTime.Now }
                };
                if (roles.Length > 0)
                {
                    bdoc.Add("roles", new BsonArray(roles));
                }
                if (categories.Length > 0)
                {
                    bdoc.Add("categories", new BsonArray(categories));
                }
                await collection.InsertOneAsync(bdoc);

                return(RedirectToAction("Manage"));
            }

            ViewBag.name  = name;
            ViewBag.uname = uname;
            ViewBag.email = email;
            ViewBag.roles = await UC.AllRolesAsync();

            ViewBag.categories = await UC.AllCategoriesAsync();

            return(View());
        }