2019-07-13 18:45:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs, cervinko, jkrehm, bodybybuddha, ok11,
|
|
|
|
# andy29485, idalin, Kyosfonica, wuqi, Kennyl, lemmsh,
|
|
|
|
# falgh1, grunjol, csitko, ytils, xybydy, trasba, vrabe,
|
|
|
|
# ruben-herold, marblepebble, JackED42, SiphonSquirrel,
|
2020-12-10 13:41:45 +00:00
|
|
|
# apetresc, nanu-c, mutschler, GammaC0de, vuolter
|
2019-07-13 18:45:48 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
import os
|
2020-05-09 17:54:28 +00:00
|
|
|
import re
|
2019-07-13 18:45:48 +00:00
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import time
|
2020-12-10 13:41:45 +00:00
|
|
|
import operator
|
2019-07-13 18:45:48 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from babel import Locale as LC
|
|
|
|
from babel.dates import format_datetime
|
2021-04-10 09:32:11 +00:00
|
|
|
from flask import Blueprint, flash, redirect, url_for, abort, request, make_response, send_from_directory, g, Response
|
2020-12-12 10:23:17 +00:00
|
|
|
from flask_login import login_required, current_user, logout_user, confirm_login
|
2019-07-13 18:45:48 +00:00
|
|
|
from flask_babel import gettext as _
|
2021-07-25 03:24:03 +00:00
|
|
|
from flask import session as flask_session
|
2019-07-13 18:45:48 +00:00
|
|
|
from sqlalchemy import and_
|
2021-02-13 12:17:02 +00:00
|
|
|
from sqlalchemy.orm.attributes import flag_modified
|
2020-06-27 10:31:26 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError, OperationalError, InvalidRequestError
|
2021-04-08 17:37:08 +00:00
|
|
|
from sqlalchemy.sql.expression import func, or_, text
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-03-28 12:50:55 +00:00
|
|
|
from . import constants, logger, helper, services
|
2020-05-21 16:16:11 +00:00
|
|
|
from . import db, calibre_db, ub, web_server, get_locale, config, updater_thread, babel, gdriveutils
|
2021-04-04 17:40:34 +00:00
|
|
|
from .helper import check_valid_domain, send_test_mail, reset_password, generate_password_hash, check_email, \
|
|
|
|
valid_email, check_username
|
2019-07-13 18:45:48 +00:00
|
|
|
from .gdriveutils import is_gdrive_ready, gdrive_support
|
2020-12-12 11:03:11 +00:00
|
|
|
from .render_template import render_title_template, get_sidebar_config
|
2021-04-28 18:49:16 +00:00
|
|
|
from . import debug_info, _BABEL_TRANSLATIONS
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-12-12 10:23:17 +00:00
|
|
|
try:
|
|
|
|
from functools import wraps
|
|
|
|
except ImportError:
|
|
|
|
pass # We're not using Python 3
|
|
|
|
|
2020-04-14 16:28:16 +00:00
|
|
|
log = logger.create()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
feature_support = {
|
2019-12-28 06:12:18 +00:00
|
|
|
'ldap': bool(services.ldap),
|
2020-01-11 18:10:39 +00:00
|
|
|
'goodreads': bool(services.goodreads_support),
|
2021-02-06 08:42:27 +00:00
|
|
|
'kobo': bool(services.kobo),
|
2021-03-28 12:50:55 +00:00
|
|
|
'updater': constants.UPDATER_AVAILABLE,
|
|
|
|
'gmail': bool(services.gmail)
|
2019-07-13 18:45:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:50:08 +00:00
|
|
|
try:
|
2021-03-21 06:15:38 +00:00
|
|
|
import rarfile # pylint: disable=unused-import
|
2020-04-28 14:50:08 +00:00
|
|
|
feature_support['rar'] = True
|
2020-11-30 17:32:21 +00:00
|
|
|
except (ImportError, SyntaxError):
|
2020-04-28 14:50:08 +00:00
|
|
|
feature_support['rar'] = False
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-20 18:01:05 +00:00
|
|
|
from .oauth_bb import oauth_check, oauthblueprints
|
2019-07-13 18:45:48 +00:00
|
|
|
feature_support['oauth'] = True
|
2020-04-14 16:28:16 +00:00
|
|
|
except ImportError as err:
|
|
|
|
log.debug('Cannot import Flask-Dance, login with Oauth will not work: %s', err)
|
2019-07-13 18:45:48 +00:00
|
|
|
feature_support['oauth'] = False
|
2019-07-20 18:01:05 +00:00
|
|
|
oauthblueprints = []
|
2019-07-13 18:45:48 +00:00
|
|
|
oauth_check = {}
|
|
|
|
|
|
|
|
|
|
|
|
feature_support['gdrive'] = gdrive_support
|
|
|
|
admi = Blueprint('admin', __name__)
|
2020-04-14 16:28:16 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-12-12 10:23:17 +00:00
|
|
|
def admin_required(f):
|
|
|
|
"""
|
|
|
|
Checks if current_user.role == 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(f)
|
|
|
|
def inner(*args, **kwargs):
|
|
|
|
if current_user.role_admin():
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
@admi.before_app_request
|
|
|
|
def before_request():
|
2021-07-30 09:43:26 +00:00
|
|
|
# make remember me function work
|
2020-12-12 10:23:17 +00:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
confirm_login()
|
2021-07-30 09:43:26 +00:00
|
|
|
if not ub.check_user_session(current_user.id, flask_session.get('_id')) and 'opds' not in request.path:
|
2021-07-25 03:24:03 +00:00
|
|
|
logout_user()
|
2020-12-12 10:23:17 +00:00
|
|
|
g.constants = constants
|
|
|
|
g.user = current_user
|
|
|
|
g.allow_registration = config.config_public_reg
|
|
|
|
g.allow_anonymous = config.config_anonbrowse
|
|
|
|
g.allow_upload = config.config_uploading
|
|
|
|
g.current_theme = config.config_theme
|
|
|
|
g.config_authors_max = config.config_authors_max
|
|
|
|
g.shelves_access = ub.session.query(ub.Shelf).filter(
|
|
|
|
or_(ub.Shelf.is_public == 1, ub.Shelf.user_id == current_user.id)).order_by(ub.Shelf.name).all()
|
2021-01-03 08:53:34 +00:00
|
|
|
if '/static/' not in request.path and not config.db_configured and \
|
2021-05-26 11:35:35 +00:00
|
|
|
request.endpoint not in ('admin.ajax_db_config',
|
|
|
|
'admin.simulatedbchange',
|
|
|
|
'admin.db_configuration',
|
|
|
|
'web.login',
|
|
|
|
'web.logout',
|
|
|
|
'admin.load_dialogtexts',
|
|
|
|
'admin.ajax_pathchooser'):
|
|
|
|
return redirect(url_for('admin.db_configuration'))
|
2020-12-12 10:23:17 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
@admi.route("/admin")
|
|
|
|
@login_required
|
|
|
|
def admin_forbidden():
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/shutdown")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def shutdown():
|
|
|
|
task = int(request.args.get("parameter").strip())
|
2020-04-05 15:31:47 +00:00
|
|
|
showtext = {}
|
2019-07-13 18:45:48 +00:00
|
|
|
if task in (0, 1): # valid commandos received
|
|
|
|
# close all database connections
|
2020-05-21 16:16:11 +00:00
|
|
|
calibre_db.dispose()
|
2019-07-13 18:45:48 +00:00
|
|
|
ub.dispose()
|
|
|
|
|
|
|
|
if task == 0:
|
|
|
|
showtext['text'] = _(u'Server restarted, please reload page')
|
|
|
|
else:
|
|
|
|
showtext['text'] = _(u'Performing shutdown of server, please close window')
|
|
|
|
# stop gevent/tornado server
|
2021-04-04 17:40:34 +00:00
|
|
|
web_server.stop(task==0)
|
2019-07-13 18:45:48 +00:00
|
|
|
return json.dumps(showtext)
|
|
|
|
|
|
|
|
if task == 2:
|
|
|
|
log.warning("reconnecting to calibre database")
|
2020-08-15 10:08:59 +00:00
|
|
|
calibre_db.reconnect_db(config, ub.app_DB_path)
|
2020-04-05 15:31:47 +00:00
|
|
|
showtext['text'] = _(u'Reconnect successful')
|
|
|
|
return json.dumps(showtext)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-04-05 15:31:47 +00:00
|
|
|
showtext['text'] = _(u'Unknown command')
|
|
|
|
return json.dumps(showtext), 400
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/view")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def admin():
|
|
|
|
version = updater_thread.get_current_version_info()
|
|
|
|
if version is False:
|
|
|
|
commit = _(u'Unknown')
|
|
|
|
else:
|
|
|
|
if 'datetime' in version:
|
|
|
|
commit = version['datetime']
|
|
|
|
|
|
|
|
tz = timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone)
|
|
|
|
form_date = datetime.strptime(commit[:19], "%Y-%m-%dT%H:%M:%S")
|
|
|
|
if len(commit) > 19: # check if string has timezone
|
|
|
|
if commit[19] == '+':
|
|
|
|
form_date -= timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
|
|
|
|
elif commit[19] == '-':
|
|
|
|
form_date += timedelta(hours=int(commit[20:22]), minutes=int(commit[23:]))
|
|
|
|
commit = format_datetime(form_date - tz, format='short', locale=get_locale())
|
|
|
|
else:
|
|
|
|
commit = version['version']
|
|
|
|
|
|
|
|
allUser = ub.session.query(ub.User).all()
|
|
|
|
email_settings = config.get_mail_settings()
|
2020-11-07 10:44:02 +00:00
|
|
|
kobo_support = feature_support['kobo'] and config.config_kobo_sync
|
2019-07-13 18:45:48 +00:00
|
|
|
return render_title_template("admin.html", allUser=allUser, email=email_settings, config=config, commit=commit,
|
2020-11-07 10:44:02 +00:00
|
|
|
feature_support=feature_support, kobo_support=kobo_support,
|
2019-07-13 18:45:48 +00:00
|
|
|
title=_(u"Admin page"), page="admin")
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
@admi.route("/admin/dbconfig", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def db_configuration():
|
|
|
|
if request.method == "POST":
|
|
|
|
return _db_configuration_update_helper()
|
|
|
|
return _db_configuration_result()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
|
|
|
|
@admi.route("/admin/config", methods=["GET"])
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def configuration():
|
2021-05-26 11:35:35 +00:00
|
|
|
return render_title_template("config_edit.html",
|
|
|
|
config=config,
|
|
|
|
provider=oauthblueprints,
|
|
|
|
feature_support=feature_support,
|
|
|
|
title=_(u"Basic Configuration"), page="config")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
@admi.route("/admin/ajaxconfig", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def ajax_config():
|
|
|
|
return _configuration_update_helper()
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/ajaxdbconfig", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def ajax_db_config():
|
|
|
|
return _db_configuration_update_helper()
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/alive", methods=["GET"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def calibreweb_alive():
|
|
|
|
return "", 200
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@admi.route("/admin/viewconfig")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def view_configuration():
|
2021-01-03 08:53:34 +00:00
|
|
|
read_column = calibre_db.session.query(db.Custom_Columns)\
|
|
|
|
.filter(and_(db.Custom_Columns.datatype == 'bool', db.Custom_Columns.mark_for_delete == 0)).all()
|
|
|
|
restrict_columns = calibre_db.session.query(db.Custom_Columns)\
|
|
|
|
.filter(and_(db.Custom_Columns.datatype == 'text', db.Custom_Columns.mark_for_delete == 0)).all()
|
|
|
|
return render_title_template("config_view_edit.html", conf=config, readColumns=read_column,
|
|
|
|
restrictColumns=restrict_columns,
|
2019-07-13 18:45:48 +00:00
|
|
|
title=_(u"UI Configuration"), page="uiconfig")
|
|
|
|
|
2021-01-31 09:17:40 +00:00
|
|
|
@admi.route("/admin/usertable")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def edit_user_table():
|
|
|
|
visibility = current_user.view_settings.get('useredit', {})
|
2021-02-20 18:55:51 +00:00
|
|
|
languages = calibre_db.speaking_language()
|
|
|
|
translations = babel.list_translations() + [LC('en')]
|
2021-01-31 13:54:45 +00:00
|
|
|
allUser = ub.session.query(ub.User)
|
2021-04-25 08:42:41 +00:00
|
|
|
tags = calibre_db.session.query(db.Tags)\
|
|
|
|
.join(db.books_tags_link)\
|
|
|
|
.join(db.Books)\
|
|
|
|
.filter(calibre_db.common_filters()) \
|
|
|
|
.group_by(text('books_tags_link.tag'))\
|
|
|
|
.order_by(db.Tags.name).all()
|
|
|
|
if config.config_restricted_column:
|
|
|
|
custom_values = calibre_db.session.query(db.cc_classes[config.config_restricted_column]).all()
|
|
|
|
else:
|
|
|
|
custom_values = []
|
2021-01-31 13:54:45 +00:00
|
|
|
if not config.config_anonbrowse:
|
|
|
|
allUser = allUser.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
|
2021-05-15 08:45:51 +00:00
|
|
|
kobo_support = feature_support['kobo'] and config.config_kobo_sync
|
2021-01-31 13:54:45 +00:00
|
|
|
return render_title_template("user_table.html",
|
|
|
|
users=allUser.all(),
|
2021-04-25 08:42:41 +00:00
|
|
|
tags=tags,
|
|
|
|
custom_values=custom_values,
|
2021-02-20 18:55:51 +00:00
|
|
|
translations=translations,
|
|
|
|
languages=languages,
|
2021-01-31 13:54:45 +00:00
|
|
|
visiblility=visibility,
|
2021-02-13 12:17:02 +00:00
|
|
|
all_roles=constants.ALL_ROLES,
|
2021-05-15 08:45:51 +00:00
|
|
|
kobo_support=kobo_support,
|
2021-02-13 12:17:02 +00:00
|
|
|
sidebar_settings=constants.sidebar_settings,
|
2021-01-31 13:54:45 +00:00
|
|
|
title=_(u"Edit Users"),
|
|
|
|
page="usertable")
|
2021-01-31 09:17:40 +00:00
|
|
|
|
2021-04-25 08:42:41 +00:00
|
|
|
|
2021-02-13 12:17:02 +00:00
|
|
|
@admi.route("/ajax/listusers")
|
2021-01-31 09:17:40 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def list_users():
|
2021-04-11 17:59:20 +00:00
|
|
|
off = int(request.args.get("offset") or 0)
|
|
|
|
limit = int(request.args.get("limit") or 10)
|
2021-01-31 09:17:40 +00:00
|
|
|
search = request.args.get("search")
|
2021-04-13 17:08:02 +00:00
|
|
|
sort = request.args.get("sort", "id")
|
2021-04-12 16:39:09 +00:00
|
|
|
order = request.args.get("order", "").lower()
|
2021-04-11 17:59:20 +00:00
|
|
|
state = None
|
2021-04-12 16:39:09 +00:00
|
|
|
if sort == "state":
|
2021-04-12 17:04:27 +00:00
|
|
|
state = json.loads(request.args.get("state", "[]"))
|
2021-04-12 16:39:09 +00:00
|
|
|
|
2021-04-11 17:59:20 +00:00
|
|
|
if sort != "state" and order:
|
2021-04-08 17:37:08 +00:00
|
|
|
order = text(sort + " " + order)
|
2021-04-12 16:39:09 +00:00
|
|
|
elif not state:
|
2021-04-13 17:08:02 +00:00
|
|
|
order = ub.User.id.asc()
|
2021-04-08 17:37:08 +00:00
|
|
|
|
2021-01-31 13:54:45 +00:00
|
|
|
all_user = ub.session.query(ub.User)
|
|
|
|
if not config.config_anonbrowse:
|
|
|
|
all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
|
2021-04-11 17:59:20 +00:00
|
|
|
|
|
|
|
total_count = filtered_count = all_user.count()
|
|
|
|
|
2021-01-31 09:17:40 +00:00
|
|
|
if search:
|
2021-04-11 17:59:20 +00:00
|
|
|
all_user = all_user.filter(or_(func.lower(ub.User.name).ilike("%" + search + "%"),
|
2021-02-13 12:17:02 +00:00
|
|
|
func.lower(ub.User.kindle_mail).ilike("%" + search + "%"),
|
2021-04-11 17:59:20 +00:00
|
|
|
func.lower(ub.User.email).ilike("%" + search + "%")))
|
|
|
|
if state:
|
2021-04-12 16:39:09 +00:00
|
|
|
users = calibre_db.get_checkbox_sorted(all_user.all(), state, off, limit, request.args.get("order", "").lower())
|
2021-01-31 09:17:40 +00:00
|
|
|
else:
|
2021-04-08 17:37:08 +00:00
|
|
|
users = all_user.order_by(order).offset(off).limit(limit).all()
|
2021-04-11 17:59:20 +00:00
|
|
|
if search:
|
|
|
|
filtered_count = len(users)
|
2021-01-31 09:17:40 +00:00
|
|
|
|
2021-02-20 13:18:27 +00:00
|
|
|
for user in users:
|
|
|
|
if user.default_language == "all":
|
2021-05-16 07:37:45 +00:00
|
|
|
user.default = _("All")
|
2021-02-20 13:18:27 +00:00
|
|
|
else:
|
|
|
|
user.default = LC.parse(user.default_language).get_language_name(get_locale())
|
|
|
|
|
2021-01-31 09:17:40 +00:00
|
|
|
table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": users}
|
|
|
|
js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)
|
|
|
|
response = make_response(js_list)
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2021-04-10 09:32:11 +00:00
|
|
|
@admi.route("/ajax/deleteuser", methods=['POST'])
|
2021-02-13 12:17:02 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def delete_user():
|
2021-04-21 17:23:11 +00:00
|
|
|
user_ids = request.form.to_dict(flat=False)
|
2021-05-01 06:36:15 +00:00
|
|
|
users = None
|
2021-04-21 17:23:11 +00:00
|
|
|
if "userid[]" in user_ids:
|
|
|
|
users = ub.session.query(ub.User).filter(ub.User.id.in_(user_ids['userid[]'])).all()
|
|
|
|
elif "userid" in user_ids:
|
|
|
|
users = ub.session.query(ub.User).filter(ub.User.id == user_ids['userid'][0]).all()
|
|
|
|
count = 0
|
|
|
|
errors = list()
|
|
|
|
success = list()
|
|
|
|
if not users:
|
|
|
|
log.error("User not found")
|
|
|
|
return Response(json.dumps({'type': "danger", 'message': _("User not found")}), mimetype='application/json')
|
|
|
|
for user in users:
|
|
|
|
try:
|
|
|
|
message = _delete_user(user)
|
|
|
|
count += 1
|
|
|
|
except Exception as ex:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error(ex)
|
2021-04-21 17:23:11 +00:00
|
|
|
errors.append({'type': "danger", 'message': str(ex)})
|
|
|
|
|
|
|
|
if count == 1:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.info("User {} deleted".format(user_ids))
|
2021-04-21 17:23:11 +00:00
|
|
|
success = [{'type': "success", 'message': message}]
|
|
|
|
elif count > 1:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.info("Users {} deleted".format(user_ids))
|
2021-04-21 17:23:11 +00:00
|
|
|
success = [{'type': "success", 'message': _("{} users deleted successfully").format(count)}]
|
|
|
|
success.extend(errors)
|
|
|
|
return Response(json.dumps(success), mimetype='application/json')
|
2021-02-20 13:18:27 +00:00
|
|
|
|
|
|
|
@admi.route("/ajax/getlocale")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def table_get_locale():
|
|
|
|
locale = babel.list_translations() + [LC('en')]
|
|
|
|
ret = list()
|
|
|
|
current_locale = get_locale()
|
|
|
|
for loc in locale:
|
2021-04-04 17:40:34 +00:00
|
|
|
ret.append({'value': str(loc), 'text': loc.get_language_name(current_locale)})
|
2021-02-20 13:18:27 +00:00
|
|
|
return json.dumps(ret)
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/ajax/getdefaultlanguage")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def table_get_default_lang():
|
|
|
|
languages = calibre_db.speaking_language()
|
|
|
|
ret = list()
|
2021-04-25 08:42:41 +00:00
|
|
|
ret.append({'value': 'all', 'text': _('Show All')})
|
2021-02-20 13:18:27 +00:00
|
|
|
for lang in languages:
|
2021-04-04 17:40:34 +00:00
|
|
|
ret.append({'value': lang.lang_code, 'text': lang.name})
|
2021-02-20 13:18:27 +00:00
|
|
|
return json.dumps(ret)
|
2021-02-13 12:17:02 +00:00
|
|
|
|
2021-01-31 09:17:40 +00:00
|
|
|
|
2021-02-14 12:09:31 +00:00
|
|
|
@admi.route("/ajax/editlistusers/<param>", methods=['POST'])
|
2021-01-31 09:17:40 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def edit_list_user(param):
|
2021-02-14 12:09:31 +00:00
|
|
|
vals = request.form.to_dict(flat=False)
|
2021-01-31 13:54:45 +00:00
|
|
|
all_user = ub.session.query(ub.User)
|
|
|
|
if not config.config_anonbrowse:
|
|
|
|
all_user = all_user.filter(ub.User.role.op('&')(constants.ROLE_ANONYMOUS) != constants.ROLE_ANONYMOUS)
|
2021-02-14 12:09:31 +00:00
|
|
|
# only one user is posted
|
|
|
|
if "pk" in vals:
|
2021-02-20 13:18:27 +00:00
|
|
|
users = [all_user.filter(ub.User.id == vals['pk'][0]).one_or_none()]
|
2021-02-14 12:09:31 +00:00
|
|
|
else:
|
2021-02-20 13:18:27 +00:00
|
|
|
if "pk[]" in vals:
|
|
|
|
users = all_user.filter(ub.User.id.in_(vals['pk[]'])).all()
|
2021-01-31 09:17:40 +00:00
|
|
|
else:
|
2021-04-28 18:49:16 +00:00
|
|
|
return _("Malformed request"), 400
|
2021-02-20 13:18:27 +00:00
|
|
|
if 'field_index' in vals:
|
|
|
|
vals['field_index'] = vals['field_index'][0]
|
|
|
|
if 'value' in vals:
|
|
|
|
vals['value'] = vals['value'][0]
|
2021-04-25 08:42:41 +00:00
|
|
|
elif not ('value[]' in vals):
|
2021-04-28 18:49:16 +00:00
|
|
|
return _("Malformed request"), 400
|
2021-02-20 13:18:27 +00:00
|
|
|
for user in users:
|
2021-04-04 17:40:34 +00:00
|
|
|
try:
|
2021-04-25 08:42:41 +00:00
|
|
|
if param in ['denied_tags', 'allowed_tags', 'allowed_column_value', 'denied_column_value']:
|
|
|
|
if 'value[]' in vals:
|
|
|
|
setattr(user, param, prepare_tags(user, vals['action'][0], param, vals['value[]']))
|
2021-04-04 17:40:34 +00:00
|
|
|
else:
|
2021-04-25 08:42:41 +00:00
|
|
|
setattr(user, param, vals['value'].strip())
|
|
|
|
else:
|
|
|
|
vals['value'] = vals['value'].strip()
|
|
|
|
if param == 'name':
|
|
|
|
if user.name == "Guest":
|
|
|
|
raise Exception(_("Guest Name can't be changed"))
|
|
|
|
user.name = check_username(vals['value'])
|
|
|
|
elif param =='email':
|
|
|
|
user.email = check_email(vals['value'])
|
2021-05-15 08:45:51 +00:00
|
|
|
elif param =='kobo_only_shelves_sync':
|
|
|
|
user.kobo_only_shelves_sync = int(vals['value'] == 'true')
|
2021-04-25 08:42:41 +00:00
|
|
|
elif param == 'kindle_mail':
|
|
|
|
user.kindle_mail = valid_email(vals['value']) if vals['value'] else ""
|
|
|
|
elif param.endswith('role'):
|
2021-05-01 06:36:15 +00:00
|
|
|
value = int(vals['field_index'])
|
|
|
|
if user.name == "Guest" and value in \
|
2021-04-25 08:42:41 +00:00
|
|
|
[constants.ROLE_ADMIN, constants.ROLE_PASSWD, constants.ROLE_EDIT_SHELFS]:
|
|
|
|
raise Exception(_("Guest can't have this role"))
|
2021-05-01 06:36:15 +00:00
|
|
|
# check for valid value, last on checks for power of 2 value
|
|
|
|
if value > 0 and value <= constants.ROLE_VIEWER and (value & value-1 == 0 or value == 1):
|
|
|
|
if vals['value'] == 'true':
|
|
|
|
user.role |= value
|
|
|
|
elif vals['value'] == 'false':
|
|
|
|
if value == constants.ROLE_ADMIN:
|
|
|
|
if not ub.session.query(ub.User).\
|
|
|
|
filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
|
|
|
|
ub.User.id != user.id).count():
|
|
|
|
return Response(
|
|
|
|
json.dumps([{'type': "danger",
|
|
|
|
'message':_(u"No admin user remaining, can't remove admin role",
|
|
|
|
nick=user.name)}]), mimetype='application/json')
|
|
|
|
user.role &= ~value
|
|
|
|
else:
|
|
|
|
raise Exception(_("Value has to be true or false"))
|
2021-04-25 08:42:41 +00:00
|
|
|
else:
|
2021-05-01 06:36:15 +00:00
|
|
|
raise Exception(_("Invalid role"))
|
2021-04-25 08:42:41 +00:00
|
|
|
elif param.startswith('sidebar'):
|
2021-05-01 06:36:15 +00:00
|
|
|
value = int(vals['field_index'])
|
|
|
|
if user.name == "Guest" and value == constants.SIDEBAR_READ_AND_UNREAD:
|
2021-04-25 08:42:41 +00:00
|
|
|
raise Exception(_("Guest can't have this view"))
|
2021-05-01 06:36:15 +00:00
|
|
|
# check for valid value, last on checks for power of 2 value
|
|
|
|
if value > 0 and value <= constants.SIDEBAR_LIST and (value & value-1 == 0 or value == 1):
|
|
|
|
if vals['value'] == 'true':
|
|
|
|
user.sidebar_view |= value
|
|
|
|
elif vals['value'] == 'false':
|
|
|
|
user.sidebar_view &= ~value
|
|
|
|
else:
|
|
|
|
raise Exception(_("Value has to be true or false"))
|
2021-04-25 08:42:41 +00:00
|
|
|
else:
|
2021-05-01 06:36:15 +00:00
|
|
|
raise Exception(_("Invalid view"))
|
2021-04-25 08:42:41 +00:00
|
|
|
elif param == 'locale':
|
|
|
|
if user.name == "Guest":
|
|
|
|
raise Exception(_("Guest's Locale is determined automatically and can't be set"))
|
2021-04-28 18:49:16 +00:00
|
|
|
if vals['value'] in _BABEL_TRANSLATIONS:
|
|
|
|
user.locale = vals['value']
|
|
|
|
else:
|
|
|
|
raise Exception(_("No Valid Locale Given"))
|
2021-04-25 08:42:41 +00:00
|
|
|
elif param == 'default_language':
|
2021-04-28 18:49:16 +00:00
|
|
|
languages = calibre_db.session.query(db.Languages) \
|
|
|
|
.join(db.books_languages_link) \
|
|
|
|
.join(db.Books) \
|
|
|
|
.filter(calibre_db.common_filters()) \
|
|
|
|
.group_by(text('books_languages_link.lang_code')).all()
|
|
|
|
lang_codes = [lang.lang_code for lang in languages] + ["all"]
|
|
|
|
if vals['value'] in lang_codes:
|
|
|
|
user.default_language = vals['value']
|
|
|
|
else:
|
|
|
|
raise Exception(_("No Valid Book Language Given"))
|
|
|
|
else:
|
|
|
|
return _("Parameter not found"), 400
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
2021-04-25 08:42:41 +00:00
|
|
|
log.debug_or_exception(ex)
|
2021-04-04 17:40:34 +00:00
|
|
|
return str(ex), 400
|
2021-01-31 09:17:40 +00:00
|
|
|
ub.session_commit()
|
|
|
|
return ""
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-02-13 12:17:02 +00:00
|
|
|
@admi.route("/ajax/user_table_settings", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def update_table_settings():
|
|
|
|
current_user.view_settings['useredit'] = json.loads(request.data)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
flag_modified(current_user, "view_settings")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
ub.session.commit()
|
|
|
|
except (InvalidRequestError, OperationalError):
|
2021-04-04 17:40:34 +00:00
|
|
|
log.error("Invalid request received: {}".format(request))
|
2021-02-13 12:17:02 +00:00
|
|
|
return "Invalid request", 400
|
|
|
|
return ""
|
|
|
|
|
2021-05-01 15:10:29 +00:00
|
|
|
def check_valid_read_column(column):
|
2021-05-01 16:42:57 +00:00
|
|
|
if column != "0":
|
2021-05-01 15:10:29 +00:00
|
|
|
if not calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.id == column) \
|
|
|
|
.filter(and_(db.Custom_Columns.datatype == 'bool', db.Custom_Columns.mark_for_delete == 0)).all():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_valid_restricted_column(column):
|
2021-05-01 16:42:57 +00:00
|
|
|
if column != "0":
|
2021-05-01 15:10:29 +00:00
|
|
|
if not calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.id == column) \
|
|
|
|
.filter(and_(db.Custom_Columns.datatype == 'text', db.Custom_Columns.mark_for_delete == 0)).all():
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-02-13 12:17:02 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@admi.route("/admin/viewconfig", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def update_view_configuration():
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
|
2021-05-02 08:05:25 +00:00
|
|
|
# _config_string = lambda x: config.set_from_dictionary(to_save, x, lambda y: y.strip() if y else y)
|
|
|
|
# _config_int = lambda x: config.set_from_dictionary(to_save, x, int)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-05-02 08:05:25 +00:00
|
|
|
_config_string(to_save, "config_calibre_web_title")
|
|
|
|
_config_string(to_save, "config_columns_to_ignore")
|
|
|
|
if _config_string(to_save, "config_title_regex"):
|
2020-12-02 10:09:55 +00:00
|
|
|
calibre_db.update_title_sort(config)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-05-01 15:10:29 +00:00
|
|
|
if not check_valid_read_column(to_save.get("config_read_column", "0")):
|
|
|
|
flash(_(u"Invalid Read Column"), category="error")
|
|
|
|
log.debug("Invalid Read column")
|
|
|
|
return view_configuration()
|
2021-05-02 08:05:25 +00:00
|
|
|
_config_int(to_save, "config_read_column")
|
2021-05-01 15:10:29 +00:00
|
|
|
|
|
|
|
if not check_valid_restricted_column(to_save.get("config_restricted_column", "0")):
|
|
|
|
flash(_(u"Invalid Restricted Column"), category="error")
|
|
|
|
log.debug("Invalid Restricted Column")
|
|
|
|
return view_configuration()
|
2021-05-02 08:05:25 +00:00
|
|
|
_config_int(to_save, "config_restricted_column")
|
2021-05-01 15:10:29 +00:00
|
|
|
|
2021-05-02 08:05:25 +00:00
|
|
|
_config_int(to_save, "config_theme")
|
|
|
|
_config_int(to_save, "config_random_books")
|
|
|
|
_config_int(to_save, "config_books_per_page")
|
|
|
|
_config_int(to_save, "config_authors_max")
|
2021-05-01 15:10:29 +00:00
|
|
|
|
2019-12-30 14:15:07 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
config.config_default_role = constants.selected_roles(to_save)
|
|
|
|
config.config_default_role &= ~constants.ROLE_ANONYMOUS
|
|
|
|
|
|
|
|
config.config_default_show = sum(int(k[5:]) for k in to_save if k.startswith('show_'))
|
2020-01-05 07:43:00 +00:00
|
|
|
if "Show_detail_random" in to_save:
|
|
|
|
config.config_default_show |= constants.DETAIL_RANDOM
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
config.save()
|
|
|
|
flash(_(u"Calibre-Web configuration updated"), category="success")
|
2021-04-25 09:20:21 +00:00
|
|
|
log.debug("Calibre-Web configuration updated")
|
2019-07-13 18:45:48 +00:00
|
|
|
before_request()
|
|
|
|
|
|
|
|
return view_configuration()
|
|
|
|
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
@admi.route("/ajax/loaddialogtexts/<element_id>", methods=['POST'])
|
2020-12-20 18:17:29 +00:00
|
|
|
@login_required
|
|
|
|
def load_dialogtexts(element_id):
|
2021-05-26 11:35:35 +00:00
|
|
|
texts = {"header": "", "main": "", "valid": 1}
|
2020-12-20 18:17:29 +00:00
|
|
|
if element_id == "config_delete_kobo_token":
|
|
|
|
texts["main"] = _('Do you really want to delete the Kobo Token?')
|
|
|
|
elif element_id == "btndeletedomain":
|
|
|
|
texts["main"] = _('Do you really want to delete this domain?')
|
|
|
|
elif element_id == "btndeluser":
|
|
|
|
texts["main"] = _('Do you really want to delete this user?')
|
|
|
|
elif element_id == "delete_shelf":
|
|
|
|
texts["main"] = _('Are you sure you want to delete this shelf?')
|
2021-03-16 14:40:58 +00:00
|
|
|
elif element_id == "select_locale":
|
|
|
|
texts["main"] = _('Are you sure you want to change locales of selected user(s)?')
|
|
|
|
elif element_id == "select_default_language":
|
|
|
|
texts["main"] = _('Are you sure you want to change visible book languages for selected user(s)?')
|
|
|
|
elif element_id == "role":
|
|
|
|
texts["main"] = _('Are you sure you want to change the selected role for the selected user(s)?')
|
2021-04-25 08:42:41 +00:00
|
|
|
elif element_id == "restrictions":
|
|
|
|
texts["main"] = _('Are you sure you want to change the selected restrictions for the selected user(s)?')
|
2021-03-16 14:40:58 +00:00
|
|
|
elif element_id == "sidebar_view":
|
|
|
|
texts["main"] = _('Are you sure you want to change the selected visibility restrictions for the selected user(s)?')
|
2021-05-15 08:45:51 +00:00
|
|
|
elif element_id == "kobo_only_shelves_sync":
|
|
|
|
texts["main"] = _('Are you sure you want to change shelf sync behavior for the selected user(s)?')
|
2021-05-26 11:35:35 +00:00
|
|
|
elif element_id == "db_submit":
|
2021-06-29 16:24:08 +00:00
|
|
|
texts["main"] = _('Are you sure you want to change Calibre library location?')
|
2020-12-20 18:17:29 +00:00
|
|
|
return json.dumps(texts)
|
|
|
|
|
|
|
|
|
2019-12-29 12:54:52 +00:00
|
|
|
@admi.route("/ajax/editdomain/<int:allow>", methods=['POST'])
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-12-29 12:54:52 +00:00
|
|
|
def edit_domain(allow):
|
2019-07-13 18:45:48 +00:00
|
|
|
# POST /post
|
|
|
|
# name: 'username', //name of field (column in db)
|
|
|
|
# pk: 1 //primary key (record id)
|
|
|
|
# value: 'superuser!' //new value
|
|
|
|
vals = request.form.to_dict()
|
|
|
|
answer = ub.session.query(ub.Registration).filter(ub.Registration.id == vals['pk']).first()
|
|
|
|
answer.domain = vals['value'].replace('*', '%').replace('?', '_').lower()
|
2021-01-03 08:53:34 +00:00
|
|
|
return ub.session_commit("Registering Domains edited {}".format(answer.domain))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2019-12-29 12:54:52 +00:00
|
|
|
@admi.route("/ajax/adddomain/<int:allow>", methods=['POST'])
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-12-29 12:54:52 +00:00
|
|
|
def add_domain(allow):
|
2019-07-13 18:45:48 +00:00
|
|
|
domain_name = request.form.to_dict()['domainname'].replace('*', '%').replace('?', '_').lower()
|
2020-11-15 13:19:25 +00:00
|
|
|
check = ub.session.query(ub.Registration).filter(ub.Registration.domain == domain_name)\
|
|
|
|
.filter(ub.Registration.allow == allow).first()
|
2019-07-13 18:45:48 +00:00
|
|
|
if not check:
|
2019-12-29 12:54:52 +00:00
|
|
|
new_domain = ub.Registration(domain=domain_name, allow=allow)
|
2019-07-13 18:45:48 +00:00
|
|
|
ub.session.add(new_domain)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("Registering Domains added {}".format(domain_name))
|
2019-07-13 18:45:48 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/ajax/deletedomain", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def delete_domain():
|
2020-12-07 18:53:34 +00:00
|
|
|
try:
|
2020-12-20 18:17:29 +00:00
|
|
|
domain_id = request.form.to_dict()['domainid'].replace('*', '%').replace('?', '_').lower()
|
|
|
|
ub.session.query(ub.Registration).filter(ub.Registration.id == domain_id).delete()
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("Registering Domains deleted {}".format(domain_id))
|
2020-12-20 18:17:29 +00:00
|
|
|
# If last domain was deleted, add all domains by default
|
2021-01-03 08:53:34 +00:00
|
|
|
if not ub.session.query(ub.Registration).filter(ub.Registration.allow == 1).count():
|
|
|
|
new_domain = ub.Registration(domain="%.%", allow=1)
|
2020-12-20 18:17:29 +00:00
|
|
|
ub.session.add(new_domain)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit("Last Registering Domain deleted, added *.* as default")
|
2020-12-20 18:17:29 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2019-07-13 18:45:48 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2019-12-29 12:54:52 +00:00
|
|
|
@admi.route("/ajax/domainlist/<int:allow>")
|
2019-07-13 18:45:48 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-12-29 12:54:52 +00:00
|
|
|
def list_domain(allow):
|
|
|
|
answer = ub.session.query(ub.Registration).filter(ub.Registration.allow == allow).all()
|
2019-07-13 18:45:48 +00:00
|
|
|
json_dumps = json.dumps([{"domain": r.domain.replace('%', '*').replace('_', '?'), "id": r.id} for r in answer])
|
|
|
|
js = json.dumps(json_dumps.replace('"', "'")).lstrip('"').strip('"')
|
|
|
|
response = make_response(js.replace("'", '"'))
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
|
|
|
@admi.route("/ajax/editrestriction/<int:res_type>", defaults={"user_id": 0}, methods=['POST'])
|
2020-12-31 14:12:37 +00:00
|
|
|
@admi.route("/ajax/editrestriction/<int:res_type>/<int:user_id>", methods=['POST'])
|
2020-01-01 16:26:47 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-12-31 14:12:37 +00:00
|
|
|
def edit_restriction(res_type, user_id):
|
2020-01-01 16:26:47 +00:00
|
|
|
element = request.form.to_dict()
|
|
|
|
if element['id'].startswith('a'):
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 0: # Tags as template
|
2020-01-05 15:43:48 +00:00
|
|
|
elementlist = config.list_allowed_tags()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-01-05 15:43:48 +00:00
|
|
|
config.config_allowed_tags = ','.join(elementlist)
|
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 1: # CustomC
|
2020-01-05 15:43:48 +00:00
|
|
|
elementlist = config.list_allowed_column_values()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-01-05 15:43:48 +00:00
|
|
|
config.config_allowed_column_value = ','.join(elementlist)
|
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 2: # Tags per user
|
2020-12-31 14:12:37 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
elementlist = usr.list_allowed_tags()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-01-05 15:43:48 +00:00
|
|
|
usr.allowed_tags = ','.join(elementlist)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed allowed tags of user {} to {}".format(usr.name, usr.allowed_tags))
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 3: # CColumn per user
|
2020-12-31 14:12:37 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
elementlist = usr.list_allowed_column_values()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-01-05 15:43:48 +00:00
|
|
|
usr.allowed_column_value = ','.join(elementlist)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed allowed columns of user {} to {}".format(usr.name, usr.allowed_column_value))
|
2020-01-05 15:43:48 +00:00
|
|
|
if element['id'].startswith('d'):
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 0: # Tags as template
|
2020-02-15 09:21:45 +00:00
|
|
|
elementlist = config.list_denied_tags()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_tags = ','.join(elementlist)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 1: # CustomC
|
2020-02-15 09:21:45 +00:00
|
|
|
elementlist = config.list_denied_column_values()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_column_value = ','.join(elementlist)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 2: # Tags per user
|
2020-12-31 14:12:37 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
2020-02-15 09:21:45 +00:00
|
|
|
elementlist = usr.list_denied_tags()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_tags = ','.join(elementlist)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed denied tags of user {} to {}".format(usr.name, usr.denied_tags))
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 3: # CColumn per user
|
2020-12-31 14:12:37 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
2020-02-15 09:21:45 +00:00
|
|
|
elementlist = usr.list_denied_column_values()
|
2021-01-03 08:53:34 +00:00
|
|
|
elementlist[int(element['id'][1:])] = element['Element']
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_column_value = ','.join(elementlist)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed denied columns of user {} to {}".format(usr.name, usr.denied_column_value))
|
2020-01-01 16:26:47 +00:00
|
|
|
return ""
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2020-01-05 15:43:48 +00:00
|
|
|
def restriction_addition(element, list_func):
|
|
|
|
elementlist = list_func()
|
|
|
|
if elementlist == ['']:
|
|
|
|
elementlist = []
|
|
|
|
if not element['add_element'] in elementlist:
|
|
|
|
elementlist += [element['add_element']]
|
|
|
|
return ','.join(elementlist)
|
|
|
|
|
|
|
|
|
|
|
|
def restriction_deletion(element, list_func):
|
|
|
|
elementlist = list_func()
|
|
|
|
if element['Element'] in elementlist:
|
|
|
|
elementlist.remove(element['Element'])
|
|
|
|
return ','.join(elementlist)
|
|
|
|
|
2020-01-01 16:26:47 +00:00
|
|
|
|
2021-04-25 08:42:41 +00:00
|
|
|
def prepare_tags(user, action, tags_name, id_list):
|
|
|
|
if "tags" in tags_name:
|
|
|
|
tags = calibre_db.session.query(db.Tags).filter(db.Tags.id.in_(id_list)).all()
|
2021-05-01 06:36:15 +00:00
|
|
|
if not tags:
|
|
|
|
raise Exception(_("Tag not found"))
|
2021-04-25 08:42:41 +00:00
|
|
|
new_tags_list = [x.name for x in tags]
|
|
|
|
else:
|
|
|
|
tags = calibre_db.session.query(db.cc_classes[config.config_restricted_column])\
|
|
|
|
.filter(db.cc_classes[config.config_restricted_column].id.in_(id_list)).all()
|
|
|
|
new_tags_list = [x.value for x in tags]
|
|
|
|
saved_tags_list = user.__dict__[tags_name].split(",") if len(user.__dict__[tags_name]) else []
|
|
|
|
if action == "remove":
|
|
|
|
saved_tags_list = [x for x in saved_tags_list if x not in new_tags_list]
|
2021-05-01 06:36:15 +00:00
|
|
|
elif action == "add":
|
2021-04-25 08:42:41 +00:00
|
|
|
saved_tags_list.extend(x for x in new_tags_list if x not in saved_tags_list)
|
2021-05-01 06:36:15 +00:00
|
|
|
else:
|
|
|
|
raise Exception(_("Invalid Action"))
|
2021-04-25 08:42:41 +00:00
|
|
|
return ",".join(saved_tags_list)
|
|
|
|
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
@admi.route("/ajax/addrestriction/<int:res_type>", defaults={"user_id": 0}, methods=['POST'])
|
2020-12-31 14:08:56 +00:00
|
|
|
@admi.route("/ajax/addrestriction/<int:res_type>/<int:user_id>", methods=['POST'])
|
2020-01-01 16:26:47 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-12-31 14:08:56 +00:00
|
|
|
def add_restriction(res_type, user_id):
|
2020-01-01 16:26:47 +00:00
|
|
|
element = request.form.to_dict()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 0: # Tags as template
|
2020-01-01 16:26:47 +00:00
|
|
|
if 'submit_allow' in element:
|
2020-01-05 15:43:48 +00:00
|
|
|
config.config_allowed_tags = restriction_addition(element, config.list_allowed_tags)
|
|
|
|
config.save()
|
2020-01-01 16:26:47 +00:00
|
|
|
elif 'submit_deny' in element:
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_tags = restriction_addition(element, config.list_denied_tags)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 1: # CCustom as template
|
2020-01-05 15:43:48 +00:00
|
|
|
if 'submit_allow' in element:
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_allowed_column_value = restriction_addition(element, config.list_denied_column_values)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
|
|
|
elif 'submit_deny' in element:
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_column_value = restriction_addition(element, config.list_allowed_column_values)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 2: # Tags per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
if 'submit_allow' in element:
|
|
|
|
usr.allowed_tags = restriction_addition(element, usr.list_allowed_tags)
|
2021-05-01 18:52:48 +00:00
|
|
|
ub.session_commit("Changed allowed tags of user {} to {}".format(usr.name, usr.list_allowed_tags()))
|
2020-01-05 15:43:48 +00:00
|
|
|
elif 'submit_deny' in element:
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_tags = restriction_addition(element, usr.list_denied_tags)
|
2021-05-01 18:52:48 +00:00
|
|
|
ub.session_commit("Changed denied tags of user {} to {}".format(usr.name, usr.list_denied_tags()))
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 3: # CustomC per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
if 'submit_allow' in element:
|
|
|
|
usr.allowed_column_value = restriction_addition(element, usr.list_allowed_column_values)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed allowed columns of user {} to {}".format(usr.name,
|
2021-05-01 18:52:48 +00:00
|
|
|
usr.list_allowed_column_values()))
|
2020-01-05 15:43:48 +00:00
|
|
|
elif 'submit_deny' in element:
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_column_value = restriction_addition(element, usr.list_denied_column_values)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Changed denied columns of user {} to {}".format(usr.name,
|
2021-05-01 18:52:48 +00:00
|
|
|
usr.list_denied_column_values()))
|
2020-01-01 16:26:47 +00:00
|
|
|
return ""
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
|
|
|
@admi.route("/ajax/deleterestriction/<int:res_type>", defaults={"user_id": 0}, methods=['POST'])
|
2020-12-31 14:08:56 +00:00
|
|
|
@admi.route("/ajax/deleterestriction/<int:res_type>/<int:user_id>", methods=['POST'])
|
2020-01-01 16:26:47 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-12-31 14:08:56 +00:00
|
|
|
def delete_restriction(res_type, user_id):
|
2020-01-01 16:26:47 +00:00
|
|
|
element = request.form.to_dict()
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 0: # Tags as template
|
2020-01-05 15:43:48 +00:00
|
|
|
if element['id'].startswith('a'):
|
|
|
|
config.config_allowed_tags = restriction_deletion(element, config.list_allowed_tags)
|
|
|
|
config.save()
|
|
|
|
elif element['id'].startswith('d'):
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_tags = restriction_deletion(element, config.list_denied_tags)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 1: # CustomC as template
|
2020-01-05 15:43:48 +00:00
|
|
|
if element['id'].startswith('a'):
|
|
|
|
config.config_allowed_column_value = restriction_deletion(element, config.list_allowed_column_values)
|
|
|
|
config.save()
|
|
|
|
elif element['id'].startswith('d'):
|
2020-02-15 09:21:45 +00:00
|
|
|
config.config_denied_column_value = restriction_deletion(element, config.list_denied_column_values)
|
2020-01-05 15:43:48 +00:00
|
|
|
config.save()
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 2: # Tags per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
if element['id'].startswith('a'):
|
|
|
|
usr.allowed_tags = restriction_deletion(element, usr.list_allowed_tags)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Deleted allowed tags of user {}: {}".format(usr.name, usr.list_allowed_tags))
|
2020-01-05 15:43:48 +00:00
|
|
|
elif element['id'].startswith('d'):
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_tags = restriction_deletion(element, usr.list_denied_tags)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Deleted denied tags of user {}: {}".format(usr.name, usr.list_allowed_tags))
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 3: # Columns per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
|
|
|
if element['id'].startswith('a'):
|
|
|
|
usr.allowed_column_value = restriction_deletion(element, usr.list_allowed_column_values)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Deleted allowed columns of user {}: {}".format(usr.name,
|
2021-01-03 08:53:34 +00:00
|
|
|
usr.list_allowed_column_values))
|
|
|
|
|
2020-01-05 15:43:48 +00:00
|
|
|
elif element['id'].startswith('d'):
|
2020-02-15 09:21:45 +00:00
|
|
|
usr.denied_column_value = restriction_deletion(element, usr.list_denied_column_values)
|
2021-03-21 17:55:02 +00:00
|
|
|
ub.session_commit("Deleted denied columns of user {}: {}".format(usr.name,
|
2021-01-03 08:53:34 +00:00
|
|
|
usr.list_denied_column_values))
|
2020-01-01 16:26:47 +00:00
|
|
|
return ""
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
|
|
|
@admi.route("/ajax/listrestriction/<int:res_type>", defaults={"user_id": 0})
|
2020-12-31 14:08:56 +00:00
|
|
|
@admi.route("/ajax/listrestriction/<int:res_type>/<int:user_id>")
|
2020-01-01 16:26:47 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-12-31 14:08:56 +00:00
|
|
|
def list_restriction(res_type, user_id):
|
2020-04-27 18:01:13 +00:00
|
|
|
if res_type == 0: # Tags as template
|
2020-02-23 19:07:21 +00:00
|
|
|
restrict = [{'Element': x, 'type':_('Deny'), 'id': 'd'+str(i) }
|
2021-01-03 08:53:34 +00:00
|
|
|
for i,x in enumerate(config.list_denied_tags()) if x != '']
|
|
|
|
allow = [{'Element': x, 'type': _('Allow'), 'id': 'a'+str(i)}
|
|
|
|
for i, x in enumerate(config.list_allowed_tags()) if x != '']
|
2020-01-05 15:43:48 +00:00
|
|
|
json_dumps = restrict + allow
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 1: # CustomC as template
|
2021-01-03 08:53:34 +00:00
|
|
|
restrict = [{'Element': x, 'type': _('Deny'), 'id': 'd'+str(i)}
|
|
|
|
for i, x in enumerate(config.list_denied_column_values()) if x != '']
|
|
|
|
allow = [{'Element': x, 'type': _('Allow'), 'id': 'a'+str(i)}
|
|
|
|
for i, x in enumerate(config.list_allowed_column_values()) if x != '']
|
2020-01-01 16:26:47 +00:00
|
|
|
json_dumps = restrict + allow
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 2: # Tags per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == user_id).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
2021-01-03 08:53:34 +00:00
|
|
|
restrict = [{'Element': x, 'type': _('Deny'), 'id': 'd'+str(i)}
|
|
|
|
for i, x in enumerate(usr.list_denied_tags()) if x != '']
|
|
|
|
allow = [{'Element': x, 'type': _('Allow'), 'id': 'a'+str(i)}
|
|
|
|
for i, x in enumerate(usr.list_allowed_tags()) if x != '']
|
2020-01-05 15:43:48 +00:00
|
|
|
json_dumps = restrict + allow
|
2020-04-27 18:01:13 +00:00
|
|
|
elif res_type == 3: # CustomC per user
|
2020-12-31 14:08:56 +00:00
|
|
|
if isinstance(user_id, int):
|
2021-01-03 08:53:34 +00:00
|
|
|
usr = ub.session.query(ub.User).filter(ub.User.id == user_id).first()
|
2020-01-05 15:43:48 +00:00
|
|
|
else:
|
|
|
|
usr = current_user
|
2021-01-03 08:53:34 +00:00
|
|
|
restrict = [{'Element': x, 'type': _('Deny'), 'id': 'd'+str(i)}
|
|
|
|
for i, x in enumerate(usr.list_denied_column_values()) if x != '']
|
|
|
|
allow = [{'Element': x, 'type': _('Allow'), 'id': 'a'+str(i)}
|
|
|
|
for i, x in enumerate(usr.list_allowed_column_values()) if x != '']
|
2020-01-05 15:43:48 +00:00
|
|
|
json_dumps = restrict + allow
|
2020-01-01 16:26:47 +00:00
|
|
|
else:
|
2021-01-03 08:53:34 +00:00
|
|
|
json_dumps = ""
|
2020-01-01 16:26:47 +00:00
|
|
|
js = json.dumps(json_dumps)
|
|
|
|
response = make_response(js.replace("'", '"'))
|
|
|
|
response.headers["Content-Type"] = "application/json; charset=utf-8"
|
|
|
|
return response
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2020-12-11 07:37:37 +00:00
|
|
|
@admi.route("/ajax/pathchooser/")
|
2020-12-10 13:41:45 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2020-12-11 07:37:37 +00:00
|
|
|
def ajax_pathchooser():
|
|
|
|
return pathchooser()
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2020-12-10 13:41:45 +00:00
|
|
|
def pathchooser():
|
2020-12-27 10:27:15 +00:00
|
|
|
browse_for = "folder"
|
2020-12-11 07:37:37 +00:00
|
|
|
folder_only = request.args.get('folder', False) == "true"
|
|
|
|
file_filter = request.args.get('filter', "")
|
2020-12-10 13:41:45 +00:00
|
|
|
path = os.path.normpath(request.args.get('path', ""))
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
oldfile = path
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
else:
|
|
|
|
oldfile = ""
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
absolute = False
|
2020-12-10 13:41:45 +00:00
|
|
|
|
|
|
|
if os.path.isdir(path):
|
2021-01-03 08:53:34 +00:00
|
|
|
# if os.path.isabs(path):
|
2020-12-11 07:37:37 +00:00
|
|
|
cwd = os.path.realpath(path)
|
2021-01-03 08:53:34 +00:00
|
|
|
absolute = True
|
|
|
|
# else:
|
2020-12-11 07:37:37 +00:00
|
|
|
# cwd = os.path.relpath(path)
|
2020-12-10 13:41:45 +00:00
|
|
|
else:
|
|
|
|
cwd = os.getcwd()
|
|
|
|
|
|
|
|
cwd = os.path.normpath(os.path.realpath(cwd))
|
|
|
|
parentdir = os.path.dirname(cwd)
|
2021-01-03 08:53:34 +00:00
|
|
|
if not absolute:
|
2020-12-10 13:41:45 +00:00
|
|
|
if os.path.realpath(cwd) == os.path.realpath("/"):
|
|
|
|
cwd = os.path.relpath(cwd)
|
|
|
|
else:
|
|
|
|
cwd = os.path.relpath(cwd) + os.path.sep
|
|
|
|
parentdir = os.path.relpath(parentdir) + os.path.sep
|
|
|
|
|
|
|
|
if os.path.realpath(cwd) == os.path.realpath("/"):
|
|
|
|
parentdir = ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
folders = os.listdir(cwd)
|
|
|
|
except Exception:
|
|
|
|
folders = []
|
|
|
|
|
|
|
|
files = []
|
|
|
|
for f in folders:
|
|
|
|
try:
|
|
|
|
data = {"name": f, "fullpath": os.path.join(cwd, f)}
|
|
|
|
data["sort"] = data["fullpath"].lower()
|
|
|
|
except Exception:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if os.path.isfile(os.path.join(cwd, f)):
|
2020-12-11 07:37:37 +00:00
|
|
|
if folder_only:
|
|
|
|
continue
|
|
|
|
if file_filter != "" and file_filter != f:
|
|
|
|
continue
|
2020-12-10 13:41:45 +00:00
|
|
|
data["type"] = "file"
|
|
|
|
data["size"] = os.path.getsize(os.path.join(cwd, f))
|
|
|
|
|
|
|
|
power = 0
|
|
|
|
while (data["size"] >> 10) > 0.3:
|
|
|
|
power += 1
|
|
|
|
data["size"] >>= 10
|
|
|
|
units = ("", "K", "M", "G", "T")
|
|
|
|
data["size"] = str(data["size"]) + " " + units[power] + "Byte"
|
|
|
|
else:
|
|
|
|
data["type"] = "dir"
|
|
|
|
data["size"] = ""
|
|
|
|
|
|
|
|
files.append(data)
|
|
|
|
|
|
|
|
files = sorted(files, key=operator.itemgetter("type", "sort"))
|
|
|
|
|
|
|
|
context = {
|
|
|
|
"cwd": cwd,
|
|
|
|
"files": files,
|
|
|
|
"parentdir": parentdir,
|
|
|
|
"type": browse_for,
|
|
|
|
"oldfile": oldfile,
|
2021-01-24 06:31:40 +00:00
|
|
|
"absolute": absolute,
|
2020-12-10 13:41:45 +00:00
|
|
|
}
|
|
|
|
return json.dumps(context)
|
|
|
|
|
|
|
|
|
2020-05-09 18:29:17 +00:00
|
|
|
def _config_int(to_save, x, func=int):
|
|
|
|
return config.set_from_dictionary(to_save, x, func)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2020-05-09 13:44:53 +00:00
|
|
|
def _config_checkbox(to_save, x):
|
|
|
|
return config.set_from_dictionary(to_save, x, lambda y: y == "on", False)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
def _config_checkbox_int(to_save, x):
|
|
|
|
return config.set_from_dictionary(to_save, x, lambda y: 1 if (y == "on") else 0, 0)
|
|
|
|
|
|
|
|
|
|
|
|
def _config_string(to_save, x):
|
|
|
|
return config.set_from_dictionary(to_save, x, lambda y: y.strip() if y else y)
|
|
|
|
|
|
|
|
|
|
|
|
def _configuration_gdrive_helper(to_save):
|
2021-05-18 19:15:46 +00:00
|
|
|
gdrive_error = None
|
2021-05-26 11:35:35 +00:00
|
|
|
if to_save.get("config_use_google_drive"):
|
|
|
|
gdrive_secrets = {}
|
|
|
|
|
|
|
|
if not os.path.isfile(gdriveutils.SETTINGS_YAML):
|
|
|
|
config.config_use_google_drive = False
|
|
|
|
|
|
|
|
if gdrive_support:
|
|
|
|
gdrive_error = gdriveutils.get_error_text(gdrive_secrets)
|
|
|
|
if "config_use_google_drive" in to_save and not config.config_use_google_drive and not gdrive_error:
|
|
|
|
with open(gdriveutils.CLIENT_SECRETS, 'r') as settings:
|
|
|
|
gdrive_secrets = json.load(settings)['web']
|
|
|
|
if not gdrive_secrets:
|
|
|
|
return _configuration_result(_('client_secrets.json Is Not Configured For Web Application'))
|
|
|
|
gdriveutils.update_settings(
|
|
|
|
gdrive_secrets['client_id'],
|
|
|
|
gdrive_secrets['client_secret'],
|
|
|
|
gdrive_secrets['redirect_uris'][0]
|
|
|
|
)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
# always show google drive settings, but in case of error deny support
|
2021-05-01 10:45:28 +00:00
|
|
|
new_gdrive_value = (not gdrive_error) and ("config_use_google_drive" in to_save)
|
|
|
|
if config.config_use_google_drive and not new_gdrive_value:
|
|
|
|
config.config_google_drive_watch_changes_response = {}
|
|
|
|
config.config_use_google_drive = new_gdrive_value
|
2020-05-09 13:44:53 +00:00
|
|
|
if _config_string(to_save, "config_google_drive_folder"):
|
2019-07-13 18:45:48 +00:00
|
|
|
gdriveutils.deleteDatabaseOnChange()
|
2021-01-03 08:53:34 +00:00
|
|
|
return gdrive_error
|
|
|
|
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
def _configuration_oauth_helper(to_save):
|
|
|
|
active_oauths = 0
|
2020-05-21 20:26:06 +00:00
|
|
|
reboot_required = False
|
2020-05-09 13:44:53 +00:00
|
|
|
for element in oauthblueprints:
|
|
|
|
if to_save["config_" + str(element['id']) + "_oauth_client_id"] != element['oauth_client_id'] \
|
|
|
|
or to_save["config_" + str(element['id']) + "_oauth_client_secret"] != element['oauth_client_secret']:
|
|
|
|
reboot_required = True
|
|
|
|
element['oauth_client_id'] = to_save["config_" + str(element['id']) + "_oauth_client_id"]
|
|
|
|
element['oauth_client_secret'] = to_save["config_" + str(element['id']) + "_oauth_client_secret"]
|
|
|
|
if to_save["config_" + str(element['id']) + "_oauth_client_id"] \
|
|
|
|
and to_save["config_" + str(element['id']) + "_oauth_client_secret"]:
|
|
|
|
active_oauths += 1
|
|
|
|
element["active"] = 1
|
|
|
|
else:
|
|
|
|
element["active"] = 0
|
|
|
|
ub.session.query(ub.OAuthProvider).filter(ub.OAuthProvider.id == element['id']).update(
|
|
|
|
{"oauth_client_id": to_save["config_" + str(element['id']) + "_oauth_client_id"],
|
|
|
|
"oauth_client_secret": to_save["config_" + str(element['id']) + "_oauth_client_secret"],
|
|
|
|
"active": element["active"]})
|
2020-05-21 20:26:06 +00:00
|
|
|
return reboot_required
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
def _configuration_logfile_helper(to_save):
|
2020-05-09 13:44:53 +00:00
|
|
|
reboot_required = False
|
|
|
|
reboot_required |= _config_int(to_save, "config_log_level")
|
|
|
|
reboot_required |= _config_string(to_save, "config_logfile")
|
|
|
|
if not logger.is_valid_logfile(config.config_logfile):
|
2020-11-15 13:19:25 +00:00
|
|
|
return reboot_required, \
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_('Logfile Location is not Valid, Please Enter Correct Path'))
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
reboot_required |= _config_checkbox_int(to_save, "config_access_log")
|
|
|
|
reboot_required |= _config_string(to_save, "config_access_logfile")
|
|
|
|
if not logger.is_valid_logfile(config.config_access_logfile):
|
2020-11-15 13:19:25 +00:00
|
|
|
return reboot_required, \
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_('Access Logfile Location is not Valid, Please Enter Correct Path'))
|
2020-05-21 11:54:28 +00:00
|
|
|
return reboot_required, None
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
def _configuration_ldap_helper(to_save):
|
2020-05-09 13:44:53 +00:00
|
|
|
reboot_required = False
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_provider_url")
|
|
|
|
reboot_required |= _config_int(to_save, "config_ldap_port")
|
|
|
|
reboot_required |= _config_int(to_save, "config_ldap_authentication")
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_dn")
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_serv_username")
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_user_object")
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_group_object_filter")
|
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_group_members_field")
|
2020-12-08 19:11:55 +00:00
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_member_user_object")
|
2020-05-09 13:44:53 +00:00
|
|
|
reboot_required |= _config_checkbox(to_save, "config_ldap_openldap")
|
|
|
|
reboot_required |= _config_int(to_save, "config_ldap_encryption")
|
2020-11-20 18:47:50 +00:00
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_cacert_path")
|
2020-05-09 13:44:53 +00:00
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_cert_path")
|
2020-11-20 18:47:50 +00:00
|
|
|
reboot_required |= _config_string(to_save, "config_ldap_key_path")
|
2020-05-09 13:44:53 +00:00
|
|
|
_config_string(to_save, "config_ldap_group_name")
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("config_ldap_serv_password", "") != "":
|
2020-05-09 13:44:53 +00:00
|
|
|
reboot_required |= 1
|
|
|
|
config.set_from_dictionary(to_save, "config_ldap_serv_password", base64.b64encode, encode='UTF-8')
|
|
|
|
config.save()
|
|
|
|
|
|
|
|
if not config.config_ldap_provider_url \
|
|
|
|
or not config.config_ldap_port \
|
|
|
|
or not config.config_ldap_dn \
|
|
|
|
or not config.config_ldap_user_object:
|
2020-05-21 20:26:06 +00:00
|
|
|
return reboot_required, _configuration_result(_('Please Enter a LDAP Provider, '
|
2021-05-26 11:35:35 +00:00
|
|
|
'Port, DN and User Object Identifier'))
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
if config.config_ldap_authentication > constants.LDAP_AUTH_ANONYMOUS:
|
|
|
|
if config.config_ldap_authentication > constants.LDAP_AUTH_UNAUTHENTICATE:
|
|
|
|
if not config.config_ldap_serv_username or not bool(config.config_ldap_serv_password):
|
2021-05-26 11:35:35 +00:00
|
|
|
return reboot_required, _configuration_result(_('Please Enter a LDAP Service Account and Password'))
|
2020-05-09 13:44:53 +00:00
|
|
|
else:
|
|
|
|
if not config.config_ldap_serv_username:
|
2021-05-26 11:35:35 +00:00
|
|
|
return reboot_required, _configuration_result(_('Please Enter a LDAP Service Account'))
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
if config.config_ldap_group_object_filter:
|
|
|
|
if config.config_ldap_group_object_filter.count("%s") != 1:
|
2020-11-15 13:19:25 +00:00
|
|
|
return reboot_required, \
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_('LDAP Group Object Filter Needs to Have One "%s" Format Identifier'))
|
2020-05-09 13:44:53 +00:00
|
|
|
if config.config_ldap_group_object_filter.count("(") != config.config_ldap_group_object_filter.count(")"):
|
2021-05-26 11:35:35 +00:00
|
|
|
return reboot_required, _configuration_result(_('LDAP Group Object Filter Has Unmatched Parenthesis'))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-05-09 13:44:53 +00:00
|
|
|
if config.config_ldap_user_object.count("%s") != 1:
|
2020-11-15 13:19:25 +00:00
|
|
|
return reboot_required, \
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_('LDAP User Object Filter needs to Have One "%s" Format Identifier'))
|
2020-05-09 13:44:53 +00:00
|
|
|
if config.config_ldap_user_object.count("(") != config.config_ldap_user_object.count(")"):
|
2021-05-26 11:35:35 +00:00
|
|
|
return reboot_required, _configuration_result(_('LDAP User Object Filter Has Unmatched Parenthesis'))
|
2020-07-23 10:12:00 +00:00
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("ldap_import_user_filter") == '0':
|
2020-12-03 15:01:15 +00:00
|
|
|
config.config_ldap_member_user_object = ""
|
|
|
|
else:
|
|
|
|
if config.config_ldap_member_user_object.count("%s") != 1:
|
|
|
|
return reboot_required, \
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_('LDAP Member User Filter needs to Have One "%s" Format Identifier'))
|
2020-12-03 15:01:15 +00:00
|
|
|
if config.config_ldap_member_user_object.count("(") != config.config_ldap_member_user_object.count(")"):
|
2021-05-26 11:35:35 +00:00
|
|
|
return reboot_required, _configuration_result(_('LDAP Member User Filter Has Unmatched Parenthesis'))
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2020-11-22 10:19:14 +00:00
|
|
|
if config.config_ldap_cacert_path or config.config_ldap_cert_path or config.config_ldap_key_path:
|
|
|
|
if not (os.path.isfile(config.config_ldap_cacert_path) and
|
|
|
|
os.path.isfile(config.config_ldap_cert_path) and
|
|
|
|
os.path.isfile(config.config_ldap_key_path)):
|
|
|
|
return reboot_required, \
|
2020-12-03 15:01:15 +00:00
|
|
|
_configuration_result(_('LDAP CACertificate, Certificate or Key Location is not Valid, '
|
2021-05-26 11:35:35 +00:00
|
|
|
'Please Enter Correct Path'))
|
2020-05-21 20:26:06 +00:00
|
|
|
return reboot_required, None
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
@admi.route("/ajax/simulatedbchange", methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def simulatedbchange():
|
|
|
|
db_change, db_valid = _db_simulate_change()
|
|
|
|
return Response(json.dumps({"change": db_change, "valid": db_valid}), mimetype='application/json')
|
|
|
|
|
|
|
|
|
|
|
|
def _db_simulate_change():
|
|
|
|
param = request.form.to_dict()
|
|
|
|
to_save = {}
|
|
|
|
to_save['config_calibre_dir'] = re.sub(r'[\\/]metadata\.db$',
|
|
|
|
'',
|
|
|
|
param['config_calibre_dir'],
|
|
|
|
flags=re.IGNORECASE).strip()
|
|
|
|
db_change = config.config_calibre_dir != to_save["config_calibre_dir"] and config.config_calibre_dir
|
|
|
|
db_valid = calibre_db.check_valid_db(to_save["config_calibre_dir"], ub.app_DB_path)
|
|
|
|
return db_change, db_valid
|
|
|
|
|
|
|
|
|
|
|
|
def _db_configuration_update_helper():
|
2020-05-09 13:44:53 +00:00
|
|
|
db_change = False
|
|
|
|
to_save = request.form.to_dict()
|
2021-01-03 08:53:34 +00:00
|
|
|
gdrive_error = None
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2020-12-11 08:11:17 +00:00
|
|
|
to_save['config_calibre_dir'] = re.sub(r'[\\/]metadata\.db$',
|
2020-11-15 13:19:25 +00:00
|
|
|
'',
|
|
|
|
to_save['config_calibre_dir'],
|
|
|
|
flags=re.IGNORECASE)
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
2021-05-26 11:35:35 +00:00
|
|
|
db_change, db_valid = _db_simulate_change()
|
2020-06-27 10:31:26 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
# gdrive_error drive setup
|
|
|
|
gdrive_error = _configuration_gdrive_helper(to_save)
|
2021-05-26 11:35:35 +00:00
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
_db_configuration_result(_("Settings DB is not Writeable"), gdrive_error)
|
|
|
|
try:
|
|
|
|
metadata_db = os.path.join(to_save['config_calibre_dir'], "metadata.db")
|
|
|
|
if config.config_use_google_drive and is_gdrive_ready() and not os.path.exists(metadata_db):
|
|
|
|
gdriveutils.downloadFile(None, "metadata.db", metadata_db)
|
|
|
|
db_change = True
|
|
|
|
except Exception as ex:
|
|
|
|
return _db_configuration_result('{}'.format(ex), gdrive_error)
|
|
|
|
|
|
|
|
if db_change or not db_valid or not config.db_configured:
|
|
|
|
if not calibre_db.setup_db(to_save['config_calibre_dir'], ub.app_DB_path):
|
|
|
|
return _db_configuration_result(_('DB Location is not Valid, Please Enter Correct Path'),
|
|
|
|
gdrive_error)
|
|
|
|
_config_string(to_save, "config_calibre_dir")
|
|
|
|
calibre_db.update_config(config)
|
|
|
|
if not os.access(os.path.join(config.config_calibre_dir, "metadata.db"), os.W_OK):
|
|
|
|
flash(_(u"DB is not Writeable"), category="warning")
|
|
|
|
# warning = {'type': "warning", 'message': _(u"DB is not Writeable")}
|
|
|
|
config.save()
|
|
|
|
return _db_configuration_result(None, gdrive_error)
|
2020-06-27 10:31:26 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
def _configuration_update_helper():
|
|
|
|
reboot_required = False
|
|
|
|
to_save = request.form.to_dict()
|
|
|
|
try:
|
2020-06-27 10:31:26 +00:00
|
|
|
reboot_required |= _config_int(to_save, "config_port")
|
|
|
|
|
|
|
|
reboot_required |= _config_string(to_save, "config_keyfile")
|
|
|
|
if config.config_keyfile and not os.path.isfile(config.config_keyfile):
|
2021-05-26 11:35:35 +00:00
|
|
|
return _configuration_result(_('Keyfile Location is not Valid, Please Enter Correct Path'))
|
2020-06-27 10:31:26 +00:00
|
|
|
|
|
|
|
reboot_required |= _config_string(to_save, "config_certfile")
|
|
|
|
if config.config_certfile and not os.path.isfile(config.config_certfile):
|
2021-05-26 11:35:35 +00:00
|
|
|
return _configuration_result(_('Certfile Location is not Valid, Please Enter Correct Path'))
|
2020-06-27 10:31:26 +00:00
|
|
|
|
|
|
|
_config_checkbox_int(to_save, "config_uploading")
|
2021-08-01 11:50:17 +00:00
|
|
|
_config_checkbox_int(to_save, "config_unicode_filename")
|
2020-11-15 16:16:01 +00:00
|
|
|
# Reboot on config_anonbrowse with enabled ldap, as decoraters are changed in this case
|
2021-01-03 08:53:34 +00:00
|
|
|
reboot_required |= (_config_checkbox_int(to_save, "config_anonbrowse")
|
2020-11-15 16:16:01 +00:00
|
|
|
and config.config_login_type == constants.LOGIN_LDAP)
|
2020-06-27 10:31:26 +00:00
|
|
|
_config_checkbox_int(to_save, "config_public_reg")
|
|
|
|
_config_checkbox_int(to_save, "config_register_email")
|
|
|
|
reboot_required |= _config_checkbox_int(to_save, "config_kobo_sync")
|
2020-08-23 16:23:22 +00:00
|
|
|
_config_int(to_save, "config_external_port")
|
2020-06-27 10:31:26 +00:00
|
|
|
_config_checkbox_int(to_save, "config_kobo_proxy")
|
|
|
|
|
2020-09-20 13:03:25 +00:00
|
|
|
if "config_upload_formats" in to_save:
|
|
|
|
to_save["config_upload_formats"] = ','.join(
|
|
|
|
helper.uniq([x.lstrip().rstrip().lower() for x in to_save["config_upload_formats"].split(',')]))
|
|
|
|
_config_string(to_save, "config_upload_formats")
|
|
|
|
constants.EXTENSIONS_UPLOAD = config.config_upload_formats.split(',')
|
2020-06-27 10:31:26 +00:00
|
|
|
|
|
|
|
_config_string(to_save, "config_calibre")
|
|
|
|
_config_string(to_save, "config_converterpath")
|
|
|
|
_config_string(to_save, "config_kepubifypath")
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
reboot_required |= _config_int(to_save, "config_login_type")
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
# LDAP configurator
|
2020-06-27 10:31:26 +00:00
|
|
|
if config.config_login_type == constants.LOGIN_LDAP:
|
2021-05-26 11:35:35 +00:00
|
|
|
reboot, message = _configuration_ldap_helper(to_save)
|
2020-06-27 10:31:26 +00:00
|
|
|
if message:
|
|
|
|
return message
|
|
|
|
reboot_required |= reboot
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
# Remote login configuration
|
|
|
|
_config_checkbox(to_save, "config_remote_login")
|
|
|
|
if not config.config_remote_login:
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.token_type == 0).delete()
|
2020-01-01 16:26:47 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
# Goodreads configuration
|
|
|
|
_config_checkbox(to_save, "config_use_goodreads")
|
|
|
|
_config_string(to_save, "config_goodreads_api_key")
|
|
|
|
_config_string(to_save, "config_goodreads_api_secret")
|
|
|
|
if services.goodreads_support:
|
|
|
|
services.goodreads_support.connect(config.config_goodreads_api_key,
|
|
|
|
config.config_goodreads_api_secret,
|
|
|
|
config.config_use_goodreads)
|
2020-01-26 15:52:40 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
_config_int(to_save, "config_updatechannel")
|
2020-05-09 14:54:22 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
# Reverse proxy login configuration
|
|
|
|
_config_checkbox(to_save, "config_allow_reverse_proxy_header_login")
|
|
|
|
_config_string(to_save, "config_reverse_proxy_login_header_name")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2020-06-27 10:31:26 +00:00
|
|
|
# OAuth configuration
|
|
|
|
if config.config_login_type == constants.LOGIN_OAUTH:
|
|
|
|
reboot_required |= _configuration_oauth_helper(to_save)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
reboot, message = _configuration_logfile_helper(to_save)
|
2020-05-21 11:54:28 +00:00
|
|
|
if message:
|
|
|
|
return message
|
|
|
|
reboot_required |= reboot
|
2020-06-27 10:31:26 +00:00
|
|
|
# Rarfile Content configuration
|
|
|
|
_config_string(to_save, "config_rarfile_location")
|
|
|
|
if "config_rarfile_location" in to_save:
|
|
|
|
unrar_status = helper.check_unrar(config.config_rarfile_location)
|
|
|
|
if unrar_status:
|
2021-05-26 11:35:35 +00:00
|
|
|
return _configuration_result(unrar_status)
|
2020-06-27 10:31:26 +00:00
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
2021-05-26 11:35:35 +00:00
|
|
|
_configuration_result(_("Settings DB is not Writeable"))
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
config.save()
|
|
|
|
if reboot_required:
|
|
|
|
web_server.stop(True)
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
return _configuration_result(None, reboot_required)
|
|
|
|
|
|
|
|
def _configuration_result(error_flash=None, reboot=False):
|
|
|
|
resp = {}
|
|
|
|
if error_flash:
|
|
|
|
log.error(error_flash)
|
|
|
|
config.load()
|
|
|
|
resp['result'] = [{'type': "danger", 'message': error_flash}]
|
|
|
|
else:
|
|
|
|
resp['result'] = [{'type': "success", 'message':_(u"Calibre-Web configuration updated")}]
|
|
|
|
resp['reboot'] = reboot
|
|
|
|
resp['config_upload']= config.config_upload_formats
|
|
|
|
return Response(json.dumps(resp), mimetype='application/json')
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
def _db_configuration_result(error_flash=None, gdrive_error=None):
|
2019-07-13 18:45:48 +00:00
|
|
|
gdrive_authenticate = not is_gdrive_ready()
|
|
|
|
gdrivefolders = []
|
2021-05-26 11:35:35 +00:00
|
|
|
if not gdrive_error and config.config_use_google_drive:
|
2021-01-03 08:53:34 +00:00
|
|
|
gdrive_error = gdriveutils.get_error_text()
|
2021-05-18 19:15:46 +00:00
|
|
|
if gdrive_error and gdrive_support:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error(gdrive_error)
|
2021-01-03 08:53:34 +00:00
|
|
|
gdrive_error = _(gdrive_error)
|
2021-05-26 11:35:35 +00:00
|
|
|
flash(gdrive_error, category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
2020-06-27 11:36:33 +00:00
|
|
|
if not gdrive_authenticate and gdrive_support:
|
2019-11-26 09:46:06 +00:00
|
|
|
gdrivefolders = gdriveutils.listRootFolders()
|
2019-07-13 18:45:48 +00:00
|
|
|
if error_flash:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error(error_flash)
|
2019-07-13 18:45:48 +00:00
|
|
|
config.load()
|
2020-04-13 20:23:58 +00:00
|
|
|
flash(error_flash, category="error")
|
2021-06-12 06:48:55 +00:00
|
|
|
elif request.method == "POST" and not gdrive_error:
|
|
|
|
flash(_("Database Settings updated"), category="success")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
2021-05-26 11:35:35 +00:00
|
|
|
return render_title_template("config_db.html",
|
2021-01-03 08:53:34 +00:00
|
|
|
config=config,
|
|
|
|
show_authenticate_google_drive=gdrive_authenticate,
|
|
|
|
gdriveError=gdrive_error,
|
|
|
|
gdrivefolders=gdrivefolders,
|
|
|
|
feature_support=feature_support,
|
2021-05-26 11:35:35 +00:00
|
|
|
title=_(u"Database Configuration"), page="dbconfig")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
def _handle_new_user(to_save, content, languages, translations, kobo_support):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.default_language = to_save["default_language"]
|
|
|
|
content.locale = to_save.get("locale", content.locale)
|
|
|
|
|
|
|
|
content.sidebar_view = sum(int(key[5:]) for key in to_save if key.startswith('show_'))
|
|
|
|
if "show_detail_random" in to_save:
|
|
|
|
content.sidebar_view |= constants.DETAIL_RANDOM
|
|
|
|
|
|
|
|
content.role = constants.selected_roles(to_save)
|
|
|
|
content.password = generate_password_hash(to_save["password"])
|
2021-04-04 17:40:34 +00:00
|
|
|
try:
|
|
|
|
if not to_save["name"] or not to_save["email"] or not to_save["password"]:
|
|
|
|
log.info("Missing entries on new user")
|
|
|
|
raise Exception(_(u"Please fill out all fields!"))
|
|
|
|
content.email = check_email(to_save["email"])
|
|
|
|
# Query User name, if not existing, change
|
|
|
|
content.name = check_username(to_save["name"])
|
|
|
|
if to_save.get("kindle_mail"):
|
|
|
|
content.kindle_mail = valid_email(to_save["kindle_mail"])
|
|
|
|
if config.config_public_reg and not check_valid_domain(content.email):
|
|
|
|
log.info("E-mail: {} for new user is not from valid domain".format(content.email))
|
|
|
|
raise Exception(_(u"E-mail is not from valid domain"))
|
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), category="error")
|
2020-05-09 13:44:53 +00:00
|
|
|
return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
|
|
|
|
languages=languages, title=_(u"Add new user"), page="newuser",
|
|
|
|
kobo_support=kobo_support, registered_oauth=oauth_check)
|
|
|
|
try:
|
|
|
|
content.allowed_tags = config.config_allowed_tags
|
|
|
|
content.denied_tags = config.config_denied_tags
|
|
|
|
content.allowed_column_value = config.config_allowed_column_value
|
|
|
|
content.denied_column_value = config.config_denied_column_value
|
2021-07-10 15:09:04 +00:00
|
|
|
# No default value for kobo sync shelf setting
|
|
|
|
content.kobo_only_shelves_sync = to_save.get("kobo_only_shelves_sync", 0) == "on"
|
2020-05-09 13:44:53 +00:00
|
|
|
ub.session.add(content)
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session.commit()
|
2021-03-21 17:55:02 +00:00
|
|
|
flash(_(u"User '%(user)s' created", user=content.name), category="success")
|
2021-04-25 09:20:21 +00:00
|
|
|
log.debug("User {} created".format(content.name))
|
2020-05-09 13:44:53 +00:00
|
|
|
return redirect(url_for('admin.admin'))
|
|
|
|
except IntegrityError:
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Found an existing account for {} or {}".format(content.name, content.email))
|
|
|
|
flash(_("Found an existing account for this e-mail address or name."), category="error")
|
2020-06-27 10:31:26 +00:00
|
|
|
except OperationalError:
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2021-04-10 09:32:11 +00:00
|
|
|
def _delete_user(content):
|
|
|
|
if ub.session.query(ub.User).filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
|
|
|
|
ub.User.id != content.id).count():
|
|
|
|
if content.name != "Guest":
|
2021-07-10 15:09:04 +00:00
|
|
|
# Delete all books in shelfs belonging to user, all shelfs of user, downloadstat of user, read status
|
|
|
|
# and user itself
|
2021-07-23 18:03:20 +00:00
|
|
|
ub.session.query(ub.ReadBook).filter(content.id == ub.ReadBook.user_id).delete()
|
|
|
|
ub.session.query(ub.Downloads).filter(content.id == ub.Downloads.user_id).delete()
|
|
|
|
for us in ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id):
|
2021-07-10 15:09:04 +00:00
|
|
|
ub.session.query(ub.BookShelf).filter(us.id == ub.BookShelf.shelf).delete()
|
2021-07-23 18:03:20 +00:00
|
|
|
ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id).delete()
|
2021-04-10 09:32:11 +00:00
|
|
|
ub.session.query(ub.User).filter(ub.User.id == content.id).delete()
|
|
|
|
ub.session_commit()
|
|
|
|
log.info(u"User {} deleted".format(content.name))
|
|
|
|
return(_(u"User '%(nick)s' deleted", nick=content.name))
|
|
|
|
else:
|
|
|
|
log.warning(_(u"Can't delete Guest User"))
|
|
|
|
raise Exception(_(u"Can't delete Guest User"))
|
|
|
|
else:
|
|
|
|
log.warning(u"No admin user remaining, can't delete user")
|
|
|
|
raise Exception(_(u"No admin user remaining, can't delete user"))
|
|
|
|
|
2020-05-09 13:44:53 +00:00
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
def _handle_edit_user(to_save, content, languages, translations, kobo_support):
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("delete"):
|
2021-04-10 09:32:11 +00:00
|
|
|
try:
|
|
|
|
flash(_delete_user(content), category="success")
|
|
|
|
except Exception as ex:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error(ex)
|
2021-04-10 09:32:11 +00:00
|
|
|
flash(str(ex), category="error")
|
|
|
|
return redirect(url_for('admin.admin'))
|
2020-05-09 13:44:53 +00:00
|
|
|
else:
|
|
|
|
if not ub.session.query(ub.User).filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.User.id != content.id).count() and 'admin_role' not in to_save:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.warning("No admin user remaining, can't remove admin role from {}".format(content.name))
|
|
|
|
flash(_("No admin user remaining, can't remove admin role"), category="error")
|
2020-05-09 13:44:53 +00:00
|
|
|
return redirect(url_for('admin.admin'))
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("password"):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.password = generate_password_hash(to_save["password"])
|
|
|
|
anonymous = content.is_anonymous
|
|
|
|
content.role = constants.selected_roles(to_save)
|
|
|
|
if anonymous:
|
|
|
|
content.role |= constants.ROLE_ANONYMOUS
|
|
|
|
else:
|
|
|
|
content.role &= ~constants.ROLE_ANONYMOUS
|
|
|
|
|
|
|
|
val = [int(k[5:]) for k in to_save if k.startswith('show_')]
|
2020-12-12 11:03:11 +00:00
|
|
|
sidebar = get_sidebar_config()
|
2020-05-09 13:44:53 +00:00
|
|
|
for element in sidebar:
|
|
|
|
value = element['visibility']
|
|
|
|
if value in val and not content.check_visibility(value):
|
|
|
|
content.sidebar_view |= value
|
2021-01-03 08:53:34 +00:00
|
|
|
elif value not in val and content.check_visibility(value):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.sidebar_view &= ~value
|
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("Show_detail_random"):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.sidebar_view |= constants.DETAIL_RANDOM
|
|
|
|
else:
|
|
|
|
content.sidebar_view &= ~constants.DETAIL_RANDOM
|
|
|
|
|
2021-05-15 08:45:51 +00:00
|
|
|
content.kobo_only_shelves_sync = int(to_save.get("kobo_only_shelves_sync") == "on") or 0
|
|
|
|
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("default_language"):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.default_language = to_save["default_language"]
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("locale"):
|
2020-05-09 13:44:53 +00:00
|
|
|
content.locale = to_save["locale"]
|
2021-04-04 17:40:34 +00:00
|
|
|
try:
|
|
|
|
if to_save.get("email", content.email) != content.email:
|
|
|
|
content.email = check_email(to_save["email"])
|
2021-03-21 17:55:02 +00:00
|
|
|
# Query User name, if not existing, change
|
2021-04-04 17:40:34 +00:00
|
|
|
if to_save.get("name", content.name) != content.name:
|
|
|
|
if to_save.get("name") == "Guest":
|
|
|
|
raise Exception(_("Guest Name can't be changed"))
|
|
|
|
content.name = check_username(to_save["name"])
|
|
|
|
if to_save.get("kindle_mail") != content.kindle_mail:
|
|
|
|
content.kindle_mail = valid_email(to_save["kindle_mail"]) if to_save["kindle_mail"] else ""
|
|
|
|
except Exception as ex:
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error(ex)
|
2021-04-04 17:40:34 +00:00
|
|
|
flash(str(ex), category="error")
|
|
|
|
return render_title_template("user_edit.html",
|
|
|
|
translations=translations,
|
|
|
|
languages=languages,
|
|
|
|
mail_configured=config.get_mail_server_configured(),
|
|
|
|
kobo_support=kobo_support,
|
|
|
|
new_user=0,
|
|
|
|
content=content,
|
|
|
|
registered_oauth=oauth_check,
|
|
|
|
title=_(u"Edit User %(nick)s", nick=content.name),
|
|
|
|
page="edituser")
|
2020-05-09 13:44:53 +00:00
|
|
|
try:
|
2021-01-03 08:53:34 +00:00
|
|
|
ub.session_commit()
|
2021-03-21 17:55:02 +00:00
|
|
|
flash(_(u"User '%(nick)s' updated", nick=content.name), category="success")
|
2021-04-25 09:20:21 +00:00
|
|
|
except IntegrityError as ex:
|
2020-05-09 13:44:53 +00:00
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("An unknown error occurred while changing user: {}".format(str(ex)))
|
2021-05-16 07:37:45 +00:00
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
2020-06-27 10:31:26 +00:00
|
|
|
except OperationalError:
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2021-04-07 16:47:48 +00:00
|
|
|
return ""
|
2020-05-09 13:44:53 +00:00
|
|
|
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
@admi.route("/admin/user/new", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def new_user():
|
|
|
|
content = ub.User()
|
2020-05-23 08:16:29 +00:00
|
|
|
languages = calibre_db.speaking_language()
|
2019-07-13 18:45:48 +00:00
|
|
|
translations = [LC('en')] + babel.list_translations()
|
2020-01-27 19:32:37 +00:00
|
|
|
kobo_support = feature_support['kobo'] and config.config_kobo_sync
|
2019-07-13 18:45:48 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2020-05-09 13:44:53 +00:00
|
|
|
_handle_new_user(to_save, content, languages, translations, kobo_support)
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
content.role = config.config_default_role
|
|
|
|
content.sidebar_view = config.config_default_show
|
|
|
|
return render_title_template("user_edit.html", new_user=1, content=content, translations=translations,
|
|
|
|
languages=languages, title=_(u"Add new user"), page="newuser",
|
2020-01-27 19:32:37 +00:00
|
|
|
kobo_support=kobo_support, registered_oauth=oauth_check)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/mailsettings")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def edit_mailsettings():
|
|
|
|
content = config.get_mail_settings()
|
2020-05-03 08:55:33 +00:00
|
|
|
return render_title_template("email_edit.html", content=content, title=_(u"Edit E-mail Server Settings"),
|
2021-03-28 12:50:55 +00:00
|
|
|
page="mailset", feature_support=feature_support)
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/mailsettings", methods=["POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def update_mailsettings():
|
|
|
|
to_save = request.form.to_dict()
|
2021-03-27 15:36:24 +00:00
|
|
|
_config_int(to_save, "mail_server_type")
|
2021-03-28 12:50:55 +00:00
|
|
|
if to_save.get("invalidate"):
|
2021-03-27 15:36:24 +00:00
|
|
|
config.mail_gmail_token = {}
|
|
|
|
try:
|
|
|
|
flag_modified(config, "mail_gmail_token")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
elif to_save.get("gmail"):
|
2021-03-28 12:50:55 +00:00
|
|
|
try:
|
|
|
|
config.mail_gmail_token = services.gmail.setup_gmail(config.mail_gmail_token)
|
2021-05-16 07:37:45 +00:00
|
|
|
flash(_(u"Gmail Account Verification Successful"), category="success")
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), category="error")
|
|
|
|
log.error(ex)
|
2021-03-28 12:50:55 +00:00
|
|
|
return edit_mailsettings()
|
|
|
|
|
2021-03-27 15:36:24 +00:00
|
|
|
else:
|
|
|
|
_config_string(to_save, "mail_server")
|
|
|
|
_config_int(to_save, "mail_port")
|
|
|
|
_config_int(to_save, "mail_use_ssl")
|
|
|
|
_config_string(to_save, "mail_login")
|
|
|
|
_config_string(to_save, "mail_password")
|
|
|
|
_config_string(to_save, "mail_from")
|
|
|
|
_config_int(to_save, "mail_size", lambda y: int(y)*1024*1024)
|
2020-06-27 10:31:26 +00:00
|
|
|
try:
|
|
|
|
config.save()
|
|
|
|
except (OperationalError, InvalidRequestError):
|
|
|
|
ub.session.rollback()
|
2021-04-25 09:20:21 +00:00
|
|
|
log.error("Settings DB is not Writeable")
|
|
|
|
flash(_("Settings DB is not Writeable"), category="error")
|
2020-06-27 10:31:26 +00:00
|
|
|
return edit_mailsettings()
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
if to_save.get("test"):
|
2019-12-15 16:08:17 +00:00
|
|
|
if current_user.email:
|
2021-03-21 17:55:02 +00:00
|
|
|
result = send_test_mail(current_user.email, current_user.name)
|
2019-07-13 18:45:48 +00:00
|
|
|
if result is None:
|
2021-03-27 15:36:24 +00:00
|
|
|
flash(_(u"Test e-mail queued for sending to %(email)s, please check Tasks for result",
|
|
|
|
email=current_user.email), category="info")
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
flash(_(u"There was an error sending the Test e-mail: %(res)s", res=result), category="error")
|
|
|
|
else:
|
2019-12-15 16:08:17 +00:00
|
|
|
flash(_(u"Please configure your e-mail address first..."), category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
else:
|
|
|
|
flash(_(u"E-mail server settings updated"), category="success")
|
|
|
|
|
|
|
|
return edit_mailsettings()
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/user/<int:user_id>", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def edit_user(user_id):
|
|
|
|
content = ub.session.query(ub.User).filter(ub.User.id == int(user_id)).first() # type: ub.User
|
2021-03-21 17:55:02 +00:00
|
|
|
if not content or (not config.config_anonbrowse and content.name == "Guest"):
|
2020-05-01 10:00:45 +00:00
|
|
|
flash(_(u"User not found"), category="error")
|
|
|
|
return redirect(url_for('admin.admin'))
|
2020-05-23 08:16:29 +00:00
|
|
|
languages = calibre_db.speaking_language()
|
2019-07-13 18:45:48 +00:00
|
|
|
translations = babel.list_translations() + [LC('en')]
|
2020-01-27 19:32:37 +00:00
|
|
|
kobo_support = feature_support['kobo'] and config.config_kobo_sync
|
2019-07-13 18:45:48 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
to_save = request.form.to_dict()
|
2021-04-07 16:47:48 +00:00
|
|
|
resp = _handle_edit_user(to_save, content, languages, translations, kobo_support)
|
|
|
|
if resp:
|
|
|
|
return resp
|
2020-01-11 18:10:39 +00:00
|
|
|
return render_title_template("user_edit.html",
|
|
|
|
translations=translations,
|
|
|
|
languages=languages,
|
|
|
|
new_user=0,
|
|
|
|
content=content,
|
|
|
|
registered_oauth=oauth_check,
|
2019-12-29 12:54:52 +00:00
|
|
|
mail_configured=config.get_mail_server_configured(),
|
2020-01-27 19:32:37 +00:00
|
|
|
kobo_support=kobo_support,
|
2021-04-04 17:40:34 +00:00
|
|
|
title=_(u"Edit User %(nick)s", nick=content.name),
|
|
|
|
page="edituser")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/resetpassword/<int:user_id>")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-12-28 15:18:21 +00:00
|
|
|
def reset_user_password(user_id):
|
2019-07-13 18:45:48 +00:00
|
|
|
if current_user is not None and current_user.is_authenticated:
|
2019-12-15 16:08:17 +00:00
|
|
|
ret, message = reset_password(user_id)
|
|
|
|
if ret == 1:
|
2020-05-13 18:03:54 +00:00
|
|
|
log.debug(u"Password for user %s reset", message)
|
2019-12-15 16:08:17 +00:00
|
|
|
flash(_(u"Password for user %(user)s reset", user=message), category="success")
|
2019-12-28 15:18:21 +00:00
|
|
|
elif ret == 0:
|
|
|
|
log.error(u"An unknown error occurred. Please try again later.")
|
2019-07-13 18:45:48 +00:00
|
|
|
flash(_(u"An unknown error occurred. Please try again later."), category="error")
|
2019-12-28 15:18:21 +00:00
|
|
|
else:
|
|
|
|
log.error(u"Please configure the SMTP mail settings first...")
|
|
|
|
flash(_(u"Please configure the SMTP mail settings first..."), category="error")
|
2019-07-13 18:45:48 +00:00
|
|
|
return redirect(url_for('admin.admin'))
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/logfile")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def view_logfile():
|
2021-01-03 08:53:34 +00:00
|
|
|
logfiles = {0: logger.get_logfile(config.config_logfile),
|
|
|
|
1: logger.get_accesslogfile(config.config_access_logfile)}
|
2020-01-27 17:14:16 +00:00
|
|
|
return render_title_template("logviewer.html",
|
|
|
|
title=_(u"Logfile viewer"),
|
2020-01-05 15:43:48 +00:00
|
|
|
accesslog_enable=config.config_access_log,
|
2020-01-27 17:14:16 +00:00
|
|
|
log_enable=bool(config.config_logfile != logger.LOG_TO_STDOUT),
|
|
|
|
logfiles=logfiles,
|
|
|
|
page="logfile")
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/ajax/log/<int:logtype>")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def send_logfile(logtype):
|
|
|
|
if logtype == 1:
|
|
|
|
logfile = logger.get_accesslogfile(config.config_access_logfile)
|
|
|
|
return send_from_directory(os.path.dirname(logfile),
|
|
|
|
os.path.basename(logfile))
|
|
|
|
if logtype == 0:
|
|
|
|
logfile = logger.get_logfile(config.config_logfile)
|
|
|
|
return send_from_directory(os.path.dirname(logfile),
|
|
|
|
os.path.basename(logfile))
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
2020-11-15 13:19:25 +00:00
|
|
|
@admi.route("/admin/logdownload/<int:logtype>")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def download_log(logtype):
|
|
|
|
if logtype == 0:
|
|
|
|
file_name = logger.get_logfile(config.config_logfile)
|
|
|
|
elif logtype == 1:
|
|
|
|
file_name = logger.get_accesslogfile(config.config_access_logfile)
|
|
|
|
else:
|
|
|
|
abort(404)
|
|
|
|
if logger.is_valid_logfile(file_name):
|
|
|
|
return debug_info.assemble_logfiles(file_name)
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/admin/debug")
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def download_debug():
|
|
|
|
return debug_info.send_debug()
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
@admi.route("/get_update_status", methods=['GET'])
|
2020-10-29 13:03:18 +00:00
|
|
|
@login_required
|
|
|
|
@admin_required
|
2019-07-13 18:45:48 +00:00
|
|
|
def get_update_status():
|
2021-02-06 08:42:27 +00:00
|
|
|
if feature_support['updater']:
|
|
|
|
log.info(u"Update status requested")
|
|
|
|
return updater_thread.get_available_updates(request.method, locale=get_locale())
|
|
|
|
else:
|
|
|
|
return ''
|
2019-07-13 18:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@admi.route("/get_updater_status", methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def get_updater_status():
|
|
|
|
status = {}
|
2021-02-06 08:42:27 +00:00
|
|
|
if feature_support['updater']:
|
|
|
|
if request.method == "POST":
|
|
|
|
commit = request.form.to_dict()
|
|
|
|
if "start" in commit and commit['start'] == 'True':
|
|
|
|
text = {
|
|
|
|
"1": _(u'Requesting update package'),
|
|
|
|
"2": _(u'Downloading update package'),
|
|
|
|
"3": _(u'Unzipping update package'),
|
|
|
|
"4": _(u'Replacing files'),
|
|
|
|
"5": _(u'Database connections are closed'),
|
|
|
|
"6": _(u'Stopping server'),
|
|
|
|
"7": _(u'Update finished, please press okay and reload page'),
|
|
|
|
"8": _(u'Update failed:') + u' ' + _(u'HTTP Error'),
|
|
|
|
"9": _(u'Update failed:') + u' ' + _(u'Connection error'),
|
|
|
|
"10": _(u'Update failed:') + u' ' + _(u'Timeout while establishing connection'),
|
|
|
|
"11": _(u'Update failed:') + u' ' + _(u'General error'),
|
2021-06-06 10:16:47 +00:00
|
|
|
"12": _(u'Update failed:') + u' ' + _(u'Update file could not be saved in temp dir'),
|
|
|
|
"13": _(u'Update failed:') + u' ' + _(u'Files could not be replaced during update')
|
2021-02-06 08:42:27 +00:00
|
|
|
}
|
|
|
|
status['text'] = text
|
|
|
|
updater_thread.status = 0
|
|
|
|
updater_thread.resume()
|
|
|
|
status['status'] = updater_thread.get_update_status()
|
|
|
|
elif request.method == "GET":
|
|
|
|
try:
|
|
|
|
status['status'] = updater_thread.get_update_status()
|
|
|
|
if status['status'] == -1:
|
|
|
|
status['status'] = 7
|
|
|
|
except Exception:
|
|
|
|
status['status'] = 11
|
|
|
|
return json.dumps(status)
|
|
|
|
return ''
|
2020-12-12 10:23:17 +00:00
|
|
|
|
|
|
|
|
2021-03-21 07:19:54 +00:00
|
|
|
def ldap_import_create_user(user, user_data):
|
|
|
|
user_login_field = extract_dynamic_field_from_filter(user, config.config_ldap_user_object)
|
|
|
|
|
|
|
|
username = user_data[user_login_field][0].decode('utf-8')
|
|
|
|
# check for duplicate username
|
2021-03-21 17:55:02 +00:00
|
|
|
if ub.session.query(ub.User).filter(func.lower(ub.User.name) == username.lower()).first():
|
|
|
|
# if ub.session.query(ub.User).filter(ub.User.name == username).first():
|
2021-03-21 07:19:54 +00:00
|
|
|
log.warning("LDAP User %s Already in Database", user_data)
|
|
|
|
return 0, None
|
|
|
|
|
|
|
|
kindlemail = ''
|
|
|
|
if 'mail' in user_data:
|
|
|
|
useremail = user_data['mail'][0].decode('utf-8')
|
|
|
|
if len(user_data['mail']) > 1:
|
|
|
|
kindlemail = user_data['mail'][1].decode('utf-8')
|
|
|
|
|
|
|
|
else:
|
|
|
|
log.debug('No Mail Field Found in LDAP Response')
|
|
|
|
useremail = username + '@email.com'
|
2021-04-04 17:40:34 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# check for duplicate email
|
|
|
|
useremail = check_email(useremail)
|
|
|
|
except Exception as ex:
|
|
|
|
log.warning("LDAP Email Error: {}, {}".format(user_data, ex))
|
2021-03-21 07:19:54 +00:00
|
|
|
return 0, None
|
|
|
|
content = ub.User()
|
2021-03-21 17:55:02 +00:00
|
|
|
content.name = username
|
2021-03-21 07:19:54 +00:00
|
|
|
content.password = '' # dummy password which will be replaced by ldap one
|
|
|
|
content.email = useremail
|
|
|
|
content.kindle_mail = kindlemail
|
|
|
|
content.role = config.config_default_role
|
|
|
|
content.sidebar_view = config.config_default_show
|
|
|
|
content.allowed_tags = config.config_allowed_tags
|
|
|
|
content.denied_tags = config.config_denied_tags
|
|
|
|
content.allowed_column_value = config.config_allowed_column_value
|
|
|
|
content.denied_column_value = config.config_denied_column_value
|
|
|
|
ub.session.add(content)
|
|
|
|
try:
|
|
|
|
ub.session.commit()
|
|
|
|
return 1, None # increase no of users
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.warning("Failed to create LDAP user: %s - %s", user, ex)
|
2021-03-21 07:19:54 +00:00
|
|
|
ub.session.rollback()
|
|
|
|
message = _(u'Failed to Create at Least One LDAP User')
|
|
|
|
return 0, message
|
|
|
|
|
|
|
|
|
2020-12-12 10:23:17 +00:00
|
|
|
@admi.route('/import_ldap_users')
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
|
|
|
def import_ldap_users():
|
|
|
|
showtext = {}
|
|
|
|
try:
|
|
|
|
new_users = services.ldap.get_group_members(config.config_ldap_group_name)
|
|
|
|
except (services.ldap.LDAPException, TypeError, AttributeError, KeyError) as e:
|
2020-12-12 14:16:22 +00:00
|
|
|
log.debug_or_exception(e)
|
2020-12-12 10:23:17 +00:00
|
|
|
showtext['text'] = _(u'Error: %(ldaperror)s', ldaperror=e)
|
|
|
|
return json.dumps(showtext)
|
|
|
|
if not new_users:
|
|
|
|
log.debug('LDAP empty response')
|
|
|
|
showtext['text'] = _(u'Error: No user returned in response of LDAP server')
|
|
|
|
return json.dumps(showtext)
|
|
|
|
|
|
|
|
imported = 0
|
|
|
|
for username in new_users:
|
|
|
|
user = username.decode('utf-8')
|
|
|
|
if '=' in user:
|
|
|
|
# if member object field is empty take user object as filter
|
|
|
|
if config.config_ldap_member_user_object:
|
|
|
|
query_filter = config.config_ldap_member_user_object
|
|
|
|
else:
|
|
|
|
query_filter = config.config_ldap_user_object
|
|
|
|
try:
|
|
|
|
user_identifier = extract_user_identifier(user, query_filter)
|
2021-04-04 17:40:34 +00:00
|
|
|
except Exception as ex:
|
|
|
|
log.warning(ex)
|
2020-12-12 10:23:17 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
user_identifier = user
|
|
|
|
query_filter = None
|
|
|
|
try:
|
|
|
|
user_data = services.ldap.get_object_details(user=user_identifier, query_filter=query_filter)
|
2021-04-04 17:40:34 +00:00
|
|
|
except AttributeError as ex:
|
|
|
|
log.debug_or_exception(ex)
|
2020-12-12 10:23:17 +00:00
|
|
|
continue
|
|
|
|
if user_data:
|
2021-03-21 12:46:13 +00:00
|
|
|
user_count, message = ldap_import_create_user(user, user_data)
|
2021-03-21 07:19:54 +00:00
|
|
|
if message:
|
|
|
|
showtext['text'] = message
|
2020-12-12 10:23:17 +00:00
|
|
|
else:
|
2021-03-21 07:19:54 +00:00
|
|
|
imported += user_count
|
2020-12-12 10:23:17 +00:00
|
|
|
else:
|
|
|
|
log.warning("LDAP User: %s Not Found", user)
|
|
|
|
showtext['text'] = _(u'At Least One LDAP User Not Found in Database')
|
|
|
|
if not showtext:
|
|
|
|
showtext['text'] = _(u'{} User Successfully Imported'.format(imported))
|
|
|
|
return json.dumps(showtext)
|
|
|
|
|
|
|
|
|
|
|
|
def extract_user_data_from_field(user, field):
|
2021-07-30 18:45:33 +00:00
|
|
|
match = re.search(field + r"=([\.\d\s\w-]+)", user, re.IGNORECASE | re.UNICODE)
|
2020-12-12 10:23:17 +00:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
else:
|
|
|
|
raise Exception("Could Not Parse LDAP User: {}".format(user))
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
|
|
|
def extract_dynamic_field_from_filter(user, filtr):
|
|
|
|
match = re.search("([a-zA-Z0-9-]+)=%s", filtr, re.IGNORECASE | re.UNICODE)
|
2020-12-12 10:23:17 +00:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
else:
|
|
|
|
raise Exception("Could Not Parse LDAP Userfield: {}", user)
|
|
|
|
|
2021-01-03 08:53:34 +00:00
|
|
|
|
|
|
|
def extract_user_identifier(user, filtr):
|
|
|
|
dynamic_field = extract_dynamic_field_from_filter(user, filtr)
|
2020-12-12 10:23:17 +00:00
|
|
|
return extract_user_data_from_field(user, dynamic_field)
|