From 17c88ef4b10bb022a4dbdb6068fa4cfeaf88bb92 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 10:43:43 +0200 Subject: [PATCH 1/4] get rols --- ereuse_devicehub/resources/user/models.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index d1caf715..272b0943 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -138,7 +138,7 @@ class User(UserMixin, Thing): data = json.dumps(data) self.api_keys_dlt = encrypt(password, data) - def allow_permitions(self, api_token=None): + def allow_permitions(self, api_token=None, rols="Operator"): if 'dpp' not in app.blueprints.keys(): return @@ -152,22 +152,25 @@ class User(UserMixin, Thing): apiUser1 = API(api_dlt, keyUser1, "ethereum") - result = apiUser1.issue_credential("Operator", target_user) + for rol in rols.split(","): + result = apiUser1.issue_credential(rol.strip(), target_user) return result - def get_rols(self): - if session.get('rols'): - return session.get('rols') + def get_rols_dlt(self): + return json.loads(self.rols_dlt) + + def get_rols(self, token_dlt=None): if 'dpp' not in app.blueprints.keys(): return [] - if not session.get('token_dlt'): - return [] + if not token_dlt: + token_dlt = session.get('token_dlt') + if not token_dlt: + return [] - token_dlt = session.get('token_dlt') api_dlt = app.config.get('API_DLT') - if not token_dlt or not api_dlt: + if not api_dlt: return [] api = API(api_dlt, token_dlt, "ethereum") From fad008b25d0ab8d2826ffe620aa629d77b2b230d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 16:41:47 +0200 Subject: [PATCH 2/4] command initdatas --- ereuse_devicehub/commands/initdatas.py | 41 +++++++++++++++++++ ereuse_devicehub/devicehub.py | 6 ++- .../versions/fbb7e2a0cde0_initial.py | 8 ++-- 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 ereuse_devicehub/commands/initdatas.py diff --git a/ereuse_devicehub/commands/initdatas.py b/ereuse_devicehub/commands/initdatas.py new file mode 100644 index 00000000..bf76cd9c --- /dev/null +++ b/ereuse_devicehub/commands/initdatas.py @@ -0,0 +1,41 @@ +from uuid import uuid4 + +import click +from boltons.urlutils import URL + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.agent.models import Person +from ereuse_devicehub.resources.inventory.model import Inventory +from ereuse_devicehub.resources.user.models import User + + +class InitDatas: + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.schema = app.config.get('DB_SCHEMA') + self.app.cli.command( + 'initdata', short_help='Save a minimum structure of datas.' + )(self.run) + + def run(self): + inv = Inventory( + id=self.schema, + name="usody", + tag_provider=URL('http://localhost:8081'), + tag_token=uuid4(), + org_id=uuid4(), + ) + + db.session.add(inv) + db.session.commit() + + email = 'user@dhub.com' + password = '1234' + name = 'user' + + user = User(email=email, password=password) + user.individuals.add(Person(name=name)) + db.session.add(user) + + db.session.commit() diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index e5e76145..dbf93a2d 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -5,20 +5,21 @@ from typing import Type import boltons.urlutils import click import click_spinner -import ereuse_devicehub.ereuse_utils.cli -from ereuse_devicehub.ereuse_utils.session import DevicehubClient from flask import _app_ctx_stack, g from flask_login import LoginManager, current_user from flask_sqlalchemy import SQLAlchemy +import ereuse_devicehub.ereuse_utils.cli from ereuse_devicehub.auth import Auth from ereuse_devicehub.client import Client, UserClient +from ereuse_devicehub.commands.initdatas import InitDatas # from ereuse_devicehub.commands.reports import Report from ereuse_devicehub.commands.users import GetToken from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.db import db from ereuse_devicehub.dummy.dummy import Dummy +from ereuse_devicehub.ereuse_utils.session import DevicehubClient from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.inventory import Inventory, InventoryDef from ereuse_devicehub.resources.user.models import User @@ -73,6 +74,7 @@ class Devicehub(Teal): self.dummy = Dummy(self) # self.report = Report(self) self.get_token = GetToken(self) + self.initdata = InitDatas(self) @self.cli.group( short_help='Inventory management.', diff --git a/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py b/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py index 08758ec3..dc39795b 100644 --- a/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py +++ b/ereuse_devicehub/migrations/versions/fbb7e2a0cde0_initial.py @@ -8,12 +8,11 @@ Create Date: 2020-05-07 10:04:40.269511 import citext import sqlalchemy as sa import sqlalchemy_utils -from ereuse_devicehub import teal -from alembic import context -from alembic import op +from alembic import context, op from sqlalchemy.dialects import postgresql -from ereuse_devicehub.resources.enums import TransferState, Severity +from ereuse_devicehub import teal +from ereuse_devicehub.resources.enums import Severity, TransferState # revision identifiers, used by Alembic. revision = 'fbb7e2a0cde0' @@ -348,6 +347,7 @@ def upgrade(): postgresql_using='hash', schema=f'{get_inv()}', ) + op.execute(f"CREATE SEQUENCE {get_inv()}.device_seq START 1;") op.create_table( 'agent', sa.Column( From 5f1c7c8b4ab0dfbe277dcf940648eb68e1e14a0d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 17:56:34 +0200 Subject: [PATCH 3/4] add command adduser --- ereuse_devicehub/commands/adduser.py | 24 ++++++++++++++++++++++++ ereuse_devicehub/commands/initdatas.py | 1 - ereuse_devicehub/devicehub.py | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ereuse_devicehub/commands/adduser.py diff --git a/ereuse_devicehub/commands/adduser.py b/ereuse_devicehub/commands/adduser.py new file mode 100644 index 00000000..21c4b25d --- /dev/null +++ b/ereuse_devicehub/commands/adduser.py @@ -0,0 +1,24 @@ +import click + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.agent.models import Person +from ereuse_devicehub.resources.user.models import User + + +class AddUser: + def __init__(self, app) -> None: + super().__init__() + self.app = app + self.schema = app.config.get('DB_SCHEMA') + self.app.cli.command('adduser', short_help='add a user.')(self.run) + + @click.argument('email') + @click.argument('password') + def run(self, email, password): + name = email.split('@')[0] + + user = User(email=email, password=password) + user.individuals.add(Person(name=name)) + db.session.add(user) + + db.session.commit() diff --git a/ereuse_devicehub/commands/initdatas.py b/ereuse_devicehub/commands/initdatas.py index bf76cd9c..4e199fa4 100644 --- a/ereuse_devicehub/commands/initdatas.py +++ b/ereuse_devicehub/commands/initdatas.py @@ -1,6 +1,5 @@ from uuid import uuid4 -import click from boltons.urlutils import URL from ereuse_devicehub.db import db diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index dbf93a2d..a6c6b7c9 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -12,6 +12,7 @@ from flask_sqlalchemy import SQLAlchemy import ereuse_devicehub.ereuse_utils.cli from ereuse_devicehub.auth import Auth from ereuse_devicehub.client import Client, UserClient +from ereuse_devicehub.commands.adduser import AddUser from ereuse_devicehub.commands.initdatas import InitDatas # from ereuse_devicehub.commands.reports import Report @@ -75,6 +76,7 @@ class Devicehub(Teal): # self.report = Report(self) self.get_token = GetToken(self) self.initdata = InitDatas(self) + self.adduser = AddUser(self) @self.cli.group( short_help='Inventory management.', From 523ca3e89289f12f99722325faef9ec049bb9808 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 15 Jun 2023 18:30:55 +0200 Subject: [PATCH 4/4] add some modules commands --- ereuse_devicehub/devicehub.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index 932dc82a..a9093fa1 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -31,6 +31,21 @@ try: except Exception: GetMembers = None +try: + from ereuse_devicehub.modules.dpp.commands.register_user_dlt import RegisterUserDlt +except Exception: + RegisterUserDlt = None + +try: + from ereuse_devicehub.modules.oidc.commands.add_member import AddMember +except Exception: + AddMember = None + +try: + from ereuse_devicehub.modules.oidc.commands.add_member import AddClientOidc +except Exception: + AddClientOidc = None + class Devicehub(Teal): test_client_class = Client @@ -80,6 +95,12 @@ class Devicehub(Teal): self.get_token = GetToken(self) if GetMembers: self.get_members = GetMembers(self) + if RegisterUserDlt: + self.register_user_dlt = RegisterUserDlt(self) + if AddMember: + self.register_user_dlt = AddMember(self) + if AddClientOidc: + self.register_user_dlt = AddClientOidc(self) @self.cli.group( short_help='Inventory management.',