Ver código fonte

fix: automatically create tenant for user (#793)

tags/0.3.13
Matri 2 anos atrás
pai
commit
fc7e4ac75b
Nenhuma conta vinculada ao e-mail do autor do commit
2 arquivos alterados com 17 adições e 3 exclusões
  1. 16
    2
      api/app.py
  2. 1
    1
      api/controllers/console/auth/login.py

+ 16
- 2
api/app.py Ver arquivo

from commands import register_commands from commands import register_commands
from models.account import TenantAccountJoin, AccountStatus from models.account import TenantAccountJoin, AccountStatus
from models.model import Account, EndUser, App from models.model import Account, EndUser, App
from services.account_service import TenantService


import warnings import warnings
warnings.simplefilter("ignore", ResourceWarning) warnings.simplefilter("ignore", ResourceWarning)
ext_sentry.init_app(app) ext_sentry.init_app(app)




def _create_tenant_for_account(account):
tenant = TenantService.create_tenant(f"{account.name}'s Workspace")

TenantService.create_tenant_member(tenant, account, role='owner')
account.current_tenant = tenant

return tenant


# Flask-Login configuration # Flask-Login configuration
@login_manager.user_loader @login_manager.user_loader
def load_user(user_id): def load_user(user_id):


if tenant_account_join: if tenant_account_join:
account.current_tenant_id = tenant_account_join.tenant_id account.current_tenant_id = tenant_account_join.tenant_id
session['workspace_id'] = account.current_tenant_id
else:
_create_tenant_for_account(account)
session['workspace_id'] = account.current_tenant_id
else: else:
account.current_tenant_id = workspace_id account.current_tenant_id = workspace_id
else: else:
TenantAccountJoin.account_id == account.id).first() TenantAccountJoin.account_id == account.id).first()
if tenant_account_join: if tenant_account_join:
account.current_tenant_id = tenant_account_join.tenant_id account.current_tenant_id = tenant_account_join.tenant_id
session['workspace_id'] = account.current_tenant_id
else:
_create_tenant_for_account(account)
session['workspace_id'] = account.current_tenant_id


account.last_active_at = datetime.utcnow() account.last_active_at = datetime.utcnow()
db.session.commit() db.session.commit()

+ 1
- 1
api/controllers/console/auth/login.py Ver arquivo

try: try:
TenantService.switch_tenant(account) TenantService.switch_tenant(account)
except Exception: except Exception:
raise AccountNotLinkTenantError("Account not link tenant")
pass


flask_login.login_user(account, remember=args['remember_me']) flask_login.login_user(account, remember=args['remember_me'])
AccountService.update_last_login(account, request) AccountService.update_last_login(account, request)

Carregando…
Cancelar
Salvar