sharedkernel 2.11.3__tar.gz → 2.12.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/PKG-INFO +3 -1
  2. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/README.md +2 -0
  3. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/setup.py +1 -1
  4. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/config.py +8 -1
  5. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/distributed_cache.py +4 -4
  6. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/__init__.py +2 -1
  7. sharedkernel-2.12.0/sharedkernel/enum/permission.py +91 -0
  8. sharedkernel-2.12.0/sharedkernel/objects/user_permissions.py +5 -0
  9. sharedkernel-2.12.0/sharedkernel/toolbox_service.py +148 -0
  10. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel.egg-info/PKG-INFO +3 -1
  11. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel.egg-info/SOURCES.txt +2 -0
  12. sharedkernel-2.11.3/sharedkernel/toolbox_service.py +0 -86
  13. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/setup.cfg +0 -0
  14. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/chunker/chunk_rule.py +0 -0
  15. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/chunker/text_chunker.py +0 -0
  16. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/common.py +0 -0
  17. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/data_format_converter.py +0 -0
  18. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/__init__.py +0 -0
  19. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/audit_model.py +0 -0
  20. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/cache/__init__.py +0 -0
  21. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/cache/cache_repository.py +0 -0
  22. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/cache/redis_generic_cache.py +0 -0
  23. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/mongo_generic_audit_repository.py +0 -0
  24. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/mongo_generic_repository.py +0 -0
  25. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/mongo_health_checker.py +0 -0
  26. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/database/pagination_response_dto.py +0 -0
  27. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/date_converter.py +0 -0
  28. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/diff_utils.py +0 -0
  29. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/encryption_service.py +0 -0
  30. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/error_code.py +0 -0
  31. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/feature_display_type.py +0 -0
  32. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/feature_key.py +0 -0
  33. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/order_type.py +0 -0
  34. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/plan_interval.py +0 -0
  35. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/plan_type.py +0 -0
  36. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/redis_mode_enum.py +0 -0
  37. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/sort_order.py +0 -0
  38. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/enum/user_role.py +0 -0
  39. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/exception/__init__.py +0 -0
  40. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/exception/exception.py +0 -0
  41. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/exception/exception_handlers.py +0 -0
  42. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/file_validation.py +0 -0
  43. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/ip_session_service.py +0 -0
  44. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/jwt_service.py +0 -0
  45. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/log_decorator.py +0 -0
  46. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/log_dto.py +0 -0
  47. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/log_enums.py +0 -0
  48. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/log_info.py +0 -0
  49. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/log_middlewares.py +0 -0
  50. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/logger/logger_service.py +0 -0
  51. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/multipart_upload.py +0 -0
  52. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/normalizer/__init__.py +0 -0
  53. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/normalizer/number_normalizer.py +0 -0
  54. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/normalizer/phone_number_normalizer.py +0 -0
  55. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/normalizer/string_normalizer.py +0 -0
  56. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/__init__.py +0 -0
  57. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/base_document.py +0 -0
  58. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/json_string_model.py +0 -0
  59. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/jwt_model.py +0 -0
  60. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/result.py +0 -0
  61. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/subscription.py +0 -0
  62. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/user_info.py +0 -0
  63. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/objects/user_workspace.py +0 -0
  64. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/regex_masking.py +0 -0
  65. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/s3_uploader.py +0 -0
  66. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel/string_extentions.py +0 -0
  67. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel.egg-info/dependency_links.txt +0 -0
  68. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel.egg-info/requires.txt +0 -0
  69. {sharedkernel-2.11.3 → sharedkernel-2.12.0}/sharedkernel.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sharedkernel
3
- Version: 2.11.3
3
+ Version: 2.12.0
4
4
  Summary: sharekernel is a shared package between all python projects
5
5
  Author: Smilinno
6
6
  Description-Content-Type: text/markdown
@@ -33,6 +33,8 @@ Dynamic: summary
33
33
  this is a shared kernel package
