feat: HR Portal - Complete Multi-Tenant System with Redis Session Storage

Major Features:
-  Multi-tenant architecture (tenant isolation)
-  Employee CRUD with lifecycle management (onboarding/offboarding)
-  Department tree structure with email domain management
-  Company info management (single-record editing)
-  System functions CRUD (permission management)
-  Email account management (multi-account per employee)
-  Keycloak SSO integration (auth.lab.taipei)
-  Redis session storage (10.1.0.254:6379)
  - Solves Cookie 4KB limitation
  - Cross-system session sharing
  - Sliding expiration (8 hours)
  - Automatic token refresh

Technical Stack:
Backend:
- FastAPI + SQLAlchemy
- PostgreSQL 16 (10.1.0.20:5433)
- Keycloak Admin API integration
- Docker Mailserver integration (SSH)
- Alembic migrations

Frontend:
- Next.js 14 (App Router)
- NextAuth 4 with Keycloak Provider
- Redis session storage (ioredis)
- Tailwind CSS

Infrastructure:
- Redis 7 (10.1.0.254:6379) - Session + Cache
- Keycloak 26.1.0 (auth.lab.taipei)
- Docker Mailserver (10.1.0.254)

Architecture Highlights:
- Session管理由 Keycloak + Redis 統一控制
- 支援多系統 (HR/WebMail/Calendar/Drive/Office) 共享 session
- Token 自動刷新,異質服務整合
- 未來可無縫遷移到雲端

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-23 20:12:43 +08:00
commit 360533393f
386 changed files with 70353 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
"""
租戶網域 Model
支援單一租戶使用多個網域 (多品牌/國際化)
"""
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import relationship
import enum
from app.db.base import Base
class DomainStatus(str, enum.Enum):
"""網域狀態"""
PENDING = "pending" # 待驗證
ACTIVE = "active" # 啟用中
DISABLED = "disabled" # 已停用
class TenantDomain(Base):
"""租戶網域表 (一個租戶可以有多個網域)"""
__tablename__ = "tenant_domains"
__table_args__ = (
# 每個租戶只能有一個主要網域
Index("idx_tenant_primary_domain", "tenant_id", unique=True, postgresql_where=Column("is_primary") == True),
# 一般索引
Index("idx_tenant_domains_tenant", "tenant_id"),
Index("idx_tenant_domains_status", "status"),
)
id = Column(Integer, primary_key=True, index=True)
tenant_id = Column(Integer, ForeignKey("organizes.id", ondelete="CASCADE"), nullable=False, index=True)
domain = Column(String(100), unique=True, nullable=False, index=True, comment="網域名稱 (abc.com.tw)")
# 網域屬性
is_primary = Column(Boolean, default=False, nullable=False, comment="是否為主要網域")
status = Column(String(20), default=DomainStatus.PENDING, nullable=False, comment="狀態")
verified = Column(Boolean, default=False, nullable=False, comment="DNS 驗證狀態")
# DNS 驗證
verification_token = Column(String(100), nullable=True, comment="驗證 Token")
verified_at = Column(DateTime, nullable=True, comment="驗證時間")
# 服務啟用狀態
enable_email = Column(Boolean, default=True, nullable=False, comment="啟用郵件服務")
enable_webmail = Column(Boolean, default=True, nullable=False, comment="啟用 WebMail")
enable_drive = Column(Boolean, default=True, nullable=False, comment="啟用雲端硬碟")
# 時間記錄
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
# 關聯
tenant = relationship("Tenant", back_populates="domains")
email_aliases = relationship(
"UserEmailAlias",
back_populates="domain",
cascade="all, delete-orphan",
lazy="dynamic"
)
def __repr__(self):
return f"<TenantDomain {self.domain} ({'主要' if self.is_primary else '次要'})>"
@property
def is_verified(self) -> bool:
"""是否已驗證"""
return self.verified and self.status == DomainStatus.ACTIVE
def generate_dns_records(self) -> list:
"""生成 DNS 驗證記錄指引"""
records = []
# TXT 記錄 - 網域所有權驗證
records.append({
"type": "TXT",
"name": "@",
"value": f"porsche-cloud-verify={self.verification_token}",
"purpose": "網域所有權驗證"
})
if self.enable_email:
# MX 記錄 - 郵件伺服器
records.append({
"type": "MX",
"name": "@",
"value": "mail.porschecloud.tw",
"priority": 10,
"purpose": "郵件伺服器"
})
# SPF 記錄 - 防止郵件偽造
records.append({
"type": "TXT",
"name": "@",
"value": "v=spf1 include:porschecloud.tw ~all",
"purpose": "郵件 SPF 記錄"
})
if self.enable_webmail:
# CNAME - WebMail
records.append({
"type": "CNAME",
"name": "mail",
"value": f"tenant-{self.tenant.tenant_code}.porschecloud.tw",
"purpose": "WebMail 訪問"
})
if self.enable_drive:
# CNAME - 雲端硬碟
records.append({
"type": "CNAME",
"name": "drive",
"value": f"tenant-{self.tenant.tenant_code}.porschecloud.tw",
"purpose": "雲端硬碟訪問"
})
return records