From a89dc40ff2e769359e05206a99ba25a359141be2 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 29 Oct 2020 16:03:00 +0000 Subject: [PATCH] delete ombi user when deleting jf user also fix ombi defaults menu in ui. responds to #11. --- api.go | 39 ++++++++++++++++++++++++++++++++++++++- data/templates/admin.html | 2 +- ombi.go | 18 +++++++++++++++--- ts/ombi.ts | 2 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/api.go b/api.go index 0c40db8..ef6dd8b 100644 --- a/api.go +++ b/api.go @@ -385,10 +385,47 @@ func (app *appContext) DeleteUser(gc *gin.Context) { var req deleteUserDTO gc.BindJSON(&req) errors := map[string]string{} + ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false) + ombiUsers := []map[string]interface{}{} + var code int + var err error + if ombiEnabled { + ombiUsers, code, err = app.ombi.getUsers() + if code != 200 || err != nil { + respond(500, fmt.Sprintf("Couldn't get users: %s (%s)", code, err), gc) + return + } + } for _, userID := range req.Users { + if ombiEnabled { + ombiID := "" + user, status, err := app.jf.userById(userID, false) + if err == nil && status == 200 { + username := user["Name"].(string) + email := app.storage.emails[userID].(string) + for _, ombiUser := range ombiUsers { + if ombiUser["userName"].(string) == username || (ombiUser["emailAddress"].(string) == email && email != "") { + ombiID = ombiUser["id"].(string) + break + } + } + if ombiID != "" { + status, err := app.ombi.deleteUser(ombiID) + if err != nil || status != 200 { + app.err.Printf("Failed to delete ombi user: %d %s", status, err) + errors[userID] = fmt.Sprintf("Ombi: %d %s, ", status, err) + } + } + } + } status, err := app.jf.deleteUser(userID) if !(status == 200 || status == 204) || err != nil { - errors[userID] = fmt.Sprintf("%d: %s", status, err) + msg := fmt.Sprintf("%d: %s", status, err) + if _, ok := errors[userID]; !ok { + errors[userID] = msg + } else { + errors[userID] += msg + } } if req.Notify { addr, ok := app.storage.emails[userID] diff --git a/data/templates/admin.html b/data/templates/admin.html index e1a9b8e..b8b42ef 100644 --- a/data/templates/admin.html +++ b/data/templates/admin.html @@ -438,7 +438,7 @@ User Profiles {{ if .ombiEnabled }} - {{ end }} diff --git a/ombi.go b/ombi.go index deeacbb..2bd9969 100644 --- a/ombi.go +++ b/ombi.go @@ -33,7 +33,7 @@ func newOmbi(server, key string, noFail bool) *Ombi { } // does a GET and returns the response as an io.reader. -func (ombi *Ombi) _getReader(url string, params map[string]string) (string, int, error) { +func (ombi *Ombi) _getJSON(url string, params map[string]string) (string, int, error) { if ombi.key == "" { return "", 401, fmt.Errorf("No API key provided") } @@ -107,16 +107,28 @@ func (ombi *Ombi) _post(url string, data map[string]interface{}, response bool) return responseText, resp.StatusCode, nil } +func (ombi *Ombi) deleteUser(id string) (code int, err error) { + url := fmt.Sprintf("%s/api/v1/Identity/%s", ombi.server, id) + req, _ := http.NewRequest("DELETE", url, nil) + req.Header.Add("Content-Type", "application/json") + for name, value := range ombi.header { + req.Header.Add(name, value) + } + resp, err := ombi.httpClient.Do(req) + defer timeoutHandler("Ombi", ombi.server, ombi.noFail) + return resp.StatusCode, err +} + // gets an ombi user by their ID. func (ombi *Ombi) userByID(id string) (result map[string]interface{}, code int, err error) { - resp, code, err := ombi._getReader(fmt.Sprintf("%s/api/v1/Identity/User/%s", ombi.server, id), nil) + resp, code, err := ombi._getJSON(fmt.Sprintf("%s/api/v1/Identity/User/%s", ombi.server, id), nil) json.Unmarshal([]byte(resp), &result) return } // gets a list of all users. func (ombi *Ombi) getUsers() (result []map[string]interface{}, code int, err error) { - resp, code, err := ombi._getReader(fmt.Sprintf("%s/api/v1/Identity/Users", ombi.server), nil) + resp, code, err := ombi._getJSON(fmt.Sprintf("%s/api/v1/Identity/Users", ombi.server), nil) json.Unmarshal([]byte(resp), &result) return } diff --git a/ts/ombi.ts b/ts/ombi.ts index febf691..b3ac076 100644 --- a/ts/ombi.ts +++ b/ts/ombi.ts @@ -1,4 +1,4 @@ -import { _get, _post, _delete, rmAttr, addAttr } from "modules/common.js"; +import { _get, _post, _delete, rmAttr, addAttr } from "./modules/common.js"; const ombiDefaultsModal = window.BS.newModal('ombiDefaults');