34
34
 
35
35
  # Change Log
36
+ ### Version 2.12.0
37
+ - implement user access level version and check permission
36
38
  ### Version 2.11.3
37
39
  - Add S3Uploader.download, which reads an object from S3 using its URL
38
40
  ### Version 2.11.2
@@ -2,6 +2,8 @@
2
2
  this is a shared kernel package
3
3
 
4
4
  # Change Log
5
+ ### Version 2.12.0
6
+ - implement user access level version and check permission
5
7
  ### Version 2.11.3
6
8
  - Add S3Uploader.download, which reads an object from S3 using its URL
7
9
  ### Version 2.11.2
@@ -44,7 +44,7 @@ setup(
44
44
  "cryptography",
45
45
  ],
46
46
  # *strongly* suggested for sharing
47
- version="2.11.3",
47
+ version="2.12.0",
48
48
  description="sharekernel is a shared package between all python projects",
49
49
  long_description=long_description,
50
50
  long_description_content_type="text/markdown",
@@ -34,4 +34,11 @@ CACHE_EXPIRE_TIME_HOURS = int(os.getenv("CACHE_EXPIRE_TIME_HOURS", 24))
34
34
  REDIS_SENTINELS = os.getenv("REDIS_SENTINELS")
35
35
  REDIS_MASTER_NAME = os.getenv("REDIS_MASTER_NAME")
36
36
  REDIS_CLUSTER_NODES = os.getenv("REDIS_CLUSTER_NODES")
37
- REDIS_MODE = os.getenv("REDIS_MODE") # sentinel / cluster / standalone
37
+ REDIS_MODE = os.getenv("REDIS_MODE") # sentinel / cluster / standalone
38
+
39
+ PRODUCTS_BASE_URL = os.getenv(
40
+ "PRODUCTS_BASE_URL",
41
+ "https://gateway-dev.hamhoush.ir/products/v1",
42
+ )
43
+ GET_PERMISSIONS_BASE_URL = os.getenv("GET_PERMISSIONS_BASE_URL", PRODUCTS_BASE_URL + "/internal/user-permissions")
44
+ USER_PERMISSIONS_CACHE_NAMESPACE = "user_permissions"
@@ -101,9 +101,9 @@ class DistributedCache(Generic[T]):
101
101
  def _deserialize(self, raw: bytes) -> T:
102
102
  return self.model.model_validate_json(raw.decode())
103
103
 
104
- def set(self, key: str, value: T) -> None:
104
+ def set(self, key: str, value: T) -> bool:
105
105
  if value is None:
106
- return
106
+ return False
107
107
  skey = self._pref(key)
108
108
  payload = self._serialize(value)
109
109
  self.client.set(skey, payload, ex=self.expire_time)
@@ -125,8 +125,8 @@ class DistributedCache(Generic[T]):
125
125
  skey = self._pref(key)
126
126
  return self.client.exists(skey)
127
127
 
128
- def delete(self, key):
129
- self.client.delete(key)
128
+ def delete(self, key: str) -> None:
129
+ self.client.delete(self._pref(key))
130
130
 
131
131
  def delete_group(self, key_prefix: str):
132
132
  pattern = f"{self.namespace}:{key_prefix}*"
@@ -4,4 +4,5 @@ from .feature_key import FeatureKey
4
4
  from .plan_type import PlanType
5
5
  from .plan_interval import PlanInterval
6
6
  from .order_type import OrderType
