Started work on 1.0
added roles/access groups
This commit is contained in:
parent
206bac4d0a
commit
4fe3cb6575
@ -20,6 +20,7 @@ class Apps(db.Model):
|
|||||||
description = db.Column(db.String())
|
description = db.Column(db.String())
|
||||||
open_in = db.Column(db.String())
|
open_in = db.Column(db.String())
|
||||||
data_template = db.Column(db.String())
|
data_template = db.Column(db.String())
|
||||||
|
groups = db.Column(db.String())
|
||||||
|
|
||||||
|
|
||||||
class TemplateApps(db.Model):
|
class TemplateApps(db.Model):
|
||||||
@ -45,5 +46,7 @@ class ApiCalls(db.Model):
|
|||||||
value_template = db.Column(db.String())
|
value_template = db.Column(db.String())
|
||||||
|
|
||||||
|
|
||||||
db.create_all()
|
class Groups(db.Model):
|
||||||
db.session.commit()
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
name = db.Column(db.String())
|
||||||
|
roles = db.Column(db.String())
|
||||||
|
@ -4,7 +4,7 @@ from shutil import copyfile
|
|||||||
from requests import get
|
from requests import get
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from dashmachine.paths import dashmachine_folder, images_folder, root_folder
|
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.settings_system.models import Settings
|
||||||
from dashmachine.user_system.models import User
|
from dashmachine.user_system.models import User
|
||||||
from dashmachine.user_system.utils import add_edit_user
|
from dashmachine.user_system.utils import add_edit_user
|
||||||
@ -29,31 +29,64 @@ def read_config():
|
|||||||
Apps.query.delete()
|
Apps.query.delete()
|
||||||
ApiCalls.query.delete()
|
ApiCalls.query.delete()
|
||||||
Settings.query.delete()
|
Settings.query.delete()
|
||||||
|
Groups.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}."}
|
|
||||||
|
|
||||||
for section in config.sections():
|
for section in config.sections():
|
||||||
if section != "Settings":
|
|
||||||
|
# Settings creation
|
||||||
|
if section == "Settings":
|
||||||
|
settings = Settings()
|
||||||
|
if "theme" in config["Settings"]:
|
||||||
|
settings.theme = config["Settings"]["theme"]
|
||||||
|
else:
|
||||||
|
settings.theme = "light"
|
||||||
|
|
||||||
|
if "accent" in config["Settings"]:
|
||||||
|
settings.accent = config["Settings"]["accent"]
|
||||||
|
else:
|
||||||
|
settings.accent = "orange"
|
||||||
|
|
||||||
|
if "background" in config["Settings"]:
|
||||||
|
settings.background = config["Settings"]["background"]
|
||||||
|
else:
|
||||||
|
settings.background = "None"
|
||||||
|
|
||||||
|
if "roles" in config["Settings"]:
|
||||||
|
settings.roles = config["Settings"]["roles"]
|
||||||
|
else:
|
||||||
|
settings.roles = "admin"
|
||||||
|
|
||||||
|
if "home_access_groups" in config["Settings"]:
|
||||||
|
settings.home_access_groups = config["Settings"]["home_access_groups"]
|
||||||
|
else:
|
||||||
|
settings.home_access_groups = "admin_only"
|
||||||
|
|
||||||
|
if "settings_access_groups" in config["Settings"]:
|
||||||
|
settings.settings_access_groups = config["Settings"][
|
||||||
|
"settings_access_groups"
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
settings.settings_access_groups = "admin_only"
|
||||||
|
|
||||||
|
db.session.add(settings)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
# 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
|
# API call creation
|
||||||
if "platform" in config[section]:
|
elif "platform" in config[section]:
|
||||||
api_call = ApiCalls()
|
api_call = ApiCalls()
|
||||||
api_call.name = section
|
api_call.name = section
|
||||||
if "resource" in config[section]:
|
if "resource" in config[section]:
|
||||||
api_call.resource = config[section]["resource"]
|
api_call.resource = config[section]["resource"]
|
||||||
else:
|
else:
|
||||||
return {
|
return {"msg": f"Invalid Config: {section} does not contain resource."}
|
||||||
"msg": f"Invalid Config: {section} does not contain resource."
|
|
||||||
}
|
|
||||||
|
|
||||||
if "method" in config[section]:
|
if "method" in config[section]:
|
||||||
api_call.method = config[section]["method"]
|
api_call.method = config[section]["method"]
|
||||||
@ -89,6 +122,7 @@ def read_config():
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
else:
|
||||||
# App creation
|
# App creation
|
||||||
app = Apps()
|
app = Apps()
|
||||||
app.name = section
|
app.name = section
|
||||||
@ -127,6 +161,11 @@ def read_config():
|
|||||||
else:
|
else:
|
||||||
app.data_template = None
|
app.data_template = None
|
||||||
|
|
||||||
|
if "groups" in config[section]:
|
||||||
|
app.groups = config[section]["groups"]
|
||||||
|
else:
|
||||||
|
app.groups = None
|
||||||
|
|
||||||
db.session.add(app)
|
db.session.add(app)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return {"msg": "success", "settings": row2dict(settings)}
|
return {"msg": "success", "settings": row2dict(settings)}
|
||||||
@ -165,13 +204,17 @@ def public_route(decorated_function):
|
|||||||
|
|
||||||
|
|
||||||
def dashmachine_init():
|
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")
|
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")
|
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")
|
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")
|
subprocess.run(upgrade_cmd, stderr=subprocess.PIPE, shell=True, encoding="utf-8")
|
||||||
|
|
||||||
read_config()
|
|
||||||
read_template_apps()
|
read_template_apps()
|
||||||
user_data_folder = os.path.join(dashmachine_folder, "user_data")
|
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")
|
config_file = os.path.join(user_data_folder, "config.ini")
|
||||||
if not os.path.exists(config_file):
|
if not os.path.exists(config_file):
|
||||||
copyfile("default_config.ini", config_file)
|
copyfile("default_config.ini", config_file)
|
||||||
|
|
||||||
read_config()
|
read_config()
|
||||||
|
|
||||||
user = User.query.first()
|
user = User.query.first()
|
||||||
if not user:
|
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):
|
def get_rest_data(template):
|
||||||
|
@ -6,7 +6,6 @@ class Settings(db.Model):
|
|||||||
theme = db.Column(db.String())
|
theme = db.Column(db.String())
|
||||||
accent = db.Column(db.String())
|
accent = db.Column(db.String())
|
||||||
background = db.Column(db.String())
|
background = db.Column(db.String())
|
||||||
|
roles = db.Column(db.String())
|
||||||
|
home_access_groups = db.Column(db.String())
|
||||||
db.create_all()
|
settings_access_groups = db.Column(db.String())
|
||||||
db.session.commit()
|
|
||||||
|
@ -11,7 +11,4 @@ class User(db.Model, UserMixin):
|
|||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(120), unique=True, nullable=False)
|
username = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
password = db.Column(db.String(60), nullable=False)
|
password = db.Column(db.String(60), nullable=False)
|
||||||
|
role = db.Column(db.String())
|
||||||
|
|
||||||
db.create_all()
|
|
||||||
db.session.commit()
|
|
||||||
|
@ -2,7 +2,7 @@ from dashmachine import db, bcrypt
|
|||||||
from dashmachine.user_system.models import User
|
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:
|
if user_id:
|
||||||
user = User.query.filter_by(id=user_id).first()
|
user = User.query.filter_by(id=user_id).first()
|
||||||
else:
|
else:
|
||||||
@ -13,5 +13,6 @@ def add_edit_user(username, password, user_id=None):
|
|||||||
hashed_password = bcrypt.generate_password_hash(password).decode("utf-8")
|
hashed_password = bcrypt.generate_password_hash(password).decode("utf-8")
|
||||||
user.username = username
|
user.username = username
|
||||||
user.password = hashed_password
|
user.password = hashed_password
|
||||||
|
user.role = role
|
||||||
db.session.merge(user)
|
db.session.merge(user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
@ -1,4 +1,31 @@
|
|||||||
|
# --------
|
||||||
|
# SETTINGS
|
||||||
|
# --------
|
||||||
[Settings]
|
[Settings]
|
||||||
theme = light
|
theme = light
|
||||||
accent = orange
|
accent = orange
|
||||||
background = None
|
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
|
||||||
|
# ----
|
28
migrations/versions/01a575cda54d_.py
Normal file
28
migrations/versions/01a575cda54d_.py
Normal file
@ -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 ###
|
28
migrations/versions/03663c18575b_.py
Normal file
28
migrations/versions/03663c18575b_.py
Normal file
@ -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 ###
|
28
migrations/versions/598477dd1193_.py
Normal file
28
migrations/versions/598477dd1193_.py
Normal file
@ -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 ###
|
34
migrations/versions/d87e35114b0b_.py
Normal file
34
migrations/versions/d87e35114b0b_.py
Normal file
@ -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 ###
|
Loading…
x
Reference in New Issue
Block a user