When subscription access is requested with an explicit access_api_key, assign the subscription to the real target user, bind that user's API key to the subscription group, and probe readiness with the same key instead of falling back to a managed synthetic user. Update the runtime/reconcile flows, adapter tests, and source-of-truth docs so subscription_ready now reflects user-visible host access rather than managed-key-only closure success.
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package access
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"sub2api-cn-relay-manager/internal/host/sub2api"
|
|
)
|
|
|
|
func (s *Service) prepareSubscriptionPlan(ctx context.Context, req ClosureRequest, plan closurePlan) (closurePlan, error) {
|
|
requestedProbeAPIKey := strings.TrimSpace(req.ProbeAPIKey)
|
|
for _, target := range req.Subscriptions {
|
|
resolvedTarget := target.UserID
|
|
if requestedProbeAPIKey != "" {
|
|
if _, err := s.host.AssignSubscription(ctx, sub2api.AssignSubscriptionRequest{
|
|
UserID: resolvedTarget,
|
|
GroupID: req.GroupID,
|
|
DurationDays: target.DurationDays,
|
|
}); err != nil {
|
|
return closurePlan{}, fmt.Errorf("assign subscription for %s: %w", target.UserID, err)
|
|
}
|
|
accessRef, err := s.host.EnsureSubscriptionAccess(ctx, sub2api.EnsureSubscriptionAccessRequest{
|
|
UserSelector: target.UserID,
|
|
GroupID: req.GroupID,
|
|
ProbeAPIKey: requestedProbeAPIKey,
|
|
})
|
|
if err != nil {
|
|
return closurePlan{}, fmt.Errorf("ensure subscription access for %s: %w", target.UserID, err)
|
|
}
|
|
if strings.TrimSpace(accessRef.APIKey) != "" {
|
|
plan.effectiveProbeAPIKey = strings.TrimSpace(accessRef.APIKey)
|
|
}
|
|
continue
|
|
}
|
|
|
|
accessRef, err := s.host.EnsureSubscriptionAccess(ctx, sub2api.EnsureSubscriptionAccessRequest{
|
|
UserSelector: target.UserID,
|
|
GroupID: req.GroupID,
|
|
})
|
|
if err != nil {
|
|
return closurePlan{}, fmt.Errorf("ensure subscription access for %s: %w", target.UserID, err)
|
|
}
|
|
if strings.TrimSpace(accessRef.UserID) != "" {
|
|
resolvedTarget = accessRef.UserID
|
|
}
|
|
if strings.TrimSpace(accessRef.APIKey) != "" {
|
|
plan.effectiveProbeAPIKey = strings.TrimSpace(accessRef.APIKey)
|
|
plan.effectiveProbeKeySource = ProbeKeySourceManagedSubscription
|
|
}
|
|
if _, err := s.host.AssignSubscription(ctx, sub2api.AssignSubscriptionRequest{
|
|
UserID: resolvedTarget,
|
|
GroupID: req.GroupID,
|
|
DurationDays: target.DurationDays,
|
|
}); err != nil {
|
|
return closurePlan{}, fmt.Errorf("assign subscription for %s: %w", target.UserID, err)
|
|
}
|
|
}
|
|
return plan, nil
|
|
}
|