From 39a27eb7629f45f1dd720b61b1b1f910eb7eb1d3 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 8 Jun 2020 13:33:04 +0100 Subject: [PATCH] Added new user defaults setting to web ui What previously had to be done through 'jf-accounts -g' can now be done in the settings menu on the web ui. --- data/static/admin.js | 111 ++++++++++++++++++++++++++++++++++- data/templates/admin.html | 56 +++++++++++++++++- jellyfin_accounts/web_api.py | 36 ++++++++++++ jf-accounts | 2 +- 4 files changed, 201 insertions(+), 4 deletions(-) diff --git a/data/static/admin.js b/data/static/admin.js index 6792b6c..8e99840 100644 --- a/data/static/admin.js +++ b/data/static/admin.js @@ -241,6 +241,115 @@ $("form#loginForm").submit(function() { }); return false; }); +document.getElementById('openSettings').onclick = function () { + $('#settingsMenu').modal('show'); +} +document.getElementById('openDefaultsWizard').onclick = function () { + this.disabled = true; + this.innerHTML = + '' + + 'Loading...'; + $.ajax('getUsers', { + type : 'GET', + dataType : 'json', + contentType : 'json', + xhrFields : { + withCredentials: true + }, + beforeSend : function (xhr) { + xhr.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":")); + }, + complete : function(data) { + if (data['status'] == 200) { + var radioList = document.getElementById('defaultUserRadios'); + radioList.textContent = ''; + if (document.getElementById('setDefaultUser')) { + document.getElementById('setDefaultUser').remove(); + }; + var users = data['responseJSON']['users']; + for (var i = 0; i < users.length; i++) { + var user = users[i] + var radio = document.createElement('div'); + radio.classList.add('radio'); + if (i == 0) { + var checked = 'checked'; + } else { + var checked = ''; + }; + radio.innerHTML = + ''; + radioList.appendChild(radio); + } + var button = document.getElementById('openDefaultsWizard'); + button.disabled = false; + button.innerHTML = 'Set new account defaults'; + var submitButton = document.getElementById('storeDefaults'); + submitButton.disabled = false; + submitButton.textContent = 'Submit'; + if (submitButton.classList.contains('btn-success')) { + submitButton.classList.remove('btn-success'); + submitButton.classList.add('btn-primary'); + } else if (submitButton.classList.contains('btn-danger')) { + submitButton.classList.remove('btn-danger'); + submitButton.classList.add('btn-primary'); + } + $('#userDefaults').modal('show'); + } + } + }); +}; +document.getElementById('storeDefaults').onclick = function () { + this.disabled = true; + this.innerHTML = + '' + + 'Loading...'; + var button = document.getElementById('storeDefaults'); + var radios = document.getElementsByName('defaultRadios'); + for (var i = 0; i < radios.length; i++) { + if (radios[i].checked) { + var data = {'username':radios[i].id.slice(8), 'homescreen':false}; + if (document.getElementById('storeDefaultHomescreen').checked) { + data['homescreen'] = true; + } + $.ajax('/setDefaults', { + data : JSON.stringify(data), + contentType : 'application/json', + type : 'POST', + xhrFields : { + withCredentials: true + }, + beforeSend : function (xhr) { + xhr.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":")); + }, + success: function() { + button.textContent = 'Success'; + if (button.classList.contains('btn-danger')) { + button.classList.remove('btn-danger'); + } else if (button.classList.contains('btn-primary')) { + button.classList.remove('btn-primary'); + }; + button.classList.add('btn-success'); + button.disabled = false; + setTimeout(function(){$('#userDefaults').modal('hide');}, 1000); + }, + error: function() { + button.textContent = 'Failed'; + button.classList.remove('btn-primary'); + button.classList.add('btn-danger'); + setTimeout(function(){ + var button = document.getElementById('storeDefaults'); + button.textContent = 'Submit'; + button.classList.remove('btn-danger'); + button.classList.add('btn-primary'); + button.disabled = false; + }, 1000); + } + }); + } + } +}; document.getElementById('openUsers').onclick = function () { this.disabled = true; this.innerHTML = @@ -262,7 +371,7 @@ document.getElementById('openUsers').onclick = function () { var list = document.getElementById('userList'); list.textContent = ''; if (document.getElementById('saveUsers')) { - document.getElementById('saveUsers').remove() + document.getElementById('saveUsers').remove(); }; var users = data['responseJSON']['users']; for (var i = 0; i < users.length; i++) { diff --git a/data/templates/admin.html b/data/templates/admin.html index aa7e642..4ab6e68 100644 --- a/data/templates/admin.html +++ b/data/templates/admin.html @@ -82,6 +82,35 @@ + +

Accounts admin

-
Current Invites
diff --git a/jellyfin_accounts/web_api.py b/jellyfin_accounts/web_api.py index c389c0d..d5b2551 100644 --- a/jellyfin_accounts/web_api.py +++ b/jellyfin_accounts/web_api.py @@ -315,6 +315,42 @@ def modifyUsers(): f.write(json.dumps(emails, indent=4)) return resp() except: + log.error('Could not store email') return resp(success=False) + +@app.route('/setDefaults', methods=['POST']) +@auth.login_required +def setDefaults(): + data = request.get_json() + username = data['username'] + log.debug(f'storing default settings from user {username}') + jf.authenticate(config['jellyfin']['username'], + config['jellyfin']['password']) + try: + user = jf.getUsers(username=username, + public=False) + except Jellyfin.UserNotFoundError: + log.error(f'couldn\'t find user {username}') + return resp(success=False) + uid = user['Id'] + policy = user['Policy'] + try: + with open(config['files']['user_template'], 'w') as f: + f.write(json.dumps(policy, indent=4)) + except: + log.error('Could not store user template') + return resp(success=False) + if data['homescreen']: + configuration = user['Configuration'] + try: + display_prefs = jf.getDisplayPreferences(uid) + with open(config['files']['user_configuration'], 'w') as f: + f.write(json.dumps(configuration, indent=4)) + with open(config['files']['user_displayprefs'], 'w') as f: + f.write(json.dumps(display_prefs, indent=4)) + except: + log.error('Could not store homescreen layout') + return resp() + import jellyfin_accounts.setup diff --git a/jf-accounts b/jf-accounts index 9927f6d..e3d07a4 100755 --- a/jf-accounts +++ b/jf-accounts @@ -152,7 +152,7 @@ if args.get_defaults: config['jellyfin']['version'], config['jellyfin']['device'], config['jellyfin']['device_id']) - # No auth needed. + print("NOTE: This can now be done through the web ui.") print(""" This tool lets you grab various settings from a user, so that they can be applied every time a new account is