Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(plan): quota check counting resources across all users in personal space, CheckQuota only filtered by workspace_id when counting resources

Jonas Kanindaabd695c51562
diff --git a/internal/handlers/api_key_handler.go b/internal/handlers/api_key_handler.go
index 3934f9b..bdd68b3 100644
--- a/internal/handlers/api_key_handler.go
+++ b/internal/handlers/api_key_handler.go
@@ -71,7 +71,7 @@ func (h *APIKeyHandler) Create(c *okapi.Context, req *CreateAPIKeyRequest) error
scope := getScope(c)
if h.quota != nil {
- if err := h.quota.CheckQuota(h.db, scope.WorkspaceID, "api_keys"); err != nil {
+ if err := h.quota.CheckQuota(h.db, scope.UserID, scope.WorkspaceID, "api_keys"); err != nil {
return c.AbortForbidden("API key quota exceeded for this workspace", err)
}
}
diff --git a/internal/handlers/domain_handler.go b/internal/handlers/domain_handler.go
index f871e93..1ba8d53 100644
--- a/internal/handlers/domain_handler.go
+++ b/internal/handlers/domain_handler.go
@@ -69,7 +69,7 @@ func (h *DomainHandler) Create(c *okapi.Context, req *CreateDomainRequest) error
scope := getScope(c)
if h.quota != nil {
- if err := h.quota.CheckQuota(h.db, scope.WorkspaceID, "domains"); err != nil {
+ if err := h.quota.CheckQuota(h.db, scope.UserID, scope.WorkspaceID, "domains"); err != nil {
return c.AbortForbidden("Domain quota exceeded for this workspace", err)
}
}
diff --git a/internal/handlers/helper.go b/internal/handlers/helper.go
index d9104ac..a42a131 100644
--- a/internal/handlers/helper.go
+++ b/internal/handlers/helper.go
@@ -32,7 +32,7 @@ var errForbidden = errors.New("insufficient workspace permissions: editor role o
// QuotaChecker verifies that creating a resource would not exceed plan limits.
type QuotaChecker interface {
- CheckQuota(db *gorm.DB, workspaceID *uint, resource string) error
+ CheckQuota(db *gorm.DB, userID uint, workspaceID *uint, resource string) error
CheckWorkspaceQuota(db *gorm.DB, userID uint) error
}
diff --git a/internal/handlers/smtp_handler.go b/internal/handlers/smtp_handler.go
index da8b4c5..9b35338 100644
--- a/internal/handlers/smtp_handler.go
+++ b/internal/handlers/smtp_handler.go
@@ -124,7 +124,7 @@ func (h *SMTPHandler) Create(c *okapi.Context, req *CreateSMTPRequest) error {
scope := getScope(c)
if h.quota != nil {
- if err := h.quota.CheckQuota(h.db, scope.WorkspaceID, "smtp_servers"); err != nil {
+ if err := h.quota.CheckQuota(h.db, scope.UserID, scope.WorkspaceID, "smtp_servers"); err != nil {
return c.AbortForbidden("SMTP server quota exceeded for this workspace", err)
}
}
diff --git a/internal/services/plan/service.go b/internal/services/plan/service.go
index d144329..10ec704 100644
--- a/internal/services/plan/service.go
+++ b/internal/services/plan/service.go
@@ -101,7 +101,7 @@ func (s *Service) EffectivePlan(workspaceID *uint) *models.Plan {
// CheckQuota verifies that creating one more resource of the given type
// would not exceed the plan limit. Returns nil if within limit.
-func (s *Service) CheckQuota(db *gorm.DB, workspaceID *uint, resource string) error {
+func (s *Service) CheckQuota(db *gorm.DB, userID uint, workspaceID *uint, resource string) error {
limits := s.EffectiveLimits(workspaceID)
var limit int
@@ -130,7 +130,7 @@ func (s *Service) CheckQuota(db *gorm.DB, workspaceID *uint, resource string) er
if workspaceID != nil {
query = query.Where("workspace_id = ?", *workspaceID)
} else {
- query = query.Where("workspace_id IS NULL")
+ query = query.Where("user_id = ? AND workspace_id IS NULL", userID)
}
if err := query.Count(&count).Error; err != nil {
return fmt.Errorf("failed to count %s: %w", resource, err)