|
5 | 5 | "context"
|
6 | 6 | "errors"
|
7 | 7 | "fmt"
|
| 8 | + "io" |
8 | 9 | "net/http"
|
9 | 10 | "net/url"
|
10 | 11 | "regexp"
|
@@ -320,6 +321,83 @@ func UseGlobalOrSingleOrg(cfg *setting.Cfg) OrgIDGetter {
|
320 | 321 | }
|
321 | 322 | }
|
322 | 323 |
|
| 324 | +// UseOrgFromRequestData returns the organization from the request data. |
| 325 | +// If no org is specified, then the org where user is logged in is returned. |
| 326 | +func UseOrgFromRequestData(c *contextmodel.ReqContext) (int64, error) { |
| 327 | + query, err := getOrgQueryFromRequest(c) |
| 328 | + if err != nil { |
| 329 | + // Special case of macaron handling invalid params |
| 330 | + return NoOrgID, org.ErrOrgNotFound.Errorf("failed to get organization from context: %w", err) |
| 331 | + } |
| 332 | + |
| 333 | + if query.OrgId == nil { |
| 334 | + return c.SignedInUser.GetOrgID(), nil |
| 335 | + } |
| 336 | + |
| 337 | + return *query.OrgId, nil |
| 338 | +} |
| 339 | + |
| 340 | +// UseGlobalOrgFromRequestData returns global org if `global` flag is set or the org where user is logged in. |
| 341 | +func UseGlobalOrgFromRequestData(c *contextmodel.ReqContext) (int64, error) { |
| 342 | + query, err := getOrgQueryFromRequest(c) |
| 343 | + if err != nil { |
| 344 | + // Special case of macaron handling invalid params |
| 345 | + return NoOrgID, org.ErrOrgNotFound.Errorf("failed to get organization from context: %w", err) |
| 346 | + } |
| 347 | + |
| 348 | + if query.Global { |
| 349 | + return GlobalOrgID, nil |
| 350 | + } |
| 351 | + |
| 352 | + return c.SignedInUser.GetOrgID(), nil |
| 353 | +} |
| 354 | + |
| 355 | +func getOrgQueryFromRequest(c *contextmodel.ReqContext) (*QueryWithOrg, error) { |
| 356 | + query := &QueryWithOrg{} |
| 357 | + |
| 358 | + req, err := CloneRequest(c.Req) |
| 359 | + if err != nil { |
| 360 | + return nil, err |
| 361 | + } |
| 362 | + |
| 363 | + if err := web.Bind(req, query); err != nil { |
| 364 | + // Special case of macaron handling invalid params |
| 365 | + return nil, err |
| 366 | + } |
| 367 | + |
| 368 | + return query, nil |
| 369 | +} |
| 370 | + |
| 371 | +// CloneRequest creates request copy including request body |
| 372 | +func CloneRequest(req *http.Request) (*http.Request, error) { |
| 373 | + // Get copy of body to prevent error when reading closed body in request handler |
| 374 | + bodyCopy, err := CopyRequestBody(req) |
| 375 | + if err != nil { |
| 376 | + return nil, err |
| 377 | + } |
| 378 | + reqCopy := req.Clone(req.Context()) |
| 379 | + reqCopy.Body = bodyCopy |
| 380 | + return reqCopy, nil |
| 381 | +} |
| 382 | + |
| 383 | +// CopyRequestBody returns copy of request body and keeps the original one to prevent error when reading closed body |
| 384 | +func CopyRequestBody(req *http.Request) (io.ReadCloser, error) { |
| 385 | + if req.Body == nil { |
| 386 | + return nil, nil |
| 387 | + } |
| 388 | + |
| 389 | + body := req.Body |
| 390 | + var buf bytes.Buffer |
| 391 | + if _, err := buf.ReadFrom(body); err != nil { |
| 392 | + return nil, err |
| 393 | + } |
| 394 | + if err := body.Close(); err != nil { |
| 395 | + return nil, err |
| 396 | + } |
| 397 | + req.Body = io.NopCloser(&buf) |
| 398 | + return io.NopCloser(bytes.NewReader(buf.Bytes())), nil |
| 399 | +} |
| 400 | + |
323 | 401 | // scopeParams holds the parameters used to fill in scope templates
|
324 | 402 | type scopeParams struct {
|
325 | 403 | OrgID int64
|
|
0 commit comments