feat: Implement cross-tenant access functionality
- Added configuration for enabling cross-tenant access in the application. - Introduced new API endpoints for listing and searching tenants, accessible only to users with the appropriate permissions. - Updated user and tenant models to include the `can_access_all_tenants` field. - Enhanced the authentication middleware to handle cross-tenant requests and validate user permissions. - Created a new TenantSelector component for selecting tenants in the frontend, improving user experience. - Updated internationalization files to support new tenant-related strings and messages.
This commit is contained in:
@@ -52,6 +52,45 @@ func (r *tenantRepository) ListTenants(ctx context.Context) ([]*types.Tenant, er
|
||||
return tenants, nil
|
||||
}
|
||||
|
||||
// SearchTenants searches tenants with pagination and filters
|
||||
func (r *tenantRepository) SearchTenants(ctx context.Context, keyword string, tenantID uint64, page, pageSize int) ([]*types.Tenant, int64, error) {
|
||||
var tenants []*types.Tenant
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&types.Tenant{})
|
||||
|
||||
// Filter by tenant ID if provided
|
||||
if tenantID > 0 {
|
||||
query = query.Where("id = ?", tenantID)
|
||||
}
|
||||
|
||||
// Filter by keyword if provided (search in name and description)
|
||||
if keyword != "" {
|
||||
query = query.Where("name LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
|
||||
// Count total
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
if page > 0 && pageSize > 0 {
|
||||
offset := (page - 1) * pageSize
|
||||
query = query.Offset(offset).Limit(pageSize)
|
||||
}
|
||||
|
||||
// Order by created_at DESC
|
||||
query = query.Order("created_at DESC")
|
||||
|
||||
// Execute query
|
||||
if err := query.Find(&tenants).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tenants, total, nil
|
||||
}
|
||||
|
||||
// UpdateTenant updates tenant
|
||||
func (r *tenantRepository) UpdateTenant(ctx context.Context, tenant *types.Tenant) error {
|
||||
return r.db.WithContext(ctx).Model(&types.Tenant{}).Where("id = ?", tenant.ID).Updates(tenant).Error
|
||||
|
||||
@@ -280,3 +280,49 @@ func (r *tenantService) ExtractTenantIDFromAPIKey(apiKey string) (uint64, error)
|
||||
|
||||
return tenantID, nil
|
||||
}
|
||||
|
||||
// ListAllTenants lists all tenants (for users with cross-tenant access permission)
|
||||
// This method returns all tenants without filtering, intended for admin users
|
||||
func (s *tenantService) ListAllTenants(ctx context.Context) ([]*types.Tenant, error) {
|
||||
tenants, err := s.repo.ListTenants(ctx)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "All tenants list retrieved successfully, total: %d", len(tenants))
|
||||
return tenants, nil
|
||||
}
|
||||
|
||||
// SearchTenants searches tenants with pagination and filters
|
||||
func (s *tenantService) SearchTenants(ctx context.Context, keyword string, tenantID uint64, page, pageSize int) ([]*types.Tenant, int64, error) {
|
||||
tenants, total, err := s.repo.SearchTenants(ctx, keyword, tenantID, page, pageSize)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"keyword": keyword,
|
||||
"tenantID": tenantID,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Tenants search completed, keyword: %s, tenantID: %d, page: %d, pageSize: %d, total: %d, found: %d",
|
||||
keyword, tenantID, page, pageSize, total, len(tenants))
|
||||
return tenants, total, nil
|
||||
}
|
||||
|
||||
// GetTenantByIDForUser gets a tenant by ID with permission check
|
||||
// This method verifies that the user has permission to access the tenant
|
||||
func (s *tenantService) GetTenantByIDForUser(ctx context.Context, tenantID uint64, userID string) (*types.Tenant, error) {
|
||||
tenant, err := s.repo.GetTenantByID(ctx, tenantID)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||
"tenant_id": tenantID,
|
||||
"user_id": userID,
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tenant, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user