2022-01-25 20:42:35 +00:00
|
|
|
import logging
|
|
|
|
from flask import Flask
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2022-01-27 22:45:17 +00:00
|
|
|
from flask_security import Security, SQLAlchemyUserDatastore, hash_password
|
|
|
|
from email_validator import validate_email
|
2022-01-25 20:42:35 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from .door_handle import DoorHandle
|
|
|
|
|
|
|
|
security = Security()
|
2022-01-29 22:48:58 +00:00
|
|
|
db = SQLAlchemy()
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
|
2022-01-27 22:45:17 +00:00
|
|
|
# create admin users (only if they don't exists already)
|
2022-02-04 21:40:56 +00:00
|
|
|
def create_super_admins(app, user_datastore):
|
|
|
|
admin_file = Path(app.config.get('ADMIN_FILE'))
|
|
|
|
|
2022-01-27 22:45:17 +00:00
|
|
|
# setup user database when starting the app
|
2022-02-04 21:40:56 +00:00
|
|
|
new_admin_data = []
|
|
|
|
if not admin_file.exists():
|
|
|
|
app.logger.warning(
|
|
|
|
f"Admin user creation file not found at path "
|
|
|
|
f"{admin_file.absolute()}."
|
|
|
|
f"No super admins have been created in the datastore."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# store data for new admins in memory s.t. the file can be deleted
|
|
|
|
# afterwards
|
|
|
|
admin_data = admin_file.read_text().split('\n')
|
|
|
|
for i, line in enumerate(admin_data):
|
|
|
|
if not line.strip().startswith('#'):
|
|
|
|
try:
|
|
|
|
user, email, pw = line.split()
|
|
|
|
validate_email(email)
|
|
|
|
new_admin_data.append(
|
|
|
|
{'username': user, 'email': email,
|
|
|
|
'password': pw})
|
|
|
|
except Exception as e:
|
|
|
|
app.logger.error(
|
|
|
|
f"Error while parsing line {i} in admin config file. Config file should contain lines of "
|
|
|
|
f"'<username> <email> <password>\\n'\n Exception: {e}\nAdmin account could not be created."
|
|
|
|
)
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
db.create_all()
|
|
|
|
super_admin_role = user_datastore.find_or_create_role(
|
|
|
|
'super_admin') # root admin = can create other admins
|
|
|
|
admin_role = user_datastore.find_or_create_role(
|
|
|
|
'admin') # 'normal' admin
|
|
|
|
local_role = user_datastore.find_or_create_role(
|
|
|
|
'local') # LDAP user or local user
|
|
|
|
|
|
|
|
for d in new_admin_data:
|
|
|
|
if user_datastore.find_user(email=d['email'],
|
|
|
|
username=d['username']) is None:
|
|
|
|
roles = [super_admin_role, admin_role]
|
|
|
|
if not d['password'] == 'LDAP':
|
|
|
|
roles.append(local_role)
|
|
|
|
|
|
|
|
# create new admin (only if admin does not already exist)
|
|
|
|
new_admin = user_datastore.create_user(
|
|
|
|
email=d['email'], username=d['username'],
|
|
|
|
password=hash_password(d['password']), roles=roles
|
|
|
|
)
|
|
|
|
app.logger.info(
|
|
|
|
f"New super admin user created with username "
|
|
|
|
f"'{new_admin.username}' and email '{new_admin.email}'"
|
|
|
|
f", roles = {[r.name for r in new_admin.roles]}"
|
|
|
|
)
|
|
|
|
|
2022-01-27 22:45:17 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
2022-02-04 21:13:15 +00:00
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
def create_app():
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_object('imaginaerraum_door_admin.default_app_config.DefaultConfig')
|
2022-02-04 21:41:55 +00:00
|
|
|
app.config.from_envvar('APPLICATION_SETTINGS', silent=True)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
|
2022-02-04 21:13:15 +00:00
|
|
|
token_file = Path(app.config.get('TOKEN_FILE'))
|
|
|
|
if not token_file.exists():
|
2022-02-04 21:40:14 +00:00
|
|
|
app.logger.warning(
|
2022-02-04 21:13:15 +00:00
|
|
|
f"Token file not found at {token_file.absolute()}. "
|
|
|
|
"An empty token file will be created."
|
|
|
|
)
|
|
|
|
token_file.touch()
|
|
|
|
|
|
|
|
# create door objects which provides access to the token file and current
|
|
|
|
# door state via MQTT
|
|
|
|
app.door = DoorHandle(
|
|
|
|
token_file=token_file, mqtt_host=app.config['MQTT_HOST'],
|
2022-02-04 21:40:14 +00:00
|
|
|
nfc_socket=app.config['NFC_SOCKET'], logger=app.logger
|
2022-02-04 21:13:15 +00:00
|
|
|
)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
|
|
|
# Mail Config
|
|
|
|
#mail = Mail(app)
|
|
|
|
|
|
|
|
# Create database connection object
|
2022-01-29 22:48:58 +00:00
|
|
|
db.init_app(app)
|
2022-01-25 20:42:35 +00:00
|
|
|
|
2022-01-29 22:48:58 +00:00
|
|
|
from . webapp import door_app
|
2022-01-25 20:42:35 +00:00
|
|
|
app.register_blueprint(door_app)
|
|
|
|
|
|
|
|
# Setup Flask-Security
|
2022-01-30 20:56:11 +00:00
|
|
|
from .auth import ExtendedLoginForm, User, Role
|
|
|
|
|
2022-01-25 20:42:35 +00:00
|
|
|
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
|
|
|
|
security.init_app(app, user_datastore, login_form=ExtendedLoginForm)
|
|
|
|
|
2022-02-04 21:40:56 +00:00
|
|
|
create_super_admins(app, user_datastore)
|
2022-01-27 22:45:17 +00:00
|
|
|
|
2022-02-04 21:13:15 +00:00
|
|
|
return app
|