Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

user_service.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. import hashlib
  17. from datetime import datetime
  18. import peewee
  19. from werkzeug.security import generate_password_hash, check_password_hash
  20. from api.db import UserTenantRole
  21. from api.db.db_models import DB, UserTenant
  22. from api.db.db_models import User, Tenant
  23. from api.db.services.common_service import CommonService
  24. from api.utils import get_uuid, current_timestamp, datetime_format
  25. from api.db import StatusEnum
  26. from rag.settings import MINIO
  27. class UserService(CommonService):
  28. model = User
  29. @classmethod
  30. @DB.connection_context()
  31. def filter_by_id(cls, user_id):
  32. try:
  33. user = cls.model.select().where(cls.model.id == user_id).get()
  34. return user
  35. except peewee.DoesNotExist:
  36. return None
  37. @classmethod
  38. @DB.connection_context()
  39. def query_user(cls, email, password):
  40. user = cls.model.select().where((cls.model.email == email),
  41. (cls.model.status == StatusEnum.VALID.value)).first()
  42. if user and check_password_hash(str(user.password), password):
  43. return user
  44. else:
  45. return None
  46. @classmethod
  47. @DB.connection_context()
  48. def save(cls, **kwargs):
  49. if "id" not in kwargs:
  50. kwargs["id"] = get_uuid()
  51. if "password" in kwargs:
  52. kwargs["password"] = generate_password_hash(
  53. str(kwargs["password"]))
  54. kwargs["create_time"] = current_timestamp()
  55. kwargs["create_date"] = datetime_format(datetime.now())
  56. kwargs["update_time"] = current_timestamp()
  57. kwargs["update_date"] = datetime_format(datetime.now())
  58. obj = cls.model(**kwargs).save(force_insert=True)
  59. return obj
  60. @classmethod
  61. @DB.connection_context()
  62. def delete_user(cls, user_ids, update_user_dict):
  63. with DB.atomic():
  64. cls.model.update({"status": 0}).where(
  65. cls.model.id.in_(user_ids)).execute()
  66. @classmethod
  67. @DB.connection_context()
  68. def update_user(cls, user_id, user_dict):
  69. with DB.atomic():
  70. if user_dict:
  71. user_dict["update_time"] = current_timestamp()
  72. user_dict["update_date"] = datetime_format(datetime.now())
  73. cls.model.update(user_dict).where(
  74. cls.model.id == user_id).execute()
  75. class TenantService(CommonService):
  76. model = Tenant
  77. @classmethod
  78. @DB.connection_context()
  79. def get_info_by(cls, user_id):
  80. fields = [
  81. cls.model.id.alias("tenant_id"),
  82. cls.model.name,
  83. cls.model.llm_id,
  84. cls.model.embd_id,
  85. cls.model.rerank_id,
  86. cls.model.asr_id,
  87. cls.model.img2txt_id,
  88. cls.model.tts_id,
  89. cls.model.parser_ids,
  90. UserTenant.role]
  91. return list(cls.model.select(*fields)
  92. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role == UserTenantRole.OWNER)))
  93. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  94. @classmethod
  95. @DB.connection_context()
  96. def get_joined_tenants_by_user_id(cls, user_id):
  97. fields = [
  98. cls.model.id.alias("tenant_id"),
  99. cls.model.name,
  100. cls.model.llm_id,
  101. cls.model.embd_id,
  102. cls.model.asr_id,
  103. cls.model.img2txt_id,
  104. UserTenant.role]
  105. return list(cls.model.select(*fields)
  106. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role == UserTenantRole.NORMAL)))
  107. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  108. @classmethod
  109. @DB.connection_context()
  110. def decrease(cls, user_id, num):
  111. num = cls.model.update(credit=cls.model.credit - num).where(
  112. cls.model.id == user_id).execute()
  113. if num == 0:
  114. raise LookupError("Tenant not found which is supposed to be there")
  115. @classmethod
  116. @DB.connection_context()
  117. def user_gateway(cls, tenant_id):
  118. hashobj = hashlib.sha256(tenant_id.encode("utf-8"))
  119. return int(hashobj.hexdigest(), 16)%len(MINIO)
  120. class UserTenantService(CommonService):
  121. model = UserTenant
  122. @classmethod
  123. @DB.connection_context()
  124. def save(cls, **kwargs):
  125. if "id" not in kwargs:
  126. kwargs["id"] = get_uuid()
  127. obj = cls.model(**kwargs).save(force_insert=True)
  128. return obj
  129. @classmethod
  130. @DB.connection_context()
  131. def get_by_tenant_id(cls, tenant_id):
  132. fields = [
  133. cls.model.user_id,
  134. cls.model.status,
  135. cls.model.role,
  136. User.nickname,
  137. User.email,
  138. User.avatar,
  139. User.is_authenticated,
  140. User.is_active,
  141. User.is_anonymous,
  142. User.status,
  143. User.update_date,
  144. User.is_superuser]
  145. return list(cls.model.select(*fields)
  146. .join(User, on=((cls.model.user_id == User.id) & (cls.model.status == StatusEnum.VALID.value) & (cls.model.role != UserTenantRole.OWNER)))
  147. .where(cls.model.tenant_id == tenant_id)
  148. .dicts())
  149. @classmethod
  150. @DB.connection_context()
  151. def get_tenants_by_user_id(cls, user_id):
  152. fields = [
  153. cls.model.tenant_id,
  154. cls.model.role,
  155. User.nickname,
  156. User.email,
  157. User.avatar,
  158. User.update_date
  159. ]
  160. return list(cls.model.select(*fields)
  161. .join(User, on=((cls.model.tenant_id == User.id) & (UserTenant.user_id == user_id) & (UserTenant.status == StatusEnum.VALID.value)))
  162. .where(cls.model.status == StatusEnum.VALID.value).dicts())