Commit ·
6b741bf
1
Parent(s): a70b412
feat: add api of tenant app (#2177)
Browse files### What problem does this PR solve?
add api of tenant app
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- api/apps/tenant_app.py +39 -0
- api/db/services/user_service.py +21 -0
api/apps/tenant_app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
from flask_login import current_user, login_required
|
| 17 |
+
|
| 18 |
+
from api.utils.api_utils import server_error_response
|
| 19 |
+
from api.db.services.user_service import TenantService, UserTenantService
|
| 20 |
+
from api.utils.api_utils import get_json_result
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@manager.route("/list", methods=["GET"])
|
| 24 |
+
@login_required
|
| 25 |
+
def tenant_list():
|
| 26 |
+
try:
|
| 27 |
+
tenants = TenantService.get_by_user_id(current_user.id)
|
| 28 |
+
return get_json_result(data=tenants)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return server_error_response(e)
|
| 31 |
+
|
| 32 |
+
@manager.route("/<tenant_id>/user/list", methods=["GET"])
|
| 33 |
+
@login_required
|
| 34 |
+
def user_list(tenant_id):
|
| 35 |
+
try:
|
| 36 |
+
users = UserTenantService.get_by_tenant_id(tenant_id)
|
| 37 |
+
return get_json_result(data=users)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return server_error_response(e)
|
api/db/services/user_service.py
CHANGED
|
@@ -137,3 +137,24 @@ class UserTenantService(CommonService):
|
|
| 137 |
kwargs["id"] = get_uuid()
|
| 138 |
obj = cls.model(**kwargs).save(force_insert=True)
|
| 139 |
return obj
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
kwargs["id"] = get_uuid()
|
| 138 |
obj = cls.model(**kwargs).save(force_insert=True)
|
| 139 |
return obj
|
| 140 |
+
|
| 141 |
+
@classmethod
|
| 142 |
+
@DB.connection_context()
|
| 143 |
+
def get_by_tenant_id(cls, tenant_id):
|
| 144 |
+
fields = [
|
| 145 |
+
cls.model.user_id,
|
| 146 |
+
cls.model.tenant_id,
|
| 147 |
+
cls.model.role,
|
| 148 |
+
cls.model.status,
|
| 149 |
+
User.nickname,
|
| 150 |
+
User.email,
|
| 151 |
+
User.avatar,
|
| 152 |
+
User.is_authenticated,
|
| 153 |
+
User.is_active,
|
| 154 |
+
User.is_anonymous,
|
| 155 |
+
User.status,
|
| 156 |
+
User.is_superuser]
|
| 157 |
+
return list(cls.model.select(*fields)
|
| 158 |
+
.join(User, on=((cls.model.user_id == User.id) & (cls.model.status == StatusEnum.VALID.value)))
|
| 159 |
+
.where(cls.model.tenant_id == tenant_id)
|
| 160 |
+
.dicts())
|