2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-30 19:19:12 +00:00
|
|
|
"os"
|
2020-09-21 23:34:11 +00:00
|
|
|
"strconv"
|
2020-09-05 16:32:49 +00:00
|
|
|
"strings"
|
2021-01-30 19:19:12 +00:00
|
|
|
"sync"
|
2020-07-29 21:11:28 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
2021-01-30 19:19:12 +00:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2020-08-16 12:36:54 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
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"
|
2020-08-16 12:36:54 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
|
|
|
"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() {
|
|
|
|
app.datePattern = app.config.Section("email").Key("date_format").String()
|
|
|
|
app.timePattern = `%H:%M`
|
|
|
|
if val, _ := app.config.Section("email").Key("use_24h").Bool(); !val {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) checkInvites() {
|
2020-11-22 16:36:43 +00:00
|
|
|
currentTime := time.Now()
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.loadInvites()
|
2020-07-29 21:11:28 +00:00
|
|
|
changed := false
|
2020-08-16 12:36:54 +00:00
|
|
|
for code, data := range app.storage.invites {
|
2020-07-29 21:11:28 +00:00
|
|
|
expiry := data.ValidTill
|
2020-11-22 16:36:43 +00:00
|
|
|
if !currentTime.After(expiry) {
|
2020-11-02 00:53:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
app.debug.Printf("Housekeeping: Deleting old invite %s", code)
|
|
|
|
notify := data.Notify
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("notifications").Key("enabled").MustBool(false) && len(notify) != 0 {
|
2020-11-02 00:53:08 +00:00
|
|
|
app.debug.Printf("%s: Expiry notification", code)
|
2021-01-30 19:19:12 +00:00
|
|
|
var wait sync.WaitGroup
|
2020-11-02 00:53:08 +00:00
|
|
|
for address, settings := range notify {
|
|
|
|
if !settings["notify-expiry"] {
|
|
|
|
continue
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
wait.Add(1)
|
|
|
|
go func(addr string) {
|
|
|
|
defer wait.Done()
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructExpiry(code, data, app, false)
|
2020-11-02 00:53:08 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct expiry notification: %v", code, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, addr); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send expiry notification: %v", code, err)
|
2020-11-02 00:53:08 +00:00
|
|
|
} else {
|
2021-01-30 19:19:12 +00:00
|
|
|
app.info.Printf("Sent expiry notification to %s", addr)
|
2020-11-02 00:53:08 +00:00
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
}(address)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
wait.Wait()
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
changed = true
|
|
|
|
delete(app.storage.invites, code)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
if changed {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.storeInvites()
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) checkInvite(code string, used bool, username string) bool {
|
2020-11-22 16:36:43 +00:00
|
|
|
currentTime := time.Now()
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.loadInvites()
|
2020-08-01 14:22:30 +00:00
|
|
|
changed := false
|
2020-11-02 00:53:08 +00:00
|
|
|
inv, match := app.storage.invites[code]
|
|
|
|
if !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
expiry := inv.ValidTill
|
2020-11-22 16:36:43 +00:00
|
|
|
if currentTime.After(expiry) {
|
2020-11-02 00:53:08 +00:00
|
|
|
app.debug.Printf("Housekeeping: Deleting old invite %s", code)
|
|
|
|
notify := inv.Notify
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("notifications").Key("enabled").MustBool(false) && len(notify) != 0 {
|
2020-11-02 00:53:08 +00:00
|
|
|
app.debug.Printf("%s: Expiry notification", code)
|
|
|
|
for address, settings := range notify {
|
|
|
|
if settings["notify-expiry"] {
|
|
|
|
go func() {
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructExpiry(code, inv, app, false)
|
2020-11-02 00:53:08 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct expiry notification: %v", code, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, address); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send expiry notification: %v", code, err)
|
2020-11-02 00:53:08 +00:00
|
|
|
} else {
|
|
|
|
app.info.Printf("Sent expiry notification to %s", address)
|
|
|
|
}
|
|
|
|
}()
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
}
|
|
|
|
changed = true
|
|
|
|
match = false
|
|
|
|
delete(app.storage.invites, code)
|
|
|
|
} else if used {
|
|
|
|
changed = true
|
|
|
|
del := false
|
|
|
|
newInv := inv
|
|
|
|
if newInv.RemainingUses == 1 {
|
|
|
|
del = true
|
2020-08-16 12:36:54 +00:00
|
|
|
delete(app.storage.invites, code)
|
2020-11-02 00:53:08 +00:00
|
|
|
} else if newInv.RemainingUses != 0 {
|
|
|
|
// 0 means infinite i guess?
|
2021-01-30 19:19:12 +00:00
|
|
|
newInv.RemainingUses--
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
2021-04-06 20:25:44 +00:00
|
|
|
newInv.UsedBy = append(newInv.UsedBy, []string{username, strconv.FormatInt(currentTime.Unix(), 10)})
|
2020-11-02 00:53:08 +00:00
|
|
|
if !del {
|
|
|
|
app.storage.invites[code] = newInv
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
if changed {
|
|
|
|
app.storage.storeInvites()
|
|
|
|
}
|
|
|
|
return match
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:13:13 +00:00
|
|
|
func (app *appContext) getOmbiUser(jfID string) (map[string]interface{}, int, error) {
|
2020-11-02 00:53:08 +00:00
|
|
|
ombiUsers, code, err := app.ombi.GetUsers()
|
2020-10-30 21:13:13 +00:00
|
|
|
if err != nil || code != 200 {
|
|
|
|
return nil, code, err
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
jfUser, code, err := app.jf.UserByID(jfID, false)
|
2020-10-30 21:13:13 +00:00
|
|
|
if err != nil || code != 200 {
|
|
|
|
return nil, code, err
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
username := jfUser.Name
|
2020-11-02 23:20:06 +00:00
|
|
|
email := ""
|
|
|
|
if e, ok := app.storage.emails[jfID]; ok {
|
2020-11-02 23:26:46 +00:00
|
|
|
email = e.(string)
|
2020-11-02 23:20:06 +00:00
|
|
|
}
|
2020-10-30 21:13:13 +00:00
|
|
|
for _, ombiUser := range ombiUsers {
|
|
|
|
ombiAddr := ""
|
|
|
|
if a, ok := ombiUser["emailAddress"]; ok && a != nil {
|
|
|
|
ombiAddr = a.(string)
|
|
|
|
}
|
|
|
|
if ombiUser["userName"].(string) == username || (ombiAddr == email && email != "") {
|
|
|
|
return ombiUser, code, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, 400, fmt.Errorf("Couldn't find user")
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:11:28 +00:00
|
|
|
// Routes from now on!
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Creates a new Jellyfin user without an invite.
|
|
|
|
// @Produce json
|
|
|
|
// @Param newUserDTO body newUserDTO true "New user request object"
|
|
|
|
// @Success 200
|
|
|
|
// @Router /users [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Users
|
2020-09-17 23:59:59 +00:00
|
|
|
func (app *appContext) NewUserAdmin(gc *gin.Context) {
|
2021-01-24 15:19:58 +00:00
|
|
|
respondUser := func(code int, user, email bool, msg string, gc *gin.Context) {
|
|
|
|
resp := newUserResponse{
|
|
|
|
User: user,
|
|
|
|
Email: email,
|
|
|
|
Error: msg,
|
|
|
|
}
|
|
|
|
gc.JSON(code, resp)
|
|
|
|
gc.Abort()
|
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
var req newUserDTO
|
2020-09-17 23:59:59 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-11-02 00:53:08 +00:00
|
|
|
existingUser, _, _ := app.jf.UserByName(req.Username, false)
|
2021-02-19 00:47:01 +00:00
|
|
|
if existingUser.Name != "" {
|
2020-09-17 23:59:59 +00:00
|
|
|
msg := fmt.Sprintf("User already exists named %s", req.Username)
|
|
|
|
app.info.Printf("%s New user failed: %s", req.Username, msg)
|
2021-01-24 15:19:58 +00:00
|
|
|
respondUser(401, false, false, msg, gc)
|
2020-09-17 23:59:59 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
user, status, err := app.jf.NewUser(req.Username, req.Password)
|
2020-09-17 23:59:59 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s New user failed (%d): %v", req.Username, status, err)
|
|
|
|
respondUser(401, false, false, err.Error(), gc)
|
2020-09-17 23:59:59 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
id := user.ID
|
|
|
|
if app.storage.policy.BlockedTags != nil {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetPolicy(id, app.storage.policy)
|
2020-11-22 16:36:43 +00:00
|
|
|
if !(status == 200 || status == 204 || err == nil) {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set user policy (%d): %v", req.Username, status, err)
|
2020-09-17 23:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
if app.storage.configuration.GroupedFolders != nil && len(app.storage.displayprefs) != 0 {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetConfiguration(id, app.storage.configuration)
|
2020-09-17 23:59:59 +00:00
|
|
|
if (status == 200 || status == 204) && err == nil {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetDisplayPreferences(id, app.storage.displayprefs)
|
2020-11-22 16:36:43 +00:00
|
|
|
}
|
|
|
|
if !((status == 200 || status == 204) && err == nil) {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set configuration template (%d): %v", req.Username, status, err)
|
2020-09-17 23:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 15:19:58 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2020-09-17 23:59:59 +00:00
|
|
|
if app.config.Section("password_resets").Key("enabled").MustBool(false) {
|
|
|
|
app.storage.emails[id] = req.Email
|
|
|
|
app.storage.storeEmails()
|
|
|
|
}
|
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
|
|
|
app.storage.loadOmbiTemplate()
|
|
|
|
if len(app.storage.ombi_template) != 0 {
|
2020-11-02 00:53:08 +00:00
|
|
|
errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, app.storage.ombi_template)
|
2020-09-17 23:59:59 +00:00
|
|
|
if err != nil || code != 200 {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to create Ombi user (%d): %v", code, err)
|
2020-09-17 23:59:59 +00:00
|
|
|
app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", "))
|
|
|
|
} else {
|
|
|
|
app.info.Println("Created Ombi user")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("welcome_email").Key("enabled").MustBool(false) && req.Email != "" {
|
2021-01-24 15:19:58 +00:00
|
|
|
app.debug.Printf("%s: Sending welcome email to %s", req.Username, req.Email)
|
2021-04-08 13:20:13 +00:00
|
|
|
msg, err := app.email.constructWelcome(req.Username, time.Time{}, app, false)
|
2021-01-24 15:19:58 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct welcome email: %v", req.Username, err)
|
2021-01-24 15:19:58 +00:00
|
|
|
respondUser(500, true, false, err.Error(), gc)
|
|
|
|
return
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send welcome email: %v", req.Username, err)
|
2021-01-24 15:19:58 +00:00
|
|
|
respondUser(500, true, false, err.Error(), gc)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
app.info.Printf("%s: Sent welcome email to %s", req.Username, req.Email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
respondUser(200, true, true, "", gc)
|
2020-09-17 23:59:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 19:19:12 +00:00
|
|
|
type errorFunc func(gc *gin.Context)
|
|
|
|
|
|
|
|
func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, success bool) {
|
|
|
|
existingUser, _, _ := app.jf.UserByName(req.Username, false)
|
2021-02-19 00:47:01 +00:00
|
|
|
if existingUser.Name != "" {
|
2021-01-30 19:19:12 +00:00
|
|
|
f = func(gc *gin.Context) {
|
|
|
|
msg := fmt.Sprintf("User %s already exists", req.Username)
|
|
|
|
app.info.Printf("%s: New user failed: %s", req.Code, msg)
|
|
|
|
respond(401, "errorUserExists", gc)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
success = false
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("email_confirmation").Key("enabled").MustBool(false) && !confirmed {
|
2021-01-30 19:19:12 +00:00
|
|
|
claims := jwt.MapClaims{
|
|
|
|
"valid": true,
|
|
|
|
"invite": req.Code,
|
|
|
|
"email": req.Email,
|
|
|
|
"username": req.Username,
|
|
|
|
"password": req.Password,
|
|
|
|
"exp": strconv.FormatInt(time.Now().Add(time.Hour*12).Unix(), 10),
|
|
|
|
"type": "confirmation",
|
|
|
|
}
|
|
|
|
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
key, err := tk.SignedString([]byte(os.Getenv("JFA_SECRET")))
|
|
|
|
if err != nil {
|
|
|
|
f = func(gc *gin.Context) {
|
|
|
|
app.info.Printf("Failed to generate confirmation token: %v", err)
|
|
|
|
respond(500, "errorUnknown", gc)
|
|
|
|
}
|
|
|
|
success = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inv := app.storage.invites[req.Code]
|
|
|
|
inv.Keys = append(inv.Keys, key)
|
|
|
|
app.storage.invites[req.Code] = inv
|
|
|
|
app.storage.storeInvites()
|
|
|
|
f = func(gc *gin.Context) {
|
|
|
|
app.debug.Printf("%s: Email confirmation required", req.Code)
|
|
|
|
respond(401, "confirmEmail", gc)
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructConfirmation(req.Code, req.Username, key, app, false)
|
2021-01-30 19:19:12 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct confirmation email: %v", req.Code, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send user confirmation email: %v", req.Code, err)
|
2021-01-30 19:19:12 +00:00
|
|
|
} else {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.info.Printf("%s: Sent user confirmation email to \"%s\"", req.Code, req.Email)
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
success = false
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
|
2020-11-02 00:53:08 +00:00
|
|
|
user, status, err := app.jf.NewUser(req.Username, req.Password)
|
2020-07-29 21:11:28 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-01-30 19:19:12 +00:00
|
|
|
f = func(gc *gin.Context) {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s New user failed (%d): %v", req.Code, status, err)
|
2021-01-30 19:19:12 +00:00
|
|
|
respond(401, app.storage.lang.Admin[app.storage.lang.chosenAdminLang].Notifications.get("errorUnknown"), gc)
|
|
|
|
}
|
|
|
|
success = false
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-23 18:12:58 +00:00
|
|
|
app.storage.loadProfiles()
|
2020-08-16 12:36:54 +00:00
|
|
|
invite := app.storage.invites[req.Code]
|
2020-09-23 18:12:58 +00:00
|
|
|
app.checkInvite(req.Code, true, req.Username)
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("notifications").Key("enabled").MustBool(false) {
|
2020-07-31 11:48:37 +00:00
|
|
|
for address, settings := range invite.Notify {
|
|
|
|
if settings["notify-creation"] {
|
2020-08-02 16:17:29 +00:00
|
|
|
go func() {
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructCreated(req.Code, req.Username, req.Email, invite, app, false)
|
2020-09-13 20:18:47 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct user creation notification: %v", req.Code, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, address); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send user creation notification: %v", req.Code, err)
|
2020-08-02 16:17:29 +00:00
|
|
|
} else {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.info.Printf("%s: Sent user creation notification to %v", req.Code, address)
|
2020-08-02 16:17:29 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
id := user.ID
|
2020-09-20 10:21:04 +00:00
|
|
|
if invite.Profile != "" {
|
2020-09-23 18:12:58 +00:00
|
|
|
app.debug.Printf("Applying settings from profile \"%s\"", invite.Profile)
|
2020-09-20 10:21:04 +00:00
|
|
|
profile, ok := app.storage.profiles[invite.Profile]
|
|
|
|
if !ok {
|
|
|
|
profile = app.storage.profiles["Default"]
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
if profile.Policy.BlockedTags != nil {
|
2020-09-20 10:21:04 +00:00
|
|
|
app.debug.Printf("Applying policy from profile \"%s\"", invite.Profile)
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetPolicy(id, profile.Policy)
|
2020-11-22 16:36:43 +00:00
|
|
|
if !((status == 200 || status == 204) && err == nil) {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set user policy (%d): %v", req.Code, status, err)
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
if profile.Configuration.GroupedFolders != nil && len(profile.Displayprefs) != 0 {
|
2020-09-20 10:21:04 +00:00
|
|
|
app.debug.Printf("Applying homescreen from profile \"%s\"", invite.Profile)
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetConfiguration(id, profile.Configuration)
|
2020-09-20 10:21:04 +00:00
|
|
|
if (status == 200 || status == 204) && err == nil {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetDisplayPreferences(id, profile.Displayprefs)
|
2020-11-22 16:36:43 +00:00
|
|
|
}
|
|
|
|
if !((status == 200 || status == 204) && err == nil) {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set configuration template (%d): %v", req.Code, status, err)
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 21:20:38 +00:00
|
|
|
// if app.config.Section("password_resets").Key("enabled").MustBool(false) {
|
|
|
|
if req.Email != "" {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.emails[id] = req.Email
|
|
|
|
app.storage.storeEmails()
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-09-05 16:32:49 +00:00
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
|
|
|
app.storage.loadOmbiTemplate()
|
|
|
|
if len(app.storage.ombi_template) != 0 {
|
2020-11-02 00:53:08 +00:00
|
|
|
errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, app.storage.ombi_template)
|
2020-09-05 16:32:49 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 13:20:13 +00:00
|
|
|
expiry := time.Time{}
|
|
|
|
if invite.UserExpiry {
|
|
|
|
app.storage.usersLock.Lock()
|
|
|
|
defer app.storage.usersLock.Unlock()
|
2021-04-08 19:43:01 +00:00
|
|
|
expiry = time.Now().AddDate(0, invite.UserMonths, invite.UserDays).Add(time.Duration((60*invite.UserHours)+invite.UserMinutes) * time.Minute)
|
2021-04-08 13:20:13 +00:00
|
|
|
app.storage.users[id] = expiry
|
|
|
|
if err := app.storage.storeUsers(); err != nil {
|
|
|
|
app.err.Printf("Failed to store user duration: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && app.config.Section("welcome_email").Key("enabled").MustBool(false) && req.Email != "" {
|
2021-01-24 15:19:58 +00:00
|
|
|
app.debug.Printf("%s: Sending welcome email to %s", req.Username, req.Email)
|
2021-04-08 13:20:13 +00:00
|
|
|
msg, err := app.email.constructWelcome(req.Username, expiry, app, false)
|
2021-01-24 15:19:58 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct welcome email: %v", req.Username, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to send welcome email: %v", req.Username, err)
|
2021-01-24 15:19:58 +00:00
|
|
|
} else {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.info.Printf("%s: Sent welcome email to \"%s\"", req.Username, req.Email)
|
2021-01-24 15:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 12:31:42 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2021-01-30 19:19:12 +00:00
|
|
|
success = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-28 17:52:24 +00:00
|
|
|
// @Summary Extend time before the user(s) expiry.
|
|
|
|
// @Produce json
|
|
|
|
// @Param extendExpiryDTO body extendExpiryDTO true "Extend expiry object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/extend [post]
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) ExtendExpiry(gc *gin.Context) {
|
|
|
|
var req extendExpiryDTO
|
|
|
|
gc.BindJSON(&req)
|
2021-04-06 12:31:42 +00:00
|
|
|
app.info.Printf("Expiry extension requested for %d user(s)", len(req.Users))
|
2021-04-08 19:43:01 +00:00
|
|
|
if req.Months <= 0 && req.Days <= 0 && req.Hours <= 0 && req.Minutes <= 0 {
|
2021-02-28 17:52:24 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 12:44:52 +00:00
|
|
|
app.storage.usersLock.Lock()
|
|
|
|
defer app.storage.usersLock.Unlock()
|
2021-02-28 17:52:24 +00:00
|
|
|
for _, id := range req.Users {
|
|
|
|
if expiry, ok := app.storage.users[id]; ok {
|
2021-04-08 19:43:01 +00:00
|
|
|
app.storage.users[id] = expiry.AddDate(0, req.Months, req.Days).Add(time.Duration(((60 * req.Hours) + req.Minutes)) * time.Minute)
|
2021-02-28 17:52:24 +00:00
|
|
|
app.debug.Printf("Expiry extended for \"%s\"", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := app.storage.storeUsers(); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to store user duration: %v", err)
|
2021-02-28 17:52:24 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
|
|
|
|
2021-01-30 19:19:12 +00:00
|
|
|
// @Summary Creates a new Jellyfin user via invite code
|
|
|
|
// @Produce json
|
|
|
|
// @Param newUserDTO body newUserDTO true "New user request object"
|
|
|
|
// @Success 200 {object} PasswordValidation
|
|
|
|
// @Failure 400 {object} PasswordValidation
|
|
|
|
// @Router /newUser [post]
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) NewUser(gc *gin.Context) {
|
|
|
|
var req newUserDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
app.debug.Printf("%s: New user attempt", req.Code)
|
|
|
|
if !app.checkInvite(req.Code, false, "") {
|
|
|
|
app.info.Printf("%s New user failed: invalid code", req.Code)
|
|
|
|
respond(401, "errorInvalidCode", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
validation := app.validator.validate(req.Password)
|
|
|
|
valid := true
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
// 200 bcs idk what i did in js
|
2021-04-02 21:13:04 +00:00
|
|
|
app.info.Printf("%s: New user failed: Invalid password", req.Code)
|
2021-01-30 19:19:12 +00:00
|
|
|
gc.JSON(200, validation)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, success := app.newUser(req, false)
|
|
|
|
if !success {
|
|
|
|
f(gc)
|
|
|
|
return
|
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
code := 200
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
code = 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gc.JSON(code, validation)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:58:53 +00:00
|
|
|
// @Summary Send an announcement via email to a given list of users.
|
|
|
|
// @Produce json
|
|
|
|
// @Param announcementDTO body announcementDTO true "Announcement request object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/announce [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) Announce(gc *gin.Context) {
|
|
|
|
var req announcementDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if !emailEnabled {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addresses := []string{}
|
|
|
|
for _, userID := range req.Users {
|
|
|
|
addr, ok := app.storage.emails[userID]
|
|
|
|
if !ok || addr == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addresses = append(addresses, addr.(string))
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
msg, err := app.email.constructTemplate(req.Subject, req.Message, app)
|
2021-02-18 14:58:53 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to construct announcement emails: %v", err)
|
2021-02-18 14:58:53 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
} else if err := app.email.send(msg, addresses...); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to send announcement emails: %v", err)
|
2021-02-18 14:58:53 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
app.info.Printf("Sent announcement email to %d users", len(addresses))
|
2021-02-18 14:58:53 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:28:36 +00:00
|
|
|
// @Summary Enable/Disable a list of users, optionally notifying them why.
|
|
|
|
// @Produce json
|
|
|
|
// @Param enableDisableUserDTO body enableDisableUserDTO true "User enable/disable request object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 500 {object} errorListDTO "List of errors"
|
|
|
|
// @Router /users/enable [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) EnableDisableUsers(gc *gin.Context) {
|
|
|
|
var req enableDisableUserDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
errors := errorListDTO{
|
|
|
|
"GetUser": map[string]string{},
|
|
|
|
"SetPolicy": map[string]string{},
|
|
|
|
}
|
|
|
|
var addresses []string
|
|
|
|
for _, userID := range req.Users {
|
|
|
|
user, status, err := app.jf.UserByID(userID, false)
|
|
|
|
if status != 200 || err != nil {
|
|
|
|
errors["GetUser"][userID] = fmt.Sprintf("%d %v", status, err)
|
|
|
|
app.err.Printf("Failed to get user \"%s\" (%d): %v", userID, status, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user.Policy.IsDisabled = !req.Enabled
|
|
|
|
status, err = app.jf.SetPolicy(userID, user.Policy)
|
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
errors["SetPolicy"][userID] = fmt.Sprintf("%d %v", status, err)
|
|
|
|
app.err.Printf("Failed to set policy for user \"%s\" (%d): %v", userID, status, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if emailEnabled && req.Notify {
|
|
|
|
addr, ok := app.storage.emails[userID]
|
|
|
|
if addr != nil && ok {
|
|
|
|
addresses = append(addresses, addr.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(addresses) != 0 {
|
|
|
|
go func(reason string, addresses []string) {
|
|
|
|
var msg *Email
|
|
|
|
var err error
|
|
|
|
if req.Enabled {
|
|
|
|
msg, err = app.email.constructEnabled(reason, app, false)
|
|
|
|
} else {
|
|
|
|
msg, err = app.email.constructDisabled(reason, app, false)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to construct account enabled/disabled emails: %v", err)
|
|
|
|
} else if err := app.email.send(msg, addresses...); err != nil {
|
|
|
|
app.err.Printf("Failed to send account enabled/disabled emails: %v", err)
|
|
|
|
} else {
|
|
|
|
app.info.Println("Sent account enabled/disabled emails")
|
|
|
|
}
|
|
|
|
}(req.Reason, addresses)
|
|
|
|
}
|
|
|
|
app.jf.CacheExpiry = time.Now()
|
|
|
|
if len(errors["GetUser"]) != 0 || len(errors["SetPolicy"]) != 0 {
|
|
|
|
gc.JSON(500, errors)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Delete a list of users, optionally notifying them why.
|
|
|
|
// @Produce json
|
|
|
|
// @Param deleteUserDTO body deleteUserDTO true "User deletion request object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 500 {object} errorListDTO "List of errors"
|
|
|
|
// @Router /users [delete]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Users
|
2021-02-19 14:51:36 +00:00
|
|
|
func (app *appContext) DeleteUsers(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req deleteUserDTO
|
2021-02-19 14:51:36 +00:00
|
|
|
var addresses []string
|
2020-09-17 22:50:07 +00:00
|
|
|
gc.BindJSON(&req)
|
|
|
|
errors := map[string]string{}
|
2020-10-29 16:03:00 +00:00
|
|
|
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
|
2020-09-17 22:50:07 +00:00
|
|
|
for _, userID := range req.Users {
|
2020-10-29 16:03:00 +00:00
|
|
|
if ombiEnabled {
|
2020-10-30 21:13:13 +00:00
|
|
|
ombiUser, code, err := app.getOmbiUser(userID)
|
|
|
|
if code == 200 && err == nil {
|
|
|
|
if id, ok := ombiUser["id"]; ok {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err := app.ombi.DeleteUser(id.(string))
|
2020-10-29 16:03:00 +00:00
|
|
|
if err != nil || status != 200 {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to delete ombi user (%d): %v", status, err)
|
|
|
|
errors[userID] = fmt.Sprintf("Ombi: %d %v, ", status, err)
|
2020-10-29 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err := app.jf.DeleteUser(userID)
|
2020-09-17 22:50:07 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
msg := fmt.Sprintf("%d: %v", status, err)
|
2020-10-29 16:03:00 +00:00
|
|
|
if _, ok := errors[userID]; !ok {
|
|
|
|
errors[userID] = msg
|
|
|
|
} else {
|
|
|
|
errors[userID] += msg
|
|
|
|
}
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && req.Notify {
|
2020-09-17 22:50:07 +00:00
|
|
|
addr, ok := app.storage.emails[userID]
|
|
|
|
if addr != nil && ok {
|
2021-02-19 14:51:36 +00:00
|
|
|
addresses = append(addresses, addr.(string))
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 14:51:36 +00:00
|
|
|
if len(addresses) != 0 {
|
|
|
|
go func(reason string, addresses []string) {
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructDeleted(reason, app, false)
|
2021-02-19 14:51:36 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to construct account deletion emails: %v", err)
|
2021-02-19 14:51:36 +00:00
|
|
|
} else if err := app.email.send(msg, addresses...); err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to send account deletion emails: %v", err)
|
2021-02-19 14:51:36 +00:00
|
|
|
} else {
|
|
|
|
app.info.Println("Sent account deletion emails")
|
|
|
|
}
|
|
|
|
}(req.Reason, addresses)
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2020-09-17 22:50:07 +00:00
|
|
|
if len(errors) == len(req.Users) {
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(500, false, gc)
|
2020-09-17 22:50:07 +00:00
|
|
|
app.err.Printf("Account deletion failed: %s", errors[req.Users[0]])
|
|
|
|
return
|
|
|
|
} else if len(errors) != 0 {
|
|
|
|
gc.JSON(500, errors)
|
|
|
|
return
|
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Create a new invite.
|
|
|
|
// @Produce json
|
|
|
|
// @Param generateInviteDTO body generateInviteDTO true "New invite request object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /invites [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Invites
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) GenerateInvite(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req generateInviteDTO
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Println("Generating new invite")
|
|
|
|
app.storage.loadInvites()
|
2020-07-29 21:11:28 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-11-22 16:36:43 +00:00
|
|
|
currentTime := time.Now()
|
2021-04-08 19:43:01 +00:00
|
|
|
validTill := currentTime.AddDate(0, req.Months, req.Days)
|
2020-11-22 16:36:43 +00:00
|
|
|
validTill = validTill.Add(time.Hour*time.Duration(req.Hours) + time.Minute*time.Duration(req.Minutes))
|
2020-09-21 23:34:11 +00:00
|
|
|
// make sure code doesn't begin with number
|
2020-11-22 16:36:43 +00:00
|
|
|
inviteCode := shortuuid.New()
|
|
|
|
_, err := strconv.Atoi(string(inviteCode[0]))
|
2020-09-21 23:34:11 +00:00
|
|
|
for err == nil {
|
2020-11-22 16:36:43 +00:00
|
|
|
inviteCode = shortuuid.New()
|
|
|
|
_, err = strconv.Atoi(string(inviteCode[0]))
|
2020-09-21 23:34:11 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
var invite Invite
|
2021-01-24 15:55:45 +00:00
|
|
|
if req.Label != "" {
|
|
|
|
invite.Label = req.Label
|
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
invite.Created = currentTime
|
2020-07-29 21:11:28 +00:00
|
|
|
if req.MultipleUses {
|
|
|
|
if req.NoLimit {
|
|
|
|
invite.NoLimit = true
|
|
|
|
} else {
|
|
|
|
invite.RemainingUses = req.RemainingUses
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
invite.RemainingUses = 1
|
|
|
|
}
|
2021-02-28 15:41:06 +00:00
|
|
|
invite.UserExpiry = req.UserExpiry
|
|
|
|
if invite.UserExpiry {
|
2021-04-08 19:43:01 +00:00
|
|
|
invite.UserMonths = req.UserMonths
|
2021-02-28 00:44:28 +00:00
|
|
|
invite.UserDays = req.UserDays
|
|
|
|
invite.UserHours = req.UserHours
|
|
|
|
invite.UserMinutes = req.UserMinutes
|
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
invite.ValidTill = validTill
|
2021-01-31 18:50:04 +00:00
|
|
|
if emailEnabled && req.Email != "" && app.config.Section("invite_emails").Key("enabled").MustBool(false) {
|
2020-11-22 16:36:43 +00:00
|
|
|
app.debug.Printf("%s: Sending invite email", inviteCode)
|
2020-07-31 11:48:37 +00:00
|
|
|
invite.Email = req.Email
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err := app.email.constructInvite(inviteCode, invite, app, false)
|
2020-09-13 20:18:47 +00:00
|
|
|
if err != nil {
|
2020-07-31 11:48:37 +00:00
|
|
|
invite.Email = fmt.Sprintf("Failed to send to %s", req.Email)
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to construct invite email: %v", inviteCode, err)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2020-07-31 11:48:37 +00:00
|
|
|
invite.Email = fmt.Sprintf("Failed to send to %s", req.Email)
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: %s: %v", inviteCode, invite.Email, err)
|
2020-07-31 21:07:09 +00:00
|
|
|
} else {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.info.Printf("%s: Sent invite email to \"%s\"", inviteCode, req.Email)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-09-20 10:21:04 +00:00
|
|
|
if req.Profile != "" {
|
|
|
|
if _, ok := app.storage.profiles[req.Profile]; ok {
|
|
|
|
invite.Profile = req.Profile
|
|
|
|
} else {
|
|
|
|
invite.Profile = "Default"
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
app.storage.invites[inviteCode] = invite
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.storeInvites()
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Set profile for an invite
|
|
|
|
// @Produce json
|
|
|
|
// @Param inviteProfileDTO body inviteProfileDTO true "Invite profile object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /invites/profile [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-20 10:21:04 +00:00
|
|
|
func (app *appContext) SetProfile(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req inviteProfileDTO
|
2020-09-20 10:21:04 +00:00
|
|
|
gc.BindJSON(&req)
|
|
|
|
app.debug.Printf("%s: Setting profile to \"%s\"", req.Invite, req.Profile)
|
2020-09-21 23:34:11 +00:00
|
|
|
// "" means "Don't apply profile"
|
|
|
|
if _, ok := app.storage.profiles[req.Profile]; !ok && req.Profile != "" {
|
2020-09-20 10:21:04 +00:00
|
|
|
app.err.Printf("%s: Profile \"%s\" not found", req.Invite, req.Profile)
|
|
|
|
respond(500, "Profile not found", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inv := app.storage.invites[req.Invite]
|
|
|
|
inv.Profile = req.Profile
|
|
|
|
app.storage.invites[req.Invite] = inv
|
|
|
|
app.storage.storeInvites()
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get a list of profiles
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getProfilesDTO
|
|
|
|
// @Router /profiles [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-22 23:01:07 +00:00
|
|
|
func (app *appContext) GetProfiles(gc *gin.Context) {
|
|
|
|
app.storage.loadProfiles()
|
|
|
|
app.debug.Println("Profiles requested")
|
2020-09-24 16:51:13 +00:00
|
|
|
out := getProfilesDTO{
|
|
|
|
DefaultProfile: app.storage.defaultProfile,
|
|
|
|
Profiles: map[string]profileDTO{},
|
2020-09-23 17:48:00 +00:00
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
for name, p := range app.storage.profiles {
|
2020-09-24 16:51:13 +00:00
|
|
|
out.Profiles[name] = profileDTO{
|
|
|
|
Admin: p.Admin,
|
|
|
|
LibraryAccess: p.LibraryAccess,
|
|
|
|
FromUser: p.FromUser,
|
2020-09-22 23:01:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
gc.JSON(200, out)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Set the default profile to use.
|
|
|
|
// @Produce json
|
|
|
|
// @Param profileChangeDTO body profileChangeDTO true "Default profile object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /profiles/default [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-23 17:48:00 +00:00
|
|
|
func (app *appContext) SetDefaultProfile(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
req := profileChangeDTO{}
|
2020-09-23 17:48:00 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-09-24 16:51:13 +00:00
|
|
|
app.info.Printf("Setting default profile to \"%s\"", req.Name)
|
|
|
|
if _, ok := app.storage.profiles[req.Name]; !ok {
|
|
|
|
app.err.Printf("Profile not found: \"%s\"", req.Name)
|
2020-09-23 17:48:00 +00:00
|
|
|
respond(500, "Profile not found", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for name, profile := range app.storage.profiles {
|
2020-09-24 16:51:13 +00:00
|
|
|
if name == req.Name {
|
2020-09-23 17:48:00 +00:00
|
|
|
profile.Admin = true
|
|
|
|
app.storage.profiles[name] = profile
|
|
|
|
} else {
|
|
|
|
profile.Admin = false
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
app.storage.defaultProfile = req.Name
|
|
|
|
respondBool(200, true, gc)
|
2020-09-23 17:48:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Create a profile based on a Jellyfin user's settings.
|
|
|
|
// @Produce json
|
|
|
|
// @Param newProfileDTO body newProfileDTO true "New profile object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /profiles [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-22 23:01:07 +00:00
|
|
|
func (app *appContext) CreateProfile(gc *gin.Context) {
|
2020-10-30 21:13:13 +00:00
|
|
|
app.info.Println("Profile creation requested")
|
2020-09-24 16:51:13 +00:00
|
|
|
var req newProfileDTO
|
2020-09-22 23:01:07 +00:00
|
|
|
gc.BindJSON(&req)
|
2021-02-28 18:26:22 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2020-11-02 00:53:08 +00:00
|
|
|
user, status, err := app.jf.UserByID(req.ID, false)
|
2020-09-22 23:01:07 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get user from Jellyfin (%d): %v", status, err)
|
2020-09-22 23:01:07 +00:00
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
profile := Profile{
|
2021-02-19 00:47:01 +00:00
|
|
|
FromUser: user.Name,
|
|
|
|
Policy: user.Policy,
|
2020-09-22 23:01:07 +00:00
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
app.debug.Printf("Creating profile from user \"%s\"", user.Name)
|
2020-09-22 23:01:07 +00:00
|
|
|
if req.Homescreen {
|
2021-02-19 00:47:01 +00:00
|
|
|
profile.Configuration = user.Configuration
|
2020-11-02 00:53:08 +00:00
|
|
|
profile.Displayprefs, status, err = app.jf.GetDisplayPreferences(req.ID)
|
2020-09-22 23:01:07 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get DisplayPrefs (%d): %v", status, err)
|
2020-09-22 23:01:07 +00:00
|
|
|
respond(500, "Couldn't get displayprefs", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.storage.loadProfiles()
|
|
|
|
app.storage.profiles[req.Name] = profile
|
|
|
|
app.storage.storeProfiles()
|
|
|
|
app.storage.loadProfiles()
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-09-22 23:01:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Delete an existing profile
|
|
|
|
// @Produce json
|
|
|
|
// @Param profileChangeDTO body profileChangeDTO true "Delete profile object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /profiles [delete]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-22 23:01:07 +00:00
|
|
|
func (app *appContext) DeleteProfile(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
req := profileChangeDTO{}
|
2020-09-22 23:01:07 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-09-24 16:51:13 +00:00
|
|
|
name := req.Name
|
2020-09-22 23:01:07 +00:00
|
|
|
if _, ok := app.storage.profiles[name]; ok {
|
2021-01-05 18:16:23 +00:00
|
|
|
if app.storage.defaultProfile == name {
|
|
|
|
app.storage.defaultProfile = ""
|
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
delete(app.storage.profiles, name)
|
|
|
|
}
|
|
|
|
app.storage.storeProfiles()
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get invites.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getInvitesDTO
|
|
|
|
// @Router /invites [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Invites
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) GetInvites(gc *gin.Context) {
|
|
|
|
app.debug.Println("Invites requested")
|
2020-10-22 16:50:40 +00:00
|
|
|
currentTime := time.Now()
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.loadInvites()
|
|
|
|
app.checkInvites()
|
2020-09-24 16:51:13 +00:00
|
|
|
var invites []inviteDTO
|
2020-08-16 12:36:54 +00:00
|
|
|
for code, inv := range app.storage.invites {
|
2021-04-08 19:43:01 +00:00
|
|
|
_, months, days, hours, minutes, _ := timeDiff(inv.ValidTill, currentTime)
|
2020-09-24 16:51:13 +00:00
|
|
|
invite := inviteDTO{
|
2021-02-28 15:41:06 +00:00
|
|
|
Code: code,
|
2021-04-08 19:43:01 +00:00
|
|
|
Months: months,
|
2021-02-28 15:41:06 +00:00
|
|
|
Days: days,
|
|
|
|
Hours: hours,
|
|
|
|
Minutes: minutes,
|
|
|
|
UserExpiry: inv.UserExpiry,
|
2021-04-08 19:43:01 +00:00
|
|
|
UserMonths: inv.UserMonths,
|
2021-02-28 15:41:06 +00:00
|
|
|
UserDays: inv.UserDays,
|
|
|
|
UserHours: inv.UserHours,
|
|
|
|
UserMinutes: inv.UserMinutes,
|
2021-04-06 20:25:44 +00:00
|
|
|
Created: inv.Created.Unix(),
|
2021-02-28 15:41:06 +00:00
|
|
|
Profile: inv.Profile,
|
|
|
|
NoLimit: inv.NoLimit,
|
|
|
|
Label: inv.Label,
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
if len(inv.UsedBy) != 0 {
|
2021-04-06 20:25:44 +00:00
|
|
|
invite.UsedBy = map[string]int64{}
|
|
|
|
for _, pair := range inv.UsedBy {
|
|
|
|
// These used to be stored formatted instead of as a unix timestamp.
|
|
|
|
unix, err := strconv.ParseInt(pair[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
date, err := timefmt.Parse(pair[1], app.datePattern+" "+app.timePattern)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to parse usedBy time: %v", err)
|
|
|
|
}
|
|
|
|
unix = date.Unix()
|
|
|
|
}
|
|
|
|
invite.UsedBy[pair[0]] = unix
|
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
invite.RemainingUses = 1
|
2020-07-29 21:11:28 +00:00
|
|
|
if inv.RemainingUses != 0 {
|
2020-09-24 16:51:13 +00:00
|
|
|
invite.RemainingUses = inv.RemainingUses
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
if inv.Email != "" {
|
2020-09-24 16:51:13 +00:00
|
|
|
invite.Email = inv.Email
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
if len(inv.Notify) != 0 {
|
|
|
|
var address string
|
2020-08-16 12:36:54 +00:00
|
|
|
if app.config.Section("ui").Key("jellyfin_login").MustBool(false) {
|
|
|
|
app.storage.loadEmails()
|
2020-09-19 08:09:30 +00:00
|
|
|
if addr := app.storage.emails[gc.GetString("jfId")]; addr != nil {
|
|
|
|
address = addr.(string)
|
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
} else {
|
2020-08-16 12:36:54 +00:00
|
|
|
address = app.config.Section("ui").Key("email").String()
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
if _, ok := inv.Notify[address]; ok {
|
2020-09-24 16:51:13 +00:00
|
|
|
if _, ok = inv.Notify[address]["notify-expiry"]; ok {
|
|
|
|
invite.NotifyExpiry = inv.Notify[address]["notify-expiry"]
|
|
|
|
}
|
|
|
|
if _, ok = inv.Notify[address]["notify-creation"]; ok {
|
|
|
|
invite.NotifyCreation = inv.Notify[address]["notify-creation"]
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
invites = append(invites, invite)
|
|
|
|
}
|
2020-09-20 10:21:04 +00:00
|
|
|
profiles := make([]string, len(app.storage.profiles))
|
2020-09-23 17:48:00 +00:00
|
|
|
if len(app.storage.profiles) != 0 {
|
|
|
|
profiles[0] = app.storage.defaultProfile
|
|
|
|
i := 1
|
|
|
|
if len(app.storage.profiles) > 1 {
|
|
|
|
for p := range app.storage.profiles {
|
|
|
|
if p != app.storage.defaultProfile {
|
|
|
|
profiles[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
resp := getInvitesDTO{
|
|
|
|
Profiles: profiles,
|
|
|
|
Invites: invites,
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
2020-07-31 11:48:37 +00:00
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Set notification preferences for an invite.
|
|
|
|
// @Produce json
|
|
|
|
// @Param setNotifyDTO body setNotifyDTO true "Map of invite codes to notification settings objects"
|
|
|
|
// @Success 200
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /invites/notify [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Other
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) SetNotify(gc *gin.Context) {
|
2020-09-19 16:05:09 +00:00
|
|
|
var req map[string]map[string]bool
|
2020-07-31 11:48:37 +00:00
|
|
|
gc.BindJSON(&req)
|
|
|
|
changed := false
|
|
|
|
for code, settings := range req {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Printf("%s: Notification settings change requested", code)
|
|
|
|
app.storage.loadInvites()
|
|
|
|
app.storage.loadEmails()
|
|
|
|
invite, ok := app.storage.invites[code]
|
2020-07-31 11:48:37 +00:00
|
|
|
if !ok {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s Notification setting change failed: Invalid code", code)
|
2020-09-24 16:51:13 +00:00
|
|
|
respond(400, "Invalid invite code", gc)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var address string
|
2020-08-16 12:36:54 +00:00
|
|
|
if app.config.Section("ui").Key("jellyfin_login").MustBool(false) {
|
2020-07-31 11:48:37 +00:00
|
|
|
var ok bool
|
2020-08-16 12:36:54 +00:00
|
|
|
address, ok = app.storage.emails[gc.GetString("jfId")].(string)
|
2020-07-31 11:48:37 +00:00
|
|
|
if !ok {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s: Couldn't find email address. Make sure it's set", code)
|
|
|
|
app.debug.Printf("%s: User ID \"%s\"", code, gc.GetString("jfId"))
|
2020-09-24 16:51:13 +00:00
|
|
|
respond(500, "Missing user email", gc)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-16 12:36:54 +00:00
|
|
|
address = app.config.Section("ui").Key("email").String()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if invite.Notify == nil {
|
|
|
|
invite.Notify = map[string]map[string]bool{}
|
|
|
|
}
|
|
|
|
if _, ok := invite.Notify[address]; !ok {
|
|
|
|
invite.Notify[address] = map[string]bool{}
|
|
|
|
} /*else {
|
|
|
|
if _, ok := invite.Notify[address]["notify-expiry"]; !ok {
|
|
|
|
*/
|
2020-09-19 16:05:09 +00:00
|
|
|
if _, ok := settings["notify-expiry"]; ok && invite.Notify[address]["notify-expiry"] != settings["notify-expiry"] {
|
|
|
|
invite.Notify[address]["notify-expiry"] = settings["notify-expiry"]
|
|
|
|
app.debug.Printf("%s: Set \"notify-expiry\" to %t for %s", code, settings["notify-expiry"], address)
|
2020-07-31 11:48:37 +00:00
|
|
|
changed = true
|
|
|
|
}
|
2020-09-19 16:05:09 +00:00
|
|
|
if _, ok := settings["notify-creation"]; ok && invite.Notify[address]["notify-creation"] != settings["notify-creation"] {
|
|
|
|
invite.Notify[address]["notify-creation"] = settings["notify-creation"]
|
|
|
|
app.debug.Printf("%s: Set \"notify-creation\" to %t for %s", code, settings["notify-creation"], address)
|
2020-07-31 11:48:37 +00:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
if changed {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.invites[code] = invite
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if changed {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.storeInvites()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 12:59:25 +00:00
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Delete an invite.
|
|
|
|
// @Produce json
|
|
|
|
// @Param deleteInviteDTO body deleteInviteDTO true "Delete invite object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} stringResponse
|
|
|
|
// @Router /invites [delete]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Invites
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) DeleteInvite(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req deleteInviteDTO
|
2020-07-31 12:59:25 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Printf("%s: Deletion requested", req.Code)
|
2020-07-31 12:59:25 +00:00
|
|
|
var ok bool
|
2020-08-16 12:36:54 +00:00
|
|
|
_, ok = app.storage.invites[req.Code]
|
2020-07-31 12:59:25 +00:00
|
|
|
if ok {
|
2020-08-16 12:36:54 +00:00
|
|
|
delete(app.storage.invites, req.Code)
|
|
|
|
app.storage.storeInvites()
|
|
|
|
app.info.Printf("%s: Invite deleted", req.Code)
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-07-31 12:59:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s: Deletion failed: Invalid code", req.Code)
|
2020-09-24 16:51:13 +00:00
|
|
|
respond(400, "Code doesn't exist", gc)
|
2020-07-31 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get a list of Jellyfin users.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getUsersDTO
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /users [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Users
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) GetUsers(gc *gin.Context) {
|
|
|
|
app.debug.Println("Users requested")
|
2020-09-24 16:51:13 +00:00
|
|
|
var resp getUsersDTO
|
2020-11-02 00:53:08 +00:00
|
|
|
users, status, err := app.jf.GetUsers(false)
|
2021-04-06 13:00:32 +00:00
|
|
|
resp.UserList = make([]respUser, len(users))
|
2020-07-31 12:59:25 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
|
2020-07-31 12:59:25 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 13:00:32 +00:00
|
|
|
i := 0
|
2021-04-06 17:02:15 +00:00
|
|
|
app.storage.usersLock.Lock()
|
|
|
|
defer app.storage.usersLock.Unlock()
|
2020-07-31 12:59:25 +00:00
|
|
|
for _, jfUser := range users {
|
2021-02-19 00:47:01 +00:00
|
|
|
user := respUser{
|
2021-02-28 17:52:24 +00:00
|
|
|
ID: jfUser.ID,
|
|
|
|
Name: jfUser.Name,
|
|
|
|
Admin: jfUser.Policy.IsAdministrator,
|
|
|
|
Disabled: jfUser.Policy.IsDisabled,
|
2021-02-19 00:47:01 +00:00
|
|
|
}
|
|
|
|
if !jfUser.LastActivityDate.IsZero() {
|
2021-04-06 19:53:30 +00:00
|
|
|
user.LastActive = jfUser.LastActivityDate.Unix()
|
2020-09-16 20:42:22 +00:00
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
if email, ok := app.storage.emails[jfUser.ID]; ok {
|
2020-07-31 12:59:25 +00:00
|
|
|
user.Email = email.(string)
|
|
|
|
}
|
2021-04-06 17:02:15 +00:00
|
|
|
expiry, ok := app.storage.users[jfUser.ID]
|
|
|
|
if ok {
|
2021-04-06 19:53:30 +00:00
|
|
|
user.Expiry = expiry.Unix()
|
2021-02-28 17:52:24 +00:00
|
|
|
}
|
2020-09-16 20:42:22 +00:00
|
|
|
|
2021-04-06 13:00:32 +00:00
|
|
|
resp.UserList[i] = user
|
|
|
|
i++
|
2020-07-31 12:59:25 +00:00
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get a list of Ombi users.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} ombiUsersDTO
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /ombi/users [get]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Ombi
|
2020-09-05 16:32:49 +00:00
|
|
|
func (app *appContext) OmbiUsers(gc *gin.Context) {
|
|
|
|
app.debug.Println("Ombi users requested")
|
2020-11-02 00:53:08 +00:00
|
|
|
users, status, err := app.ombi.GetUsers()
|
2020-09-05 16:32:49 +00:00
|
|
|
if err != nil || status != 200 {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get users from Ombi (%d): %v", status, err)
|
2020-09-05 16:32:49 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
userlist := make([]ombiUser, len(users))
|
|
|
|
for i, data := range users {
|
|
|
|
userlist[i] = ombiUser{
|
|
|
|
Name: data["userName"].(string),
|
|
|
|
ID: data["id"].(string),
|
|
|
|
}
|
|
|
|
}
|
2020-09-24 16:51:13 +00:00
|
|
|
gc.JSON(200, ombiUsersDTO{Users: userlist})
|
2020-09-05 16:32:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Set new user defaults for Ombi accounts.
|
|
|
|
// @Produce json
|
|
|
|
// @Param ombiUser body ombiUser true "User to source settings from"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /ombi/defaults [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Ombi
|
2020-09-24 16:51:13 +00:00
|
|
|
func (app *appContext) SetOmbiDefaults(gc *gin.Context) {
|
|
|
|
var req ombiUser
|
|
|
|
gc.BindJSON(&req)
|
2020-11-02 00:53:08 +00:00
|
|
|
template, code, err := app.ombi.TemplateByID(req.ID)
|
2020-09-24 16:51:13 +00:00
|
|
|
if err != nil || code != 200 || len(template) == 0 {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Couldn't get user from Ombi (%d): %v", code, err)
|
2020-09-24 16:51:13 +00:00
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.storage.ombi_template = template
|
|
|
|
app.storage.storeOmbiTemplate()
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Modify user's email addresses.
|
|
|
|
// @Produce json
|
|
|
|
// @Param modifyEmailsDTO body modifyEmailsDTO true "Map of userIDs to email addresses"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /users/emails [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Users
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) ModifyEmails(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req modifyEmailsDTO
|
2020-07-31 12:59:25 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Println("Email modification requested")
|
2020-11-02 00:53:08 +00:00
|
|
|
users, status, err := app.jf.GetUsers(false)
|
2020-07-31 12:59:25 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get users from Jellyfin (%d): %v", status, err)
|
2020-07-31 12:59:25 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
2020-10-30 21:13:13 +00:00
|
|
|
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
|
2020-07-31 12:59:25 +00:00
|
|
|
for _, jfUser := range users {
|
2021-02-19 00:47:01 +00:00
|
|
|
id := jfUser.ID
|
2020-10-30 21:13:13 +00:00
|
|
|
if address, ok := req[id]; ok {
|
2021-02-19 00:47:01 +00:00
|
|
|
app.storage.emails[id] = address
|
2020-10-30 21:13:13 +00:00
|
|
|
if ombiEnabled {
|
|
|
|
ombiUser, code, err := app.getOmbiUser(id)
|
|
|
|
if code == 200 && err == nil {
|
|
|
|
ombiUser["emailAddress"] = address
|
2020-11-02 00:53:08 +00:00
|
|
|
code, err = app.ombi.ModifyUser(ombiUser)
|
2020-10-30 21:13:13 +00:00
|
|
|
if code != 200 || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("%s: Failed to change ombi email address (%d): %v", ombiUser["userName"].(string), code, err)
|
2020-10-30 21:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 12:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.storeEmails()
|
|
|
|
app.info.Println("Email list modified")
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(200, true, gc)
|
2020-07-31 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Apply settings to a list of users, either from a profile or from another user.
|
|
|
|
// @Produce json
|
|
|
|
// @Param userSettingsDTO body userSettingsDTO true "Parameters for applying settings"
|
|
|
|
// @Success 200 {object} errorListDTO
|
2020-11-22 16:36:43 +00:00
|
|
|
// @Failure 500 {object} errorListDTO "Lists of errors that occurred while applying settings"
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Router /users/settings [post]
|
2020-11-12 21:04:35 +00:00
|
|
|
// @Security Bearer
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Profiles & Settings
|
2020-09-17 15:51:19 +00:00
|
|
|
func (app *appContext) ApplySettings(gc *gin.Context) {
|
2020-09-22 23:01:07 +00:00
|
|
|
app.info.Println("User settings change requested")
|
2020-09-24 16:51:13 +00:00
|
|
|
var req userSettingsDTO
|
2020-09-17 15:51:19 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-09-22 23:01:07 +00:00
|
|
|
applyingFrom := "profile"
|
2021-02-19 00:47:01 +00:00
|
|
|
var policy mediabrowser.Policy
|
|
|
|
var configuration mediabrowser.Configuration
|
|
|
|
var displayprefs map[string]interface{}
|
2020-09-22 23:01:07 +00:00
|
|
|
if req.From == "profile" {
|
|
|
|
app.storage.loadProfiles()
|
2021-04-02 21:13:04 +00:00
|
|
|
// Check profile exists & isn't empty
|
2021-02-19 00:47:01 +00:00
|
|
|
if _, ok := app.storage.profiles[req.Profile]; !ok || app.storage.profiles[req.Profile].Policy.BlockedTags == nil {
|
2020-09-22 23:01:07 +00:00
|
|
|
app.err.Printf("Couldn't find profile \"%s\" or profile was empty", req.Profile)
|
|
|
|
respond(500, "Couldn't find profile", gc)
|
2020-09-17 15:51:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.Homescreen {
|
2021-02-19 00:47:01 +00:00
|
|
|
if app.storage.profiles[req.Profile].Configuration.GroupedFolders == nil || len(app.storage.profiles[req.Profile].Displayprefs) == 0 {
|
2020-09-22 23:01:07 +00:00
|
|
|
app.err.Printf("No homescreen saved in profile \"%s\"", req.Profile)
|
2020-09-17 15:51:19 +00:00
|
|
|
respond(500, "No homescreen template available", gc)
|
|
|
|
return
|
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
configuration = app.storage.profiles[req.Profile].Configuration
|
|
|
|
displayprefs = app.storage.profiles[req.Profile].Displayprefs
|
2020-09-17 15:51:19 +00:00
|
|
|
}
|
2020-09-22 23:01:07 +00:00
|
|
|
policy = app.storage.profiles[req.Profile].Policy
|
2020-09-17 15:51:19 +00:00
|
|
|
} else if req.From == "user" {
|
|
|
|
applyingFrom = "user"
|
2021-03-01 00:32:09 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2020-11-02 00:53:08 +00:00
|
|
|
user, status, err := app.jf.UserByID(req.ID, false)
|
2020-09-17 15:51:19 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get user from Jellyfin (%d): %v", status, err)
|
2020-09-17 15:51:19 +00:00
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
applyingFrom = "\"" + user.Name + "\""
|
|
|
|
policy = user.Policy
|
2020-09-17 15:51:19 +00:00
|
|
|
if req.Homescreen {
|
2020-11-02 00:53:08 +00:00
|
|
|
displayprefs, status, err = app.jf.GetDisplayPreferences(req.ID)
|
2020-09-17 15:51:19 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Failed to get DisplayPrefs (%d): %v", status, err)
|
2020-09-17 15:51:19 +00:00
|
|
|
respond(500, "Couldn't get displayprefs", gc)
|
|
|
|
return
|
|
|
|
}
|
2021-02-19 00:47:01 +00:00
|
|
|
configuration = user.Configuration
|
2020-09-17 15:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
app.info.Printf("Applying settings to %d user(s) from %s", len(req.ApplyTo), applyingFrom)
|
2020-09-24 16:51:13 +00:00
|
|
|
errors := errorListDTO{
|
2020-09-17 15:51:19 +00:00
|
|
|
"policy": map[string]string{},
|
|
|
|
"homescreen": map[string]string{},
|
|
|
|
}
|
|
|
|
for _, id := range req.ApplyTo {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err := app.jf.SetPolicy(id, policy)
|
2020-09-17 15:51:19 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
errors["policy"][id] = fmt.Sprintf("%d: %s", status, err)
|
|
|
|
}
|
|
|
|
if req.Homescreen {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetConfiguration(id, configuration)
|
2020-09-17 15:51:19 +00:00
|
|
|
errorString := ""
|
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
errorString += fmt.Sprintf("Configuration %d: %s ", status, err)
|
|
|
|
} else {
|
2020-11-02 00:53:08 +00:00
|
|
|
status, err = app.jf.SetDisplayPreferences(id, displayprefs)
|
2020-09-17 15:51:19 +00:00
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
errorString += fmt.Sprintf("Displayprefs %d: %s ", status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errorString != "" {
|
|
|
|
errors["homescreen"][id] = errorString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code := 200
|
|
|
|
if len(errors["policy"]) == len(req.ApplyTo) || len(errors["homescreen"]) == len(req.ApplyTo) {
|
|
|
|
code = 500
|
|
|
|
}
|
|
|
|
gc.JSON(code, errors)
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Get jfa-go configuration.
|
|
|
|
// @Produce json
|
2021-01-05 18:16:23 +00:00
|
|
|
// @Success 200 {object} settings "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) {
|
|
|
|
app.info.Println("Config requested")
|
2021-01-05 18:16:23 +00:00
|
|
|
resp := app.configBase
|
|
|
|
// Load language options
|
2021-01-31 18:50:04 +00:00
|
|
|
formOptions := app.storage.lang.Form.getOptions()
|
2021-01-12 23:37:22 +00:00
|
|
|
fl := resp.Sections["ui"].Settings["language-form"]
|
|
|
|
fl.Options = formOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
fl.Value = app.config.Section("ui").Key("language-form").MustString("en-us")
|
2021-03-30 21:41:28 +00:00
|
|
|
pwrOptions := app.storage.lang.PasswordReset.getOptions()
|
|
|
|
pl := resp.Sections["password_resets"].Settings["language"]
|
|
|
|
pl.Options = pwrOptions
|
|
|
|
pl.Value = app.config.Section("password_resets").Key("language").MustString("en-us")
|
2021-01-31 18:50:04 +00:00
|
|
|
adminOptions := app.storage.lang.Admin.getOptions()
|
2021-01-12 23:37:22 +00:00
|
|
|
al := resp.Sections["ui"].Settings["language-admin"]
|
|
|
|
al.Options = adminOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
al.Value = app.config.Section("ui").Key("language-admin").MustString("en-us")
|
|
|
|
emailOptions := app.storage.lang.Email.getOptions()
|
2021-01-14 20:24:28 +00:00
|
|
|
el := resp.Sections["email"].Settings["language"]
|
|
|
|
el.Options = emailOptions
|
2021-01-31 18:50:04 +00:00
|
|
|
el.Value = app.config.Section("email").Key("language").MustString("en-us")
|
2021-03-20 23:32:32 +00:00
|
|
|
if updater == "" {
|
|
|
|
delete(resp.Sections, "updates")
|
|
|
|
for i, v := range resp.Order {
|
|
|
|
if v == "updates" {
|
|
|
|
resp.Order = append(resp.Order[:i], resp.Order[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
for sectName, section := range resp.Sections {
|
|
|
|
for settingName, setting := range section.Settings {
|
|
|
|
val := app.config.Section(sectName).Key(settingName)
|
|
|
|
s := resp.Sections[sectName].Settings[settingName]
|
|
|
|
switch setting.Type {
|
|
|
|
case "text", "email", "select", "password":
|
|
|
|
s.Value = val.MustString("")
|
|
|
|
case "number":
|
|
|
|
s.Value = val.MustInt(0)
|
|
|
|
case "bool":
|
|
|
|
s.Value = val.MustBool(false)
|
2020-07-31 15:09:30 +00:00
|
|
|
}
|
2021-01-05 18:16:23 +00:00
|
|
|
resp.Sections[sectName].Settings[settingName] = s
|
2020-07-31 15:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-12 23:37:22 +00:00
|
|
|
resp.Sections["ui"].Settings["language-form"] = fl
|
|
|
|
resp.Sections["ui"].Settings["language-admin"] = al
|
2021-01-14 20:24:28 +00:00
|
|
|
resp.Sections["email"].Settings["language"] = el
|
2021-03-30 21:41:28 +00:00
|
|
|
resp.Sections["password_resets"].Settings["language"] = pl
|
2021-01-09 20:38:13 +00:00
|
|
|
|
2020-07-31 15:09:30 +00:00
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
|
2020-09-24 16:51:13 +00:00
|
|
|
// @Summary Modify app config.
|
|
|
|
// @Produce json
|
|
|
|
// @Param appConfig body configDTO true "Config split into sections as in config.ini, all values as strings."
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @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) {
|
|
|
|
app.info.Println("Config modification requested")
|
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.
|
2020-11-22 16:36:43 +00:00
|
|
|
tempConfig, _ := ini.Load(app.configPath)
|
2020-07-31 21:07:09 +00:00
|
|
|
for section, settings := range req {
|
2020-09-23 16:20:48 +00:00
|
|
|
if section != "restart-program" {
|
|
|
|
_, err := tempConfig.GetSection(section)
|
|
|
|
if err != nil {
|
|
|
|
tempConfig.NewSection(section)
|
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
for setting, value := range settings.(map[string]interface{}) {
|
2021-01-31 18:50:04 +00:00
|
|
|
if value.(string) != app.config.Section(section).Key(setting).MustString("") {
|
2020-11-03 21:11:43 +00:00
|
|
|
tempConfig.Section(section).Key(setting).SetValue(value.(string))
|
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
tempConfig.SaveTo(app.configPath)
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Println("Config saved")
|
2020-07-31 21:07:09 +00:00
|
|
|
gc.JSON(200, map[string]bool{"success": true})
|
2020-09-22 19:46:48 +00:00
|
|
|
if req["restart-program"] != nil && req["restart-program"].(bool) {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Println("Restarting...")
|
|
|
|
err := app.Restart()
|
2020-08-01 23:05:35 +00:00
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Couldn't restart, try restarting manually: %s", err)
|
2020-08-01 23:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.loadConfig()
|
2020-08-02 23:12:45 +00:00
|
|
|
// Reinitialize password validator on config change, as opposed to every applicable request like in python.
|
|
|
|
if _, ok := req["password_validation"]; ok {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.debug.Println("Reinitializing validator")
|
2020-08-02 23:12:45 +00:00
|
|
|
validatorConf := ValidatorConf{
|
2021-01-05 18:16:23 +00:00
|
|
|
"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),
|
2020-08-02 23:12:45 +00:00
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
if !app.config.Section("password_validation").Key("enabled").MustBool(false) {
|
2020-08-02 23:12:45 +00:00
|
|
|
for key := range validatorConf {
|
|
|
|
validatorConf[key] = 0
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
app.validator.init(validatorConf)
|
2020-08-02 23:12:45 +00:00
|
|
|
}
|
2020-07-31 21:07:09 +00:00
|
|
|
}
|
2020-08-01 23:05:35 +00:00
|
|
|
|
2021-02-19 21:38:20 +00:00
|
|
|
// @Summary Get a list of email names and IDs.
|
|
|
|
// @Produce json
|
2021-02-28 15:41:06 +00:00
|
|
|
// @Param lang query string false "Language for email titles."
|
2021-02-19 21:38:20 +00:00
|
|
|
// @Success 200 {object} emailListDTO
|
|
|
|
// @Router /config/emails [get]
|
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) GetEmails(gc *gin.Context) {
|
2021-02-28 15:41:06 +00:00
|
|
|
lang := gc.Query("lang")
|
|
|
|
if _, ok := app.storage.lang.Email[lang]; !ok {
|
|
|
|
lang = app.storage.lang.chosenEmailLang
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
gc.JSON(200, emailListDTO{
|
2021-02-28 15:41:06 +00:00
|
|
|
"UserCreated": {Name: app.storage.lang.Email[lang].UserCreated["name"], Enabled: app.storage.customEmails.UserCreated.Enabled},
|
|
|
|
"InviteExpiry": {Name: app.storage.lang.Email[lang].InviteExpiry["name"], Enabled: app.storage.customEmails.InviteExpiry.Enabled},
|
|
|
|
"PasswordReset": {Name: app.storage.lang.Email[lang].PasswordReset["name"], Enabled: app.storage.customEmails.PasswordReset.Enabled},
|
|
|
|
"UserDeleted": {Name: app.storage.lang.Email[lang].UserDeleted["name"], Enabled: app.storage.customEmails.UserDeleted.Enabled},
|
2021-04-12 20:28:36 +00:00
|
|
|
"UserDisabled": {Name: app.storage.lang.Email[lang].UserDisabled["name"], Enabled: app.storage.customEmails.UserDisabled.Enabled},
|
|
|
|
"UserEnabled": {Name: app.storage.lang.Email[lang].UserEnabled["name"], Enabled: app.storage.customEmails.UserEnabled.Enabled},
|
2021-02-28 15:41:06 +00:00
|
|
|
"InviteEmail": {Name: app.storage.lang.Email[lang].InviteEmail["name"], Enabled: app.storage.customEmails.InviteEmail.Enabled},
|
|
|
|
"WelcomeEmail": {Name: app.storage.lang.Email[lang].WelcomeEmail["name"], Enabled: app.storage.customEmails.WelcomeEmail.Enabled},
|
|
|
|
"EmailConfirmation": {Name: app.storage.lang.Email[lang].EmailConfirmation["name"], Enabled: app.storage.customEmails.EmailConfirmation.Enabled},
|
|
|
|
"UserExpired": {Name: app.storage.lang.Email[lang].UserExpired["name"], Enabled: app.storage.customEmails.UserExpired.Enabled},
|
2021-02-19 21:38:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-20 00:22:40 +00:00
|
|
|
// @Summary Sets the corresponding custom email.
|
|
|
|
// @Produce json
|
|
|
|
// @Param customEmail body customEmail true "Content = email (in markdown)."
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /config/emails/{id} [post]
|
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) SetEmail(gc *gin.Context) {
|
|
|
|
var req customEmail
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
id := gc.Param("id")
|
|
|
|
if req.Content == "" {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if id == "UserCreated" {
|
|
|
|
app.storage.customEmails.UserCreated.Content = req.Content
|
|
|
|
app.storage.customEmails.UserCreated.Enabled = true
|
|
|
|
} else if id == "InviteExpiry" {
|
|
|
|
app.storage.customEmails.InviteExpiry.Content = req.Content
|
|
|
|
app.storage.customEmails.InviteExpiry.Enabled = true
|
|
|
|
} else if id == "PasswordReset" {
|
|
|
|
app.storage.customEmails.PasswordReset.Content = req.Content
|
|
|
|
app.storage.customEmails.PasswordReset.Enabled = true
|
|
|
|
} else if id == "UserDeleted" {
|
|
|
|
app.storage.customEmails.UserDeleted.Content = req.Content
|
|
|
|
app.storage.customEmails.UserDeleted.Enabled = true
|
2021-04-12 20:28:36 +00:00
|
|
|
} else if id == "UserDisabled" {
|
|
|
|
app.storage.customEmails.UserDisabled.Content = req.Content
|
|
|
|
app.storage.customEmails.UserDisabled.Enabled = true
|
|
|
|
} else if id == "UserEnabled" {
|
|
|
|
app.storage.customEmails.UserEnabled.Content = req.Content
|
|
|
|
app.storage.customEmails.UserEnabled.Enabled = true
|
2021-02-20 00:22:40 +00:00
|
|
|
} else if id == "InviteEmail" {
|
|
|
|
app.storage.customEmails.InviteEmail.Content = req.Content
|
|
|
|
app.storage.customEmails.InviteEmail.Enabled = true
|
|
|
|
} else if id == "WelcomeEmail" {
|
|
|
|
app.storage.customEmails.WelcomeEmail.Content = req.Content
|
|
|
|
app.storage.customEmails.WelcomeEmail.Enabled = true
|
|
|
|
} else if id == "EmailConfirmation" {
|
|
|
|
app.storage.customEmails.EmailConfirmation.Content = req.Content
|
|
|
|
app.storage.customEmails.EmailConfirmation.Enabled = true
|
2021-02-28 15:41:06 +00:00
|
|
|
} else if id == "UserExpired" {
|
|
|
|
app.storage.customEmails.UserExpired.Content = req.Content
|
|
|
|
app.storage.customEmails.UserExpired.Enabled = true
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.storage.storeCustomEmails() != nil {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Enable/Disable custom email.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
2021-02-21 15:51:42 +00:00
|
|
|
// @Router /config/emails/{id}/state/{enable/disable} [post]
|
2021-02-20 00:22:40 +00:00
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) SetEmailState(gc *gin.Context) {
|
|
|
|
id := gc.Param("id")
|
|
|
|
s := gc.Param("state")
|
|
|
|
enabled := false
|
|
|
|
if s == "enable" {
|
|
|
|
enabled = true
|
|
|
|
} else if s != "disable" {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
if id == "UserCreated" {
|
|
|
|
app.storage.customEmails.UserCreated.Enabled = enabled
|
|
|
|
} else if id == "InviteExpiry" {
|
|
|
|
app.storage.customEmails.InviteExpiry.Enabled = enabled
|
|
|
|
} else if id == "PasswordReset" {
|
|
|
|
app.storage.customEmails.PasswordReset.Enabled = enabled
|
|
|
|
} else if id == "UserDeleted" {
|
|
|
|
app.storage.customEmails.UserDeleted.Enabled = enabled
|
2021-04-12 20:28:36 +00:00
|
|
|
} else if id == "UserDisabled" {
|
|
|
|
app.storage.customEmails.UserDisabled.Enabled = enabled
|
|
|
|
} else if id == "UserEnabled" {
|
|
|
|
app.storage.customEmails.UserEnabled.Enabled = enabled
|
2021-02-20 00:22:40 +00:00
|
|
|
} else if id == "InviteEmail" {
|
|
|
|
app.storage.customEmails.InviteEmail.Enabled = enabled
|
|
|
|
} else if id == "WelcomeEmail" {
|
|
|
|
app.storage.customEmails.WelcomeEmail.Enabled = enabled
|
|
|
|
} else if id == "EmailConfirmation" {
|
|
|
|
app.storage.customEmails.EmailConfirmation.Enabled = enabled
|
2021-02-28 15:41:06 +00:00
|
|
|
} else if id == "UserExpired" {
|
|
|
|
app.storage.customEmails.UserExpired.Enabled = enabled
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.storage.storeCustomEmails() != nil {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
// @Summary Returns the custom email (generating it if not set) and list of used variables in it.
|
2021-02-20 22:49:59 +00:00
|
|
|
// @Produce json
|
2021-02-22 00:43:36 +00:00
|
|
|
// @Success 200 {object} customEmailDTO
|
2021-02-20 00:22:40 +00:00
|
|
|
// @Failure 400 {object} boolResponse
|
2021-02-19 21:38:20 +00:00
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /config/emails/{id} [get]
|
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) GetEmail(gc *gin.Context) {
|
2021-02-28 15:41:06 +00:00
|
|
|
lang := app.storage.lang.chosenEmailLang
|
2021-02-19 21:38:20 +00:00
|
|
|
id := gc.Param("id")
|
|
|
|
var content string
|
|
|
|
var err error
|
|
|
|
var msg *Email
|
2021-02-20 00:22:40 +00:00
|
|
|
var variables []string
|
2021-02-22 00:43:36 +00:00
|
|
|
var values map[string]interface{}
|
2021-02-20 00:22:40 +00:00
|
|
|
var writeVars func(variables []string)
|
|
|
|
newEmail := false
|
2021-02-28 15:41:06 +00:00
|
|
|
username := app.storage.lang.Email[lang].Strings.get("username")
|
|
|
|
emailAddress := app.storage.lang.Email[lang].Strings.get("emailAddress")
|
2021-02-19 21:38:20 +00:00
|
|
|
if id == "UserCreated" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.UserCreated.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructCreated("", "", "", Invite{}, app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.UserCreated.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.UserCreated.Variables = variables }
|
2021-02-22 00:43:36 +00:00
|
|
|
values = app.email.createdValues("xxxxxx", username, emailAddress, Invite{}, app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.UserCreated = content
|
|
|
|
} else if id == "InviteExpiry" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.InviteExpiry.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructExpiry("", Invite{}, app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.InviteExpiry.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.InviteExpiry.Variables = variables }
|
2021-02-22 00:43:36 +00:00
|
|
|
values = app.email.expiryValues("xxxxxx", Invite{}, app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.InviteExpiry = content
|
|
|
|
} else if id == "PasswordReset" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.PasswordReset.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructReset(PasswordReset{}, app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.PasswordReset.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.PasswordReset.Variables = variables }
|
2021-02-22 00:43:36 +00:00
|
|
|
values = app.email.resetValues(PasswordReset{Pin: "12-34-56", Username: username}, app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.PasswordReset = content
|
|
|
|
} else if id == "UserDeleted" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.UserDeleted.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructDeleted("", app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.UserDeleted.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.UserDeleted.Variables = variables }
|
2021-04-12 20:28:36 +00:00
|
|
|
values = app.email.deletedValues(app.storage.lang.Email[lang].Strings.get("reason"), app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.UserDeleted = content
|
2021-04-12 20:28:36 +00:00
|
|
|
} else if id == "UserDisabled" {
|
|
|
|
content = app.storage.customEmails.UserDisabled.Content
|
|
|
|
if content == "" {
|
|
|
|
newEmail = true
|
|
|
|
msg, err = app.email.constructDisabled("", app, true)
|
|
|
|
content = msg.Text
|
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.UserDisabled.Variables
|
|
|
|
}
|
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.UserDisabled.Variables = variables }
|
|
|
|
values = app.email.deletedValues(app.storage.lang.Email[lang].Strings.get("reason"), app, false)
|
|
|
|
// app.storage.customEmails.UserDeleted = content
|
|
|
|
} else if id == "UserEnabled" {
|
|
|
|
content = app.storage.customEmails.UserEnabled.Content
|
|
|
|
if content == "" {
|
|
|
|
newEmail = true
|
|
|
|
msg, err = app.email.constructEnabled("", app, true)
|
|
|
|
content = msg.Text
|
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.UserEnabled.Variables
|
|
|
|
}
|
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.UserEnabled.Variables = variables }
|
|
|
|
values = app.email.deletedValues(app.storage.lang.Email[lang].Strings.get("reason"), app, false)
|
|
|
|
// app.storage.customEmails.UserEnabled = content
|
2021-02-19 21:38:20 +00:00
|
|
|
} else if id == "InviteEmail" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.InviteEmail.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructInvite("", Invite{}, app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.InviteEmail.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.InviteEmail.Variables = variables }
|
2021-02-22 00:43:36 +00:00
|
|
|
values = app.email.inviteValues("xxxxxx", Invite{}, app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.InviteEmail = content
|
|
|
|
} else if id == "WelcomeEmail" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.WelcomeEmail.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-04-08 13:20:13 +00:00
|
|
|
msg, err = app.email.constructWelcome("", time.Time{}, app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.WelcomeEmail.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.WelcomeEmail.Variables = variables }
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.WelcomeEmail = content
|
2021-04-08 13:20:13 +00:00
|
|
|
values = app.email.welcomeValues(username, time.Time{}, app, false, true)
|
2021-02-19 21:38:20 +00:00
|
|
|
} else if id == "EmailConfirmation" {
|
2021-02-20 00:22:40 +00:00
|
|
|
content = app.storage.customEmails.EmailConfirmation.Content
|
2021-02-19 21:38:20 +00:00
|
|
|
if content == "" {
|
2021-02-20 00:22:40 +00:00
|
|
|
newEmail = true
|
2021-02-19 21:38:20 +00:00
|
|
|
msg, err = app.email.constructConfirmation("", "", "", app, true)
|
2021-02-20 22:49:59 +00:00
|
|
|
content = msg.Text
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.EmailConfirmation.Variables
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.EmailConfirmation.Variables = variables }
|
2021-02-22 00:43:36 +00:00
|
|
|
values = app.email.confirmationValues("xxxxxx", username, "xxxxxx", app, false)
|
2021-02-19 21:38:20 +00:00
|
|
|
// app.storage.customEmails.EmailConfirmation = content
|
2021-02-28 15:41:06 +00:00
|
|
|
} else if id == "UserExpired" {
|
|
|
|
content = app.storage.customEmails.UserExpired.Content
|
|
|
|
if content == "" {
|
|
|
|
newEmail = true
|
|
|
|
msg, err = app.email.constructUserExpired(app, true)
|
|
|
|
content = msg.Text
|
|
|
|
} else {
|
|
|
|
variables = app.storage.customEmails.UserExpired.Variables
|
|
|
|
}
|
|
|
|
writeVars = func(variables []string) { app.storage.customEmails.UserExpired.Variables = variables }
|
|
|
|
values = app.email.userExpiredValues(app, false)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
if newEmail {
|
|
|
|
variables = make([]string, strings.Count(content, "{"))
|
|
|
|
i := 0
|
|
|
|
found := false
|
|
|
|
buf := ""
|
|
|
|
for _, c := range content {
|
|
|
|
if !found && c != '{' && c != '}' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
buf += string(c)
|
|
|
|
if c == '}' {
|
|
|
|
found = false
|
|
|
|
variables[i] = buf
|
|
|
|
buf = ""
|
|
|
|
i++
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
writeVars(variables)
|
|
|
|
}
|
2021-02-28 15:41:06 +00:00
|
|
|
if variables == nil {
|
|
|
|
variables = []string{}
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.storeCustomEmails() != nil {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
email, err := app.email.constructTemplate("", "<div id=\"preview-content\"></div>", app)
|
|
|
|
if err != nil {
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2021-02-22 16:40:37 +00:00
|
|
|
gc.JSON(200, customEmailDTO{Content: content, Variables: variables, Values: values, HTML: email.HTML, Plaintext: email.Text})
|
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]
|
|
|
|
// @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]
|
|
|
|
// @tags Configuration
|
|
|
|
func (app *appContext) ApplyUpdate(gc *gin.Context) {
|
|
|
|
if !app.update.CanUpdate {
|
|
|
|
respond(400, "Update is manual", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := app.update.update()
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to apply update: %v", err)
|
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
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]
|
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 {
|
|
|
|
app.debug.Printf("Couldn't get cookies: %s", err)
|
|
|
|
respond(500, "Couldn't fetch cookies", gc)
|
|
|
|
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-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-01-12 23:43:44 +00:00
|
|
|
if page == "form" {
|
|
|
|
for key, lang := range app.storage.lang.Form {
|
2021-01-19 00:29:29 +00:00
|
|
|
resp[key] = lang.Meta.Name
|
2021-01-12 23:43:44 +00:00
|
|
|
}
|
|
|
|
} else if page == "admin" {
|
|
|
|
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-01-26 22:57:29 +00:00
|
|
|
} else if page == "setup" {
|
|
|
|
for key, lang := range app.storage.lang.Setup {
|
|
|
|
resp[key] = lang.Meta.Name
|
|
|
|
}
|
|
|
|
} else if page == "email" {
|
|
|
|
for key, lang := range app.storage.lang.Email {
|
|
|
|
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-02-02 18:09:02 +00:00
|
|
|
// @Summary Restarts the program. No response means success.
|
|
|
|
// @Router /restart [post]
|
|
|
|
// @tags Other
|
2021-01-31 19:01:20 +00:00
|
|
|
func (app *appContext) restart(gc *gin.Context) {
|
|
|
|
app.info.Println("Restarting...")
|
|
|
|
err := app.Restart()
|
|
|
|
if err != nil {
|
2021-04-02 21:13:04 +00:00
|
|
|
app.err.Printf("Couldn't restart, try restarting manually: %v", err)
|
2021-01-31 19:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
} else if page == "form" {
|
|
|
|
gc.JSON(200, app.storage.lang.Form[lang])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-09-08 22:08:50 +00:00
|
|
|
RESTART <- true
|
|
|
|
return nil
|
2020-08-01 23:05:35 +00:00
|
|
|
}
|