7
- from .feature_display_type import FeatureDisplayType
7
+ from .feature_display_type import FeatureDisplayType
8
+ from .permission import PermissionsEnum
@@ -0,0 +1,91 @@
1
+ from enum import Enum
2
+
3
+
4
+ class PermissionsEnum(str, Enum):
5
+ WORKSPACE_USAGE_VIEW = "workspace.usage.view"
6
+ WORKSPACE_SETTINGS_VIEW = "workspace.settings.view"
7
+ WORKSPACE_SETTINGS_EDIT = "workspace.settings.edit"
8
+ WORKSPACE_SETTINGS_DELETE = "workspace.settings.delete"
9
+ WORKSPACE_MEMBERS_VIEW = "workspace.members.view"
10
+ WORKSPACE_MEMBERS_INVITE = "workspace.members.invite"
11
+ WORKSPACE_MEMBERS_EDIT = "workspace.members.edit"
12
+ WORKSPACE_MEMBERS_SUSPEND = "workspace.members.suspend"
13
+ WORKSPACE_MEMBERS_REMOVE = "workspace.members.remove"
14
+ WORKSPACE_MEMBERS_ACTIVATE = "workspace.members.activate"
15
+ WORKSPACE_ROLES_VIEW = "workspace.roles.view"
16
+ WORKSPACE_ROLES_CREATE = "workspace.roles.create"
17
+ WORKSPACE_ROLES_EDIT = "workspace.roles.edit"
18
+ WORKSPACE_ROLES_DELETE = "workspace.roles.delete"
19
+ WORKSPACE_ROLES_CLONE = "workspace.roles.clone"
20
+ WORKSPACE_AUDIT_VIEW = "workspace.audit.view"
21
+ WORKSPACE_AUDIT_EXPORT = "workspace.audit.export"
22
+ PAYMENT_PLANS_VIEW = "payment.plans.view"
23
+ PAYMENT_PLANS_EDIT = "payment.plans.edit"
24
+ PAYMENT_PLANS_PURCHASE = "payment.plans.purchase"
25
+ PAYMENT_ADDON_PURCHASE = "payment.addon.purchase"
26
+ PAYMENT_HISTORY_VIEW = "payment.history.view"
27
+ FAQBOT_ANALYTICS_VIEW = "faqbot.analytics.view"
28
+ FAQBOT_ANALYTICS_EXPORT = "faqbot.analytics.export"
29
+ FAQBOT_CONVERSATION_VIEW = "faqbot.conversation.view"
30
+ FAQBOT_CONVERSATION_EXPORT = "faqbot.conversation.export"
31
+ FAQBOT_KNOWLEDGE_VIEW = "faqbot.knowledge.view"
32
+ FAQBOT_KNOWLEDGE_EDIT = "faqbot.knowledge.edit"
33
+ FAQBOT_BOT_DELETE = "faqbot.bot.delete"
34
+ FAQBOT_INTEGRATION_VIEW = "faqbot.integration.view"
35
+ FAQBOT_INTEGRATION_API_KEY_EDIT = "faqbot.integration.api_key.edit"
36
+ FAQBOT_APPEARANCE_VIEW = "faqbot.appearance.view"
37
+ FAQBOT_APPEARANCE_EDIT = "faqbot.appearance.edit"
38
+ FAQBOT_PROSETTINGS_VIEW = "faqbot.prosettings.view"
39
+ FAQBOT_PROSETTINGS_ASR_EDIT = "faqbot.prosettings.asr.edit"
40
+ FAQBOT_PROSETTINGS_TTS_EDIT = "faqbot.prosettings.tts.edit"
41
+ FAQBOT_PROSETTINGS_TONE_EDIT = "faqbot.prosettings.tone.edit"
42
+ FAQBOT_PROSETTINGS_SUGGESTIONS_EDIT = "faqbot.prosettings.suggestions.edit"
43
+ FAQBOT_PROSETTINGS_KNOWLEDGE_ONLY_EDIT = "faqbot.prosettings.knowledge_only.edit"
44
+ FAQBOT_PROSETTINGS_SOURCES_EDIT = "faqbot.prosettings.sources.edit"
45
+ FAQBOT_PROSETTINGS_LEAD_CAPTURE_EDIT = "faqbot.prosettings.lead_capture.edit"
46
+ FAQBOT_PROSETTINGS_OPERATOR_EDIT = "faqbot.prosettings.operator.edit"
47
+ FAQBOT_PROSETTINGS_USAGE_LIMIT_EDIT = "faqbot.prosettings.usage_limit.edit"
48
+ FAQBOT_PROSETTINGS_DOMAIN_EDIT = "faqbot.prosettings.domain.edit"
49
+ SENTIMENT_CREATE = "sentiment.create"
50
+ SENTIMENT_DELETE = "sentiment.delete"
51
+ SENTIMENT_TOPIC_VIEW = "sentiment.topic.view"
52
+ SENTIMENT_SUMMARY_VIEW = "sentiment.summary.view"
53
+ SENTIMENT_RECOMMENDATION_VIEW = "sentiment.recommendation.view"
54
+ SENTIMENT_RECOMMENDATION_REVIEW = "sentiment.recommendation.review"
55
+ SENTIMENT_SOURCE_CONTENT_VIEW = "sentiment.source.content.view"
56
+ SENTIMENT_SETTINGS_VIEW = "sentiment.settings.view"
57
+ SENTIMENT_EDIT = "sentiment.edit"
58
+ SENTIMENT_SOURCE_MAPPING_VIEW = "sentiment.source.mapping.view"
59
+ SENTIMENT_SOURCE_MAPPING_EXPORT = "sentiment.source.mapping.export"
60
+ CALLCENTER_ANALYTICS_VIEW = "callcenter.analytics.view"
61
+ CALLCENTER_HISTORY_VIEW = "callcenter.history.view"
62
+ CALLCENTER_TRANSCRIPT_VIEW = "callcenter.transcript.view"
63
+ CALLCENTER_CONTACT_VIEW = "callcenter.contact.view"
64
+ CALLCENTER_RECORDING_LISTEN = "callcenter.recording.listen"
65
+ CALLCENTER_RECORDING_DOWNLOAD = "callcenter.recording.download"
66
+ CALLCENTER_CALL_EVENTS_VIEW = "callcenter.call.events.view"
67
+ CALLCENTER_SCENARIO_VIEW = "callcenter.scenario.view"
68
+ CALLCENTER_SCENARIO_MESSAGES_EDIT = "callcenter.scenario.messages.edit"
69
+ CALLCENTER_SCENARIO_FLOW_EDIT = "callcenter.scenario.flow.edit"
70
+ CALLCENTER_SCENARIO_DTMF_EDIT = "callcenter.scenario.dtmf.edit"
71
+ CALLCENTER_VOICE_SETTINGS_VIEW = "callcenter.voice.settings.view"
72
+ CALLCENTER_VOICE_TTS_EDIT = "callcenter.voice.tts.edit"
73
+ CALLCENTER_VOICE_VAD_EDIT = "callcenter.voice.vad.edit"
74
+ CALLCENTER_VOICE_BARGE_IN_EDIT = "callcenter.voice.barge_in.edit"
75
+ ASR_AUDIO_UPLOAD = "asr.audio.upload"
76
+ ASR_RUN = "asr.run"
77
+ ASR_HISTORY_VIEW = "asr.history.view"
78
+ ASR_RESULT_VIEW = "asr.result.view"
79
+ ASR_AUDIO_LISTEN = "asr.audio.listen"
80
+ ASR_RESULT_EXPORT = "asr.result.export"
81
+ ASR_RESULT_RENAME = "asr.result.rename"
82
+ ASR_RESULT_DELETE = "asr.result.delete"
83
+ ASR_SETTINGS_EDIT = "asr.settings.edit"
84
+ TTS_RUN = "tts.run"
85
+ TTS_HISTORY_VIEW = "tts.history.view"
86
+ TTS_RESULT_VIEW = "tts.result.view"
87
+ TTS_RESULT_LISTEN = "tts.result.listen"
88
+ TTS_RESULT_DOWNLOAD = "tts.result.download"
89
+ TTS_RESULT_RENAME = "tts.result.rename"
90
+ TTS_RESULT_DELETE = "tts.result.delete"
91
+ TTS_SETTINGS_EDIT = "tts.settings.edit"
@@ -0,0 +1,5 @@
1
+ from pydantic import BaseModel
2
+
3
+
4
+ class UserPermissions(BaseModel):
5
+ permission_codes: list[str]
@@ -0,0 +1,148 @@
1
+ from functools import wraps
2
+ from typing import Type
3
+
4
+ import aiohttp
5
+ from fastapi import Request
6
+ from pydantic import BaseModel
7
+
8
+ from sharedkernel import config
9
+ from sharedkernel.database.cache.cache_repository import CacheRepository
10
+ from sharedkernel.database.distributed_cache import DistributedCache
11
+ from sharedkernel.enum import ErrorCode, FeatureKey, PlanType
12
+ from sharedkernel.enum.permission import PermissionsEnum
13
+ from sharedkernel.exception.exception import BusinessException
14
+ from sharedkernel.objects.result import Result
15
+ from sharedkernel.objects.subscription import Subscription
16
+ from sharedkernel.objects.user_info import current_user_info
17
+ from sharedkernel.objects.user_permissions import UserPermissions
18
+
19
+
20
+ class ToolboxService:
21
+ __permission_cache: DistributedCache[UserPermissions] | None = None
22
+
23
+ def __init__(self, cache: CacheRepository | None = None):
24
+ self.cache = cache
25
+
26
+ async def send_request(
27
+ self,
28
+ method: str,
29
+ endpoint: str,
30
+ payload: dict | None = None,
31
+ params: dict | None = None,
32
+ headers: dict | None = None,
33
+ model: Type[BaseModel] | None = None,
34
+ ) -> Result:
35
+ async with aiohttp.ClientSession() as session:
36
+ async with session.request(
37
+ method=method.upper(),
38
+ url=endpoint,
39
+ json=payload,
40
+ params=params,
41
+ headers=headers,
42
+ ) as response:
43
+ if not response.ok:
44
+ print(f"API Request to {endpoint} Failed: {response.status} {response.text}")
45
+ raise BusinessException(error_code=ErrorCode.Internal_Server)
46
+
47
+ response_data = await response.json()
48
+
49
+ return Result(**response_data)
50
+
51
+ async def check_permissions(
52
+ self,
53
+ required_permission_codes: list[PermissionsEnum],
54
+ workspace_id: str,
55
+ *,
56
+ match_any: bool = False,
57
+ ) -> None:
58
+ if not required_permission_codes:
59
+ raise BusinessException(error_code=ErrorCode.User_Access_Denied)
60
+
61
+ if self.__permission_cache is None:
62
+ self.__permission_cache = DistributedCache(
63
+ namespace=config.USER_PERMISSIONS_CACHE_NAMESPACE,
64
+ model=UserPermissions,
65
+ )
66
+
67
+ user_id = current_user_info.get().nameid
68
+ cache_key = f"{workspace_id}:{user_id}"
69
+ permissions = self.__permission_cache.get(cache_key)
70
+ if permissions is None:
71
+ result = await self.send_request(
72
+ method="GET",
73
+ endpoint=config.GET_PERMISSIONS_BASE_URL,
74
+ params={"user_id": user_id},
75
+ headers={"x-workspace-id": workspace_id},
76
+ )
77
+ if not result.isSucceed or result.data is None:
78
+ error_code = (
79
+ ErrorCode.Workspace_Access_Denied
80
+ if result.errorCode == ErrorCode.Workspace_Access_Denied.name
81
+ else ErrorCode.User_Access_Denied
82
+ )
83
+ raise BusinessException(error_code=error_code)
84
+
85
+ permissions = UserPermissions.model_validate(result.data)
86
+ self.__permission_cache.set(cache_key, permissions)
87
+
88
+ granted_permission_codes = set(permissions.permission_codes)
89
+ required_codes = {code.value for code in required_permission_codes}
90
+ allowed = (
91
+ not granted_permission_codes.isdisjoint(required_codes)
92
+ if match_any
93
+ else required_codes.issubset(granted_permission_codes)
94
+ )
95
+ if not allowed:
96
+ raise BusinessException(error_code=ErrorCode.User_Access_Denied)
97
+
98
+ def check_user_subscription(
99
+ self,
100
+ user_subscription: Subscription,
101
+ feature: FeatureKey,
102
+ quantity: int = 1,
103
+ ):
104
+ feature_errors = {
105
+ FeatureKey.MESSAGES: ErrorCode.Subscription_Insufficient_Message_Feature,
106
+ FeatureKey.BOTS: ErrorCode.Subscription_Insufficient_Bot_Feature,
107
+ FeatureKey.URLS: ErrorCode.Subscription_Insufficient_URL_FEATURE,
108
+ FeatureKey.TEXTS: ErrorCode.Subscription_Insufficient_TEXT_FEATURE,
109
+ FeatureKey.FILES: ErrorCode.Subscription_Insufficient_FILE_FEATURE,
110
+ FeatureKey.OCR: ErrorCode.Subscription_Insufficient_OCR_FEATURE,
111
+ FeatureKey.TTS: ErrorCode.Subscription_Insufficient_TTS_FEATURE,
112
+ FeatureKey.ASR: ErrorCode.Subscription_Insufficient_ASR_FEATURE,
113
+ }
114
+
115
+ if not user_subscription:
116
+ raise BusinessException(error_code=ErrorCode.Subscription_Not_Found)
117
+
118
+ if user_subscription.plan_type == PlanType.UNLIMITED:
119
+ return
120
+
121
+ for _feature in user_subscription.features:
122
+ if _feature.feature_key == feature:
123
+ if _feature.remaining - quantity < 0:
124
+ raise BusinessException(error_code=feature_errors[feature])
125
+ break
126
+
127
+
128
+ _permission_service = ToolboxService()
129
+
130
+
131
+ def check_permission(
132
+ required_permission_codes: list[PermissionsEnum],
133
+ *,
134
+ match_any: bool = False,
135
+ ):
136
+ def decorator(func):
137
+ @wraps(func)
138
+ async def wrapper(*args, request: Request, **kwargs):
139
+ await _permission_service.check_permissions(
140
+ required_permission_codes=list(required_permission_codes),
141
+ workspace_id=request.state.workspace_id,
142
+ match_any=match_any,
143
+ )
144
+ return await func(*args, request=request, **kwargs)
145
+
146
+ return wrapper
147
+
148
+ return decorator
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sharedkernel
3
- Version: 2.11.3
3
+ Version: 2.12.0
4
4
  Summary: sharekernel is a shared package between all python projects
