linuxrouter/docker/routlin-dash/app/pages/preferences/action.py

95 lines
2.9 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, session, redirect, flash
import json, bcrypt
2026-06-07 00:21:08 -04:00
import auth
import config_utils
2026-05-27 20:56:30 -04:00
import sanitize
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
def _load_accounts():
try:
2026-06-07 00:21:08 -04:00
with open(config_utils.ACCOUNTS_FILE) as f:
2026-05-17 03:26:01 -04:00
return json.load(f)
except Exception:
return {'accounts': []}
def _save_accounts(data):
2026-06-07 00:21:08 -04:00
with open(config_utils.ACCOUNTS_FILE, 'w') as f:
2026-05-17 03:26:01 -04:00
json.dump(data, f, indent=2)
2026-05-27 22:04:04 -04:00
@bp.route('/action/preferences/accountdetails_save', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('viewer')
2026-05-27 22:04:04 -04:00
def accountdetails_save():
2026-05-27 20:56:30 -04:00
tz = sanitize.timezone(request.form.get('timezone', '').strip())
if not tz:
flash('Timezone is required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-27 20:56:30 -04:00
email = session.get('email_address', '').lower()
data = _load_accounts()
accounts = data.get('accounts', [])
account = next((a for a in accounts if a.get('email_address', '').lower() == email), None)
if account is None:
flash('Account not found. Please log in again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect('/accountlogin')
2026-05-27 20:56:30 -04:00
account['timezone'] = tz
_save_accounts(data)
session['timezone'] = tz
flash('Preferences saved.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-27 20:56:30 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/preferences/changepassword_save', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('viewer')
2026-05-27 22:04:04 -04:00
def changepassword_save():
2026-05-17 03:26:01 -04:00
current_password = request.form.get('current_password', '')
new_password = request.form.get('new_password', '')
confirm_password = request.form.get('confirm_password', '')
if not current_password or not new_password or not confirm_password:
flash('All fields are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if new_password != confirm_password:
flash('New passwords do not match.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if len(new_password) < 8:
flash('New password must be at least 8 characters.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
email = session.get('email_address', '').lower()
data = _load_accounts()
accounts = data.get('accounts', [])
account = next((a for a in accounts if a.get('email_address', '').lower() == email), None)
if account is None:
flash('Account not found. Please log in again.', 'error')
2026-05-27 22:04:04 -04:00
return redirect('/accountlogin')
2026-05-17 03:26:01 -04:00
stored_hash = account.get('hashed_password', '').encode('utf-8')
if not bcrypt.checkpw(current_password.encode('utf-8'), stored_hash):
flash('Current password is incorrect.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
salt = bcrypt.gensalt()
hashed = bcrypt.hashpw(new_password.encode('utf-8'), salt)
account['hashed_password'] = hashed.decode('utf-8')
account['salt'] = salt.decode('utf-8')
_save_accounts(data)
flash('Password changed successfully.', 'success')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')