mirror of
https://github.com/hrfee/jfa-go.git
synced 2025-01-04 07:20:12 +00:00
Compare commits
5 Commits
8fd097836f
...
fafb524a47
Author | SHA1 | Date | |
---|---|---|---|
fafb524a47 | |||
da1b9ccac7 | |||
7b97e1ca26 | |||
7f11549337 | |||
987e0ddd4e |
53
api.go
53
api.go
@ -513,9 +513,11 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
id := user.ID
|
id := user.ID
|
||||||
|
var profile Profile
|
||||||
if invite.Profile != "" {
|
if invite.Profile != "" {
|
||||||
app.debug.Printf("Applying settings from profile \"%s\"", 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 {
|
if !ok {
|
||||||
profile = app.storage.profiles["Default"]
|
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)
|
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 app.config.Section("password_resets").Key("enabled").MustBool(false) {
|
||||||
if req.Email != "" {
|
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]
|
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 {
|
if matrixVerified {
|
||||||
matrixUser.Contact = req.MatrixContact
|
matrixUser.Contact = req.MatrixContact
|
||||||
delete(app.matrix.tokens, req.MatrixPIN)
|
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[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]
|
app.telegram.verifiedTokens = app.telegram.verifiedTokens[:len(app.telegram.verifiedTokens)-1]
|
||||||
}
|
}
|
||||||
|
linkExistingOmbiDiscordTelegram(app)
|
||||||
respondBool(200, true, gc)
|
respondBool(200, true, gc)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2941,6 +2965,7 @@ func (app *appContext) DiscordConnect(gc *gin.Context) {
|
|||||||
respondBool(500, false, gc)
|
respondBool(500, false, gc)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
linkExistingOmbiDiscordTelegram(app)
|
||||||
respondBool(200, true, gc)
|
respondBool(200, true, gc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,6 +205,10 @@
|
|||||||
"singular": "Extend expiry for {n} user",
|
"singular": "Extend expiry for {n} user",
|
||||||
"plural": "Extend expiry for {n} users"
|
"plural": "Extend expiry for {n} users"
|
||||||
},
|
},
|
||||||
|
"setExpiry": {
|
||||||
|
"singular": "Set expiry for {n} user",
|
||||||
|
"plural": "Set expiry for {n} users"
|
||||||
|
},
|
||||||
"extendedExpiry": {
|
"extendedExpiry": {
|
||||||
"singular": "Extended expiry for {n} user.",
|
"singular": "Extended expiry for {n} user.",
|
||||||
"plural": "Extended expiry for {n} users."
|
"plural": "Extended expiry for {n} users."
|
||||||
|
@ -14,6 +14,7 @@ func runMigrations(app *appContext) {
|
|||||||
migrateBootstrap(app)
|
migrateBootstrap(app)
|
||||||
migrateEmailStorage(app)
|
migrateEmailStorage(app)
|
||||||
migrateNotificationMethods(app)
|
migrateNotificationMethods(app)
|
||||||
|
linkExistingOmbiDiscordTelegram(app)
|
||||||
// migrateHyphens(app)
|
// migrateHyphens(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +159,41 @@ func migrateNotificationMethods(app *appContext) error {
|
|||||||
return nil
|
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.
|
// Migrate between hyphenated & non-hyphenated user IDs. Doesn't seem to happen anymore, so disabled.
|
||||||
// func migrateHyphens(app *appContext) {
|
// func migrateHyphens(app *appContext) {
|
||||||
// checkVersion := func(version string) int {
|
// checkVersion := func(version string) int {
|
||||||
|
35
ombi/ombi.go
35
ombi/ombi.go
@ -13,6 +13,11 @@ import (
|
|||||||
"github.com/hrfee/jfa-go/common"
|
"github.com/hrfee/jfa-go/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
NotifAgentDiscord = 1
|
||||||
|
NotifAgentTelegram = 4
|
||||||
|
)
|
||||||
|
|
||||||
// Ombi represents a running Ombi instance.
|
// Ombi represents a running Ombi instance.
|
||||||
type Ombi struct {
|
type Ombi struct {
|
||||||
server, key string
|
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.
|
// 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 := ""
|
responseText := ""
|
||||||
params, _ := json.Marshal(data)
|
params, _ := json.Marshal(data)
|
||||||
req, _ := http.NewRequest(mode, url, bytes.NewBuffer(params))
|
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 {
|
for name, value := range ombi.header {
|
||||||
req.Header.Add(name, value)
|
req.Header.Add(name, value)
|
||||||
}
|
}
|
||||||
|
for name, value := range headers {
|
||||||
|
req.Header.Add(name, value)
|
||||||
|
}
|
||||||
resp, err := ombi.httpClient.Do(req)
|
resp, err := ombi.httpClient.Do(req)
|
||||||
defer ombi.timeoutHandler()
|
defer ombi.timeoutHandler()
|
||||||
if err != nil || !(resp.StatusCode == 200 || resp.StatusCode == 201) {
|
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) {
|
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) {
|
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.
|
// 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()
|
ombi.cacheExpiry = time.Now()
|
||||||
return nil, code, err
|
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
|
||||||
|
}
|
||||||
|
@ -699,6 +699,9 @@ export class accountsList {
|
|||||||
private _addUserEmail = this._addUserForm.querySelector("input[type=email]") as HTMLInputElement;
|
private _addUserEmail = this._addUserForm.querySelector("input[type=email]") as HTMLInputElement;
|
||||||
private _addUserPassword = this._addUserForm.querySelector("input[type=password]") 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 _count = 30;
|
||||||
private _populateNumbers = () => {
|
private _populateNumbers = () => {
|
||||||
const fieldIDs = ["months", "days", "hours", "minutes"];
|
const fieldIDs = ["months", "days", "hours", "minutes"];
|
||||||
@ -836,6 +839,7 @@ export class accountsList {
|
|||||||
this._announceButton.classList.remove("unfocused");
|
this._announceButton.classList.remove("unfocused");
|
||||||
}
|
}
|
||||||
let anyNonExpiries = list.length == 0 ? true : false;
|
let anyNonExpiries = list.length == 0 ? true : false;
|
||||||
|
let allNonExpiries = true;
|
||||||
let noContactCount = 0;
|
let noContactCount = 0;
|
||||||
// Only show enable/disable button if all selected have the same state.
|
// Only show enable/disable button if all selected have the same state.
|
||||||
this._shouldEnable = this._users[list[0]].disabled
|
this._shouldEnable = this._users[list[0]].disabled
|
||||||
@ -845,6 +849,9 @@ export class accountsList {
|
|||||||
anyNonExpiries = true;
|
anyNonExpiries = true;
|
||||||
this._extendExpiry.classList.add("unfocused");
|
this._extendExpiry.classList.add("unfocused");
|
||||||
}
|
}
|
||||||
|
if (this._users[id].expiry) {
|
||||||
|
allNonExpiries = false;
|
||||||
|
}
|
||||||
if (showDisableEnable && this._users[id].disabled != this._shouldEnable) {
|
if (showDisableEnable && this._users[id].disabled != this._shouldEnable) {
|
||||||
showDisableEnable = false;
|
showDisableEnable = false;
|
||||||
this._disableEnable.classList.add("unfocused");
|
this._disableEnable.classList.add("unfocused");
|
||||||
@ -854,8 +861,15 @@ export class accountsList {
|
|||||||
noContactCount++;
|
noContactCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!anyNonExpiries) {
|
this._settingExpiry = false;
|
||||||
|
if (!anyNonExpiries && !allNonExpiries) {
|
||||||
this._extendExpiry.classList.remove("unfocused");
|
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
|
// Only show "Send PWR" if a maximum of 1 user selected doesn't have a contact method
|
||||||
if (noContactCount > 1) {
|
if (noContactCount > 1) {
|
||||||
@ -1317,6 +1331,9 @@ export class accountsList {
|
|||||||
this._enableExpiryNotify.parentElement.classList.remove("unfocused");
|
this._enableExpiryNotify.parentElement.classList.remove("unfocused");
|
||||||
this._enableExpiryNotify.checked = false;
|
this._enableExpiryNotify.checked = false;
|
||||||
this._enableExpiryReason.value = "";
|
this._enableExpiryReason.value = "";
|
||||||
|
} else if (this._settingExpiry) {
|
||||||
|
header = window.lang.quantity("setExpiry", list.length);
|
||||||
|
this._enableExpiryNotify.parentElement.classList.add("unfocused");
|
||||||
} else {
|
} else {
|
||||||
header = window.lang.quantity("extendExpiry", applyList.length);
|
header = window.lang.quantity("extendExpiry", applyList.length);
|
||||||
this._enableExpiryNotify.parentElement.classList.add("unfocused");
|
this._enableExpiryNotify.parentElement.classList.add("unfocused");
|
||||||
|
Loading…
Reference in New Issue
Block a user