@@ -29203,3 +29203,125 @@ func HandleUserPrivateTraining(resp http.ResponseWriter, request *http.Request)
2920329203 resp.WriteHeader(http.StatusOK)
2920429204 resp.Write([]byte(`{"success": true}`))
2920529205}
29206+
29207+ // An API to ONLY return PUBLIC forms for an org
29208+ // A public form = Workflow with "sharing": "form"
29209+ func HandleGetOrgForms(resp http.ResponseWriter, request *http.Request) {
29210+ cors := HandleCors(resp, request)
29211+ if cors {
29212+ return
29213+ }
29214+
29215+ err := ValidateRequestOverload(resp, request)
29216+ if err != nil {
29217+ log.Printf("[INFO] Request overload for IP %s Get Org Forms", GetRequestIp(request))
29218+ resp.WriteHeader(429)
29219+ resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Too many requests"}`)))
29220+ return
29221+ }
29222+
29223+ var orgId string
29224+ location := strings.Split(request.URL.String(), "/")
29225+ if location[1] == "api" {
29226+ if len(location) <= 4 {
29227+ log.Printf("Path too short: %d", len(location))
29228+ resp.WriteHeader(401)
29229+ resp.Write([]byte(`{"success": false}`))
29230+ return
29231+ }
29232+
29233+ orgId = location[4]
29234+ }
29235+
29236+ if strings.Contains(orgId, "?") {
29237+ orgId = strings.Split(orgId, "?")[0]
29238+ }
29239+
29240+ if len(orgId) < 36 || len(orgId) > 36 {
29241+ log.Printf("[WARNING] Bad ID '%s' of length %d when getting forms is not valid", orgId, len(orgId))
29242+
29243+ resp.WriteHeader(400)
29244+ resp.Write([]byte(`{"success": false, "reason": "Org ID when getting forms is not valid"}`))
29245+ return
29246+ }
29247+
29248+ // Load the org to see if it wants them public or not
29249+ ctx := GetContext(request)
29250+ org, err := GetOrg(ctx, orgId)
29251+ if err != nil {
29252+ log.Printf("[WARNING] Org %s doesn't exist.", orgId)
29253+ resp.WriteHeader(403)
29254+ resp.Write([]byte(`{"success": false, "reason": "Failed finding org"}`))
29255+ return
29256+ }
29257+
29258+ log.Printf("[INFO] Getting forms for org %s (%s)", org.Name, org.Id)
29259+
29260+
29261+ // Prevent cache steals in any way
29262+ randomUserId := uuid.NewV4().String()
29263+ user := User{
29264+ Id: randomUserId,
29265+ ActiveOrg: OrgMini{
29266+ Id: orgId,
29267+ Name: org.Name,
29268+ },
29269+ }
29270+
29271+ workflows, err := GetAllWorkflowsByQuery(ctx, user, 50, "")
29272+ if err != nil {
29273+ log.Printf("[WARNING] Failed getting workflows for user %s (0): %s", user.Username, err)
29274+ resp.WriteHeader(400)
29275+ resp.Write([]byte(`{"success": false}`))
29276+ return
29277+ }
29278+
29279+ if len(workflows) == 0 {
29280+ log.Printf("[INFO] No workflows found for user %s (%s) in org %s (%s)", user.Username, user.Id, user.ActiveOrg.Name, user.ActiveOrg.Id)
29281+ resp.WriteHeader(200)
29282+ resp.Write([]byte("[]"))
29283+ return
29284+ }
29285+
29286+ relevantForms := []Workflow{}
29287+ for _, workflow := range workflows {
29288+ if workflow.Sharing != "form" {
29289+ continue
29290+ }
29291+
29292+ // Overwrite to remove anything unecessary for most locations
29293+ workflow = Workflow{
29294+ Name: workflow.Name,
29295+ ID: workflow.ID,
29296+ Owner: workflow.Owner,
29297+ OrgId: workflow.OrgId,
29298+
29299+ OutputYields: workflow.OutputYields,
29300+ Sharing: workflow.Sharing,
29301+ Description: workflow.Description,
29302+ InputQuestions: workflow.InputQuestions,
29303+ InputMarkdown: workflow.InputMarkdown,
29304+ }
29305+ relevantForms = append(relevantForms, workflow)
29306+ }
29307+
29308+ if len(relevantForms) == 0 {
29309+ log.Printf("[INFO] No forms found for user %s (%s) in org %s (%s)", user.Username, user.Id, user.ActiveOrg.Name, user.ActiveOrg.Id)
29310+ resp.WriteHeader(200)
29311+ resp.Write([]byte("[]"))
29312+ return
29313+ }
29314+
29315+ log.Printf("[INFO] Found %d forms for org %s (%s)", len(relevantForms), user.ActiveOrg.Name, user.ActiveOrg.Id)
29316+
29317+ body, err := json.Marshal(relevantForms)
29318+ if err != nil {
29319+ log.Printf("[WARNING] Failed form GET marshalling: %s", err)
29320+ resp.WriteHeader(http.StatusInternalServerError)
29321+ resp.Write([]byte(`{"success": false}`))
29322+ return
29323+ }
29324+
29325+ resp.WriteHeader(200)
29326+ resp.Write(body)
29327+ }
0 commit comments