5
5
  Author: Smilinno
6
6
  Description-Content-Type: text/markdown
@@ -33,6 +33,8 @@ Dynamic: summary
33
33
  this is a shared kernel package
34
34
 
35
35
  # Change Log
36
+ ### Version 2.12.0
37
+ - implement user access level version and check permission
36
38
  ### Version 2.11.3
37
39
  - Add S3Uploader.download, which reads an object from S3 using its URL
38
40
  ### Version 2.11.2
@@ -36,6 +36,7 @@ sharedkernel/enum/error_code.py
36
36
  sharedkernel/enum/feature_display_type.py
37
37
  sharedkernel/enum/feature_key.py
38
38
  sharedkernel/enum/order_type.py
39
+ sharedkernel/enum/permission.py
39
40
  sharedkernel/enum/plan_interval.py
40
41
  sharedkernel/enum/plan_type.py
41
42
  sharedkernel/enum/redis_mode_enum.py
@@ -61,4 +62,5 @@ sharedkernel/objects/jwt_model.py
61
62
  sharedkernel/objects/result.py
62
63
  sharedkernel/objects/subscription.py
63
64
  sharedkernel/objects/user_info.py
65
+ sharedkernel/objects/user_permissions.py
64
66
  sharedkernel/objects/user_workspace.py
