Esempio n. 1
0
        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!"
            }));
        }
Esempio n. 2
0
        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!"
            }));
        }