func xml2CleanMap(body []byte) (resultMap map[string]interface{}, err error) { values, err := x2j.XmlToMap(body) if err != nil { return } cleanProperties := make(map[string]interface{}) resultMap = make(map[string]interface{}) for key, value := range values { resultMap["type"] = key ciType, err := metadata.Type(key) if err != nil { return nil, err } valueMap := value.(map[string]interface{}) for k, v := range valueMap { if strings.HasPrefix(k, "-") { resultMap[k[1:]] = v } else { cleanProperties[k], err = readProperty(k, v, ciType) if err != nil { return nil, err } } } } resultMap["content"] = cleanProperties return resultMap, nil }
func grant(args intf.Command) (result string, err error) { subs := args.Subs() ci := subs[0] roleToChange := subs[1] perms := subs[2:] // Get current permissions // (already works for global keyword :) ) body, err := http.Read("/internal/security/roles/permissions/" + repo.AntiAbbreviate(ci)) if err != nil { return } values, err := x2j.XmlToMap(body) if err != nil { return } arr := arrayFromMap(values["collection"], "rolePermissions") changed := false for _, elMap := range arr { role := elMap["role"].(map[string]interface{}) if role["-name"] == roleToChange { oldPerms := elMap["permissions"].([]interface{}) for _, el := range perms { oldPerms = append(oldPerms, el) } elMap["permissions"], changed = oldPerms, true } } if !changed { roleMap := map[string]string{"-id": strconv.Itoa(len(arr)), "-role": roleToChange} fmt.Println("Need to add it.... :( ", roleMap) fullMap := map[string]interface{}{"permissions": perms, "role": roleMap} arr = append(arr, fullMap) values["collection"].(map[string]interface{})["rolePermissions"] = arr } // TODO Make this a util? json, _ := j2x.MapToJson(values) xml, _ := j2x.JsonToXml(json) fmt.Println("modified(?) response = ", string(xml)) /* body, err = http.Update("/internal/security/roles/permissions/" + repo.AntiAbbreviate(ci), bytes.NewBuffer(xml)) if err != nil { return } fmt.Println("modified(?) response = ", body) //*/ //fmt.Println("permset = ", permSet) // decode XML to ... JSON or marshall to objects? // is role already in there? // if not: add role // is permission already in there? -> ignore // add permission // encode as XML // post to http return }