@@ -1,86 +0,0 @@
1
- from typing import Type
2
- from pydantic import BaseModel
3
- import aiohttp
4
- from typing import List
5
-
6
- from sharedkernel.objects.result import Result
7
- from sharedkernel.enum import FeatureKey, PlanType
8
- from sharedkernel.enum import ErrorCode
9
- from sharedkernel.objects.subscription import Subscription
10
- from sharedkernel.objects.user_workspace import UserWorkspace
11
- from sharedkernel.exception.exception import BusinessException
12
- from sharedkernel.database.cache.cache_repository import CacheRepository
13
- from sharedkernel.enum.user_role import Role
14
- from sharedkernel.objects.user_info import current_user_info
15
-
16
-
17
- class ToolboxService:
18
- def __init__(self, cache: CacheRepository | None = None):
19
- self.cache = cache
20
-
21
- async def send_request(
22
- self,
23
- method: str,
24
- endpoint: str,
25
- payload: dict = None,
26
- params: dict | None = None,
27
- headers: dict | None = None,
28
- model: Type[BaseModel] | None = None,
29
- ) -> Result:
30
-
31
- async with aiohttp.ClientSession() as session:
32
- async with session.request(
33
- method=method.upper(),
34
- url=endpoint,
35
- json=payload,
36
- params=params,
37
- headers=headers,
38
- ) as response:
39
-
40
- if not response.ok:
41
- print(f"API Request to {endpoint} Failed: {response.status} {response.text}")
42
- raise BusinessException(error_code=ErrorCode.Internal_Server)
43
-
44
- response_data = await response.json()
45
- return Result(
46
- **response_data,
47
- )
48
-
49
-
50
-
51
- def check_user_subscription(
52
- self,
53
- user_subscription: Subscription,
54
- feature: FeatureKey,
55
- quantity: int = 1
56
- ):
57
-
58
- feature_errors = {
59
- FeatureKey.MESSAGES: ErrorCode.Subscription_Insufficient_Message_Feature,
60
- FeatureKey.BOTS: ErrorCode.Subscription_Insufficient_Bot_Feature,
61
- FeatureKey.URLS: ErrorCode.Subscription_Insufficient_URL_FEATURE,
62
- FeatureKey.TEXTS: ErrorCode.Subscription_Insufficient_TEXT_FEATURE,
63
- FeatureKey.FILES: ErrorCode.Subscription_Insufficient_FILE_FEATURE,
64
- FeatureKey.OCR: ErrorCode.Subscription_Insufficient_OCR_FEATURE,
65
- FeatureKey.TTS: ErrorCode.Subscription_Insufficient_TTS_FEATURE,
66
- FeatureKey.ASR: ErrorCode.Subscription_Insufficient_ASR_FEATURE,
67
- }
68
-
69
- if not user_subscription:
70
- raise BusinessException(error_code=ErrorCode.Subscription_Not_Found)
71
-
72
- if user_subscription.plan_type == PlanType.UNLIMITED:
73
- return
74
-
75
- for _feature in user_subscription.features:
76
- if _feature.feature_key == feature:
77
- if _feature.remaining - quantity < 0:
78
- raise BusinessException(error_code=feature_errors[feature])
79
- break
80
-
81
- def check_access_level(self, allowed_roles: List[Role], user_workspace: UserWorkspace):
82
- if not user_workspace:
83
- raise BusinessException(ErrorCode.Workspace_Access_Denied)
84
-
85
- if user_workspace.role not in allowed_roles and user_workspace.role != Role.ADMIN:
86
- raise BusinessException(ErrorCode.User_Access_Denied)
File without changes