2022-03-22 14:58:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-07-21 16:45:36 +00:00
|
|
|
"net/url"
|
2022-03-22 14:58:39 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/golang-jwt/jwt"
|
2024-07-30 15:36:59 +00:00
|
|
|
"github.com/hrfee/jfa-go/jellyseerr"
|
2024-07-31 17:49:52 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2022-03-22 14:58:39 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2023-10-19 17:14:40 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
2023-09-06 21:46:16 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2022-03-22 14:58:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// @Summary Creates a new Jellyfin user without an invite.
|
|
|
|
// @Produce json
|
|
|
|
// @Param newUserDTO body newUserDTO true "New user request object"
|
|
|
|
// @Success 200
|
2024-08-21 19:35:08 +00:00
|
|
|
// @Router /user [post]
|
2022-03-22 14:58:39 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
2024-08-03 20:23:59 +00:00
|
|
|
func (app *appContext) NewUserFromAdmin(gc *gin.Context) {
|
2022-03-22 14:58:39 +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()
|
|
|
|
}
|
|
|
|
var req newUserDTO
|
|
|
|
gc.BindJSON(&req)
|
2024-08-03 20:23:59 +00:00
|
|
|
|
|
|
|
profile := app.storage.GetDefaultProfile()
|
2024-08-04 12:40:16 +00:00
|
|
|
if req.Profile != "" && req.Profile != "none" {
|
|
|
|
if p, ok := app.storage.GetProfileKey(req.Profile); ok {
|
|
|
|
profile = p
|
|
|
|
}
|
|
|
|
}
|
2024-08-20 20:33:43 +00:00
|
|
|
nu /*wg*/, _ := app.NewUserPostVerification(NewUserParams{
|
2024-08-03 20:23:59 +00:00
|
|
|
Req: req,
|
|
|
|
SourceType: ActivityAdmin,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
ContextForIPLogging: gc,
|
|
|
|
Profile: &profile,
|
|
|
|
})
|
|
|
|
if !nu.Success {
|
|
|
|
nu.Log()
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
|
2024-11-05 22:53:09 +00:00
|
|
|
if emailEnabled && req.Email != "" {
|
|
|
|
emailStore := EmailAddress{
|
|
|
|
Addr: req.Email,
|
|
|
|
Contact: true,
|
|
|
|
}
|
|
|
|
app.storage.SetEmailsKey(nu.User.ID, emailStore)
|
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
welcomeMessageSentIfNecessary := true
|
|
|
|
if nu.Created {
|
|
|
|
welcomeMessageSentIfNecessary = app.WelcomeNewUser(nu.User, time.Time{})
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2023-10-19 17:14:40 +00:00
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
respondUser(nu.Status, nu.Created, welcomeMessageSentIfNecessary, nu.Message, gc)
|
2024-08-20 20:33:43 +00:00
|
|
|
// These don't need to complete anytime soon
|
|
|
|
// wg.Wait()
|
2024-08-03 20:23:59 +00:00
|
|
|
}
|
2023-10-19 17:14:40 +00:00
|
|
|
|
2024-08-03 20:23:59 +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
|
2024-08-21 19:35:08 +00:00
|
|
|
// @Router /user/invite [post]
|
2024-08-03 20:23:59 +00:00
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) NewUserFromInvite(gc *gin.Context) {
|
|
|
|
/*
|
|
|
|
-- STEPS --
|
|
|
|
- Validate CAPTCHA
|
|
|
|
- Validate Invite
|
|
|
|
- Validate Password
|
|
|
|
- a) Discord (Require, Verify, ExistingUser, ApplyRole)
|
|
|
|
b) Telegram (Require, Verify, ExistingUser)
|
|
|
|
c) Matrix (Require, Verify, ExistingUser)
|
|
|
|
d) Email (Require, Verify, ExistingUser) (only occurs here, PWRs are sent by mail so do this on their own, kinda)
|
|
|
|
*/
|
|
|
|
var req newUserDTO
|
|
|
|
gc.BindJSON(&req)
|
2023-06-26 11:57:16 +00:00
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
// Validate CAPTCHA
|
|
|
|
if app.config.Section("captcha").Key("enabled").MustBool(false) && !app.verifyCaptcha(req.Code, req.CaptchaID, req.CaptchaText, false) {
|
|
|
|
app.info.Printf(lm.FailedCreateUser, lm.Jellyfin, req.Username, lm.IncorrectCaptcha)
|
|
|
|
respond(400, "errorCaptcha", gc)
|
|
|
|
return
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// Validate Invite
|
|
|
|
if !app.checkInvite(req.Code, false, "") {
|
|
|
|
app.info.Printf(lm.FailedCreateUser, lm.Jellyfin, req.Username, fmt.Sprintf(lm.InvalidInviteCode, req.Code))
|
|
|
|
respond(401, "errorInvalidCode", gc)
|
|
|
|
return
|
2024-07-30 15:36:59 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// Validate Password
|
|
|
|
validation := app.validator.validate(req.Password)
|
|
|
|
valid := true
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
valid = false
|
|
|
|
break
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if !valid {
|
|
|
|
// 200 bcs idk what i did in js
|
|
|
|
gc.JSON(200, validation)
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
}
|
2024-10-11 15:38:19 +00:00
|
|
|
completeContactMethods := make([]ContactMethodKey, len(app.contactMethods))
|
2024-08-03 20:23:59 +00:00
|
|
|
for i, cm := range app.contactMethods {
|
|
|
|
completeContactMethods[i].PIN = cm.PIN(req)
|
|
|
|
if completeContactMethods[i].PIN == "" {
|
|
|
|
if cm.Required() {
|
|
|
|
app.info.Printf(lm.FailedLinkUser, cm.Name(), "?", req.Code, lm.AccountUnverified)
|
|
|
|
respond(401, fmt.Sprintf("error%sVerification", cm.Name()), gc)
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-03 20:23:59 +00:00
|
|
|
completeContactMethods[i].User, completeContactMethods[i].Verified = cm.UserVerified(completeContactMethods[i].PIN)
|
|
|
|
if !completeContactMethods[i].Verified {
|
|
|
|
app.info.Printf(lm.FailedLinkUser, cm.Name(), "?", req.Code, fmt.Sprintf(lm.InvalidPIN, completeContactMethods[i].PIN))
|
|
|
|
respond(401, "errorInvalidPIN", gc)
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if cm.UniqueRequired() && cm.Exists(completeContactMethods[i].User) {
|
|
|
|
app.debug.Printf(lm.FailedLinkUser, cm.Name(), completeContactMethods[i].User.Name(), req.Code, lm.AccountLinked)
|
|
|
|
respond(400, "errorAccountLinked", gc)
|
2023-06-24 16:01:52 +00:00
|
|
|
return
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if err := cm.PostVerificationTasks(completeContactMethods[i].PIN, completeContactMethods[i].User); err != nil {
|
|
|
|
app.err.Printf(lm.FailedLinkUser, cm.Name(), completeContactMethods[i].User.Name(), req.Code, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// Email (FIXME: Potentially make this a ContactMethodLinker)
|
|
|
|
if emailEnabled {
|
|
|
|
// Require
|
|
|
|
if app.config.Section("email").Key("required").MustBool(false) && !strings.Contains(req.Email, "@") {
|
|
|
|
respond(400, "errorNoEmail", gc)
|
|
|
|
return
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// ExistingUser
|
|
|
|
if app.config.Section("email").Key("require_unique").MustBool(false) && req.Email != "" && app.EmailAddressExists(req.Email) {
|
|
|
|
respond(400, "errorEmailLinked", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Verify
|
|
|
|
if app.config.Section("email_confirmation").Key("enabled").MustBool(false) {
|
|
|
|
claims := jwt.MapClaims{
|
|
|
|
"valid": true,
|
|
|
|
"invite": req.Code,
|
|
|
|
"exp": time.Now().Add(30 * time.Minute).Unix(),
|
|
|
|
"type": "confirmation",
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
tk := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
key, err := tk.SignedString([]byte(os.Getenv("JFA_SECRET")))
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf(lm.FailedSignJWT, err)
|
|
|
|
respond(500, "errorUnknown", gc)
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
if app.ConfirmationKeys == nil {
|
2024-10-11 15:38:19 +00:00
|
|
|
app.ConfirmationKeys = map[string]map[string]ConfirmationKey{}
|
2023-02-01 15:11:10 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
cKeys, ok := app.ConfirmationKeys[req.Code]
|
|
|
|
if !ok {
|
2024-10-11 15:38:19 +00:00
|
|
|
cKeys = map[string]ConfirmationKey{}
|
|
|
|
}
|
|
|
|
cKeys[key] = ConfirmationKey{
|
|
|
|
newUserDTO: req,
|
|
|
|
completeContactMethods: completeContactMethods,
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
app.confirmationKeysLock.Lock()
|
|
|
|
app.ConfirmationKeys[req.Code] = cKeys
|
|
|
|
app.confirmationKeysLock.Unlock()
|
|
|
|
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.EmailConfirmationRequired, req.Username)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(401, "confirmEmail", gc)
|
|
|
|
msg, err := app.email.constructConfirmation(req.Code, req.Username, key, app, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructConfirmationEmail, req.Code, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else if err := app.email.send(msg, req.Email); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendConfirmationEmail, req.Code, req.Email, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-10-11 10:45:18 +00:00
|
|
|
app.debug.Printf(lm.SentConfirmationEmail, req.Code, req.Email)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
return
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
invite, _ := app.storage.GetInvitesKey(req.Code)
|
|
|
|
|
|
|
|
sourceType, source := invite.Source()
|
|
|
|
|
|
|
|
var profile *Profile = nil
|
|
|
|
if invite.Profile != "" {
|
|
|
|
p, ok := app.storage.GetProfileKey(invite.Profile)
|
|
|
|
if !ok {
|
|
|
|
app.debug.Printf(lm.FailedGetProfile+lm.FallbackToDefault, invite.Profile)
|
|
|
|
p = app.storage.GetDefaultProfile()
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
profile = &p
|
|
|
|
}
|
|
|
|
|
2024-08-20 20:33:43 +00:00
|
|
|
nu /*wg*/, _ := app.NewUserPostVerification(NewUserParams{
|
2024-08-03 20:23:59 +00:00
|
|
|
Req: req,
|
|
|
|
SourceType: sourceType,
|
|
|
|
Source: source,
|
|
|
|
ContextForIPLogging: gc,
|
|
|
|
Profile: profile,
|
|
|
|
})
|
|
|
|
if !nu.Success {
|
|
|
|
nu.Log()
|
|
|
|
}
|
|
|
|
if !nu.Created {
|
|
|
|
respond(nu.Status, nu.Message, gc)
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
app.checkInvite(req.Code, true, req.Username)
|
2024-08-03 20:23:59 +00:00
|
|
|
|
2024-10-11 15:38:19 +00:00
|
|
|
app.PostNewUserFromInvite(nu, ConfirmationKey{newUserDTO: req, completeContactMethods: completeContactMethods}, profile, invite)
|
|
|
|
|
|
|
|
/*responseFunc, logFunc, success := app.NewUser(req, false, gc)
|
|
|
|
if !success {
|
|
|
|
logFunc()
|
|
|
|
responseFunc(gc)
|
|
|
|
return
|
|
|
|
}*/
|
|
|
|
code := 200
|
|
|
|
for _, val := range validation {
|
|
|
|
if !val {
|
|
|
|
code = 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gc.JSON(code, validation)
|
|
|
|
// These don't need to complete anytime soon
|
|
|
|
// wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostNewUserFromInvite attaches user details (e.g. contact method details) to a new user once they've been created from an invite.
|
|
|
|
func (app *appContext) PostNewUserFromInvite(nu NewUserData, req ConfirmationKey, profile *Profile, invite Invite) {
|
2024-08-03 20:23:59 +00:00
|
|
|
nonEmailContactMethodEnabled := false
|
2024-10-11 15:38:19 +00:00
|
|
|
for i, c := range req.completeContactMethods {
|
2024-08-03 20:23:59 +00:00
|
|
|
if c.Verified {
|
2024-10-11 15:38:19 +00:00
|
|
|
c.User.SetAllowContactFromDTO(req.newUserDTO)
|
2024-08-03 20:23:59 +00:00
|
|
|
if c.User.AllowContact() {
|
|
|
|
nonEmailContactMethodEnabled = true
|
|
|
|
}
|
|
|
|
app.contactMethods[i].DeleteVerifiedToken(c.PIN)
|
2024-08-04 12:40:16 +00:00
|
|
|
c.User.SetJellyfin(nu.User.ID)
|
2024-08-03 20:23:59 +00:00
|
|
|
c.User.Store(&(app.storage))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
referralsEnabled := profile != nil && profile.ReferralTemplateKey != "" && app.config.Section("user_page").Key("enabled").MustBool(false) && app.config.Section("user_page").Key("referrals").MustBool(false)
|
|
|
|
|
|
|
|
if (emailEnabled && req.Email != "") || invite.UserLabel != "" || referralsEnabled {
|
|
|
|
emailStore := EmailAddress{
|
2024-10-11 10:45:18 +00:00
|
|
|
Addr: req.Email,
|
|
|
|
Contact: (req.Email != ""),
|
|
|
|
Label: invite.UserLabel,
|
|
|
|
}
|
|
|
|
if profile != nil {
|
|
|
|
profile.ReferralTemplateKey = profile.ReferralTemplateKey
|
2024-08-03 20:23:59 +00:00
|
|
|
}
|
|
|
|
/// Ensures at least one contact method is enabled.
|
|
|
|
if nonEmailContactMethodEnabled {
|
|
|
|
emailStore.Contact = req.EmailContact
|
|
|
|
}
|
|
|
|
app.storage.SetEmailsKey(nu.User.ID, emailStore)
|
|
|
|
}
|
|
|
|
if emailEnabled {
|
|
|
|
if app.config.Section("notifications").Key("enabled").MustBool(false) {
|
|
|
|
for address, settings := range invite.Notify {
|
|
|
|
if !settings["notify-creation"] {
|
|
|
|
continue
|
|
|
|
}
|
2024-07-28 15:53:27 +00:00
|
|
|
go func(addr string) {
|
2022-03-22 14:58:39 +00:00
|
|
|
msg, err := app.email.constructCreated(req.Code, req.Username, req.Email, invite, app, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructCreationAdmin, req.Code, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-07-28 15:53:27 +00:00
|
|
|
// Check whether notify "addr" is an email address of Jellyfin ID
|
|
|
|
if strings.Contains(addr, "@") {
|
|
|
|
err = app.email.send(msg, addr)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-07-28 15:53:27 +00:00
|
|
|
err = app.sendByID(msg, addr)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendCreationAdmin, req.Code, addr, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-10-11 10:45:18 +00:00
|
|
|
app.debug.Printf(lm.SentCreationAdmin, req.Code, addr)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-28 15:53:27 +00:00
|
|
|
}(address)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 13:31:42 +00:00
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
if referralsEnabled {
|
|
|
|
refInv := Invite{}
|
|
|
|
/*err := */ app.storage.db.Get(profile.ReferralTemplateKey, &refInv)
|
|
|
|
// If UseReferralExpiry is enabled, create the ref now so the clock starts ticking
|
|
|
|
if refInv.UseReferralExpiry {
|
|
|
|
refInv.Code = GenerateInviteCode()
|
|
|
|
expiryDelta := refInv.ValidTill.Sub(refInv.Created)
|
|
|
|
refInv.Created = time.Now()
|
|
|
|
refInv.ValidTill = refInv.Created.Add(expiryDelta)
|
|
|
|
refInv.IsReferral = true
|
|
|
|
refInv.ReferrerJellyfinID = nu.User.ID
|
|
|
|
app.storage.SetInvitesKey(refInv.Code, refInv)
|
2023-09-07 13:31:42 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
expiry := time.Time{}
|
|
|
|
if invite.UserExpiry {
|
|
|
|
expiry = time.Now().AddDate(0, invite.UserMonths, invite.UserDays).Add(time.Duration((60*invite.UserHours)+invite.UserMinutes) * time.Minute)
|
2024-08-03 20:23:59 +00:00
|
|
|
app.storage.SetUserExpiryKey(nu.User.ID, UserExpiry{Expiry: expiry})
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
|
|
|
|
var discordUser *DiscordUser = nil
|
|
|
|
var telegramUser *TelegramUser = nil
|
|
|
|
if app.ombi.Enabled(app, profile) || app.js.Enabled(app, profile) {
|
|
|
|
// FIXME: figure these out in a nicer way? this relies on the current ordering,
|
|
|
|
// which may not be fixed.
|
|
|
|
if discordEnabled {
|
2024-10-11 15:38:19 +00:00
|
|
|
discordUser = req.completeContactMethods[0].User.(*DiscordUser)
|
2024-08-03 20:23:59 +00:00
|
|
|
if telegramEnabled {
|
2024-10-11 15:38:19 +00:00
|
|
|
telegramUser = req.completeContactMethods[1].User.(*TelegramUser)
|
2024-07-30 15:36:59 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
} else if telegramEnabled {
|
2024-10-11 15:38:19 +00:00
|
|
|
telegramUser = req.completeContactMethods[0].User.(*TelegramUser)
|
2024-07-30 15:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
|
|
|
|
for _, tps := range app.thirdPartyServices {
|
|
|
|
if !tps.Enabled(app, profile) {
|
|
|
|
continue
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// User already created, now we can link contact methods
|
2024-10-11 15:38:19 +00:00
|
|
|
err := tps.AddContactMethods(nu.User.ID, req.newUserDTO, discordUser, telegramUser)
|
2022-03-22 14:58:39 +00:00
|
|
|
if err != nil {
|
2024-08-03 20:23:59 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, tps.Name(), err)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 20:23:59 +00:00
|
|
|
app.WelcomeNewUser(nu.User, expiry)
|
2022-03-22 14:58:39 +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{},
|
|
|
|
}
|
|
|
|
sendMail := messagesEnabled
|
|
|
|
var msg *Message
|
|
|
|
var err error
|
|
|
|
if sendMail {
|
|
|
|
if req.Enabled {
|
|
|
|
msg, err = app.email.constructEnabled(req.Reason, app, false)
|
|
|
|
} else {
|
|
|
|
msg, err = app.email.constructDisabled(req.Reason, app, false)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructEnableDisableMessage, "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
sendMail = false
|
|
|
|
}
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
activityType := ActivityDisabled
|
|
|
|
if req.Enabled {
|
|
|
|
activityType = ActivityEnabled
|
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
for _, userID := range req.Users {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(userID, false)
|
|
|
|
if err != nil {
|
|
|
|
errors["GetUser"][user.ID] = err.Error()
|
2024-08-04 14:14:51 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-08-04 14:14:51 +00:00
|
|
|
err, _, _ = app.SetUserDisabled(user, !req.Enabled)
|
|
|
|
if err != nil {
|
|
|
|
errors["SetPolicy"][user.ID] = err.Error()
|
|
|
|
app.err.Printf(lm.FailedApplyTemplate, "policy", lm.Jellyfin, user.ID, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
|
|
|
// Record activity
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: activityType,
|
2024-08-04 14:14:51 +00:00
|
|
|
UserID: user.ID,
|
2023-10-19 17:56:35 +00:00
|
|
|
SourceType: ActivityAdmin,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, false)
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
if sendMail && req.Notify {
|
2024-08-04 14:14:51 +00:00
|
|
|
if err := app.sendByID(msg, user.ID); err != nil {
|
|
|
|
app.err.Printf(lm.FailedSendEnableDisableMessage, user.ID, "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.jf.CacheExpiry = time.Now()
|
|
|
|
if len(errors["GetUser"]) != 0 || len(errors["SetPolicy"]) != 0 {
|
|
|
|
gc.JSON(500, errors)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @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]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) DeleteUsers(gc *gin.Context) {
|
|
|
|
var req deleteUserDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
errors := map[string]string{}
|
|
|
|
sendMail := messagesEnabled
|
|
|
|
var msg *Message
|
|
|
|
var err error
|
|
|
|
if sendMail {
|
|
|
|
msg, err = app.email.constructDeleted(req.Reason, app, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructDeletionMessage, "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
sendMail = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, userID := range req.Users {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(userID, false)
|
2024-08-04 14:14:51 +00:00
|
|
|
if err != nil {
|
|
|
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err)
|
|
|
|
errors[userID] = err.Error()
|
2023-10-21 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
2024-08-04 14:14:51 +00:00
|
|
|
deleted := false
|
|
|
|
err, deleted = app.DeleteUser(user)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := errors[user.ID]; !ok {
|
|
|
|
errors[user.ID] = err.Error() + " "
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-08-04 14:14:51 +00:00
|
|
|
errors[user.ID] += err.Error() + " "
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-04 14:14:51 +00:00
|
|
|
if deleted {
|
|
|
|
// Record activity
|
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
|
|
|
Type: ActivityDeletion,
|
|
|
|
UserID: userID,
|
|
|
|
SourceType: ActivityAdmin,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: user.Name,
|
|
|
|
Time: time.Now(),
|
|
|
|
}, gc, false)
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-04 14:14:51 +00:00
|
|
|
// Contact details are stored separately and periodically removed,
|
|
|
|
// putting this here is hoping the daemon doesn't beat us.
|
2022-03-22 14:58:39 +00:00
|
|
|
if sendMail && req.Notify {
|
2024-08-04 14:14:51 +00:00
|
|
|
if err := app.sendByID(msg, user.ID); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendDeletionMessage, userID, "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.jf.CacheExpiry = time.Now()
|
|
|
|
if len(errors) == len(req.Users) {
|
|
|
|
respondBool(500, false, gc)
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedDeleteUsers, lm.Jellyfin, errors[req.Users[0]])
|
2022-03-22 14:58:39 +00:00
|
|
|
return
|
|
|
|
} else if len(errors) != 0 {
|
|
|
|
gc.JSON(500, errors)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2023-11-16 11:19:49 +00:00
|
|
|
// @Summary Extend time before the user(s) expiry, or create an expiry if it doesn't exist.
|
2022-03-22 14:58:39 +00:00
|
|
|
// @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)
|
2023-12-20 17:20:59 +00:00
|
|
|
if req.Months <= 0 && req.Days <= 0 && req.Hours <= 0 && req.Minutes <= 0 && req.Timestamp <= 0 {
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, id := range req.Users {
|
2023-06-24 18:13:05 +00:00
|
|
|
base := time.Now()
|
|
|
|
if expiry, ok := app.storage.GetUserExpiryKey(id); ok {
|
|
|
|
base = expiry.Expiry
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.ExtendCreateExpiry, id)
|
2023-12-20 17:20:59 +00:00
|
|
|
expiry := UserExpiry{}
|
|
|
|
if req.Timestamp != 0 {
|
|
|
|
expiry.Expiry = time.Unix(req.Timestamp, 0)
|
|
|
|
} else {
|
|
|
|
expiry.Expiry = base.AddDate(0, req.Months, req.Days).Add(time.Duration(((60 * req.Hours) + req.Minutes)) * time.Minute)
|
|
|
|
}
|
2023-06-24 18:13:05 +00:00
|
|
|
app.storage.SetUserExpiryKey(id, expiry)
|
2024-07-28 15:53:27 +00:00
|
|
|
if messagesEnabled && req.Notify {
|
|
|
|
go func(uid string, exp time.Time) {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(uid, false)
|
|
|
|
if err != nil {
|
2024-07-28 15:53:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
msg, err := app.email.constructExpiryAdjusted(user.Name, exp, req.Reason, app, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructExpiryAdjustmentMessage, uid, err)
|
2024-07-28 15:53:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := app.sendByID(msg, uid); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendExpiryAdjustmentMessage, uid, "?", err)
|
2024-07-28 15:53:27 +00:00
|
|
|
}
|
|
|
|
}(id, expiry.Expiry)
|
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
|
|
|
|
2023-11-16 11:19:49 +00:00
|
|
|
// @Summary Remove an expiry from a user's account.
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "id of user to extend expiry of."
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /users/{id}/expiry [delete]
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) RemoveExpiry(gc *gin.Context) {
|
|
|
|
app.storage.DeleteUserExpiryKey(gc.Param("id"))
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2023-06-28 15:05:24 +00:00
|
|
|
// @Summary Enable referrals for the given user(s) based on the rules set in the given invite code, or profile.
|
|
|
|
// @Produce json
|
2023-09-07 13:00:30 +00:00
|
|
|
// @Param EnableDisableReferralDTO body EnableDisableReferralDTO true "List of users"
|
2023-06-28 15:05:24 +00:00
|
|
|
// @Param mode path string true "mode of template sourcing from 'invite' or 'profile'."
|
|
|
|
// @Param source path string true "invite code or profile name, depending on what mode is."
|
2023-11-10 15:07:29 +00:00
|
|
|
// @Param useExpiry path string true "with-expiry or none."
|
2023-06-28 15:05:24 +00:00
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
2023-11-10 15:07:29 +00:00
|
|
|
// @Router /users/referral/{mode}/{source}/{useExpiry} [post]
|
2023-06-28 15:05:24 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) EnableReferralForUsers(gc *gin.Context) {
|
|
|
|
var req EnableDisableReferralDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
mode := gc.Param("mode")
|
2023-11-16 11:19:49 +00:00
|
|
|
|
2023-06-28 15:05:24 +00:00
|
|
|
source := gc.Param("source")
|
2023-11-10 15:07:29 +00:00
|
|
|
useExpiry := gc.Param("useExpiry") == "with-expiry"
|
2023-06-28 15:05:24 +00:00
|
|
|
baseInv := Invite{}
|
|
|
|
if mode == "profile" {
|
|
|
|
profile, ok := app.storage.GetProfileKey(source)
|
|
|
|
err := app.storage.db.Get(profile.ReferralTemplateKey, &baseInv)
|
|
|
|
if !ok || profile.ReferralTemplateKey == "" || err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.FailedGetReferralTemplate, profile.ReferralTemplateKey, err)
|
2023-06-28 15:05:24 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.GetReferralTemplate, profile.ReferralTemplateKey)
|
2023-06-28 15:05:24 +00:00
|
|
|
} else if mode == "invite" {
|
|
|
|
// Get the invite, and modify it to turn it into a referral
|
|
|
|
err := app.storage.db.Get(source, &baseInv)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.InvalidInviteCode, source)
|
2023-06-28 15:05:24 +00:00
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, u := range req.Users {
|
2023-09-06 21:46:16 +00:00
|
|
|
// 1. Wipe out any existing referral codes.
|
|
|
|
app.storage.db.DeleteMatching(Invite{}, badgerhold.Where("ReferrerJellyfinID").Eq(u))
|
|
|
|
|
|
|
|
// 2. Generate referral invite.
|
2023-06-28 15:05:24 +00:00
|
|
|
inv := baseInv
|
2023-10-11 10:30:28 +00:00
|
|
|
inv.Code = GenerateInviteCode()
|
2023-11-10 15:07:29 +00:00
|
|
|
expiryDelta := inv.ValidTill.Sub(inv.Created)
|
2023-06-28 15:05:24 +00:00
|
|
|
inv.Created = time.Now()
|
2023-11-10 15:07:29 +00:00
|
|
|
if useExpiry {
|
|
|
|
inv.ValidTill = inv.Created.Add(expiryDelta)
|
|
|
|
} else {
|
|
|
|
inv.ValidTill = inv.Created.Add(REFERRAL_EXPIRY_DAYS * 24 * time.Hour)
|
|
|
|
}
|
2023-06-28 15:05:24 +00:00
|
|
|
inv.IsReferral = true
|
|
|
|
inv.ReferrerJellyfinID = u
|
2023-11-10 15:07:29 +00:00
|
|
|
inv.UseReferralExpiry = useExpiry
|
2023-06-28 15:05:24 +00:00
|
|
|
app.storage.SetInvitesKey(inv.Code, inv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 13:00:30 +00:00
|
|
|
// @Summary Disable referrals for the given user(s).
|
|
|
|
// @Produce json
|
|
|
|
// @Param EnableDisableReferralDTO body EnableDisableReferralDTO true "List of users"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Router /users/referral [delete]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) DisableReferralForUsers(gc *gin.Context) {
|
|
|
|
var req EnableDisableReferralDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
for _, u := range req.Users {
|
|
|
|
// 1. Delete directly bound template
|
|
|
|
app.storage.db.DeleteMatching(Invite{}, badgerhold.Where("ReferrerJellyfinID").Eq(u))
|
|
|
|
// 2. Check for and delete profile-attached template
|
|
|
|
user, ok := app.storage.GetEmailsKey(u)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
user.ReferralTemplateKey = ""
|
|
|
|
app.storage.SetEmailsKey(u, user)
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +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 !messagesEnabled {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Generally, we only need to construct once. If {username} is included, however, this needs to be done for each user.
|
|
|
|
unique := strings.Contains(req.Message, "{username}")
|
|
|
|
if unique {
|
|
|
|
for _, userID := range req.Users {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(userID, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, userID, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg, err := app.email.constructTemplate(req.Subject, req.Message, app, user.Name)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructAnnouncementMessage, userID, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
} else if err := app.sendByID(msg, userID); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendAnnouncementMessage, userID, "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// app.info.Printf(lm.SentAnnouncementMessage, "*", "?")
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
|
|
|
msg, err := app.email.constructTemplate(req.Subject, req.Message, app)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructAnnouncementMessage, "*", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
} else if err := app.sendByID(msg, req.Users...); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendAnnouncementMessage, "*", "?", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
// app.info.Printf(lm.SentAnnouncementMessage, "*", "?")
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
2024-08-03 20:23:59 +00:00
|
|
|
app.info.Printf(lm.SentAnnouncementMessage, "*", "?")
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Save an announcement as a template for use or editing later.
|
|
|
|
// @Produce json
|
|
|
|
// @Param announcementTemplate body announcementTemplate true "Announcement request object"
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/announce/template [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) SaveAnnounceTemplate(gc *gin.Context) {
|
|
|
|
var req announcementTemplate
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if !messagesEnabled {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
|
|
|
|
app.storage.SetAnnouncementsKey(req.Name, req)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
2024-08-21 19:35:08 +00:00
|
|
|
// @Summary Gets the names of each available announcement template.
|
2022-03-22 14:58:39 +00:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getAnnouncementsDTO
|
2023-06-24 16:01:52 +00:00
|
|
|
// @Router /users/announce [get]
|
2022-03-22 14:58:39 +00:00
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) GetAnnounceTemplates(gc *gin.Context) {
|
2023-06-24 16:01:52 +00:00
|
|
|
resp := &getAnnouncementsDTO{make([]string, len(app.storage.GetAnnouncements()))}
|
|
|
|
for i, a := range app.storage.GetAnnouncements() {
|
|
|
|
resp.Announcements[i] = a.Name
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get an announcement template.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} announcementTemplate
|
|
|
|
// @Failure 400 {object} boolResponse
|
2024-07-21 16:45:36 +00:00
|
|
|
// @Param name path string true "name of template (url encoded if necessary)"
|
2022-03-22 14:58:39 +00:00
|
|
|
// @Router /users/announce/template/{name} [get]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) GetAnnounceTemplate(gc *gin.Context) {
|
2024-07-21 16:45:36 +00:00
|
|
|
escapedName := gc.Param("name")
|
|
|
|
name, err := url.QueryUnescape(escapedName)
|
|
|
|
if err != nil {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
if announcement, ok := app.storage.GetAnnouncementsKey(name); ok {
|
2022-03-22 14:58:39 +00:00
|
|
|
gc.JSON(200, announcement)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Delete an announcement template.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Param name path string true "name of template"
|
|
|
|
// @Router /users/announce/template/{name} [delete]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) DeleteAnnounceTemplate(gc *gin.Context) {
|
|
|
|
name := gc.Param("name")
|
2023-06-24 16:01:52 +00:00
|
|
|
app.storage.DeleteAnnouncementsKey(name)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(200, false, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Generate password reset links for a list of users, sending the links to them if possible.
|
|
|
|
// @Produce json
|
|
|
|
// @Param AdminPasswordResetDTO body AdminPasswordResetDTO true "List of user IDs"
|
|
|
|
// @Success 204 {object} boolResponse
|
|
|
|
// @Success 200 {object} AdminPasswordResetRespDTO
|
|
|
|
// @Failure 400 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/password-reset [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) AdminPasswordReset(gc *gin.Context) {
|
|
|
|
var req AdminPasswordResetDTO
|
|
|
|
gc.BindJSON(&req)
|
|
|
|
if req.Users == nil || len(req.Users) == 0 {
|
|
|
|
respondBool(400, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
linkCount := 0
|
|
|
|
var pwr InternalPWR
|
|
|
|
var err error
|
|
|
|
resp := AdminPasswordResetRespDTO{}
|
|
|
|
for _, id := range req.Users {
|
|
|
|
pwr, err = app.GenInternalReset(id)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, id, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.internalPWRs == nil {
|
|
|
|
app.internalPWRs = map[string]InternalPWR{}
|
|
|
|
}
|
|
|
|
app.internalPWRs[pwr.PIN] = pwr
|
|
|
|
sendAddress := app.getAddressOrName(id)
|
|
|
|
if sendAddress == "" || len(req.Users) == 1 {
|
|
|
|
resp.Link, err = app.GenResetLink(pwr.PIN)
|
|
|
|
linkCount++
|
|
|
|
if sendAddress == "" {
|
|
|
|
resp.Manual = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sendAddress != "" {
|
|
|
|
msg, err := app.email.constructReset(
|
|
|
|
PasswordReset{
|
|
|
|
Pin: pwr.PIN,
|
|
|
|
Username: pwr.Username,
|
|
|
|
Expiry: pwr.Expiry,
|
|
|
|
Internal: true,
|
|
|
|
}, app, false,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedConstructPWRMessage, id, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respondBool(500, false, gc)
|
|
|
|
return
|
|
|
|
} else if err := app.sendByID(msg, id); err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSendPWRMessage, id, sendAddress, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.SentPWRMessage, id, sendAddress)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.Link != "" && linkCount == 1 {
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
|
|
|
|
2024-08-20 20:33:43 +00:00
|
|
|
func (app *appContext) userSummary(jfUser mediabrowser.User) respUser {
|
|
|
|
adminOnly := app.config.Section("ui").Key("admin_only").MustBool(true)
|
|
|
|
allowAll := app.config.Section("ui").Key("allow_all").MustBool(false)
|
|
|
|
referralsEnabled := app.config.Section("user_page").Key("referrals").MustBool(false)
|
|
|
|
user := respUser{
|
|
|
|
ID: jfUser.ID,
|
|
|
|
Name: jfUser.Name,
|
|
|
|
Admin: jfUser.Policy.IsAdministrator,
|
|
|
|
Disabled: jfUser.Policy.IsDisabled,
|
|
|
|
ReferralsEnabled: false,
|
|
|
|
}
|
|
|
|
if !jfUser.LastActivityDate.IsZero() {
|
|
|
|
user.LastActive = jfUser.LastActivityDate.Unix()
|
|
|
|
}
|
|
|
|
if email, ok := app.storage.GetEmailsKey(jfUser.ID); ok {
|
|
|
|
user.Email = email.Addr
|
|
|
|
user.NotifyThroughEmail = email.Contact
|
|
|
|
user.Label = email.Label
|
|
|
|
user.AccountsAdmin = (app.jellyfinLogin) && (email.Admin || (adminOnly && jfUser.Policy.IsAdministrator) || allowAll)
|
|
|
|
}
|
|
|
|
expiry, ok := app.storage.GetUserExpiryKey(jfUser.ID)
|
|
|
|
if ok {
|
|
|
|
user.Expiry = expiry.Expiry.Unix()
|
|
|
|
}
|
|
|
|
if tgUser, ok := app.storage.GetTelegramKey(jfUser.ID); ok {
|
|
|
|
user.Telegram = tgUser.Username
|
|
|
|
user.NotifyThroughTelegram = tgUser.Contact
|
|
|
|
}
|
|
|
|
if mxUser, ok := app.storage.GetMatrixKey(jfUser.ID); ok {
|
|
|
|
user.Matrix = mxUser.UserID
|
|
|
|
user.NotifyThroughMatrix = mxUser.Contact
|
|
|
|
}
|
|
|
|
if dcUser, ok := app.storage.GetDiscordKey(jfUser.ID); ok {
|
|
|
|
user.Discord = RenderDiscordUsername(dcUser)
|
|
|
|
// user.Discord = dcUser.Username + "#" + dcUser.Discriminator
|
|
|
|
user.DiscordID = dcUser.ID
|
|
|
|
user.NotifyThroughDiscord = dcUser.Contact
|
|
|
|
}
|
|
|
|
// FIXME: Send referral data
|
|
|
|
referrerInv := Invite{}
|
|
|
|
if referralsEnabled {
|
|
|
|
// 1. Directly attached invite.
|
|
|
|
err := app.storage.db.FindOne(&referrerInv, badgerhold.Where("ReferrerJellyfinID").Eq(jfUser.ID))
|
|
|
|
if err == nil {
|
|
|
|
user.ReferralsEnabled = true
|
|
|
|
// 2. Referrals via profile template. Shallow check, doesn't look for the thing in the database.
|
|
|
|
} else if email, ok := app.storage.GetEmailsKey(jfUser.ID); ok && email.ReferralTemplateKey != "" {
|
|
|
|
user.ReferralsEnabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
// @Summary Get a list of Jellyfin users.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} getUsersDTO
|
|
|
|
// @Failure 500 {object} stringResponse
|
|
|
|
// @Router /users [get]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) GetUsers(gc *gin.Context) {
|
|
|
|
var resp getUsersDTO
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
2022-03-22 14:58:39 +00:00
|
|
|
resp.UserList = make([]respUser, len(users))
|
2024-08-06 13:48:31 +00:00
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for _, jfUser := range users {
|
2024-08-20 20:33:43 +00:00
|
|
|
user := app.userSummary(jfUser)
|
2022-03-22 14:58:39 +00:00
|
|
|
resp.UserList[i] = user
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
gc.JSON(200, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Set whether or not a user can access jfa-go. Redundant if the user is a Jellyfin admin.
|
|
|
|
// @Produce json
|
|
|
|
// @Param setAccountsAdminDTO body setAccountsAdminDTO true "Map of userIDs to whether or not they have access."
|
|
|
|
// @Success 204 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/accounts-admin [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) SetAccountsAdmin(gc *gin.Context) {
|
|
|
|
var req setAccountsAdminDTO
|
|
|
|
gc.BindJSON(&req)
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, jfUser := range users {
|
|
|
|
id := jfUser.ID
|
|
|
|
if admin, ok := req[id]; ok {
|
|
|
|
var emailStore = EmailAddress{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if oldEmail, ok := app.storage.GetEmailsKey(id); ok {
|
2022-03-22 14:58:39 +00:00
|
|
|
emailStore = oldEmail
|
|
|
|
}
|
|
|
|
emailStore.Admin = admin
|
2023-06-20 11:19:24 +00:00
|
|
|
app.storage.SetEmailsKey(id, emailStore)
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.UserAdminAdjusted, id, admin)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Modify user's labels, which show next to their name in the accounts tab.
|
|
|
|
// @Produce json
|
|
|
|
// @Param modifyEmailsDTO body modifyEmailsDTO true "Map of userIDs to labels"
|
|
|
|
// @Success 204 {object} boolResponse
|
|
|
|
// @Failure 500 {object} boolResponse
|
|
|
|
// @Router /users/labels [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) ModifyLabels(gc *gin.Context) {
|
|
|
|
var req modifyEmailsDTO
|
|
|
|
gc.BindJSON(&req)
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, jfUser := range users {
|
|
|
|
id := jfUser.ID
|
|
|
|
if label, ok := req[id]; ok {
|
|
|
|
var emailStore = EmailAddress{}
|
2023-06-20 11:19:24 +00:00
|
|
|
if oldEmail, ok := app.storage.GetEmailsKey(id); ok {
|
2022-03-22 14:58:39 +00:00
|
|
|
emailStore = oldEmail
|
|
|
|
}
|
|
|
|
emailStore.Label = label
|
2024-08-28 19:22:25 +00:00
|
|
|
app.debug.Printf(lm.UserLabelAdjusted, id, label)
|
2023-06-20 11:19:24 +00:00
|
|
|
app.storage.SetEmailsKey(id, emailStore)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
respondBool(204, true, gc)
|
|
|
|
}
|
|
|
|
|
2024-07-30 19:55:45 +00:00
|
|
|
func (app *appContext) modifyEmail(jfID string, addr string) {
|
|
|
|
contactPrefChanged := false
|
|
|
|
emailStore, ok := app.storage.GetEmailsKey(jfID)
|
|
|
|
// Auto enable contact by email for newly added addresses
|
|
|
|
if !ok || emailStore.Addr == "" {
|
|
|
|
emailStore = EmailAddress{
|
|
|
|
Contact: true,
|
|
|
|
}
|
|
|
|
contactPrefChanged = true
|
|
|
|
}
|
|
|
|
emailStore.Addr = addr
|
|
|
|
app.storage.SetEmailsKey(jfID, emailStore)
|
|
|
|
if app.config.Section("ombi").Key("enabled").MustBool(false) {
|
2024-08-06 13:48:31 +00:00
|
|
|
ombiUser, err := app.getOmbiUser(jfID)
|
|
|
|
if err == nil {
|
2024-07-30 19:55:45 +00:00
|
|
|
ombiUser["emailAddress"] = addr
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.ombi.ModifyUser(ombiUser)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSetEmailAddress, lm.Ombi, jfID, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if app.config.Section("jellyseerr").Key("enabled").MustBool(false) {
|
|
|
|
err := app.js.ModifyMainUserSettings(jfID, jellyseerr.MainUserSettings{Email: addr})
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSetEmailAddress, lm.Jellyseerr, jfID, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
} else if contactPrefChanged {
|
|
|
|
contactMethods := map[jellyseerr.NotificationsField]any{
|
|
|
|
jellyseerr.FieldEmailEnabled: true,
|
|
|
|
}
|
|
|
|
err := app.js.ModifyNotifications(jfID, contactMethods)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedSyncContactMethods, lm.Jellyseerr, err)
|
2024-07-30 19:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
// @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]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Users
|
|
|
|
func (app *appContext) ModifyEmails(gc *gin.Context) {
|
|
|
|
var req modifyEmailsDTO
|
|
|
|
gc.BindJSON(&req)
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get users", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, jfUser := range users {
|
|
|
|
id := jfUser.ID
|
|
|
|
if address, ok := req[id]; ok {
|
2024-07-30 19:55:45 +00:00
|
|
|
app.modifyEmail(id, address)
|
2023-10-19 20:13:00 +00:00
|
|
|
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.UserEmailAdjusted, gc.GetString("jfId"))
|
|
|
|
|
2023-10-21 11:53:53 +00:00
|
|
|
activityType := ActivityContactLinked
|
|
|
|
if address == "" {
|
|
|
|
activityType = ActivityContactUnlinked
|
|
|
|
}
|
2023-10-19 20:13:00 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
2023-10-21 11:53:53 +00:00
|
|
|
Type: activityType,
|
2023-10-19 20:13:00 +00:00
|
|
|
UserID: id,
|
|
|
|
SourceType: ActivityAdmin,
|
|
|
|
Source: gc.GetString("jfId"),
|
|
|
|
Value: "email",
|
|
|
|
Time: time.Now(),
|
2023-12-23 21:47:41 +00:00
|
|
|
}, gc, false)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
respondBool(200, true, gc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @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
|
|
|
|
// @Failure 500 {object} errorListDTO "Lists of errors that occurred while applying settings"
|
|
|
|
// @Router /users/settings [post]
|
|
|
|
// @Security Bearer
|
|
|
|
// @tags Profiles & Settings
|
|
|
|
func (app *appContext) ApplySettings(gc *gin.Context) {
|
|
|
|
app.info.Println("User settings change requested")
|
|
|
|
var req userSettingsDTO
|
|
|
|
gc.BindJSON(&req)
|
2024-07-31 17:49:52 +00:00
|
|
|
applyingFromType := lm.Profile
|
|
|
|
applyingFromSource := "?"
|
2022-03-22 14:58:39 +00:00
|
|
|
var policy mediabrowser.Policy
|
|
|
|
var configuration mediabrowser.Configuration
|
|
|
|
var displayprefs map[string]interface{}
|
|
|
|
var ombi map[string]interface{}
|
2024-07-30 15:36:59 +00:00
|
|
|
var jellyseerr JellyseerrTemplate
|
|
|
|
jellyseerr.Enabled = false
|
2022-03-22 14:58:39 +00:00
|
|
|
if req.From == "profile" {
|
|
|
|
// Check profile exists & isn't empty
|
2023-06-24 20:29:16 +00:00
|
|
|
profile, ok := app.storage.GetProfileKey(req.Profile)
|
2023-06-26 11:57:16 +00:00
|
|
|
if !ok {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetProfile, req.Profile)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't find profile", gc)
|
|
|
|
return
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
applyingFromSource = req.Profile
|
2022-03-22 14:58:39 +00:00
|
|
|
if req.Homescreen {
|
2024-07-31 17:49:52 +00:00
|
|
|
if profile.Homescreen {
|
|
|
|
configuration = profile.Configuration
|
|
|
|
displayprefs = profile.Displayprefs
|
|
|
|
} else {
|
|
|
|
req.Homescreen = false
|
|
|
|
app.err.Printf(lm.ProfileNoHomescreen, req.Profile)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "No homescreen template available", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 14:54:05 +00:00
|
|
|
if req.Policy {
|
|
|
|
policy = profile.Policy
|
|
|
|
}
|
2024-07-30 20:36:06 +00:00
|
|
|
if req.Ombi && app.config.Section("ombi").Key("enabled").MustBool(false) {
|
2022-03-22 14:58:39 +00:00
|
|
|
if profile.Ombi != nil && len(profile.Ombi) != 0 {
|
|
|
|
ombi = profile.Ombi
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 20:36:06 +00:00
|
|
|
if req.Jellyseerr && app.config.Section("jellyseerr").Key("enabled").MustBool(false) {
|
2024-07-30 15:36:59 +00:00
|
|
|
if profile.Jellyseerr.Enabled {
|
|
|
|
jellyseerr = profile.Jellyseerr
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
|
|
|
|
} else if req.From == "user" {
|
2024-07-31 17:49:52 +00:00
|
|
|
applyingFromType = lm.User
|
2022-03-22 14:58:39 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.jf.UserByID(req.ID, false)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetUser, req.ID, lm.Jellyfin, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get user", gc)
|
|
|
|
return
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
applyingFromSource = user.Name
|
2024-07-31 14:54:05 +00:00
|
|
|
if req.Policy {
|
|
|
|
policy = user.Policy
|
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
if req.Homescreen {
|
2024-08-06 13:48:31 +00:00
|
|
|
displayprefs, err = app.jf.GetDisplayPreferences(req.ID)
|
|
|
|
if err != nil {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.err.Printf(lm.FailedGetJellyfinDisplayPrefs, req.ID, err)
|
2022-03-22 14:58:39 +00:00
|
|
|
respond(500, "Couldn't get displayprefs", gc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configuration = user.Configuration
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 17:49:52 +00:00
|
|
|
app.info.Printf(lm.ApplyingTemplatesFrom, applyingFromType, applyingFromSource, len(req.ApplyTo))
|
2022-03-22 14:58:39 +00:00
|
|
|
errors := errorListDTO{
|
|
|
|
"policy": map[string]string{},
|
|
|
|
"homescreen": map[string]string{},
|
|
|
|
"ombi": map[string]string{},
|
2024-07-30 15:36:59 +00:00
|
|
|
"jellyseerr": map[string]string{},
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
/* Jellyfin doesn't seem to like too many of these requests sent in succession
|
|
|
|
and can crash and mess up its database. Issue #160 says this occurs when more
|
|
|
|
than 100 users are modified. A delay totalling 500ms between requests is used
|
|
|
|
if so. */
|
2024-07-31 17:49:52 +00:00
|
|
|
const requestDelayThreshold = 100
|
|
|
|
var shouldDelay bool = len(req.ApplyTo) >= requestDelayThreshold
|
2022-03-22 14:58:39 +00:00
|
|
|
if shouldDelay {
|
2024-07-31 17:49:52 +00:00
|
|
|
app.debug.Printf(lm.DelayingRequests, requestDelayThreshold)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
for _, id := range req.ApplyTo {
|
2024-07-31 14:54:05 +00:00
|
|
|
var err error
|
|
|
|
if req.Policy {
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.jf.SetPolicy(id, policy)
|
|
|
|
if err != nil {
|
|
|
|
errors["policy"][id] = err.Error()
|
2024-07-31 14:54:05 +00:00
|
|
|
}
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
if shouldDelay {
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
if req.Homescreen {
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.jf.SetConfiguration(id, configuration)
|
2022-03-22 14:58:39 +00:00
|
|
|
errorString := ""
|
2024-08-06 13:48:31 +00:00
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("Configuration: %v", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.jf.SetDisplayPreferences(id, displayprefs)
|
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("Displayprefs;) %v ", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if errorString != "" {
|
|
|
|
errors["homescreen"][id] = errorString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ombi != nil {
|
|
|
|
errorString := ""
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err := app.getOmbiUser(id)
|
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("Ombi GetUser: %v ", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
} else {
|
|
|
|
// newUser := ombi
|
|
|
|
// newUser["id"] = user["id"]
|
|
|
|
// newUser["userName"] = user["userName"]
|
|
|
|
// newUser["alias"] = user["alias"]
|
|
|
|
// newUser["emailAddress"] = user["emailAddress"]
|
2024-08-06 13:48:31 +00:00
|
|
|
err = app.ombi.applyProfile(user, ombi)
|
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("Apply: %v ", err)
|
2022-03-22 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if errorString != "" {
|
|
|
|
errors["ombi"][id] = errorString
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 15:36:59 +00:00
|
|
|
if jellyseerr.Enabled {
|
|
|
|
errorString := ""
|
|
|
|
// newUser := ombi
|
|
|
|
// newUser["id"] = user["id"]
|
|
|
|
// newUser["userName"] = user["userName"]
|
|
|
|
// newUser["alias"] = user["alias"]
|
|
|
|
// newUser["emailAddress"] = user["emailAddress"]
|
|
|
|
err := app.js.ApplyTemplateToUser(id, jellyseerr.User)
|
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("ApplyUser: %v ", err)
|
|
|
|
}
|
|
|
|
err = app.js.ApplyNotificationsTemplateToUser(id, jellyseerr.Notifications)
|
|
|
|
if err != nil {
|
|
|
|
errorString += fmt.Sprintf("ApplyNotifications: %v ", err)
|
|
|
|
}
|
|
|
|
if errorString != "" {
|
|
|
|
errors["jellyseerr"][id] = errorString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 14:58:39 +00:00
|
|
|
if shouldDelay {
|
|
|
|
time.Sleep(250 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code := 200
|
|
|
|
if len(errors["policy"]) == len(req.ApplyTo) || len(errors["homescreen"]) == len(req.ApplyTo) {
|
|
|
|
code = 500
|
|
|
|
}
|
|
|
|
gc.JSON(code, errors)
|
|
|
|
}
|