Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
25f28295
Commit
25f28295
authored
Feb 01, 2024
by
crazywoola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: switch account
parent
26d09857
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
12 deletions
+56
-12
login.py
api/controllers/console/auth/login.py
+4
-4
16830a790f0f_.py
api/migrations/versions/16830a790f0f_.py
+32
-0
account.py
api/models/account.py
+1
-0
account_service.py
api/services/account_service.py
+19
-8
No files found.
api/controllers/console/auth/login.py
View file @
25f28295
...
...
@@ -30,10 +30,10 @@ class LoginApi(Resource):
except
services
.
errors
.
account
.
AccountLoginError
:
return
{
'code'
:
'unauthorized'
,
'message'
:
'Invalid email or password'
},
401
try
:
TenantService
.
switch_tenant
(
account
)
except
Exception
:
pass
#
try:
#
TenantService.switch_tenant(account)
#
except Exception:
#
pass
AccountService
.
update_last_login
(
account
,
request
)
...
...
api/migrations/versions/16830a790f0f_.py
0 → 100644
View file @
25f28295
"""empty message
Revision ID: 16830a790f0f
Revises: 380c6aa5a70d
Create Date: 2024-02-01 08:21:31.111119
"""
from
alembic
import
op
import
sqlalchemy
as
sa
# revision identifiers, used by Alembic.
revision
=
'16830a790f0f'
down_revision
=
'380c6aa5a70d'
branch_labels
=
None
depends_on
=
None
def
upgrade
():
# ### commands auto generated by Alembic - please adjust! ###
with
op
.
batch_alter_table
(
'tenant_account_joins'
,
schema
=
None
)
as
batch_op
:
batch_op
.
add_column
(
sa
.
Column
(
'current'
,
sa
.
Boolean
(),
server_default
=
sa
.
text
(
'false'
),
nullable
=
False
))
# ### end Alembic commands ###
def
downgrade
():
# ### commands auto generated by Alembic - please adjust! ###
with
op
.
batch_alter_table
(
'tenant_account_joins'
,
schema
=
None
)
as
batch_op
:
batch_op
.
drop_column
(
'current'
)
# ### end Alembic commands ###
api/models/account.py
View file @
25f28295
...
...
@@ -155,6 +155,7 @@ class TenantAccountJoin(db.Model):
id
=
db
.
Column
(
UUID
,
server_default
=
db
.
text
(
'uuid_generate_v4()'
))
tenant_id
=
db
.
Column
(
UUID
,
nullable
=
False
)
account_id
=
db
.
Column
(
UUID
,
nullable
=
False
)
current
=
db
.
Column
(
db
.
Boolean
,
nullable
=
False
,
server_default
=
db
.
text
(
'false'
))
role
=
db
.
Column
(
db
.
String
(
16
),
nullable
=
False
,
server_default
=
'normal'
)
invited_by
=
db
.
Column
(
UUID
,
nullable
=
True
)
created_at
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
,
server_default
=
db
.
text
(
'CURRENT_TIMESTAMP(0)'
))
...
...
api/services/account_service.py
View file @
25f28295
...
...
@@ -45,11 +45,20 @@ class AccountService:
if
account
.
status
in
[
AccountStatus
.
BANNED
.
value
,
AccountStatus
.
CLOSED
.
value
]:
raise
Forbidden
(
'Account is banned or closed.'
)
tenant_account_join
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
)
.
first
()
if
not
tenant_account_join
:
# init owner's tenant
tenant_owner
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
,
role
=
'owner'
)
.
first
()
if
not
tenant_owner
:
_create_tenant_for_account
(
account
)
# Update last_active_at if more than 10 minutes have passed
current_tenant
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
,
current
=
True
)
.
first
()
if
current_tenant
:
account
.
current_tenant_id
=
current_tenant
.
tenant_id
else
:
account
.
current_tenant_id
=
tenant_owner
.
tenant_id
tenant_owner
.
current
=
True
db
.
session
.
commit
()
if
datetime
.
utcnow
()
-
account
.
last_active_at
>
timedelta
(
minutes
=
10
):
account
.
last_active_at
=
datetime
.
utcnow
()
db
.
session
.
commit
()
...
...
@@ -247,14 +256,16 @@ class TenantService:
@
staticmethod
def
switch_tenant
(
account
:
Account
,
tenant_id
:
int
=
None
)
->
None
:
"""Switch the current workspace for the account"""
if
not
tenant_id
:
tenant_account_join
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
)
.
first
()
else
:
tenant_account_join
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
,
tenant_id
=
tenant_id
)
.
first
()
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
)
.
update
({
'current'
:
False
})
tenant_account_join
=
TenantAccountJoin
.
query
.
filter_by
(
account_id
=
account
.
id
,
tenant_id
=
tenant_id
)
.
first
()
# Check if the tenant exists and the account is a member of the tenant
if
not
tenant_account_join
:
raise
AccountNotLinkTenantError
(
"Tenant not found or account is not a member of the tenant."
)
else
:
tenant_account_join
.
current
=
True
db
.
session
.
commit
()
# Set the current tenant for the account
account
.
current_tenant_id
=
tenant_account_join
.
tenant_id
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment