Skip to content

Commit

Permalink
delete ombi user when deleting jf user
Browse files Browse the repository at this point in the history
also fix ombi defaults menu in ui. responds to #11.
  • Loading branch information
hrfee committed Oct 29, 2020
1 parent 8089187 commit a89dc40
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
39 changes: 38 additions & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion data/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ <h2><a id="settingsTabButton" class="nl nav-link">Settings</a></h2>
User Profiles <i class="fa fa-user settingIcon"></i>
</button>
{{ if .ombiEnabled }}
<button type="button" class="list-group-item list-group-item-action static" id="openOmbiDefaults">
<button type="button" class="list-group-item list-group-item-action static" id="openOmbiDefaults" onclick="window.openOmbiDefaults()">
Ombi User Defaults <i class="fa fa-chain-broken settingIcon"></i>
</button>
{{ end }}
Expand Down
18 changes: 15 additions & 3 deletions ombi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion ts/ombi.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down

0 comments on commit a89dc40

Please sign in to comment.