diff --git a/login/app.py b/login/app.py index 1ea53adb68590735e544253184dd06d454b956b0..cfd0868a50841bcd009bedefdee43c31860ad4fa 100644 --- a/login/app.py +++ b/login/app.py @@ -12,10 +12,6 @@ from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate from flask.cli import AppGroup -from sqlalchemy import select -from sqlalchemy.orm import Session -from sqlalchemy import create_engine - # Hydra admin import hydra_client @@ -30,7 +26,7 @@ app = Flask(__name__) # Load config app.config.from_object(os.environ['APP_SETTINGS']) - +# # Move to config? app.logger.setLevel(logging.INFO) @@ -40,25 +36,24 @@ HYDRA_ADMIN_URL = os.environ['HYDRA_ADMIN_URL'] HYDRA = hydra_client.HydraAdmin(HYDRA_ADMIN_URL) -# Create DB interface +# Create DB & migrate interface db = SQLAlchemy(app) -engine = create_engine(app.config["SQLALCHEMY_DATABASE_URI"]) - - -# Migrate interface migrate = Migrate(app, db) # Import models from models import User, App, AppRole -# Import CLI routes - -# Flask CLI +############################################################################## +# CLI INTERFACE # +############################################################################## +# Define Flask CLI command groups and commands user_cli = AppGroup('user') app_cli = AppGroup('app') +## CLI APP COMMANDS + @app_cli.command('create') @click.argument('slug') @click.argument('name') @@ -72,12 +67,12 @@ def create_app(slug, name): db.session.add(obj) db.session.commit() + @app_cli.command('list') def list_app(): app.logger.info("Listing apps") - apps = App.query.all() - + for obj in apps: print("App name: %s \t Slug: %s" %(obj.name, obj.slug)) @@ -101,12 +96,100 @@ def delete_app(slug): app.cli.add_command(app_cli) +## CLI USER COMMANDS + @user_cli.command('create') @click.argument('email') def create_user(email): - app.logger.info("Creating user: {0}".format(email)) + app.logger.info("Creating user with email: ({0})".format(email)) + + obj = User() + obj.email = email + + db.session.add(obj) + db.session.commit() + + +@user_cli.command('list') +def list_user(): + app.logger.info("Listing users") + users = User.query.all() + + for obj in users: + print("Email: %s (admin: %s)" %(obj.email, obj.admin)) + + +@user_cli.command('delete',) +@click.argument('email') +def delete_user(email): + app.logger.info("Trying to delete user: {0}".format(email)) + obj = User.query.filter_by(email=email).first() + + if not obj: + app.logger.info("Not found") + return + + db.session.delete(obj) + db.session.commit() + app.logger.info("Success") + return + +@user_cli.command('setadmin') +@click.argument('email') +def setadmin_user(email): + app.logger.info("Trying to make user into admin: {0}".format(email)) + obj = User.query.filter_by(email=email).first() + + if not obj: + app.logger.info("Not found") + return + + obj.admin = True + db.session.commit() + app.logger.info("Success") + return + + +@user_cli.command('unsetadmin') +@click.argument('email') +def unsetadmin_user(email): + app.logger.info("Trying to make user into normal user: {0}".format(email)) + obj = User.query.filter_by(email=email).first() + + if not obj: + app.logger.info("Not found") + return + + obj.admin = False + db.session.commit() + app.logger.info("Success") + return + + + +@user_cli.command('recover') +@click.argument('email') +def recover_user(email): + app.logger.info("Trying to sent recover email for user: {0}".format(email)) + obj = User.query.filter_by(email=email).first() + + if not obj: + app.logger.info("Not found") + return + + ## + return + + app.cli.add_command(user_cli) + + + +############################################################################## +# WEB ROUTES # +############################################################################## + @app.route('/login', methods=['GET', 'POST']) def login(): """Provides login form and handles login attempt diff --git a/login/models.py b/login/models.py index 2a898cd5d90b51b8d9d469b8edc7df2c190e7c21..e12e0e131deb66150696d86d6c25fef519b245f1 100644 --- a/login/models.py +++ b/login/models.py @@ -3,7 +3,7 @@ from app import db from sqlalchemy.dialects.postgresql import JSON from sqlalchemy.orm import relationship -from sqlalchemy import Column, Integer, String, ForeignKey +from sqlalchemy import Column, Integer, String, ForeignKey, Boolean @@ -12,6 +12,7 @@ class User(db.Model): id = db.Column(Integer, primary_key=True) email = db.Column(String, unique=True) + admin = db.Column(Boolean, default=False) app_roles = relationship('AppRole', back_populates="user")