From d09d9d77bd4519e903de9e3a175459000cf5775a Mon Sep 17 00:00:00 2001 From: Mart van Santen Date: Tue, 22 Mar 2022 15:56:36 +0800 Subject: [PATCH] Removed circular import for DB --- app.py | 18 ++++++++++++++- areas/login/login.py | 12 +++++++--- database.py | 7 ++++++ helpers/__init__.py | 1 + helpers/models.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 database.py create mode 100644 helpers/models.py diff --git a/app.py b/app.py index ce8e019..d95a7e7 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ from flask_jwt_extended import JWTManager from flask_cors import CORS from jsonschema.exceptions import ValidationError from werkzeug.exceptions import BadRequest +from flask_sqlalchemy import SQLAlchemy # These imports are required from areas import api_v1 @@ -13,6 +14,7 @@ from areas import apps from areas import auth from areas import login +from database import db from helpers import ( BadRequest, @@ -23,14 +25,28 @@ from helpers import ( kratos_error, global_error, hydra_error, - KratosUser + KratosUser, + App, + AppRole ) + from config import * import logging app = Flask(__name__) + cors = CORS(app) + app.config["SECRET_KEY"] = SECRET_KEY +app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI + +## from database import db +#db = SQLAlchemy() +db.init_app(app) + +# Late beceuse of circular import +## + app.logger.setLevel(logging.INFO) diff --git a/areas/login/login.py b/areas/login/login.py index 37210d1..ef6750a 100644 --- a/areas/login/login.py +++ b/areas/login/login.py @@ -41,9 +41,15 @@ from helpers import ( kratos_error, global_error, hydra_error, - KratosUser + KratosUser, + App, + AppRole ) +# This is a circular import and should be solved differently +#from app import db +from database import db + # APIs # Create HYDRA & KRATOS API interfaces HYDRA = hydra_client.HydraAdmin(HYDRA_ADMIN_URL) @@ -240,8 +246,7 @@ def consent(): abort(401, description="User not found. Please try again.") # Get role on this app - #app_obj = db.session.query(App).filter(App.slug == app_id).first() - app_obj = False + app_obj = db.session.query(App).filter(App.slug == app_id).first() # Default access level roles = [] @@ -253,6 +258,7 @@ def consent(): ) for role_obj in role_objects: roles.append(role_obj.role) + current_app.logger.info(f"Using '{roles}' when applying consent for {kratos_id}") # Get claims for this user, provided the current app diff --git a/database.py b/database.py new file mode 100644 index 0000000..5c1c832 --- /dev/null +++ b/database.py @@ -0,0 +1,7 @@ + + +from flask_sqlalchemy import SQLAlchemy +db = SQLAlchemy() + + + diff --git a/helpers/__init__.py b/helpers/__init__.py index 3b76a3a..37e81cc 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -2,3 +2,4 @@ from .kratos_api import * from .error_handler import * from .hydra_oauth import * from .kratos import * +from .models import * diff --git a/helpers/models.py b/helpers/models.py new file mode 100644 index 0000000..6286f25 --- /dev/null +++ b/helpers/models.py @@ -0,0 +1,54 @@ +""" +Implement different models used by Stackspin panel +""" + + +from flask import current_app +from flask_sqlalchemy import SQLAlchemy + +# pylint: disable=cyclic-import +# This is based on the documentation of Flask Alchemy +#from app import db + +# We need this import at some point to hook up roles and users +# from sqlalchemy.orm import relationship +from sqlalchemy import ForeignKey, Integer, String + +db = SQLAlchemy() + +# Pylint complains about too-few-public-methods. Methods will be added once +# this is implemented. +# pylint: disable=too-few-public-methods +class App(db.Model): + """ + The App object, interact with the App database object. Data is stored in + the local database. + """ + + + id = db.Column(Integer, primary_key=True) + name = db.Column(String(length=64)) + slug = db.Column(String(length=64), unique=True) + + def __repr__(self): + return f"{self.id} <{self.name}>" + +# Pylint complains about too-few-public-methods. Methods will be added once +# this is implemented. +# pylint: disable=too-few-public-methods +class AppRole(db.Model): + """ + The AppRole object, stores the roles Users have on Apps + """ + + # pylint: disable=no-member + user_id = db.Column(String(length=64), primary_key=True) + # pylint: disable=no-member + app_id = db.Column(Integer, ForeignKey('app.id'), + primary_key=True) + + # pylint: disable=no-member + role = db.Column(String(length=64)) + + def __repr__(self): + return f"{self.role} for {self.user_id} on {self.app_id}"