forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg_redirect.go
48 lines (38 loc) · 1006 Bytes
/
org_redirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package middleware
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
)
func OrgRedirect() macaron.Handler {
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
orgIdValue := req.URL.Query().Get("orgId")
orgId, err := strconv.ParseInt(orgIdValue, 10, 32)
if err != nil || orgId == 0 {
return
}
ctx, ok := c.Data["ctx"].(*m.ReqContext)
if !ok || !ctx.IsSignedIn {
return
}
if orgId == ctx.OrgId {
return
}
cmd := m.SetUsingOrgCommand{UserId: ctx.UserId, OrgId: orgId}
if err := bus.Dispatch(&cmd); err != nil {
if ctx.IsApiRequest() {
ctx.JsonApiErr(404, "Not found", nil)
} else {
ctx.Error(404, "Not found")
}
return
}
newURL := setting.ToAbsUrl(fmt.Sprintf("%s?%s", strings.TrimPrefix(c.Req.URL.Path, "/"), c.Req.URL.Query().Encode()))
c.Redirect(newURL, 302)
}
}