2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-01 19:17:05 +00:00
|
|
|
"fmt"
|
2020-09-05 16:32:49 +00:00
|
|
|
"strings"
|
2020-07-29 21:11:28 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-08-26 14:43:28 +00:00
|
|
|
"github.com/hrfee/jfa-go/common"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2021-03-29 20:49:46 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2021-04-06 20:25:44 +00:00
|
|
|
"github.com/itchyny/timefmt-go"
|
2023-10-19 17:56:35 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
2020-08-16 12:36:54 +00:00
|
|
|
"gopkg.in/ini.v1"
|
2020-07-29 21:11:28 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
func respond(code int, message string, gc *gin.Context) {
|
|
|
|
resp := stringResponse{}
|
|
|
|
if code == 200 || code == 204 {
|
|
|
|
resp.Response = message
|
|
|
|
} else {
|
|
|
|
resp.Error = message
|
|
|
|
}
|
|
|
|
gc.JSON(code, resp)
|
|
|
|
gc.Abort()
|
|
|
|
}
|
|
|
|
|
|
|
|
func respondBool(code int, val bool, gc *gin.Context) {
|
|
|
|
resp := boolResponse{}
|
|
|
|
if !val {
|
|
|
|
resp.Error = true
|
|
|
|
} else {
|
|
|
|
resp.Success = true
|
|
|
|
}
|
|
|
|
gc.JSON(code, resp)
|
|
|
|
gc.Abort()
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) loadStrftime() {
|
2021-05-07 20:53:29 +00:00
|
|
|
app.datePattern = app.config.Section("messages").Key("date_format").String()
|
2020-08-16 12:36:54 +00:00
|
|
|
app.timePattern = `%H:%M`
|
2021-05-07 20:53:29 +00:00
|
|
|
if val, _ := app.config.Section("messages").Key("use_24h").Bool(); !val {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.timePattern = `%I:%M %p`
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) prettyTime(dt time.Time) (date, time string) {
|
2021-04-06 20:25:44 +00:00
|
|
|
date = timefmt.Format(dt, app.datePattern)
|
|
|
|
time = timefmt.Format(dt, app.timePattern)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) formatDatetime(dt time.Time) string {
|
|
|
|
d, t := app.prettyTime(dt)
|
2020-07-31 11:48:37 +00:00
|
|
|
return d + " " + t
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/36530251/time-since-with-months-and-years/36531443#36531443 THANKS
|
|
|
|
func timeDiff(a, b time.Time) (year, month, day, hour, min, sec int) {
|
|
|
|
if a.Location() != b.Location() {
|
|
|
|
b = b.In(a.Location())
|
|
|
|
}
|
|
|
|
if a.After(b) {
|
|
|
|
a, b = b, a
|
|
|
|
}
|
|
|
|
y1, M1, d1 := a.Date()
|
|
|
|
y2, M2, d2 := b.Date()
|
|
|
|
|
|
|
|
h1, m1, s1 := a.Clock()
|
|
|
|
h2, m2, s2 := b.Clock()
|
|
|
|
|
|
|
|
year = int(y2 - y1)
|
|
|
|
month = int(M2 - M1)
|
|
|
|
day = int(d2 - d1)
|
|
|
|
hour = int(h2 - h1)
|
|
|
|
min = int(m2 - m1)
|
|
|
|
sec = int(s2 - s1)
|
|
|
|
|
|
|
|
// Normalize negative values
|
|
|
|
if sec < 0 {
|
|
|
|
sec += 60
|
|
|
|
min--
|
|
|
|
}
|
|
|
|
if min < 0 {
|
|
|
|
min += 60
|
|
|
|
hour--
|
|
|
|
}
|
|
|
|
if hour < 0 {
|
|
|
|
hour += 24
|
|
|
|
day--
|
|
|
|
}
|
|
|
|
if day < 0 {
|
|
|
|
// days in month:
|
|
|
|
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
|
|
|
|
day += 32 - t.Day()
|
|
|
|
month--
|
|
|
|
}
|
|
|
|
if month < 0 {
|
|
|
|
month += 12
|
|
|
|
year--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
// Routes from now on!
|
2020-07-31 12:59:25 +00:00
|
|
|
|
2021-06-07 12:46:46 +00:00
|
|
|
// @Summary Resets a user's password with a PIN, and optionally set a new password if given.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Success 400 {object} PasswordValidation
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Param ResetPasswordDTO body ResetPasswordDTO true "Pin and optional Password."
|
|
|
|
// @Router /reset [post]
|
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) ResetSetPassword(gc *gin.Context) {
|
|
|
|
var req ResetPasswordDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
validation := app.validator.validate(req.Password)
|
2023-12-23 19:56:21 +00:00
|
|
|
captcha := app.config.Section("captcha").Key("enabled").MustBool(false)
|
2021-06-07 12:46:46 +00:00
|
|
|
valid := true
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid || req.PIN == "" {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.FailedChangePassword, lm.Jellyfin, "?", lm.InvalidPassword)
|
2021-06-07 12:46:46 +00:00
|
|
|
gc.JSON(400, validation)
|
|
|
|
return
|
|
|
|
}
|
2021-10-13 14:04:22 +00:00
|
|
|
isInternal := false
|
2023-12-23 19:56:21 +00:00
|
|
|
|
|
|
|
if captcha && !app.verifyCaptcha(req.PIN, req.PIN, req.CaptchaText, true) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.FailedChangePassword, lm.Jellyfin, "?", lm.IncorrectCaptcha)
|
2023-12-23 19:56:21 +00:00
|
|
|
respond(400, "errorCaptcha", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-13 14:04:22 +00:00
|
|
|
var userID, username string
|
|
|
|
if reset, ok := app.internalPWRs[req.PIN]; ok {
|
|
|
|
isInternal = true
|
|
|
|
if time.Now().After(reset.Expiry) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.FailedChangePassword, lm.Jellyfin, "?", fmt.Sprintf(lm.ExpiredPIN, reset.PIN))
|
2021-10-13 14:04:22 +00:00
|
|
|
respondBool(401, false, gc)
|
|
|
|
delete(app.internalPWRs, req.PIN)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userID = reset.ID
|
|
|
|
username = reset.Username
|
2023-12-23 19:56:21 +00:00
|
|
|
|
2024-08-06 13:48:31 +00:00
|
|
|
err := app.jf.ResetPasswordAdmin(userID)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedChangePassword, lm.Jellyfin, userID, err)
|
2024-08-06 13:48:31 +00:00
|
|
|
respondBool(500, false, gc)
|
2021-10-13 14:04:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-22 16:35:34 +00:00
|
|
|
delete(app.internalPWRs, req.PIN)
|
2021-10-13 14:04:22 +00:00
|
|
|
} else {
|
2024-08-06 13:48:31 +00:00
|
|
|
resp, err := app.jf.ResetPassword(req.PIN)
|
|
|
|
if err != nil || !resp.Success {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedChangePassword, lm.Jellyfin, userID, err)
|
2024-08-06 13:48:31 +00:00
|
|
|
respondBool(500, false, gc)
|
2021-10-13 14:04:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.Password == "" || len(resp.UsersReset) == 0 {
|
|
|
|
respondBool(200, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username = resp.UsersReset[0]
|
2021-06-07 12:46:46 +00:00
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2021-10-13 14:04:22 +00:00
|
|
|
var user mediabrowser.User
|
|
|
|
var err error
|
|
|
|
if isInternal {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByID(userID, false)
|
2021-10-13 14:04:22 +00:00
|
|
|
} else {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByName(username, false)
|
2021-06-07 12:46:46 +00:00
|
|
|
}
|
2024-08-06 13:48:31 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, userID, lm.Jellyfin, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityResetPassword,
|
|
|
|
UserID: user.ID,
|
|
|
|
SourceType: ActivityUser,
|
|
|
|
Source: user.ID,
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, true)
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2021-10-13 14:04:22 +00:00
|
|
|
prevPassword := req.PIN
|
|
|
|
if isInternal {
|
|
|
|
prevPassword = ""
|
|
|
|
}
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.jf.SetPassword(user.ID, prevPassword, req.Password)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedChangePassword, lm.Jellyfin, user.ID, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
2024-08-01 19:17:05 +00:00
|
|
|
// This makes no sense so has been commented out.
|
|
|
|
// It probably did at some point in the past.
|
|
|
|
/* Silently fail for changing ombi passwords
|
2023-10-09 09:39:22 +00:00
|
|
|
if (status != 200 && status != 204) || err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
2024-08-01 19:17:05 +00:00
|
|
|
} */
|
2024-08-06 13:48:31 +00:00
|
|
|
ombiUser, err := app.getOmbiUser(user.ID)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Ombi, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ombiUser["password"] = req.Password
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.ombi.ModifyUser(ombiUser)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedChangePassword, lm.Ombi, user.ID, err)
|
2021-06-07 12:46:46 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2024-08-01 19:17:05 +00:00
|
|
|
app.debug.Printf(lm.ChangePassword, lm.Ombi, user.ID)
|
2021-06-07 12:46:46 +00:00
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get jfa-go configuration.
|
|
|
|
// @Produce json
|
2024-08-26 14:43:28 +00:00
|
|
|
// @Success 200 {object} common.Config "Uses the same format as config-base.json"
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Router /config [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Configuration
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) GetConfig(gc *gin.Context) {
|
2022-01-13 22:34:47 +00:00
|
|
|
if discordEnabled {
|
2024-08-26 14:43:28 +00:00
|
|
|
app.PatchConfigDiscordRoles()
|
2022-01-13 22:34:47 +00:00
|
|
|
}
|
2024-08-26 14:43:28 +00:00
|
|
|
gc.JSON(200, app.patchedConfig)
|
2020-07-31 15:09:30 +00:00
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Modify app config.
|
|
|
|
// @Produce json
|
2024-08-20 19:19:32 +00:00
|
|
|
// @Param appConfig body configDTO true "Config split into sections as in config.ini, all values as strings (lists split with | delimiter)."
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Success 200 {object} boolResponse
|
2022-02-03 19:19:21 +00:00
|
|
|
// @Failure 500 {object} stringResponse
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Router /config [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Configuration
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) ModifyConfig(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req configDTO
|
2020-07-31 21:07:09 +00:00
|
|
|
gc.BindJSON(&req)
|
2021-04-05 15:34:47 +00:00
|
|
|
// Load a new config, as we set various default values in app.config that shouldn't be stored.
|
2024-08-20 19:19:32 +00:00
|
|
|
tempConfig, _ := ini.ShadowLoad(app.configPath)
|
2024-08-26 14:43:28 +00:00
|
|
|
for _, section := range app.configBase.Sections {
|
|
|
|
ns, ok := req[section.Section]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newSection := ns.(map[string]any)
|
|
|
|
iniSection, err := tempConfig.GetSection(section.Section)
|
|
|
|
if err != nil {
|
2024-08-29 12:30:03 +00:00
|
|
|
iniSection, err = tempConfig.NewSection(section.Section)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf(lm.FailedModifyConfig, app.configPath, err)
|
|
|
|
respond(500, err.Error(), gc)
|
|
|
|
return
|
|
|
|
}
|
2024-08-26 14:43:28 +00:00
|
|
|
}
|
|
|
|
for _, setting := range section.Settings {
|
|
|
|
newValue, ok := newSection[setting.Setting]
|
|
|
|
if !ok {
|
|
|
|
continue
|
2020-09-23 16:20:48 +00:00
|
|
|
}
|
2024-08-26 14:43:28 +00:00
|
|
|
// Patch disabled to actually be an empty string
|
|
|
|
if section.Section == "email" && setting.Setting == "method" && newValue == "disabled" {
|
|
|
|
newValue = ""
|
|
|
|
}
|
|
|
|
// Copy language preference for chatbots to root one in "telegram"
|
|
|
|
if (section.Section == "discord" || section.Section == "matrix") && setting.Setting == "language" {
|
|
|
|
iniSection.Key("language").SetValue(newValue.(string))
|
|
|
|
} else if setting.Type == common.ListType {
|
|
|
|
splitValues := strings.Split(newValue.(string), "|")
|
|
|
|
// Delete the key first to get rid of any shadow values
|
|
|
|
iniSection.DeleteKey(setting.Setting)
|
|
|
|
for i, v := range splitValues {
|
|
|
|
if i == 0 {
|
|
|
|
iniSection.Key(setting.Setting).SetValue(v)
|
|
|
|
} else {
|
|
|
|
iniSection.Key(setting.Setting).AddShadow(v)
|
2024-08-20 19:19:32 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-26 14:43:28 +00:00
|
|
|
|
|
|
|
} else if newValue.(string) != iniSection.Key(setting.Setting).MustString("") {
|
|
|
|
iniSection.Key(setting.Setting).SetValue(newValue.(string))
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2024-08-26 14:43:28 +00:00
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
tempConfig.Section("").Key("first_run").SetValue("false")
|
|
|
|
if err := tempConfig.SaveTo(app.configPath); err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedWriting, app.configPath, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, err.Error(), gc)
|
2021-07-24 18:49:08 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.ModifyConfig, app.configPath)
|
2022-03-22 14:58:39 +00:00
|
|
|
gc.JSON(200, map[string]bool{"success": true})
|
|
|
|
if req["restart-program"] != nil && req["restart-program"].(bool) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.Restart()
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
app.loadConfig()
|
2024-08-26 14:43:28 +00:00
|
|
|
// Patch new settings for next GetConfig
|
|
|
|
app.PatchConfigBase()
|
2022-03-22 14:58:39 +00:00
|
|
|
// Reinitialize password validator on config change, as opposed to every applicable request like in python.
|
|
|
|
if _, ok := req["password_validation"]; ok {
|
|
|
|
validatorConf := ValidatorConf{
|
|
|
|
"length": app.config.Section("password_validation").Key("min_length").MustInt(0),
|
|
|
|
"uppercase": app.config.Section("password_validation").Key("upper").MustInt(0),
|
|
|
|
"lowercase": app.config.Section("password_validation").Key("lower").MustInt(0),
|
|
|
|
"number": app.config.Section("password_validation").Key("number").MustInt(0),
|
|
|
|
"special": app.config.Section("password_validation").Key("special").MustInt(0),
|
|
|
|
}
|
|
|
|
if !app.config.Section("password_validation").Key("enabled").MustBool(false) {
|
|
|
|
for key := range validatorConf {
|
|
|
|
validatorConf[key] = 0
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
app.validator.init(validatorConf)
|
2021-02-22 00:43:36 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 20:28:36 +00:00
|
|
|
// @Summary Returns whether there's a new update, and extra info if there is.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} checkUpdateDTO
|
|
|
|
// @Router /config/update [get]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-04-12 20:28:36 +00:00
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) CheckUpdate(gc *gin.Context) {
|
|
|
|
if !app.newUpdate {
|
|
|
|
app.update = Update{}
|
|
|
|
}
|
|
|
|
gc.JSON(200, checkUpdateDTO{New: app.newUpdate, Update: app.update})
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Apply an update.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Success 400 {object} stringResponse
|
|
|
|
// @Success 500 {object} boolResponse
|
|
|
|
// @Router /config/update [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-04-12 20:28:36 +00:00
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) ApplyUpdate(gc *gin.Context) {
|
|
|
|
if !app.update.CanUpdate {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.FailedApplyUpdate, lm.UpdateManual)
|
|
|
|
respond(400, lm.UpdateManual, gc)
|
2021-04-12 20:28:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err := app.update.update()
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedApplyUpdate, err)
|
2021-04-12 20:28:36 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-05-03 19:08:23 +00:00
|
|
|
if PLATFORM == "windows" {
|
|
|
|
respondBool(500, true, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-04-12 20:28:36 +00:00
|
|
|
respondBool(200, true, gc)
|
2021-05-03 19:08:23 +00:00
|
|
|
app.HardRestart()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Logout by deleting refresh token from cookies.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /logout [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Other
|
2020-08-19 21:30:54 +00:00
|
|
|
func (app *appContext) Logout(gc *gin.Context) {
|
2020-08-20 19:20:31 +00:00
|
|
|
cookie, err := gc.Cookie("refresh")
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
msg := fmt.Sprintf(lm.FailedGetCookies, "refresh", err)
|
|
|
|
app.debug.Println(msg)
|
|
|
|
respond(500, msg, gc)
|
2020-08-20 19:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
app.invalidTokens = append(app.invalidTokens, cookie)
|
|
|
|
gc.SetCookie("refresh", "invalid", -1, "/", gc.Request.URL.Hostname(), true, true)
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-08-19 21:30:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 19:17:43 +00:00
|
|
|
// @Summary Returns a map of available language codes to their full names, usable in the lang query parameter.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} langDTO
|
|
|
|
// @Failure 500 {object} stringResponse
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Param page path string true "admin/form/setup/email/pwr"
|
2021-02-19 21:38:20 +00:00
|
|
|
// @Router /lang/{page} [get]
|
2021-01-11 19:17:43 +00:00
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) GetLanguages(gc *gin.Context) {
|
2021-01-12 23:43:44 +00:00
|
|
|
page := gc.Param("page")
|
2021-01-11 19:17:43 +00:00
|
|
|
resp := langDTO{}
|
2021-05-02 13:15:03 +00:00
|
|
|
switch page {
|
2023-06-16 13:43:37 +00:00
|
|
|
case "form", "user":
|
|
|
|
for key, lang := range app.storage.lang.User {
|
2021-01-19 00:29:29 +00:00
|
|
|
resp[key] = lang.Meta.Name
|
2021-01-12 23:43:44 +00:00
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "admin":
|
2021-01-12 23:43:44 +00:00
|
|
|
for key, lang := range app.storage.lang.Admin {
|
2021-01-19 00:29:29 +00:00
|
|
|
resp[key] = lang.Meta.Name
|
2021-01-12 23:43:44 +00:00
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "setup":
|
2021-01-26 22:57:29 +00:00
|
|
|
for key, lang := range app.storage.lang.Setup {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "email":
|
2021-01-26 22:57:29 +00:00
|
|
|
for key, lang := range app.storage.lang.Email {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-05-02 13:15:03 +00:00
|
|
|
case "pwr":
|
|
|
|
for key, lang := range app.storage.lang.PasswordReset {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
2021-01-11 19:17:43 +00:00
|
|
|
}
|
|
|
|
if len(resp) == 0 {
|
|
|
|
respond(500, "Couldn't get languages", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Summary Serves a translations for pages "admin" or "form".
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} adminLang
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Param page path string true "admin or form."
|
|
|
|
// @Param language path string true "language code, e.g en-us."
|
|
|
|
// @Router /lang/{page}/{language} [get]
|
2021-02-02 18:09:02 +00:00
|
|
|
// @tags Other
|
2021-01-19 00:29:29 +00:00
|
|
|
func (app *appContext) ServeLang(gc *gin.Context) {
|
|
|
|
page := gc.Param("page")
|
|
|
|
lang := strings.Replace(gc.Param("file"), ".json", "", 1)
|
|
|
|
if page == "admin" {
|
|
|
|
gc.JSON(200, app.storage.lang.Admin[lang])
|
|
|
|
return
|
2023-06-16 13:43:37 +00:00
|
|
|
} else if page == "form" || page == "user" {
|
|
|
|
gc.JSON(200, app.storage.lang.User[lang])
|
2021-01-19 00:29:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:41:08 +00:00
|
|
|
// @Summary Restarts the program. No response means success.
|
|
|
|
// @Router /restart [post]
|
2021-05-07 17:20:35 +00:00
|
|
|
// @Security Bearer
|
2021-05-02 19:41:08 +00:00
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) restart(gc *gin.Context) {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.Restart()
|
2021-05-02 19:41:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 17:54:27 +00:00
|
|
|
// @Summary Returns the last 100 lines of the log.
|
|
|
|
// @Router /log [get]
|
|
|
|
// @Success 200 {object} LogDTO
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Other
|
|
|
|
func (app *appContext) GetLog(gc *gin.Context) {
|
|
|
|
gc.JSON(200, LogDTO{lineCache.String()})
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:08:50 +00:00
|
|
|
// no need to syscall.exec anymore!
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) Restart() error {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Println(lm.Restarting)
|
2021-08-16 19:41:07 +00:00
|
|
|
if TRAY {
|
|
|
|
TRAYRESTART <- true
|
|
|
|
} else {
|
|
|
|
RESTART <- true
|
|
|
|
}
|
2023-06-11 18:48:03 +00:00
|
|
|
// Safety Sleep (Ensure shutdown tasks get done)
|
|
|
|
time.Sleep(time.Second)
|
2020-09-08 22:08:50 +00:00
|
|
|
return nil
|
2020-08-01 23:05:35 +00:00
|
|
|
}
|