From 9a3345bcaf9f8d34ae24c055553900d9e26a5783 Mon Sep 17 00:00:00 2001 From: frikky Date: Sat, 10 Oct 2020 17:03:35 +0200 Subject: [PATCH] Continued autocomplete sync setup --- backend/go-app/main.go | 71 ++++++++++++++++++++++++++++++++++-- backend/go-app/walkoff.go | 10 +++++ frontend/src/views/Admin.jsx | 42 +-------------------- 3 files changed, 78 insertions(+), 45 deletions(-) diff --git a/backend/go-app/main.go b/backend/go-app/main.go index db2e91ab..d9ec58f0 100644 --- a/backend/go-app/main.go +++ b/backend/go-app/main.go @@ -1757,7 +1757,7 @@ func handleInfo(resp http.ResponseWriter, request *http.Request) { "tutorials": [], "id": "%s", "orgs": [%s], - "selected_org": %s, + "active_org": %s, "cookies": [{"key": "session_token", "value": "%s", "expiration": %d}] }`, parsedAdmin, userInfo.Id, currentOrg, currentOrg, userInfo.Session, expiration.Unix()) @@ -6680,6 +6680,7 @@ func runInit(ctx context.Context) { log.Printf("Finished INIT") } +// INFO: https://docs.google.com/drawings/d/1JJebpPeEVEbmH_qsAC6zf9Noygp7PytvesrkhE19QrY/edit func handleCloudSetup(resp http.ResponseWriter, request *http.Request) { cors := handleCors(resp, request) if cors { @@ -6722,25 +6723,87 @@ func handleCloudSetup(resp http.ResponseWriter, request *http.Request) { return } + ctx := context.Background() + org, err := getOrg(ctx, tmpData.Organization.Id) + if err != nil { + log.Printf("Organization doesn't exist: %s", err) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + + // FIXME: Check if user is admin of this org + userFound := false + admin := false + for _, inneruser := range org.Users { + if inneruser.Id == user.Id { + userFound = true + log.Printf("Role: %s", inneruser.Role) + if inneruser.Role == "admin" { + admin = true + } + + break + } + } + + if !userFound { + log.Printf("User %s doesn't exist in organization %s", user.Id, org.Id) + resp.WriteHeader(401) + resp.Write([]byte(`{"success": false}`)) + return + } + + // FIXME: Enable admin check in org for sync setup and conf. + _ = admin + //if !admin { + // log.Printf("User %s isn't admin hence can't set up sync for org %s", user.Id, org.Id) + // resp.WriteHeader(401) + // resp.Write([]byte(`{"success": false}`)) + // return + //} + log.Printf("Apidata: %s", tmpData.Apikey) - // FIXME: https://docs.google.com/drawings/d/1JJebpPeEVEbmH_qsAC6zf9Noygp7PytvesrkhE19QrY/edit client := &http.Client{} syncPath := "http://192.168.3.6:5002/api/v1/cloud/sync" + + type requestStruct struct { + ApiKey string `json:"api_key"` + } + + requestData := requestStruct{ + ApiKey: tmpData.Apikey, + } + + b, err := json.Marshal(requestData) + if err != nil { + log.Printf("Failed marshaling api key data: %s", err) + resp.WriteHeader(401) + resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed cloud sync."`, err))) + return + } + req, err := http.NewRequest( "POST", syncPath, - nil, + bytes.NewBuffer(b), ) newresp, err := client.Do(req) if err != nil { - resp.WriteHeader(401) + resp.WriteHeader(400) resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed cloud sync: %s"`, err))) //setBadMemcache(ctx, docPath) return } + if newresp.StatusCode != 200 { + resp.WriteHeader(400) + resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Response code %d during sync. Expecting 200."`, newresp.StatusCode))) + return + } + respBody, err := ioutil.ReadAll(newresp.Body) if err != nil { resp.WriteHeader(500) diff --git a/backend/go-app/walkoff.go b/backend/go-app/walkoff.go index e1de6414..3157111a 100644 --- a/backend/go-app/walkoff.go +++ b/backend/go-app/walkoff.go @@ -356,6 +356,14 @@ type ExecutionRequestWrapper struct { Data []ExecutionRequest `json:"data"` } +type AppExecutionExample struct { + AppName string `json:"app_name"` + AppVersion string `json:"app_version"` + AppAction string `json:"app_action"` + AppId string `json:"app_id"` + Examples []string `json:"examples"` +} + // This might be... a bit off, but that's fine :) // This might also be stupid, as we want timelines and such // Anyway, these are super basic stupid stats. @@ -1081,6 +1089,8 @@ func handleWorkflowQueue(resp http.ResponseWriter, request *http.Request) { if err != nil { log.Printf("Failed to increase success execution stats: %s", err) } + + // type AppExecutionExample struct { } } diff --git a/frontend/src/views/Admin.jsx b/frontend/src/views/Admin.jsx index 51a10b60..77f2f2c2 100644 --- a/frontend/src/views/Admin.jsx +++ b/frontend/src/views/Admin.jsx @@ -1347,7 +1347,6 @@ const Admin = (props) => { : null - console.log("Userdata: ", props.userdata) const organizationsTab = curTab === 5 ?
@@ -1389,49 +1388,10 @@ const Admin = (props) => { style={{minWidth: 150, maxWidth: 150}} /> - {props.userdata !== undefined && props.userdata.orgs !== null && props.userdata.orgs !== undefined && props.userdata.orgs.length > 0 ? - - {props.userdata.orgs.map((data, index) => { - const isSelected = props.userdata.selected_org === undefined ? "False" : props.userdata.selected_org.id === data.id ? "True" : "False" - - return ( - - - - - - { - setCloudSyncModalOpen(true) - setSelectedOrganization(data) - console.log("INVERT CLOUD SYNC") - }} /> - style={{minWidth: 150, maxWidth: 150}} - /> - - ) - - })} - - : - null - } {organizations !== undefined && organizations !== null && organizations.length > 0 ? {organizations.map((data, index) => { - const isSelected = props.userdata.selected_org === undefined ? "False" : props.userdata.selected_org.id === data.id ? "True" : "False" + const isSelected = props.userdata.active_org.id === undefined ? "False" : props.userdata.active_org.id === data.id ? "True" : "False" return (