From 17766aedbdafa1052ceaeb17821eaf456ab798f7 Mon Sep 17 00:00:00 2001 From: Josako Date: Mon, 29 Apr 2024 12:58:48 +0200 Subject: [PATCH] finish implementing Flask_Security_Too and finished dynamic navbar creation. --- config.py | 7 +-- eveai_app/__init__.py | 16 ++++++- eveai_app/context_processors.py | 9 ++++ .../logging_config.py | 0 eveai_app/models/user.py | 6 ++- eveai_app/templates/header.html | 2 +- eveai_app/templates/navbar.html | 44 ++++++++++++++++++- eveai_app/views/user_views.py | 10 ++++- 8 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 eveai_app/context_processors.py rename logging_config.py => eveai_app/logging_config.py (100%) diff --git a/config.py b/config.py index 1d1a958..b2b4fae 100644 --- a/config.py +++ b/config.py @@ -12,17 +12,18 @@ class Config(object): # flask-security-too settings SECURITY_PASSWORD_SALT = '228614859439123264035565568761433607235' - REMEMBER_COOKIE_SAMESITE = 'strict' - SESSION_COOKIE_SAMESITE = 'strict' + # REMEMBER_COOKIE_SAMESITE = 'strict' + # SESSION_COOKIE_SAMESITE = 'strict' SECURITY_CONFIRMABLE = True SECURITY_TRACKABLE = True SECURITY_PASSWORD_COMPLEXITY_CHECKER = 'zxcvbn' SECURITY_POST_LOGIN_VIEW = '/user/tenant' SECURITY_RECOVERABLE = True + SECURITY_EMAIL_SENDER = "eveai_super@flow-it.net" # flask-mailman settings MAIL_SERVER = 'mail.flow-it.net' - MAIL_PORT = 465 + MAIL_PORT = 587 MAIL_USE_TLS = True MAIL_USE_SSL = False MAIL_DEFAULT_SENDER = ('eveAI Admin', 'eveai_admin@flow-it.net') diff --git a/eveai_app/__init__.py b/eveai_app/__init__.py index c5ec3bb..37df35d 100644 --- a/eveai_app/__init__.py +++ b/eveai_app/__init__.py @@ -1,14 +1,15 @@ import logging import os from flask import Flask -from flask_security import SQLAlchemyUserDatastore, current_user +from flask_security import SQLAlchemyUserDatastore from werkzeug.middleware.proxy_fix import ProxyFix import logging.config from .extensions import db, migrate, bootstrap, security, mail, login_manager from .models.user import User, Tenant, Role from .models.document import Document, DocumentLanguage, DocumentVersion -from logging_config import LOGGING +from .logging_config import LOGGING +# from .context_processors import inject_user # Create app @@ -33,6 +34,8 @@ def create_app(config_file=None): user_datastore = SQLAlchemyUserDatastore(db, User, Role) security.init_app(app, user_datastore) + # app.context_processor(inject_user) + # Register Blueprints register_blueprints(app) @@ -40,6 +43,15 @@ def create_app(config_file=None): app.logger.setLevel(logging.DEBUG) mail_logger = logging.getLogger('flask_mailman') mail_logger.setLevel(logging.DEBUG) + security_logger = logging.getLogger('flask_security') + security_logger.setLevel(logging.DEBUG) + # with app.app_context(): + # mail.send_mail( + # subject="Dit is een debugging test", + # message="Die wordt enkel verzonden als de app in debug mode staat", + # from_email="eveai_super@flow-it.net", + # recipient_list=["eveai_super@flow-it.net"], + # ) # Register API register_api(app) diff --git a/eveai_app/context_processors.py b/eveai_app/context_processors.py new file mode 100644 index 0000000..d696288 --- /dev/null +++ b/eveai_app/context_processors.py @@ -0,0 +1,9 @@ +from flask_security import current_user + + +# def inject_user(): +# if current_user: +# print("Injecting current_user: ", current_user) +# else: +# print("No user to inject") +# return {'current_user': current_user} diff --git a/logging_config.py b/eveai_app/logging_config.py similarity index 100% rename from logging_config.py rename to eveai_app/logging_config.py diff --git a/eveai_app/models/user.py b/eveai_app/models/user.py index 7d2b0bc..b54e07f 100644 --- a/eveai_app/models/user.py +++ b/eveai_app/models/user.py @@ -64,6 +64,7 @@ class User(db.Model, UserMixin): first_name = db.Column(db.String(80), nullable=False) last_name = db.Column(db.String(80), nullable=False) is_active = db.Column(db.Boolean, default=True) + active = db.Column(db.Boolean) fs_uniquifier = db.Column(db.String(255), unique=True, nullable=False) confirmed_at = db.Column(db.DateTime, nullable=True) valid_to = db.Column(db.Date, nullable=True) @@ -80,5 +81,8 @@ class User(db.Model, UserMixin): tenant_id = db.Column(db.Integer, db.ForeignKey('public.tenant.id'), nullable=False) def __repr__(self): - return '' % self.name() + return '' % self.user_name + + def has_roles(self, *args): + return any(role.name in args for role in self.roles) diff --git a/eveai_app/templates/header.html b/eveai_app/templates/header.html index f81b23e..e1e41de 100644 --- a/eveai_app/templates/header.html +++ b/eveai_app/templates/header.html @@ -1,5 +1,5 @@
-