(ctx context.Context, req *adminv1.UnsubscribeReportRequest)
| 298 | } |
| 299 | |
| 300 | func (s *Server) UnsubscribeReport(ctx context.Context, req *adminv1.UnsubscribeReportRequest) (*adminv1.UnsubscribeReportResponse, error) { |
| 301 | observability.AddRequestAttributes(ctx, |
| 302 | attribute.String("args.organization", req.Org), |
| 303 | attribute.String("args.project", req.Project), |
| 304 | attribute.String("args.name", req.Name), |
| 305 | ) |
| 306 | if req.Email != "" { |
| 307 | observability.AddRequestAttributes(ctx, attribute.String("args.email", req.Email)) |
| 308 | } |
| 309 | |
| 310 | proj, err := s.admin.DB.FindProjectByName(ctx, req.Org, req.Project) |
| 311 | if err != nil { |
| 312 | return nil, err |
| 313 | } |
| 314 | |
| 315 | claims := auth.GetClaims(ctx) |
| 316 | |
| 317 | depl, err := s.admin.DB.FindDeployment(ctx, *proj.PrimaryDeploymentID) |
| 318 | if err != nil { |
| 319 | return nil, err |
| 320 | } |
| 321 | |
| 322 | spec, err := s.admin.LookupReport(ctx, depl, req.Name) |
| 323 | if err != nil { |
| 324 | return nil, fmt.Errorf("could not get report: %w", err) |
| 325 | } |
| 326 | annotations := parseReportAnnotations(spec.Annotations) |
| 327 | |
| 328 | if !annotations.AdminManaged { |
| 329 | return nil, status.Error(codes.FailedPrecondition, "can't edit report because it was not created from the UI") |
| 330 | } |
| 331 | |
| 332 | if claims.OwnerType() != auth.OwnerTypeUser && claims.OwnerType() != auth.OwnerTypeMagicAuthToken { |
| 333 | return nil, status.Error(codes.PermissionDenied, "only users can unsubscribe from reports") |
| 334 | } |
| 335 | |
| 336 | var userEmail string |
| 337 | var slackEmail string |
| 338 | if claims.OwnerType() == auth.OwnerTypeUser { |
| 339 | user, err := s.admin.DB.FindUser(ctx, claims.OwnerID()) |
| 340 | if err != nil { |
| 341 | return nil, err |
| 342 | } |
| 343 | userEmail = user.Email |
| 344 | } |
| 345 | |
| 346 | if claims.OwnerType() == auth.OwnerTypeMagicAuthToken { |
| 347 | reportTkn, err := s.admin.DB.FindNotificationTokenForMagicAuthToken(ctx, claims.OwnerID()) |
| 348 | if err != nil { |
| 349 | return nil, fmt.Errorf("failed to find notification token: %w", err) |
| 350 | } |
| 351 | |
| 352 | if reportTkn.ResourceKind != runtime.ResourceKindReport || reportTkn.ResourceName != req.Name { |
| 353 | return nil, status.Error(codes.PermissionDenied, "token is not valid for this report") |
| 354 | } |
| 355 | |
| 356 | if reportTkn.RecipientEmail == "" { |
| 357 | if req.Email != "" { |
nothing calls this directly
no test coverage detected