You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tenant_app.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 import request
  17. from flask_login import current_user, login_required
  18. from api.db import UserTenantRole, StatusEnum
  19. from api.db.db_models import UserTenant
  20. from api.db.services.user_service import TenantService, UserTenantService
  21. from api.settings import RetCode
  22. from api.utils import get_uuid
  23. from api.utils.api_utils import get_json_result, validate_request, server_error_response
  24. @manager.route("/list", methods=["GET"])
  25. @login_required
  26. def tenant_list():
  27. try:
  28. tenants = TenantService.get_by_user_id(current_user.id)
  29. return get_json_result(data=tenants)
  30. except Exception as e:
  31. return server_error_response(e)
  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)
  40. @manager.route('/<tenant_id>/user', methods=['POST'])
  41. @login_required
  42. @validate_request("user_id")
  43. def create(tenant_id):
  44. user_id = request.json.get("user_id")
  45. if not user_id:
  46. return get_json_result(
  47. data=False, retmsg='Lack of "USER ID"', retcode=RetCode.ARGUMENT_ERROR)
  48. try:
  49. user_tenants = UserTenantService.query(user_id=user_id, tenant_id=tenant_id)
  50. if user_tenants:
  51. uuid = user_tenants[0].id
  52. return get_json_result(data={"id": uuid})
  53. uuid = get_uuid()
  54. UserTenantService.save(
  55. id = uuid,
  56. user_id = user_id,
  57. tenant_id = tenant_id,
  58. role = UserTenantRole.NORMAL.value,
  59. status = StatusEnum.VALID.value)
  60. return get_json_result(data={"id": uuid})
  61. except Exception as e:
  62. return server_error_response(e)
  63. @manager.route('/<tenant_id>/user/<user_id>', methods=['DELETE'])
  64. @login_required
  65. def rm(tenant_id, user_id):
  66. try:
  67. UserTenantService.filter_delete([UserTenant.tenant_id == tenant_id, UserTenant.user_id == user_id])
  68. return get_json_result(data=True)
  69. except Exception as e:
  70. return server_error_response(e)