From 4fe3cb657579b071fe6a23d82326f8713aa2df03 Mon Sep 17 00:00:00 2001 From: Ross Mountjoy Date: Tue, 4 Feb 2020 08:40:36 -0500 Subject: [PATCH] Started work on 1.0 added roles/access groups --- dashmachine/main/models.py | 7 +- dashmachine/main/utils.py | 155 +++++++++++++++++--------- dashmachine/settings_system/models.py | 7 +- dashmachine/user_system/models.py | 5 +- dashmachine/user_system/utils.py | 3 +- default_config.ini | 29 ++++- migrations/versions/01a575cda54d_.py | 28 +++++ migrations/versions/03663c18575b_.py | 28 +++++ migrations/versions/598477dd1193_.py | 28 +++++ migrations/versions/d87e35114b0b_.py | 34 ++++++ 10 files changed, 259 insertions(+), 65 deletions(-) create mode 100644 migrations/versions/01a575cda54d_.py create mode 100644 migrations/versions/03663c18575b_.py create mode 100644 migrations/versions/598477dd1193_.py create mode 100644 migrations/versions/d87e35114b0b_.py diff --git a/dashmachine/main/models.py b/dashmachine/main/models.py index 8a6f749..7b61141 100644 --- a/dashmachine/main/models.py +++ b/dashmachine/main/models.py @@ -20,6 +20,7 @@ class Apps(db.Model): description = db.Column(db.String()) open_in = db.Column(db.String()) data_template = db.Column(db.String()) + groups = db.Column(db.String()) class TemplateApps(db.Model): @@ -45,5 +46,7 @@ class ApiCalls(db.Model): value_template = db.Column(db.String()) -db.create_all() -db.session.commit() +class Groups(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String()) + roles = db.Column(db.String()) diff --git a/dashmachine/main/utils.py b/dashmachine/main/utils.py index a144ad5..b9b6706 100755 --- a/dashmachine/main/utils.py +++ b/dashmachine/main/utils.py @@ -4,7 +4,7 @@ from shutil import copyfile from requests import get from configparser import ConfigParser from dashmachine.paths import dashmachine_folder, images_folder, root_folder -from dashmachine.main.models import Apps, ApiCalls, TemplateApps +from dashmachine.main.models import Apps, ApiCalls, TemplateApps, Groups from dashmachine.settings_system.models import Settings from dashmachine.user_system.models import User from dashmachine.user_system.utils import add_edit_user @@ -29,66 +29,100 @@ def read_config(): Apps.query.delete() ApiCalls.query.delete() Settings.query.delete() - - try: - settings = Settings( - theme=config["Settings"]["theme"], - accent=config["Settings"]["accent"], - background=config["Settings"]["background"], - ) - db.session.add(settings) - db.session.commit() - except Exception as e: - return {"msg": f"Invalid Config: {e}."} + Groups.query.delete() for section in config.sections(): - if section != "Settings": - # API call creation - if "platform" in config[section]: - api_call = ApiCalls() - api_call.name = section - if "resource" in config[section]: - api_call.resource = config[section]["resource"] - else: - return { - "msg": f"Invalid Config: {section} does not contain resource." - } + # Settings creation + if section == "Settings": + settings = Settings() + if "theme" in config["Settings"]: + settings.theme = config["Settings"]["theme"] + else: + settings.theme = "light" - if "method" in config[section]: - api_call.method = config[section]["method"] - else: - api_call.method = "GET" + if "accent" in config["Settings"]: + settings.accent = config["Settings"]["accent"] + else: + settings.accent = "orange" - if "payload" in config[section]: - api_call.payload = config[section]["payload"] - else: - api_call.payload = None + if "background" in config["Settings"]: + settings.background = config["Settings"]["background"] + else: + settings.background = "None" - if "authentication" in config[section]: - api_call.authentication = config[section]["authentication"] - else: - api_call.authentication = None + if "roles" in config["Settings"]: + settings.roles = config["Settings"]["roles"] + else: + settings.roles = "admin" - if "username" in config[section]: - api_call.username = config[section]["username"] - else: - api_call.username = None + if "home_access_groups" in config["Settings"]: + settings.home_access_groups = config["Settings"]["home_access_groups"] + else: + settings.home_access_groups = "admin_only" - if "password" in config[section]: - api_call.password = config[section]["password"] - else: - api_call.password = None + if "settings_access_groups" in config["Settings"]: + settings.settings_access_groups = config["Settings"][ + "settings_access_groups" + ] + else: + settings.settings_access_groups = "admin_only" - if "value_template" in config[section]: - api_call.value_template = config[section]["value_template"] - else: - api_call.value_template = section + db.session.add(settings) + db.session.commit() - db.session.add(api_call) - db.session.commit() - continue + # Groups creation + elif "roles" in config[section]: + group = Groups() + group.name = section + group.roles = config[section]["roles"] + db.session.add(group) + db.session.commit() + # API call creation + elif "platform" in config[section]: + api_call = ApiCalls() + api_call.name = section + if "resource" in config[section]: + api_call.resource = config[section]["resource"] + else: + return {"msg": f"Invalid Config: {section} does not contain resource."} + + if "method" in config[section]: + api_call.method = config[section]["method"] + else: + api_call.method = "GET" + + if "payload" in config[section]: + api_call.payload = config[section]["payload"] + else: + api_call.payload = None + + if "authentication" in config[section]: + api_call.authentication = config[section]["authentication"] + else: + api_call.authentication = None + + if "username" in config[section]: + api_call.username = config[section]["username"] + else: + api_call.username = None + + if "password" in config[section]: + api_call.password = config[section]["password"] + else: + api_call.password = None + + if "value_template" in config[section]: + api_call.value_template = config[section]["value_template"] + else: + api_call.value_template = section + + db.session.add(api_call) + db.session.commit() + continue + + else: # App creation app = Apps() app.name = section @@ -127,6 +161,11 @@ def read_config(): else: app.data_template = None + if "groups" in config[section]: + app.groups = config[section]["groups"] + else: + app.groups = None + db.session.add(app) db.session.commit() return {"msg": "success", "settings": row2dict(settings)} @@ -165,13 +204,17 @@ def public_route(decorated_function): def dashmachine_init(): + db.create_all() + db.session.commit() + migrate_cmd = "python " + os.path.join(root_folder, "manage_db.py db stamp head") + subprocess.run(migrate_cmd, stderr=subprocess.PIPE, shell=True, encoding="utf-8") + migrate_cmd = "python " + os.path.join(root_folder, "manage_db.py db migrate") subprocess.run(migrate_cmd, stderr=subprocess.PIPE, shell=True, encoding="utf-8") upgrade_cmd = "python " + os.path.join(root_folder, "manage_db.py db upgrade") subprocess.run(upgrade_cmd, stderr=subprocess.PIPE, shell=True, encoding="utf-8") - read_config() read_template_apps() user_data_folder = os.path.join(dashmachine_folder, "user_data") @@ -193,11 +236,17 @@ def dashmachine_init(): config_file = os.path.join(user_data_folder, "config.ini") if not os.path.exists(config_file): copyfile("default_config.ini", config_file) - read_config() + + read_config() user = User.query.first() if not user: - add_edit_user(username="admin", password="adminadmin") + settings = Settings.query.first() + add_edit_user( + username="admin", + password="adminadmin", + role=settings.roles.split(",")[0].strip(), + ) def get_rest_data(template): diff --git a/dashmachine/settings_system/models.py b/dashmachine/settings_system/models.py index 80f2d94..bc2c7e7 100644 --- a/dashmachine/settings_system/models.py +++ b/dashmachine/settings_system/models.py @@ -6,7 +6,6 @@ class Settings(db.Model): theme = db.Column(db.String()) accent = db.Column(db.String()) background = db.Column(db.String()) - - -db.create_all() -db.session.commit() + roles = db.Column(db.String()) + home_access_groups = db.Column(db.String()) + settings_access_groups = db.Column(db.String()) diff --git a/dashmachine/user_system/models.py b/dashmachine/user_system/models.py index 32c75e9..ba3ef34 100644 --- a/dashmachine/user_system/models.py +++ b/dashmachine/user_system/models.py @@ -11,7 +11,4 @@ class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) - - -db.create_all() -db.session.commit() + role = db.Column(db.String()) diff --git a/dashmachine/user_system/utils.py b/dashmachine/user_system/utils.py index 9b981de..6b6ef0c 100755 --- a/dashmachine/user_system/utils.py +++ b/dashmachine/user_system/utils.py @@ -2,7 +2,7 @@ from dashmachine import db, bcrypt from dashmachine.user_system.models import User -def add_edit_user(username, password, user_id=None): +def add_edit_user(username, password, user_id=None, role=None): if user_id: user = User.query.filter_by(id=user_id).first() else: @@ -13,5 +13,6 @@ def add_edit_user(username, password, user_id=None): hashed_password = bcrypt.generate_password_hash(password).decode("utf-8") user.username = username user.password = hashed_password + user.role = role db.session.merge(user) db.session.commit() diff --git a/default_config.ini b/default_config.ini index c768f39..172cb3d 100644 --- a/default_config.ini +++ b/default_config.ini @@ -1,4 +1,31 @@ +# -------- +# SETTINGS +# -------- [Settings] theme = light accent = orange -background = None \ No newline at end of file +background = None +roles = admin, user, public_user +home_access_groups = admin_only +settings_access_groups = admin_only + +# ------------- +# ACCESS GROUPS +# ------------- +[public] +roles = admin, user, public_user + +[private] +roles = admin, user + +[admin_only] +roles = admin + +# -------- +# API DATA +# -------- + + +# ---- +# APPS +# ---- \ No newline at end of file diff --git a/migrations/versions/01a575cda54d_.py b/migrations/versions/01a575cda54d_.py new file mode 100644 index 0000000..0c61a57 --- /dev/null +++ b/migrations/versions/01a575cda54d_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 01a575cda54d +Revises: 598477dd1193 +Create Date: 2020-02-04 07:39:43.504475 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '01a575cda54d' +down_revision = '598477dd1193' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('apps', sa.Column('groups', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('apps', 'groups') + # ### end Alembic commands ### diff --git a/migrations/versions/03663c18575b_.py b/migrations/versions/03663c18575b_.py new file mode 100644 index 0000000..c96460f --- /dev/null +++ b/migrations/versions/03663c18575b_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 03663c18575b +Revises: af72304ae017 +Create Date: 2020-02-04 07:14:23.184567 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '03663c18575b' +down_revision = 'af72304ae017' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('user', sa.Column('role', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('user', 'role') + # ### end Alembic commands ### diff --git a/migrations/versions/598477dd1193_.py b/migrations/versions/598477dd1193_.py new file mode 100644 index 0000000..2c4e0d5 --- /dev/null +++ b/migrations/versions/598477dd1193_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 598477dd1193 +Revises: 03663c18575b +Create Date: 2020-02-04 07:33:25.019173 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '598477dd1193' +down_revision = '03663c18575b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('settings', sa.Column('roles', sa.String(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('settings', 'roles') + # ### end Alembic commands ### diff --git a/migrations/versions/d87e35114b0b_.py b/migrations/versions/d87e35114b0b_.py new file mode 100644 index 0000000..6b18fd0 --- /dev/null +++ b/migrations/versions/d87e35114b0b_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: d87e35114b0b +Revises: 01a575cda54d +Create Date: 2020-02-04 08:13:35.783741 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "d87e35114b0b" +down_revision = "01a575cda54d" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "settings", sa.Column("home_access_groups", sa.String(), nullable=True) + ) + op.add_column( + "settings", sa.Column("settings_access_groups", sa.String(), nullable=True) + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("settings", "settings_access_groups") + op.drop_column("settings", "home_access_groups") + # ### end Alembic commands ###