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.

user_service.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # Copyright 2019 The RAG Flow 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 peewee
  17. from werkzeug.security import generate_password_hash, check_password_hash
  18. from web_server.db import UserTenantRole
  19. from web_server.db.db_models import DB, UserTenant
  20. from web_server.db.db_models import User, Tenant
  21. from web_server.db.services.common_service import CommonService
  22. from web_server.utils import get_uuid, get_format_time
  23. from web_server.db.db_utils import StatusEnum
  24. class UserService(CommonService):
  25. model = User
  26. @classmethod
  27. @DB.connection_context()
  28. def filter_by_id(cls, user_id):
  29. try:
  30. user = cls.model.select().where(cls.model.id == user_id).get()
  31. return user
  32. except peewee.DoesNotExist:
  33. return None
  34. @classmethod
  35. @DB.connection_context()
  36. def query_user(cls, email, password):
  37. user = cls.model.select().where((cls.model.email == email),
  38. (cls.model.status == StatusEnum.VALID.value)).first()
  39. if user and check_password_hash(str(user.password), password):
  40. return user
  41. else:
  42. return None
  43. @classmethod
  44. @DB.connection_context()
  45. def save(cls, **kwargs):
  46. if "id" not in kwargs:
  47. kwargs["id"] = get_uuid()
  48. if "password" in kwargs:
  49. kwargs["password"] = generate_password_hash(str(kwargs["password"]))
  50. obj = cls.model(**kwargs).save(force_insert=True)
  51. return obj
  52. @classmethod
  53. @DB.connection_context()
  54. def delete_user(cls, user_ids, update_user_dict):
  55. with DB.atomic():
  56. cls.model.update({"status": 0}).where(cls.model.id.in_(user_ids)).execute()
  57. @classmethod
  58. @DB.connection_context()
  59. def update_user(cls, user_id, user_dict):
  60. date_time = get_format_time()
  61. with DB.atomic():
  62. if user_dict:
  63. user_dict["update_time"] = date_time
  64. cls.model.update(user_dict).where(cls.model.id == user_id).execute()
  65. class TenantService(CommonService):
  66. model = Tenant
  67. @classmethod
  68. @DB.connection_context()
  69. def get_by_user_id(cls, user_id):
  70. fields = [cls.model.id.alias("tenant_id"), cls.model.name, cls.model.llm_id, cls.model.embd_id, cls.model.asr_id, cls.model.img2txt_id, cls.model.parser_ids, UserTenant.role]
  71. return list(cls.model.select(*fields)\
  72. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id==user_id) & (UserTenant.status == StatusEnum.VALID.value)))\
  73. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  74. @classmethod
  75. @DB.connection_context()
  76. def get_joined_tenants_by_user_id(cls, user_id):
  77. fields = [cls.model.id.alias("tenant_id"), cls.model.name, cls.model.llm_id, cls.model.embd_id, cls.model.asr_id, cls.model.img2txt_id, UserTenant.role]
  78. return list(cls.model.select(*fields)\
  79. .join(UserTenant, on=((cls.model.id == UserTenant.tenant_id) & (UserTenant.user_id==user_id) & (UserTenant.status == StatusEnum.VALID.value) & (UserTenant.role==UserTenantRole.NORMAL.value)))\
  80. .where(cls.model.status == StatusEnum.VALID.value).dicts())
  81. class UserTenantService(CommonService):
  82. model = UserTenant
  83. @classmethod
  84. @DB.connection_context()
  85. def save(cls, **kwargs):
  86. if "id" not in kwargs:
  87. kwargs["id"] = get_uuid()
  88. obj = cls.model(**kwargs).save(force_insert=True)
  89. return obj