Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /DeepselSystems_deepsel /deepsel /orm /user_mixin.py
| import json | |
| import logging | |
| from datetime import datetime, timedelta, UTC | |
| from typing import Optional | |
| from fastapi import HTTPException, status | |
| from sqlalchemy import or_ | |
| from sqlalchemy.orm import Session | |
| logger = logging.getLogger(__name__) | |
| class UserMixin: | |
| """ | |
| Mixin providing user authentication, role/permission resolution, and email methods. | |
| Concrete User model should inherit from `ORMBaseMixin` (NOT `BaseModel`), so it | |
| skips `OrganizationMetaDataMixin` and has no `organization_id`/`owner_id` column. | |
| Org membership lives exclusively in the `organizations` M2M relationship. | |
| Per-request "current org" context is supplied via the `X-Organization-Id` header | |
| (see `deepsel.auth.current_org.resolve_current_organization_id`) and attached as | |
| `user.current_organization_id` by the consumer's `get_current_user` dependency. | |
| Subclass must override: | |
| _get_app_secret() -> str | |
| _get_auth_algorithm() -> str | |
| _get_frontend_url() -> str | |
| _get_is_authless() -> bool | |
| _get_default_org_id() -> int | |
| _get_password_context() -> CryptContext | |
| _get_admin_role_string_ids() -> list[str] | |
| _get_admin_user_string_id() -> str | |
| _get_set_password_template_id() -> str | |
| _get_reset_password_template_id() -> str | |
| _get_email_verification_template_id() -> str | |
| Subclass may override: | |
| _get_platform_brand_name() -> str | None | |
| Return a fixed brand name for owner-facing emails (password | |
| reset, setup, email verification) instead of using the | |
| organization's name. The default implementation in | |
| ``deepsel.apps.core.models.user`` reads ``settings.PRODUCT_NAME``. | |
| """ | |
| csv_export_exclude: set[str] = { | |
| "hashed_password", | |
| "secret_key_2fa", | |
| "temp_secret_key_2fa", | |
| "recovery_codes", | |
| "email_verification_code", | |
| "email_verification_code_expires", | |
| "email_verification_attempts", | |
| "email_verification_sent_at", | |
| } | |
| def _get_app_secret(cls) -> str: | |
| raise NotImplementedError("Subclass must implement _get_app_secret()") | |
| def _get_auth_algorithm(cls) -> str: | |
| raise NotImplementedError("Subclass must implement _get_auth_algorithm()") | |
| def _get_frontend_url(cls) -> str: | |
| raise NotImplementedError("Subclass must implement _get_frontend_url()") | |
| def _get_is_authless(cls) -> bool: | |
| raise NotImplementedError("Subclass must implement _get_is_authless()") | |
| def _get_default_org_id(cls) -> int: | |
| raise NotImplementedError("Subclass must implement _get_default_org_id()") | |
| def _get_password_context(cls): | |
| raise NotImplementedError("Subclass must implement _get_password_context()") | |
| def _get_admin_role_string_ids(cls) -> list[str]: | |
| raise NotImplementedError( | |
| "Subclass must implement _get_admin_role_string_ids()" | |
| ) | |
| def _get_admin_user_string_id(cls) -> str: | |
| raise NotImplementedError("Subclass must implement _get_admin_user_string_id()") | |
| def _get_set_password_template_id(cls) -> str: | |
| raise NotImplementedError( | |
| "Subclass must implement _get_set_password_template_id()" | |
| ) | |
| def _get_reset_password_template_id(cls) -> str: | |
| raise NotImplementedError( | |
| "Subclass must implement _get_reset_password_template_id()" | |
| ) | |
| def _get_platform_brand_name(cls) -> Optional[str]: | |
| return None | |
| def get_org_ids(self): | |
| return [org.id for org in self.organizations] | |
| def _get_protected_org_role_string_ids(cls) -> tuple: | |
| """Roles an organization must never be left without. | |
| Defaults to the admin roles plus `owner_role`, the string_id | |
| `AuthService.provision_organization` conventionally attaches to a new | |
| org's first user. Override per deployment with | |
| `PROTECTED_ORG_ROLE_STRING_IDS` in the settings module. | |
| """ | |
| from deepsel import deps | |
| configured = getattr(deps.settings, "PROTECTED_ORG_ROLE_STRING_IDS", None) | |
| if configured is not None: | |
| return tuple(configured) | |
| return ("owner_role",) + tuple(cls._get_admin_role_string_ids()) | |
| def _check_not_last_protected_role_holder(self, db: Session, values: dict) -> None: | |
| """SB-15: refuse an update that would strip the last owner/admin of an | |
| organization of that role. | |
| Without this the owner of a self-serve org could demote themselves and | |
| leave the org with nobody able to reach Settings, invite anyone, or | |
| manage billing — an unrecoverable state through the product's own UI. | |
| """ | |
| if "roles" not in values or values["roles"] is None: | |
| return | |
| protected = self._get_protected_org_role_string_ids() | |
| if not protected: | |
| return | |
| held = [ | |
| role | |
| for role in (self.roles or []) | |
| if getattr(role, "string_id", None) in protected | |
| ] | |
| if not held: | |
| return | |
| incoming_ids = set() | |
| for record in values["roles"]: | |
| record_id = record.get("id") if isinstance(record, dict) else record | |
| if record_id is not None: | |
| incoming_ids.add(record_id) | |
| from deepsel.utils.models_pool import models_pool | |
| UserModel = models_pool["user"] | |
| RoleModel = models_pool["role"] | |
| for role in held: | |
| if role.id in incoming_ids: | |
| continue | |
| # Role rows are per-organization, so counting the other users linked | |
| # to *this* row is already scoped to the org that would be stranded. | |
| other_holders = ( | |
| db.query(UserModel) | |
| .filter( | |
| UserModel.roles.any(RoleModel.id == role.id), | |
| UserModel.id != self.id, | |
| ) | |
| .count() | |
| ) | |
| if other_holders == 0: | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail=( | |
| f"Cannot remove the '{role.string_id}' role from the last " | |
| "user who holds it in this organization. Grant it to " | |
| "someone else first." | |
| ), | |
| ) | |
| def update(self, db: Session, user, values: dict, *args, **kwargs): | |
| if not kwargs.get("bypass_permission"): | |
| self._check_not_last_protected_role_holder(db, values) | |
| return super().update(db, user, values, *args, **kwargs) | |
| def check_and_raise_if_not_admin_or_super_admin(self): | |
| if not any( | |
| role.string_id in self._get_admin_role_string_ids() for role in self.roles | |
| ): | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| detail="Only admin or super admin can update user", | |
| ) | |
| def is_admin(self): | |
| roles = self.get_user_roles() | |
| return any( | |
| [role.string_id in ["admin_role", "super_admin_role"] for role in roles] | |
| ) | |
| def _get_roles_recursively(self, role, processed_roles: list = None) -> set: | |
| if processed_roles is None: | |
| processed_roles = set() | |
| if role in processed_roles: | |
| return set() | |
| processed_roles.add(role) | |
| roles = set() | |
| roles.add(role) | |
| for implied_role in role.implied_roles: | |
| roles.update(self._get_roles_recursively(implied_role, processed_roles)) | |
| return roles | |
| def _get_permissions_recursively( | |
| self, role, processed_roles: list = None | |
| ) -> set[str]: | |
| if processed_roles is None: | |
| processed_roles = set() | |
| if role in processed_roles: | |
| return set() | |
| processed_roles.add(role) | |
| permissions = set() | |
| if role.permissions: | |
| these_permissions = json.loads(role.permissions) | |
| for permission in these_permissions: | |
| permissions.add(permission) | |
| for implied_role in role.implied_roles: | |
| permissions.update( | |
| self._get_permissions_recursively(implied_role, processed_roles) | |
| ) | |
| return permissions | |
| def get_user_permissions(self, user: "UserMixin" = None) -> list[str]: | |
| user = user or self | |
| roles = user.roles | |
| permissions = set() | |
| for role in roles: | |
| permissions.update(self._get_permissions_recursively(role)) | |
| return list(permissions) | |
| def get_user_roles(self, user: "UserMixin" = None) -> list: | |
| user = user or self | |
| roles = user.roles | |
| all_roles = set() | |
| for role in roles: | |
| all_roles.update(self._get_roles_recursively(role)) | |
| return list(all_roles) | |
| def get_user_has_roles(cls, role_string_ids: list[str], db: Session): | |
| from deepsel.utils.models_pool import models_pool | |
| ImpliedRoleModel = models_pool["implied_role"] | |
| UserRoleModel = models_pool["user_role"] | |
| RoleModel = models_pool["role"] | |
| roles = ( | |
| db.query(RoleModel).filter(RoleModel.string_id.in_(role_string_ids)).all() | |
| ) | |
| role_ids = [role.id for role in roles] | |
| main_roles = ( | |
| db.query(ImpliedRoleModel) | |
| .filter(ImpliedRoleModel.implied_role_id.in_(role_ids)) | |
| .all() | |
| ) | |
| role_ids += [role.role_id for role in main_roles] | |
| users = ( | |
| db.query(cls) | |
| .join(UserRoleModel) | |
| .filter(UserRoleModel.role_id.in_(list(set(role_ids)))) | |
| ).all() | |
| return users | |
| def _resolve_email_template( | |
| db: Session, string_id: str, organization_id: Optional[int] | |
| ): | |
| """Resolve an email template deterministically. | |
| SB-6 / HB-1 / HB-2: core's seed CSV installs one copy of every template | |
| into *every* organization, so a bare `.first()` with no org filter and | |
| no ORDER BY returned an arbitrary row — the invite could arrive in | |
| another tenant's language, or (once the arbitrary row belonged to an org | |
| with no SMTP) never arrive at all. | |
| `EmailTemplateMixin.send` sends through the template's *own* | |
| organization's SMTP config, so a copy in an org that cannot send is a | |
| silent no-op. Preference order: | |
| 1. the caller's organization, when that organization can send; | |
| 2. the platform organization (`settings.DEFAULT_ORG_ID`), when it can; | |
| 3. the caller's copy, then the platform copy, then the lowest-id copy — | |
| so a dev environment with no SMTP anywhere still resolves a template | |
| (and always the same one) instead of blowing up on `None`. | |
| """ | |
| from deepsel import deps | |
| from deepsel.utils.models_pool import models_pool | |
| EmailTemplateModel = models_pool["email_template"] | |
| def _in_org(org_id): | |
| if org_id is None: | |
| return None | |
| return ( | |
| db.query(EmailTemplateModel) | |
| .filter_by(string_id=string_id, organization_id=org_id) | |
| .order_by(EmailTemplateModel.id) | |
| .first() | |
| ) | |
| OrganizationModel = models_pool["organization"] | |
| def _can_send(template): | |
| if template is None: | |
| return False | |
| org = db.query(OrganizationModel).get(template.organization_id) | |
| return bool(org is not None and getattr(org, "is_smtp_configured", False)) | |
| platform_org_id = getattr(deps.settings, "DEFAULT_ORG_ID", None) | |
| own = _in_org(organization_id) | |
| platform = ( | |
| _in_org(platform_org_id) if platform_org_id != organization_id else own | |
| ) | |
| for candidate in (own, platform): | |
| if _can_send(candidate): | |
| return candidate | |
| if own is not None: | |
| return own | |
| if platform is not None: | |
| return platform | |
| return ( | |
| db.query(EmailTemplateModel) | |
| .filter_by(string_id=string_id) | |
| .order_by(EmailTemplateModel.id) | |
| .first() | |
| ) | |
| async def send_set_password_email(self, db: Session, organization_id: int): | |
| import jwt | |
| from deepsel.utils.models_pool import models_pool | |
| OrganizationModel = models_pool["organization"] | |
| org = db.query(OrganizationModel).get(organization_id) | |
| token = jwt.encode( | |
| { | |
| "uid": self.id, | |
| "org_id": organization_id, | |
| "exp": datetime.now(UTC) + timedelta(days=7), | |
| }, | |
| self._get_app_secret(), | |
| algorithm=self._get_auth_algorithm(), | |
| ) | |
| context = { | |
| "name": self.name or self.email or self.username, | |
| "username": self.email or self.username, | |
| "first_name": self.first_name, | |
| "last_name": self.last_name, | |
| "action_url": self._get_frontend_url() + "/reset-password?t=" + token, | |
| "business_name": self._get_platform_brand_name() | |
| or (org.name if org else ""), | |
| } | |
| template = self._resolve_email_template( | |
| db, self._get_set_password_template_id(), organization_id | |
| ) | |
| if template is None: | |
| logger.error( | |
| f"No '{self._get_set_password_template_id()}' email template is " | |
| f"installed; cannot send the password setup email to {self.email}" | |
| ) | |
| return False | |
| ok = await template.send(db, [self.email], context) | |
| if not ok: | |
| logger.error(f"Failed to send password setup email to {self.email}") | |
| else: | |
| logger.info(f"Password setup email sent to {self.email}") | |
| return ok | |
| async def email_reset_password(self, db: Session, organization_id: int): | |
| import jwt | |
| from deepsel.utils.models_pool import models_pool | |
| OrganizationModel = models_pool["organization"] | |
| org = db.query(OrganizationModel).get(organization_id) | |
| token = jwt.encode( | |
| { | |
| "uid": self.id, | |
| "org_id": organization_id, | |
| "exp": datetime.now(UTC) + timedelta(hours=24), | |
| }, | |
| self._get_app_secret(), | |
| algorithm=self._get_auth_algorithm(), | |
| ) | |
| context = { | |
| "name": self.name or self.email or self.username, | |
| "username": self.email or self.username, | |
| "first_name": self.first_name, | |
| "last_name": self.last_name, | |
| "action_url": self._get_frontend_url() + "/reset-password" + "?t=" + token, | |
| "business_name": self._get_platform_brand_name() | |
| or (org.name if org else ""), | |
| } | |
| template = self._resolve_email_template( | |
| db, self._get_reset_password_template_id(), organization_id | |
| ) | |
| if template is None: | |
| logger.error( | |
| f"No '{self._get_reset_password_template_id()}' email template is " | |
| f"installed; cannot send the password reset email to {self.email}" | |
| ) | |
| return False | |
| ok = await template.send(db, [self.email], context) | |
| return ok | |
| def _get_email_verification_template_id(cls) -> str: | |
| raise NotImplementedError( | |
| "Subclass must implement _get_email_verification_template_id()" | |
| ) | |
| async def send_email_verification_code( | |
| self, db: Session, organization_id: int | |
| ) -> bool: | |
| """Generate a 6-digit confirmation code, store its hash on the user, and | |
| email it via the given org's template + SMTP config (the platform org for | |
| SaaS signups). Falls back to logging the code when mail is unconfigured, | |
| so dev environments stay usable.""" | |
| import secrets | |
| from deepsel.utils.models_pool import models_pool | |
| code = f"{secrets.randbelow(1_000_000):06d}" | |
| now = datetime.now(UTC).replace(tzinfo=None) | |
| self.email_verification_code = self._get_password_context().hash(code) | |
| self.email_verification_code_expires = now + timedelta(minutes=10) | |
| self.email_verification_sent_at = now | |
| self.email_verification_attempts = 0 | |
| db.commit() | |
| OrganizationModel = models_pool["organization"] | |
| EmailTemplateModel = models_pool["email_template"] | |
| org = db.query(OrganizationModel).get(organization_id) | |
| template = ( | |
| db.query(EmailTemplateModel) | |
| .filter_by( | |
| string_id=self._get_email_verification_template_id(), | |
| organization_id=organization_id, | |
| ) | |
| .first() | |
| ) | |
| if template is None or not org or not org.is_smtp_configured: | |
| logger.warning( | |
| f"[DEV fallback] Email verification code for {self.email}: {code}" | |
| ) | |
| return True | |
| context = { | |
| "name": self.name or self.email or self.username, | |
| "username": self.email or self.username, | |
| "first_name": self.first_name, | |
| "last_name": self.last_name, | |
| "code": code, | |
| "business_name": self._get_platform_brand_name() | |
| or (org.name if org else ""), | |
| } | |
| ok = await template.send(db, [self.email], context) | |
| if not ok: | |
| logger.error(f"Failed to send email verification code to {self.email}") | |
| return ok | |
| def check_email_verification_code(self, db: Session, code: str) -> bool: | |
| """Validate a submitted confirmation code. Raises on expiry/attempt | |
| exhaustion so callers can surface distinct errors; returns False on a | |
| plain wrong code.""" | |
| if self.email_verified: | |
| return True | |
| now = datetime.now(UTC).replace(tzinfo=None) | |
| if ( | |
| not self.email_verification_code | |
| or not self.email_verification_code_expires | |
| or now > self.email_verification_code_expires | |
| ): | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, detail="code_expired" | |
| ) | |
| if (self.email_verification_attempts or 0) >= 5: | |
| raise HTTPException( | |
| status_code=status.HTTP_429_TOO_MANY_REQUESTS, | |
| detail="too_many_attempts", | |
| ) | |
| # count the attempt before verifying so failed tries always consume one | |
| self.email_verification_attempts = (self.email_verification_attempts or 0) + 1 | |
| db.commit() | |
| if not self._get_password_context().verify(code, self.email_verification_code): | |
| return False | |
| self.email_verified = True | |
| self.email_verification_code = None | |
| self.email_verification_code_expires = None | |
| self.email_verification_attempts = 0 | |
| db.commit() | |
| return True | |
| def authenticate_user(cls, db: Session, identifier: str, password: str): | |
| from deepsel.utils.models_pool import models_pool | |
| OrgModel = models_pool["organization"] | |
| default_org_id = cls._get_default_org_id() | |
| org = db.query(OrgModel).get(default_org_id) | |
| if cls._get_is_authless() and org and not org.enable_auth: | |
| user = ( | |
| db.query(cls) | |
| .filter_by( | |
| string_id=cls._get_admin_user_string_id(), | |
| ) | |
| .first() | |
| ) | |
| return user | |
| if not identifier: | |
| return False | |
| user = ( | |
| db.query(cls) | |
| .filter(or_(cls.email == identifier, cls.username == identifier)) | |
| .filter(cls.active == True) # noqa: E712 | |
| .first() | |
| ) | |
| if not user: | |
| return False | |
| if not cls._get_password_context().verify(password, user.hashed_password): | |
| return False | |
| # getattr default keeps consumers whose user model lacks the column working | |
| if getattr(user, "email_verified", True) is False: | |
| raise HTTPException( | |
| status_code=status.HTTP_403_FORBIDDEN, detail="email_not_verified" | |
| ) | |
| return user | |
Xet Storage Details
- Size:
- 20.5 kB
- Xet hash:
- 73514eaf585ced30b25cc004c7c2db7566e3225bedd34135d864d2a29d4d1619
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.