Compare commits

...

5 Commits

Author SHA1 Message Date
Harvey Tindall fafb524a47
accounts: link ombi/discord when adding 2022-01-25 18:05:17 +00:00
Harvey Tindall da1b9ccac7
ombi: add migration to link telegram/discord 2022-01-25 18:02:04 +00:00
Harvey Tindall 7b97e1ca26
fix discord/telegram linking with ombi
original issue creator replied with the fix.
2022-01-25 17:37:14 +00:00
Harvey Tindall 7f11549337
ombi: broken discord/telegram linking
doesn't work due to https://github.com/ombi-app/ombi/issues/3484
committing to save progress.
2022-01-25 17:01:18 +00:00
Harvey Tindall 987e0ddd4e
accounts: show "Set expiry" button for non-disabled users
previously hidden in a dropdown on the "re-enable" button for disabled
users only, now can be used on active ones.
2022-01-25 15:27:16 +00:00
5 changed files with 129 additions and 18 deletions

53
api.go
View File

@ -513,9 +513,11 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc
}
}
id := user.ID
var profile Profile
if invite.Profile != "" {
app.debug.Printf("Applying settings from profile \"%s\"", invite.Profile)
profile, ok := app.storage.profiles[invite.Profile]
var ok bool
profile, ok = app.storage.profiles[invite.Profile]
if !ok {
profile = app.storage.profiles["Default"]
}
@ -536,19 +538,6 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc
app.err.Printf("%s: Failed to set configuration template (%d): %v", req.Code, status, err)
}
}
if app.config.Section("ombi").Key("enabled").MustBool(false) {
if profile.Ombi != nil && len(profile.Ombi) != 0 {
errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, profile.Ombi)
if err != nil || code != 200 {
app.info.Printf("Failed to create Ombi user (%d): %s", code, err)
app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", "))
} else {
app.info.Println("Created Ombi user")
}
} else {
app.debug.Printf("Skipping Ombi: Profile \"%s\" was empty", invite.Profile)
}
}
}
// if app.config.Section("password_resets").Key("enabled").MustBool(false) {
if req.Email != "" {
@ -598,6 +587,40 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc
app.telegram.verifiedTokens = app.telegram.verifiedTokens[:len(app.telegram.verifiedTokens)-1]
}
}
if invite.Profile != "" && app.config.Section("ombi").Key("enabled").MustBool(false) {
if profile.Ombi != nil && len(profile.Ombi) != 0 {
template := profile.Ombi
errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, template)
if err != nil || code != 200 {
app.info.Printf("Failed to create Ombi user (%d): %s", code, err)
app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", "))
} else {
app.info.Println("Created Ombi user")
if (discordEnabled && discordVerified) || (telegramEnabled && telegramTokenIndex != -1) {
ombiUser, status, err := app.getOmbiUser(id)
if status != 200 || err != nil {
app.err.Printf("Failed to get Ombi user (%d): %v", status, err)
} else {
dID := ""
tUser := ""
if discordEnabled && discordVerified {
dID = discordUser.ID
}
if telegramEnabled && telegramTokenIndex != -1 {
tUser = app.storage.telegram[user.ID].Username
}
resp, status, err := app.ombi.SetNotificationPrefs(ombiUser, dID, tUser)
if !(status == 200 || status == 204) || err != nil {
app.err.Printf("Failed to link Telegram/Discord to Ombi (%d): %v", status, err)
app.debug.Printf("Response: %v", resp)
}
}
}
}
} else {
app.debug.Printf("Skipping Ombi: Profile \"%s\" was empty", invite.Profile)
}
}
if matrixVerified {
matrixUser.Contact = req.MatrixContact
delete(app.matrix.tokens, req.MatrixPIN)
@ -2563,6 +2586,7 @@ func (app *appContext) TelegramAddUser(gc *gin.Context) {
app.telegram.verifiedTokens[len(app.telegram.verifiedTokens)-1], app.telegram.verifiedTokens[tokenIndex] = app.telegram.verifiedTokens[tokenIndex], app.telegram.verifiedTokens[len(app.telegram.verifiedTokens)-1]
app.telegram.verifiedTokens = app.telegram.verifiedTokens[:len(app.telegram.verifiedTokens)-1]
}
linkExistingOmbiDiscordTelegram(app)
respondBool(200, true, gc)
}
@ -2941,6 +2965,7 @@ func (app *appContext) DiscordConnect(gc *gin.Context) {
respondBool(500, false, gc)
return
}
linkExistingOmbiDiscordTelegram(app)
respondBool(200, true, gc)
}

