add new register user in dlt
This commit is contained in:
parent
c9238e8e6a
commit
6a75423532
|
@ -2,8 +2,12 @@ import json
|
|||
|
||||
import click
|
||||
|
||||
from flask import current_app as app
|
||||
from ereuseapi.methods import register_user
|
||||
from ereuse_devicehub.db import db
|
||||
from ereuse_devicehub.resources.user.models import User
|
||||
from ereuse_devicehub.resources.agent.models import Person
|
||||
from ereuse_devicehub.modules.dpp.utils import encrypt
|
||||
|
||||
|
||||
class RegisterUserDlt:
|
||||
|
@ -12,23 +16,53 @@ class RegisterUserDlt:
|
|||
def __init__(self, app) -> None:
|
||||
super().__init__()
|
||||
self.app = app
|
||||
help = "Register user in Dlt with params: email password rols"
|
||||
help = "Insert users than are in Dlt with params: path of data set file"
|
||||
self.app.cli.command('dlt_register_user', short_help=help)(self.run)
|
||||
|
||||
@click.argument('email')
|
||||
@click.argument('password')
|
||||
@click.argument('rols')
|
||||
def run(self, email, password, rols):
|
||||
if not rols:
|
||||
rols = "Operator"
|
||||
user = User.query.filter_by(email=email).one()
|
||||
@click.argument('dataset_file')
|
||||
def run(self, dataset_file):
|
||||
with open(dataset_file) as f:
|
||||
dataset = json.loads(f.read())
|
||||
|
||||
token_dlt = user.set_new_dlt_keys(password)
|
||||
result = user.allow_permitions(api_token=token_dlt, rols=rols)
|
||||
rols = user.get_rols(token_dlt=token_dlt)
|
||||
rols = [k for k, v in rols]
|
||||
user.rols_dlt = json.dumps(rols)
|
||||
for d in dataset:
|
||||
self.add_user(d)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return result, rols
|
||||
def add_user(self, data):
|
||||
email = data.get("email")
|
||||
name = email.split('@')[0]
|
||||
password = data.get("password")
|
||||
eth_priv_key = data.get("eth_priv_key")
|
||||
eth_pub_key = data.get("eth_pub_key")
|
||||
user = User.query.filter_by(email=email).first()
|
||||
import pdb; pdb.set_trace()
|
||||
|
||||
if not user:
|
||||
user = User(email=email, password=password)
|
||||
user.individuals.add(Person(name=name))
|
||||
|
||||
api_dlt = app.config.get('API_DLT')
|
||||
try:
|
||||
response = register_user(api_dlt, eth_priv_key)
|
||||
api_token = response.get('data', {}).get('api_token')
|
||||
except Exception:
|
||||
api_token = ""
|
||||
ethereum = {
|
||||
"eth_pub_key": eth_pub_key,
|
||||
"eth_priv_key": eth_priv_key,
|
||||
"api_token": api_token
|
||||
}
|
||||
data_eth = json.dumps(ethereum)
|
||||
user.api_keys_dlt = encrypt(password, data_eth)
|
||||
|
||||
try:
|
||||
attributes = user.get_abac_attributes()
|
||||
roles = attributes.get("role", ["Operator"])
|
||||
except Exception:
|
||||
roles ["Operator"]
|
||||
|
||||
user.rols_dlt = json.dumps(roles)
|
||||
|
||||
if not user.id:
|
||||
db.session.add(user)
|
||||
|
|
Reference in New Issue