Added user email modification to admin page

Pressing the user settings button brings up a list of all jellyfin
users, and allows you to add or change their stored email addresses.
Additionally, changed emails.json to use user ID instead of username.
The program automatically converts the file to the new format at start.
This commit is contained in:
2020-04-20 20:37:39 +01:00
parent e8ad3f98d6
commit 200ad24f96
6 changed files with 230 additions and 26 deletions

View File

@@ -35,11 +35,21 @@ class Jellyfin:
"User-Agent": self.useragent,
"X-Emby-Authorization": self.auth
}
def getUsers(self, username="all"):
response = requests.get(self.server+"/emby/Users/Public").json()
if username == "all":
return response
def getUsers(self, username="all", id="all", public=True):
if public:
response = requests.get(self.server+"/emby/Users/Public").json()
else:
response = requests.get(self.server+"/emby/Users",
headers=self.header,
params={'Username': self.username,
'Pw': self.password})
if response.status_code == 200:
response = response.json()
else:
raise self.AuthenticationRequiredError
if username == "all" and id == "all":
return response
elif id == "all":
match = False
for user in response:
if user['Name'] == username:
@@ -47,6 +57,14 @@ class Jellyfin:
return user
if not match:
raise self.UserNotFoundError
else:
match = False
for user in response:
if user['Id'] == id:
match = True
return user
if not match:
raise self.UserNotFoundError
def authenticate(self, username, password):
self.username = username
self.password = password

View File

@@ -3,6 +3,7 @@ import json
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from jellyfin_accounts.email import Mailgun, Smtp
from jellyfin_accounts.web_api import jf
from __main__ import config
from __main__ import email_log as log
@@ -41,7 +42,8 @@ class Handler(FileSystemEventHandler):
try:
with open(config['files']['emails'], 'r') as f:
emails = json.load(f)
address = emails[reset['UserName']]
id = jf.getUsers(reset['UserName'], public=False)['Id']
address = emails[id]
method = config['email']['method']
if method == 'mailgun':
email = Mailgun(address)

View File

@@ -66,6 +66,36 @@ while attempts != 3:
'. Retrying...'))
time.sleep(5)
def switchToIds():
try:
with open(config['files']['emails'], 'r') as f:
emails = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
emails = {}
users = jf.getUsers(public=False)
new_emails = {}
match = False
for key in emails:
for user in users:
if user['Name'] == key:
match = True
new_emails[user['Id']] = emails[key]
elif user['Id'] == key:
new_emails[user['Id']] = emails[key]
if match:
from pathlib import Path
email_file = Path(config['files']['emails']).name
log.info((f'{email_file} modified to use userID instead of ' +
'usernames. These will be used in future.'))
emails = new_emails
with open(config['files']['emails'], 'w') as f:
f.write(json.dumps(emails, indent=4))
# Temporary, switches emails.json over from using Usernames to User IDs.
switchToIds()
if config.getboolean('password_validation', 'enabled'):
validator = PasswordValidator(config['password_validation']['min_length'],
config['password_validation']['upper'],
@@ -113,13 +143,13 @@ def newUser():
jf.setPolicy(user.json()['Id'], default_policy)
except:
log.debug('setPolicy failed')
if config.getboolean('email', 'enabled'):
if config.getboolean('password_resets', 'enabled'):
try:
with open(config['files']['emails'], 'r') as f:
emails = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
emails = {}
emails[data['username']] = data['email']
emails[user.json()['Id']] = data['email']
with open(config['files']['emails'], 'w') as f:
f.write(json.dumps(emails, indent=4))
log.debug('Email address stored')
@@ -251,3 +281,45 @@ def modifyConfig():
temp_config.write(config_file)
log.debug('Config written')
return jsonify(data)
@app.route('/getUsers', methods=['GET', 'POST'])
@auth.login_required
def getUsers():
log.debug('User and email list requested')
try:
with open(config['files']['emails'], 'r') as f:
emails = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
emails = {}
response = {'users': []}
users = jf.getUsers(public=False)
for user in users:
entry = {'name': user['Name']}
if user['Id'] in emails:
entry['email'] = emails[user['Id']]
response['users'].append(entry)
return jsonify(response)
@app.route('/modifyUsers', methods=['POST'])
@auth.login_required
def modifyUsers():
data = request.get_json()
log.debug('User and email list modification requested')
try:
with open(config['files']['emails'], 'r') as f:
emails = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
emails = {}
for key in data:
uid = jf.getUsers(key, public=False)['Id']
log.debug(f'Email for user "{key}" modified')
emails[uid] = data[key]
try:
with open(config['files']['emails'], 'w') as f:
f.write(json.dumps(emails, indent=4))
return resp()
except:
return resp(success=False)