Ejemplo n.º 1
0
        public override void ToJSON(JObject ParentObject)
        {
            //! the data should be stored this way in the DB and not transformed into it, but we can't do this in Magnolia because of design mode and field type shenanigans

            /*
             * data is coming from the server in two dictionaries: a mapping of value to string, and an array of held permissions
             *                          Options: {
             *                             'nt_1_Create': 'Role',
             *                             'nt_1_tab_1_View': 'Role, Role',
             *                             ...
             *                          }
             *                          Values: [
             *                            'nt_1_Create',
             *                            'nt_1_tab_5_Create',
             *                            ...
             *                         ]
             *
             * We need it in the format: Options: [
             *                            { name: 'Role',
             *                              Create: true,
             *                              Edit: true,
             *                              Delete: true,
             *                              View: true,
             *                              Children: [
             *                                 { name: 'Role Tab',
             *                                   Create: true,
             *                                   Edit: true,
             *                                   Delete: true,
             *                                   View: true,
             *                                   leaf: true
             *                                 }]
             *                             },
             *                             ...
             *                             ]
             *
             */

            ParentObject[_ValueSubField.ToXmlNodeName(true)] = Value.ToString();

            JObject OptionsArray = new JObject();

            ParentObject["options"] = OptionsArray;

            //first build the skeleton of the object we want to return
            foreach (CswNbtMetaDataNodeType NT in _CswNbtResources.MetaData.getNodeTypes())
            {
                JObject NewNT = new JObject();
                NewNT["itemname"] = NT.NodeTypeName;
                NewNT["create"]   = false;
                NewNT["edit"]     = false;
                NewNT["view"]     = false;
                NewNT["delete"]   = false;
                NewNT["itemid"]   = NT.NodeTypeId;
                JArray Children = new JArray();
                foreach (CswNbtMetaDataNodeTypeTab Tab in NT.getNodeTypeTabs())
                {
                    JObject NewTab = new JObject();
                    NewTab["itemname"] = Tab.TabName + " Tab";
                    NewTab["create"]   = false;
                    NewTab["edit"]     = false;
                    NewTab["view"]     = false;
                    NewTab["delete"]   = false;
                    NewTab["itemid"]   = Tab.TabId;
                    NewTab["leaf"]     = true;

                    Children.Add(NewTab);
                }
                NewNT["children"] = Children;
                OptionsArray[NT.NodeTypeId.ToString()] = NewNT;
            }

            //now iterate the values we've stored and assign them to the correct permissions in the skeleton:
            foreach (string Permission in Value)
            {
                string[] PermissionComponents = Permission.Split('_');
                bool     IsTab              = PermissionComponents.Length == 5;
                string   Nodetype           = PermissionComponents[1];
                string   PermissionCategory = PermissionComponents[PermissionComponents.Length - 1].ToLower();

                //our permissions data needs a good cleaning... there are invalid nodetypes in the Values dictionary
                if (ParentObject["options"][Nodetype] != null)
                {
                    if (false == IsTab)
                    {
                        //if we're not dealing with a tab, we can just set the proper permission
                        ParentObject["options"][Nodetype][PermissionCategory] = true;
                    }
                    else
                    {
                        string TabId = PermissionComponents[3];
                        //for tabs we have no choice but to iterate to find the correct tab, because of client-side restrictions
                        foreach (JObject Tab in ParentObject["options"][Nodetype]["children"])
                        {
                            if (Tab["itemid"].ToString() == TabId.ToString())
                            {
                                Tab[PermissionCategory] = true;
                            }
                        } //for each tab in the returned JObject
                    }     //else -- if the permission is a tab
                }         //if this nodetype exists
            }             //for each permission stored in the database
        }                 // ToJSON()