2020-07-29 21:11:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-16 20:42:22 +00:00
|
|
|
"encoding/json"
|
2020-07-29 21:11:28 +00:00
|
|
|
"fmt"
|
2020-09-21 23:34:11 +00:00
|
|
|
"strconv"
|
2020-09-05 16:32:49 +00:00
|
|
|
"strings"
|
2020-07-29 21:11:28 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/knz/strtime"
|
|
|
|
"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) {
|
|
|
|
date, _ = strtime.Strftime(dt, app.datePattern)
|
|
|
|
time, _ = strtime.Strftime(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
|
|
|
|
if app.config.Section("notifications").Key("enabled").MustBool(false) && len(notify) != 0 {
|
|
|
|
app.debug.Printf("%s: Expiry notification", code)
|
|
|
|
for address, settings := range notify {
|
|
|
|
if !settings["notify-expiry"] {
|
|
|
|
continue
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
go func() {
|
|
|
|
msg, err := app.email.constructExpiry(code, data, app)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("%s: Failed to construct expiry notification", code)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
} else if err := app.email.send(address, msg); err != nil {
|
|
|
|
app.err.Printf("%s: Failed to send expiry notification", code)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
} else {
|
|
|
|
app.info.Printf("Sent expiry notification to %s", address)
|
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
|
if app.config.Section("notifications").Key("enabled").MustBool(false) && len(notify) != 0 {
|
|
|
|
app.debug.Printf("%s: Expiry notification", code)
|
|
|
|
for address, settings := range notify {
|
|
|
|
if settings["notify-expiry"] {
|
|
|
|
go func() {
|
|
|
|
msg, err := app.email.constructExpiry(code, inv, app)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("%s: Failed to construct expiry notification", code)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
} else if err := app.email.send(address, msg); err != nil {
|
|
|
|
app.err.Printf("%s: Failed to send expiry notification", code)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
} 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?
|
|
|
|
newInv.RemainingUses -= 1
|
2020-08-01 14:22:30 +00:00
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
newInv.UsedBy = append(newInv.UsedBy, []string{username, app.formatDatetime(currentTime)})
|
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
|
|
|
|
}
|
|
|
|
username := jfUser["Name"].(string)
|
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)
|
2020-09-17 23:59:59 +00:00
|
|
|
if existingUser != nil {
|
|
|
|
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 {
|
|
|
|
app.err.Printf("%s New user failed: Jellyfin responded with %d", req.Username, status)
|
2021-01-24 15:19:58 +00:00
|
|
|
respondUser(401, false, false, "Unknown error", gc)
|
2020-09-17 23:59:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if user["Id"] != nil {
|
|
|
|
id = user["Id"].(string)
|
|
|
|
}
|
|
|
|
if len(app.storage.policy) != 0 {
|
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) {
|
2020-09-17 23:59:59 +00:00
|
|
|
app.err.Printf("%s: Failed to set user policy: Code %d", req.Username, status)
|
2020-11-22 16:36:43 +00:00
|
|
|
app.debug.Printf("%s: Error: %s", req.Username, err)
|
2020-09-17 23:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(app.storage.configuration) != 0 && 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) {
|
2020-09-17 23:59:59 +00:00
|
|
|
app.err.Printf("%s: Failed to set configuration template: Code %d", req.Username, status)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
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-01-24 15:19:58 +00:00
|
|
|
if app.config.Section("welcome_email").Key("enabled").MustBool(false) && req.Email != "" {
|
|
|
|
app.debug.Printf("%s: Sending welcome email to %s", req.Username, req.Email)
|
|
|
|
msg, err := app.email.constructWelcome(req.Username, app)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("%s: Failed to construct welcome email: %s", req.Username, err)
|
|
|
|
respondUser(500, true, false, err.Error(), gc)
|
|
|
|
return
|
|
|
|
} else if err := app.email.send(req.Email, msg); err != nil {
|
|
|
|
app.err.Printf("%s: Failed to send welcome email: %s", req.Username, err)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-24 16:51:13 +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]
|
2020-09-24 17:50:03 +00:00
|
|
|
// @tags Users
|
2020-08-16 12:36:54 +00:00
|
|
|
func (app *appContext) NewUser(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req newUserDTO
|
2020-07-29 21:11:28 +00:00
|
|
|
gc.BindJSON(&req)
|
2020-08-16 12:36:54 +00:00
|
|
|
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)
|
2020-09-24 16:51:13 +00:00
|
|
|
respondBool(401, false, gc)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-08-16 12:36:54 +00:00
|
|
|
validation := app.validator.validate(req.Password)
|
2020-07-29 21:11:28 +00:00
|
|
|
valid := true
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
// 200 bcs idk what i did in js
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("%s New user failed: Invalid password", req.Code)
|
2020-07-29 21:11:28 +00:00
|
|
|
gc.JSON(200, validation)
|
|
|
|
gc.Abort()
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
existingUser, _, _ := app.jf.UserByName(req.Username, false)
|
2020-07-29 21:11:28 +00:00
|
|
|
if existingUser != nil {
|
2020-07-31 21:07:09 +00:00
|
|
|
msg := fmt.Sprintf("User already exists named %s", req.Username)
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("%s New user failed: %s", req.Code, msg)
|
2020-07-31 21:07:09 +00:00
|
|
|
respond(401, msg, gc)
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
2020-07-29 21:11:28 +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 {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s New user failed: Jellyfin responded with %d", req.Code, status)
|
2020-07-29 21:11:28 +00:00
|
|
|
respond(401, "Unknown error", gc)
|
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)
|
2020-08-16 12:36:54 +00:00
|
|
|
if 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() {
|
2020-09-13 20:18:47 +00:00
|
|
|
msg, err := app.email.constructCreated(req.Code, req.Username, req.Email, invite, app)
|
|
|
|
if err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s: Failed to construct user creation notification", req.Code)
|
|
|
|
app.debug.Printf("%s: Error: %s", req.Code, err)
|
2020-09-13 20:18:47 +00:00
|
|
|
} else if err := app.email.send(address, msg); err != nil {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("%s: Failed to send user creation notification", req.Code)
|
|
|
|
app.debug.Printf("%s: Error: %s", req.Code, err)
|
2020-08-02 16:17:29 +00:00
|
|
|
} else {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.info.Printf("%s: Sent user creation notification to %s", req.Code, address)
|
2020-08-02 16:17:29 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if user["Id"] != nil {
|
|
|
|
id = user["Id"].(string)
|
2020-07-29 21:11:28 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-09-20 10:21:04 +00:00
|
|
|
if len(profile.Policy) != 0 {
|
|
|
|
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) {
|
2020-09-20 10:21:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set user policy: Code %d", req.Code, status)
|
2020-11-22 16:36:43 +00:00
|
|
|
app.debug.Printf("%s: Error: %s", req.Code, err)
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(profile.Configuration) != 0 && len(profile.Displayprefs) != 0 {
|
|
|
|
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) {
|
2020-09-20 10:21:04 +00:00
|
|
|
app.err.Printf("%s: Failed to set configuration template: Code %d", req.Code, status)
|
2020-11-22 16:36:43 +00:00
|
|
|
app.debug.Printf("%s: Error: %s", req.Code, 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-01-24 15:19:58 +00:00
|
|
|
if app.config.Section("welcome_email").Key("enabled").MustBool(false) && req.Email != "" {
|
|
|
|
app.debug.Printf("%s: Sending welcome email to %s", req.Username, req.Email)
|
|
|
|
msg, err := app.email.constructWelcome(req.Username, app)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("%s: Failed to construct welcome email: %s", req.Username, err)
|
|
|
|
} else if err := app.email.send(req.Email, msg); err != nil {
|
|
|
|
app.err.Printf("%s: Failed to send welcome email: %s", req.Username, err)
|
|
|
|
} else {
|
|
|
|
app.info.Printf("%s: Sent welcome email to %s", req.Username, req.Email)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
2020-09-17 22:50:07 +00:00
|
|
|
func (app *appContext) DeleteUser(gc *gin.Context) {
|
2020-09-24 16:51:13 +00:00
|
|
|
var req deleteUserDTO
|
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 {
|
|
|
|
app.err.Printf("Failed to delete ombi user: %d %s", status, err)
|
|
|
|
errors[userID] = fmt.Sprintf("Ombi: %d %s, ", status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2020-10-29 16:03:00 +00:00
|
|
|
msg := fmt.Sprintf("%d: %s", status, err)
|
|
|
|
if _, ok := errors[userID]; !ok {
|
|
|
|
errors[userID] = msg
|
|
|
|
} else {
|
|
|
|
errors[userID] += msg
|
|
|
|
}
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
if req.Notify {
|
|
|
|
addr, ok := app.storage.emails[userID]
|
|
|
|
if addr != nil && ok {
|
|
|
|
go func(userID, reason, address string) {
|
|
|
|
msg, err := app.email.constructDeleted(reason, app)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("%s: Failed to construct account deletion email", userID)
|
|
|
|
app.debug.Printf("%s: Error: %s", userID, err)
|
|
|
|
} else if err := app.email.send(address, msg); err != nil {
|
|
|
|
app.err.Printf("%s: Failed to send to %s", userID, address)
|
|
|
|
app.debug.Printf("%s: Error: %s", userID, err)
|
|
|
|
} else {
|
2021-01-14 17:51:12 +00:00
|
|
|
app.info.Printf("%s: Sent deletion email to %s", userID, address)
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
}(userID, req.Reason, addr.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
validTill := currentTime.AddDate(0, 0, req.Days)
|
|
|
|
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
|
|
|
|
}
|
2020-11-22 16:36:43 +00:00
|
|
|
invite.ValidTill = validTill
|
2020-08-16 12:36:54 +00:00
|
|
|
if 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
|
2020-11-22 16:36:43 +00:00
|
|
|
msg, err := app.email.constructInvite(inviteCode, invite, app)
|
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)
|
2020-11-22 16:36:43 +00:00
|
|
|
app.err.Printf("%s: Failed to construct invite email", inviteCode)
|
|
|
|
app.debug.Printf("%s: Error: %s", inviteCode, err)
|
2020-09-13 20:18:47 +00:00
|
|
|
} else if err := app.email.send(req.Email, msg); err != nil {
|
2020-07-31 11:48:37 +00:00
|
|
|
invite.Email = fmt.Sprintf("Failed to send to %s", req.Email)
|
2020-11-22 16:36:43 +00:00
|
|
|
app.err.Printf("%s: %s", inviteCode, invite.Email)
|
|
|
|
app.debug.Printf("%s: Error: %s", inviteCode, err)
|
2020-07-31 21:07:09 +00:00
|
|
|
} else {
|
2020-11-22 16:36:43 +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)
|
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 {
|
|
|
|
app.err.Printf("Failed to get user from Jellyfin: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
profile := Profile{
|
|
|
|
FromUser: user["Name"].(string),
|
|
|
|
Policy: user["Policy"].(map[string]interface{}),
|
|
|
|
}
|
|
|
|
app.debug.Printf("Creating profile from user \"%s\"", user["Name"].(string))
|
|
|
|
if req.Homescreen {
|
|
|
|
profile.Configuration = user["Configuration"].(map[string]interface{})
|
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 {
|
|
|
|
app.err.Printf("Failed to get DisplayPrefs: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
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 {
|
2020-10-22 16:50:40 +00:00
|
|
|
_, _, days, hours, minutes, _ := timeDiff(inv.ValidTill, currentTime)
|
2020-09-24 16:51:13 +00:00
|
|
|
invite := inviteDTO{
|
|
|
|
Code: code,
|
|
|
|
Days: days,
|
|
|
|
Hours: hours,
|
|
|
|
Minutes: minutes,
|
|
|
|
Created: app.formatDatetime(inv.Created),
|
|
|
|
Profile: inv.Profile,
|
|
|
|
NoLimit: inv.NoLimit,
|
2021-01-24 15:55:45 +00:00
|
|
|
Label: inv.Label,
|
2020-09-20 10:21:04 +00:00
|
|
|
}
|
2020-07-29 21:11:28 +00:00
|
|
|
if len(inv.UsedBy) != 0 {
|
2020-09-24 16:51:13 +00:00
|
|
|
invite.UsedBy = inv.UsedBy
|
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-16 20:42:22 +00:00
|
|
|
type dateToParse struct {
|
|
|
|
Parsed time.Time `json:"parseme"`
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:49:50 +00:00
|
|
|
func parseDT(date string) time.Time {
|
|
|
|
// decent method
|
|
|
|
dt, err := time.Parse("2006-01-02T15:04:05.000000", date)
|
|
|
|
if err == nil {
|
|
|
|
return dt
|
|
|
|
}
|
2021-01-08 23:47:26 +00:00
|
|
|
// emby method
|
|
|
|
dt, err = time.Parse("2006-01-02T15:04:05.0000000+00:00", date)
|
|
|
|
if err == nil {
|
|
|
|
return dt
|
|
|
|
}
|
2020-12-03 20:49:50 +00:00
|
|
|
// magic method
|
|
|
|
// some stored dates from jellyfin have no timezone at the end, if not we assume UTC
|
|
|
|
if date[len(date)-1] != 'Z' {
|
|
|
|
date += "Z"
|
|
|
|
}
|
2020-09-16 20:42:22 +00:00
|
|
|
timeJSON := []byte("{ \"parseme\": \"" + date + "\" }")
|
|
|
|
var parsed dateToParse
|
2020-12-03 20:49:50 +00:00
|
|
|
// Magically turn it into a time.Time
|
2020-09-16 20:42:22 +00:00
|
|
|
json.Unmarshal(timeJSON, &parsed)
|
|
|
|
return parsed.Parsed
|
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-07-31 12:59:25 +00:00
|
|
|
resp.UserList = []respUser{}
|
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 {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to get users from Jellyfin: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
2020-07-31 12:59:25 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, jfUser := range users {
|
|
|
|
var user respUser
|
2020-09-16 20:42:22 +00:00
|
|
|
user.LastActive = "n/a"
|
|
|
|
if jfUser["LastActivityDate"] != nil {
|
2020-12-03 20:49:50 +00:00
|
|
|
date := parseDT(jfUser["LastActivityDate"].(string))
|
2020-09-16 20:42:22 +00:00
|
|
|
user.LastActive = app.formatDatetime(date)
|
|
|
|
}
|
|
|
|
user.ID = jfUser["Id"].(string)
|
2020-07-31 12:59:25 +00:00
|
|
|
user.Name = jfUser["Name"].(string)
|
2020-09-16 20:42:22 +00:00
|
|
|
user.Admin = jfUser["Policy"].(map[string]interface{})["IsAdministrator"].(bool)
|
2020-08-16 12:36:54 +00:00
|
|
|
if email, ok := app.storage.emails[jfUser["Id"].(string)]; ok {
|
2020-07-31 12:59:25 +00:00
|
|
|
user.Email = email.(string)
|
|
|
|
}
|
2020-09-16 20:42:22 +00:00
|
|
|
|
2020-07-31 12:59:25 +00:00
|
|
|
resp.UserList = append(resp.UserList, user)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
app.err.Printf("Failed to get users from Ombi: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
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 {
|
|
|
|
app.err.Printf("Couldn't get user from Ombi: %d %s", code, err)
|
|
|
|
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 {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.err.Printf("Failed to get users from Jellyfin: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", 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 {
|
2020-10-30 21:13:13 +00:00
|
|
|
id := jfUser["Id"].(string)
|
|
|
|
if address, ok := req[id]; ok {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.storage.emails[jfUser["Id"].(string)] = 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 {
|
|
|
|
app.err.Printf("%s: Failed to change ombi email address: %d %s", ombiUser["userName"].(string), code, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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"
|
2020-09-17 15:51:19 +00:00
|
|
|
var policy, configuration, displayprefs map[string]interface{}
|
2020-09-22 23:01:07 +00:00
|
|
|
if req.From == "profile" {
|
|
|
|
app.storage.loadProfiles()
|
|
|
|
if _, ok := app.storage.profiles[req.Profile]; !ok || len(app.storage.profiles[req.Profile].Policy) == 0 {
|
|
|
|
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 {
|
2020-09-22 23:01:07 +00:00
|
|
|
if len(app.storage.profiles[req.Profile].Configuration) == 0 || len(app.storage.profiles[req.Profile].Displayprefs) == 0 {
|
|
|
|
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"
|
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 {
|
|
|
|
app.err.Printf("Failed to get user from Jellyfin: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
applyingFrom = "\"" + user["Name"].(string) + "\""
|
|
|
|
policy = user["Policy"].(map[string]interface{})
|
|
|
|
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 {
|
|
|
|
app.err.Printf("Failed to get DisplayPrefs: Code %d", status)
|
|
|
|
app.debug.Printf("Error: %s", err)
|
|
|
|
respond(500, "Couldn't get displayprefs", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configuration = user["Configuration"].(map[string]interface{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-19 00:29:29 +00:00
|
|
|
formChosen, formOptions := app.storage.lang.Form.getOptions(app.config.Section("ui").Key("language-form").MustString("en-us"))
|
2021-01-12 23:37:22 +00:00
|
|
|
fl := resp.Sections["ui"].Settings["language-form"]
|
|
|
|
fl.Options = formOptions
|
|
|
|
fl.Value = formChosen
|
2021-01-19 00:29:29 +00:00
|
|
|
adminChosen, adminOptions := app.storage.lang.Admin.getOptions(app.config.Section("ui").Key("language-admin").MustString("en-us"))
|
2021-01-12 23:37:22 +00:00
|
|
|
al := resp.Sections["ui"].Settings["language-admin"]
|
|
|
|
al.Options = adminOptions
|
|
|
|
al.Value = adminChosen
|
2021-01-19 00:29:29 +00:00
|
|
|
emailChosen, emailOptions := app.storage.lang.Email.getOptions(app.config.Section("email").Key("language").MustString("en-us"))
|
2021-01-14 20:24:28 +00:00
|
|
|
el := resp.Sections["email"].Settings["language"]
|
|
|
|
el.Options = emailOptions
|
|
|
|
el.Value = emailChosen
|
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-01-09 20:38:13 +00:00
|
|
|
|
|
|
|
t := resp.Sections["jellyfin"].Settings["type"]
|
|
|
|
opts := make([]string, len(serverTypes))
|
2021-01-19 00:29:29 +00:00
|
|
|
i := 0
|
2021-01-09 20:38:13 +00:00
|
|
|
for _, v := range serverTypes {
|
|
|
|
opts[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
t.Options = opts
|
|
|
|
t.Value = serverTypes[app.config.Section("jellyfin").Key("type").MustString("jellyfin")]
|
|
|
|
resp.Sections["jellyfin"].Settings["type"] = t
|
|
|
|
|
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)
|
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-12 23:37:22 +00:00
|
|
|
if section == "ui" && setting == "language-form" {
|
2021-01-11 19:17:43 +00:00
|
|
|
for key, lang := range app.storage.lang.Form {
|
2021-01-19 00:29:29 +00:00
|
|
|
if lang.Meta.Name == value.(string) {
|
2021-01-12 23:37:22 +00:00
|
|
|
tempConfig.Section("ui").Key("language-form").SetValue(key)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if section == "ui" && setting == "language-admin" {
|
|
|
|
for key, lang := range app.storage.lang.Admin {
|
2021-01-19 00:29:29 +00:00
|
|
|
if lang.Meta.Name == value.(string) {
|
2021-01-12 23:37:22 +00:00
|
|
|
tempConfig.Section("ui").Key("language-admin").SetValue(key)
|
2020-11-03 21:11:43 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 20:24:28 +00:00
|
|
|
} else if section == "email" && setting == "language" {
|
2021-01-19 00:29:29 +00:00
|
|
|
for key, lang := range app.storage.lang.Email {
|
|
|
|
if lang.Meta.Name == value.(string) {
|
2021-01-14 20:24:28 +00:00
|
|
|
tempConfig.Section("email").Key("language").SetValue(key)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 20:38:13 +00:00
|
|
|
} else if section == "jellyfin" && setting == "type" {
|
|
|
|
for k, v := range serverTypes {
|
|
|
|
if v == value.(string) {
|
|
|
|
tempConfig.Section("jellyfin").Key("type").SetValue(k)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 17:51:12 +00:00
|
|
|
} else 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 {
|
2020-08-16 12:36:54 +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
|
|
|
|
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
|
|
|
|
// @Router /lang [get]
|
|
|
|
// @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-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-08-01 23:05:35 +00:00
|
|
|
// func Restart() error {
|
|
|
|
// defer func() {
|
|
|
|
// if r := recover(); r != nil {
|
|
|
|
// os.Exit(0)
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
// cwd, err := os.Getwd()
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// args := os.Args
|
|
|
|
// // for _, key := range args {
|
|
|
|
// // fmt.Println(key)
|
|
|
|
// // }
|
|
|
|
// cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
// cmd.Stdout = os.Stdout
|
|
|
|
// cmd.Stderr = os.Stderr
|
|
|
|
// cmd.Dir = cwd
|
|
|
|
// err = cmd.Start()
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// // cmd.Process.Release()
|
|
|
|
// panic(fmt.Errorf("restarting"))
|
|
|
|
// }
|
|
|
|
|
2020-09-08 22:08:50 +00:00
|
|
|
// func (app *appContext) Restart() error {
|
|
|
|
// defer func() {
|
|
|
|
// if r := recover(); r != nil {
|
|
|
|
// signal.Notify(app.quit, os.Interrupt)
|
|
|
|
// <-app.quit
|
|
|
|
// }
|
|
|
|
// }()
|
|
|
|
// args := os.Args
|
|
|
|
// // After a single restart, args[0] gets messed up and isnt the real executable.
|
|
|
|
// // JFA_DEEP tells the new process its a child, and JFA_EXEC is the real executable
|
|
|
|
// if os.Getenv("JFA_DEEP") == "" {
|
|
|
|
// os.Setenv("JFA_DEEP", "1")
|
|
|
|
// os.Setenv("JFA_EXEC", args[0])
|
|
|
|
// }
|
|
|
|
// env := os.Environ()
|
|
|
|
// err := syscall.Exec(os.Getenv("JFA_EXEC"), []string{""}, env)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// panic(fmt.Errorf("restarting"))
|
|
|
|
// }
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|