View File

@ -205,6 +205,10 @@
"singular": "Extend expiry for {n} user",
"plural": "Extend expiry for {n} users"
},
"setExpiry": {
"singular": "Set expiry for {n} user",
"plural": "Set expiry for {n} users"
},
"extendedExpiry": {
"singular": "Extended expiry for {n} user.",
"plural": "Extended expiry for {n} users."

View File

@ -14,6 +14,7 @@ func runMigrations(app *appContext) {
migrateBootstrap(app)
migrateEmailStorage(app)
migrateNotificationMethods(app)
linkExistingOmbiDiscordTelegram(app)
// migrateHyphens(app)
}
@ -158,6 +159,41 @@ func migrateNotificationMethods(app *appContext) error {
return nil
}
// Pre-0.3.10, Ombi users were created without linking their Discord & Telegram accounts. This will add them.
func linkExistingOmbiDiscordTelegram(app *appContext) error {
if !discordEnabled && !telegramEnabled {
return nil
}
if !app.config.Section("ombi").Key("enabled").MustBool(false) {
return nil
}
idList := map[string][2]string{}
for jfID, user := range app.storage.discord {
idList[jfID] = [2]string{user.ID, ""}
}
for jfID, user := range app.storage.telegram {
vals, ok := idList[jfID]
if !ok {
vals = [2]string{"", ""}
}
vals[1] = user.Username
idList[jfID] = vals
}
for jfID, ids := range idList {
ombiUser, status, err := app.getOmbiUser(jfID)
if status != 200 || err != nil {
app.debug.Printf("Failed to get Ombi user with Discord/Telegram \"%s\"/\"%s\" (%d): %v", ids[0], ids[1], status, err)
continue
}
_, status, err = app.ombi.SetNotificationPrefs(ombiUser, ids[0], ids[1])
if status != 200 || err != nil {
app.debug.Printf("Failed to set prefs for Ombi user \"%s\" (%d): %v", ombiUser["userName"].(string), status, err)
continue
}
}
return nil
}
// Migrate between hyphenated & non-hyphenated user IDs. Doesn't seem to happen anymore, so disabled.
// func migrateHyphens(app *appContext) {
// checkVersion := func(version string) int {

View File

@ -13,6 +13,11 @@ import (
"github.com/hrfee/jfa-go/common"
)
const (
NotifAgentDiscord = 1
NotifAgentTelegram = 4
)
// Ombi represents a running Ombi instance.
type Ombi struct {
server, key string
@ -81,7 +86,7 @@ func (ombi *Ombi) getJSON(url string, params map[string]string) (string, int, er
}
// does a POST and optionally returns response as string. Returns a string instead of an io.reader bcs i couldn't get it working otherwise.
func (ombi *Ombi) send(mode string, url string, data map[string]interface{}, response bool) (string, int, error) {
func (ombi *Ombi) send(mode string, url string, data interface{}, response bool, headers map[string]string) (string, int, error) {
responseText := ""
params, _ := json.Marshal(data)
req, _ := http.NewRequest(mode, url, bytes.NewBuffer(params))
@ -89,6 +94,9 @@ func (ombi *Ombi) send(mode string, url string, data map[string]interface{}, res
for name, value := range ombi.header {
req.Header.Add(name, value)
}
for name, value := range headers {
req.Header.Add(name, value)
}
resp, err := ombi.httpClient.Do(req)
defer ombi.timeoutHandler()
if err != nil || !(resp.StatusCode == 200 || resp.StatusCode == 201) {
@ -117,11 +125,11 @@ func (ombi *Ombi) send(mode string, url string, data map[string]interface{}, res
}
func (ombi *Ombi) post(url string, data map[string]interface{}, response bool) (string, int, error) {
return ombi.send("POST", url, data, response)
return ombi.send("POST", url, data, response, nil)
}
func (ombi *Ombi) put(url string, data map[string]interface{}, response bool) (string, int, error) {
return ombi.send("PUT", url, data, response)
return ombi.send("PUT", url, data, response, nil)
}
// ModifyUser applies the given modified user object to the corresponding user.
@ -220,3 +228,24 @@ func (ombi *Ombi) NewUser(username, password, email string, template map[string]
ombi.cacheExpiry = time.Now()
return nil, code, err
}
type NotificationPref struct {
Agent int `json:"agent"`
UserID string `json:"userId"`
Value string `json:"value"`
Enabled bool `json:"enabled"`
}
func (ombi *Ombi) SetNotificationPrefs(user map[string]interface{}, discordID, telegramUser string) (result string, code int, err error) {
id := user["id"].(string)
url := fmt.Sprintf("%s/api/v1/Identity/NotificationPreferences", ombi.server)
var data []NotificationPref
if discordID != "" {
data = []NotificationPref{NotificationPref{NotifAgentDiscord, id, discordID, true}}
}
if telegramUser != "" {
data = append(data, NotificationPref{NotifAgentTelegram, id, telegramUser, true})
}
result, code, err = ombi.send("POST", url, data, true, map[string]string{"UserName": user["userName"].(string)})
return
}

View File

@ -699,6 +699,9 @@ export class accountsList {
private _addUserEmail = this._addUserForm.querySelector("input[type=email]") as HTMLInputElement;
private _addUserPassword = this._addUserForm.querySelector("input[type=password]") as HTMLInputElement;
// Whether the "Extend expiry" is extending or setting an expiry.
private _settingExpiry = false;
private _count = 30;
private _populateNumbers = () => {
const fieldIDs = ["months", "days", "hours", "minutes"];
@ -836,6 +839,7 @@ export class accountsList {
this._announceButton.classList.remove("unfocused");
}
let anyNonExpiries = list.length == 0 ? true : false;
let allNonExpiries = true;
let noContactCount = 0;
// Only show enable/disable button if all selected have the same state.
this._shouldEnable = this._users[list[0]].disabled
@ -845,6 +849,9 @@ export class accountsList {
anyNonExpiries = true;
this._extendExpiry.classList.add("unfocused");
}
if (this._users[id].expiry) {
allNonExpiries = false;
}
if (showDisableEnable && this._users[id].disabled != this._shouldEnable) {
showDisableEnable = false;
this._disableEnable.classList.add("unfocused");
@ -854,8 +861,15 @@ export class accountsList {
noContactCount++;
}
}
if (!anyNonExpiries) {
this._settingExpiry = false;
if (!anyNonExpiries && !allNonExpiries) {
this._extendExpiry.classList.remove("unfocused");
this._extendExpiry.textContent = window.lang.strings("extendExpiry");
}
if (allNonExpiries) {
this._extendExpiry.classList.remove("unfocused");
this._extendExpiry.textContent = window.lang.strings("setExpiry");
this._settingExpiry = true;
}
// Only show "Send PWR" if a maximum of 1 user selected doesn't have a contact method
if (noContactCount > 1) {
@ -1317,6 +1331,9 @@ export class accountsList {
this._enableExpiryNotify.parentElement.classList.remove("unfocused");
this._enableExpiryNotify.checked = false;
this._enableExpiryReason.value = "";
} else if (this._settingExpiry) {
header = window.lang.quantity("setExpiry", list.length);
this._enableExpiryNotify.parentElement.classList.add("unfocused");
} else {
header = window.lang.quantity("extendExpiry", applyList.length);
this._enableExpiryNotify.parentElement.classList.add("unfocused");