Skip to content

Commit 370589d

Browse files
committed
fix(org switch): fixed issue with switching orgs, fixes grafana#4614
1 parent 262c341 commit 370589d

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

pkg/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func Register(r *macaron.Macaron) {
3030
// authed views
3131
r.Get("/profile/", reqSignedIn, Index)
3232
r.Get("/profile/password", reqSignedIn, Index)
33+
r.Get("/profile/switch-org/:id", reqSignedIn, ChangeActiveOrgAndRedirectToHome)
3334
r.Get("/org/", reqSignedIn, Index)
3435
r.Get("/org/new", reqSignedIn, Index)
3536
r.Get("/datasources/", reqSignedIn, Index)

pkg/api/user.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/grafana/grafana/pkg/bus"
55
"github.com/grafana/grafana/pkg/middleware"
66
m "github.com/grafana/grafana/pkg/models"
7+
"github.com/grafana/grafana/pkg/setting"
78
"github.com/grafana/grafana/pkg/util"
89
)
910

@@ -109,6 +110,23 @@ func UserSetUsingOrg(c *middleware.Context) Response {
109110
return ApiSuccess("Active organization changed")
110111
}
111112

113+
// GET /profile/switch-org/:id
114+
func ChangeActiveOrgAndRedirectToHome(c *middleware.Context) {
115+
orgId := c.ParamsInt64(":id")
116+
117+
if !validateUsingOrg(c.UserId, orgId) {
118+
NotFoundHandler(c)
119+
}
120+
121+
cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
122+
123+
if err := bus.Dispatch(&cmd); err != nil {
124+
NotFoundHandler(c)
125+
}
126+
127+
c.Redirect(setting.AppSubUrl + "/")
128+
}
129+
112130
func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) Response {
113131
userQuery := m.GetUserByIdQuery{Id: c.UserId}
114132

public/app/core/components/sidemenu/sidemenu.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ export class SideMenuCtrl {
7272
this.orgMenu.push({
7373
text: "Switch to " + org.name,
7474
icon: "fa fa-fw fa-random",
75-
click: () => {
76-
this.switchOrg(org.orgId);
77-
}
75+
url: this.getUrl('/profile/switch-org/' + org.orgId),
76+
target: '_self'
7877
});
7978
});
8079

0 commit comments

Comments
 (0)