Commit e3408156 authored by crazywoola's avatar crazywoola

feat: remove some session['workspace_id']

parent d8672796
...@@ -47,7 +47,6 @@ class LogoutApi(Resource): ...@@ -47,7 +47,6 @@ class LogoutApi(Resource):
@setup_required @setup_required
def get(self): def get(self):
flask.session.pop('workspace_id', None)
flask_login.logout_user() flask_login.logout_user()
return {'result': 'success'} return {'result': 'success'}
......
...@@ -39,54 +39,26 @@ class AccountService: ...@@ -39,54 +39,26 @@ class AccountService:
@staticmethod @staticmethod
def load_user(user_id: str) -> Account: def load_user(user_id: str) -> Account:
# todo: used by flask_login account = Account.query.filter_by(id=user_id).first()
if '.' in user_id: if not account:
tenant_id, account_id = user_id.split('.') return None
else:
account_id = user_id
account = db.session.query(Account).filter(Account.id == account_id).first()
if account:
if account.status == AccountStatus.BANNED.value or account.status == AccountStatus.CLOSED.value:
raise Forbidden('Account is banned or closed.')
workspace_id = session.get('workspace_id')
if workspace_id:
tenant_account_join = db.session.query(TenantAccountJoin).filter(
TenantAccountJoin.account_id == account.id,
TenantAccountJoin.tenant_id == workspace_id
).first()
if not tenant_account_join:
tenant_account_join = db.session.query(TenantAccountJoin).filter(
TenantAccountJoin.account_id == account.id).first()
if tenant_account_join:
account.current_tenant_id = tenant_account_join.tenant_id
else:
_create_tenant_for_account(account)
session['workspace_id'] = account.current_tenant_id
else:
account.current_tenant_id = workspace_id
else:
tenant_account_join = db.session.query(TenantAccountJoin).filter(
TenantAccountJoin.account_id == account.id).first()
if tenant_account_join:
account.current_tenant_id = tenant_account_join.tenant_id
else:
_create_tenant_for_account(account)
session['workspace_id'] = account.current_tenant_id
current_time = datetime.utcnow() if account.status in [AccountStatus.BANNED.value, AccountStatus.CLOSED.value]:
raise Forbidden('Account is banned or closed.')
# update last_active_at when last_active_at is more than 10 minutes ago tenant_account_join = TenantAccountJoin.query.filter_by(account_id=account.id).first()
if current_time - account.last_active_at > timedelta(minutes=10): if not tenant_account_join:
account.last_active_at = current_time _create_tenant_for_account(account)
db.session.commit() else:
account.current_tenant_id = tenant_account_join.tenant_id
# Update last_active_at if more than 10 minutes have passed
if datetime.utcnow() - account.last_active_at > timedelta(minutes=10):
account.last_active_at = datetime.utcnow()
db.session.commit()
return account return account
@staticmethod @staticmethod
def get_account_jwt_token(account): def get_account_jwt_token(account):
payload = { payload = {
...@@ -288,7 +260,6 @@ class TenantService: ...@@ -288,7 +260,6 @@ class TenantService:
# Set the current tenant for the account # Set the current tenant for the account
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
@staticmethod @staticmethod
def get_tenant_members(tenant: Tenant) -> List[Account]: def get_tenant_members(tenant: Tenant) -> List[Account]:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment