From fb5c59c6111beb8ee5541c21715e3d0a013e624c Mon Sep 17 00:00:00 2001 From: supercharlesliu Date: Mon, 19 Nov 2018 17:08:10 +0800 Subject: [PATCH 1/3] Add GET /api/users/:id/teams for orgAdmin --- pkg/api/api.go | 1 + pkg/api/user.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index c372debdb72..0526ee80afe 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -140,6 +140,7 @@ func (hs *HTTPServer) registerRoutes() { usersRoute.Get("/", Wrap(SearchUsers)) usersRoute.Get("/search", Wrap(SearchUsersWithPaging)) usersRoute.Get("/:id", Wrap(GetUserByID)) + usersRoute.Get("/:id/teams", Wrap(GetUserTeams)) usersRoute.Get("/:id/orgs", Wrap(GetUserOrgList)) // query parameters /users/lookup?loginOrEmail=admin@example.com usersRoute.Get("/lookup", Wrap(GetUserByLoginOrEmail)) diff --git a/pkg/api/user.go b/pkg/api/user.go index 7116ad83f3f..f03f2d90990 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -113,7 +113,16 @@ func GetSignedInUserOrgList(c *m.ReqContext) Response { // GET /api/user/teams func GetSignedInUserTeamList(c *m.ReqContext) Response { - query := m.GetTeamsByUserQuery{OrgId: c.OrgId, UserId: c.UserId} + return getUserTeamList(c.OrgId, c.UserId) +} + +// GET /api/users/:id/teams +func GetUserTeams(c *m.ReqContext) Response { + return getUserTeamList(c.OrgId, c.ParamsInt64("id")) +} + +func getUserTeamList(userID int64, orgID int64) Response { + query := m.GetTeamsByUserQuery{OrgId: orgID, UserId: userID} if err := bus.Dispatch(&query); err != nil { return Error(500, "Failed to get user teams", err) @@ -122,11 +131,10 @@ func GetSignedInUserTeamList(c *m.ReqContext) Response { for _, team := range query.Result { team.AvatarUrl = dtos.GetGravatarUrlWithDefault(team.Email, team.Name) } - return JSON(200, query.Result) } -// GET /api/user/:id/orgs +// GET /api/users/:id/orgs func GetUserOrgList(c *m.ReqContext) Response { return getUserOrgList(c.ParamsInt64(":id")) } From 215242128bdefb5aa0cb6e7e537760b936ad0370 Mon Sep 17 00:00:00 2001 From: supercharlesliu Date: Mon, 19 Nov 2018 17:09:45 +0800 Subject: [PATCH 2/3] Fix param --- pkg/api/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/api/user.go b/pkg/api/user.go index f03f2d90990..6db9c6f6baf 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -118,7 +118,7 @@ func GetSignedInUserTeamList(c *m.ReqContext) Response { // GET /api/users/:id/teams func GetUserTeams(c *m.ReqContext) Response { - return getUserTeamList(c.OrgId, c.ParamsInt64("id")) + return getUserTeamList(c.OrgId, c.ParamsInt64(":id")) } func getUserTeamList(userID int64, orgID int64) Response { From a241f67fba94e319741e5d5e5275fac4dd08b48d Mon Sep 17 00:00:00 2001 From: supercharlesliu Date: Tue, 20 Nov 2018 10:17:46 +0800 Subject: [PATCH 3/3] Add doc for api "GET /api/users/:id/teams" --- docs/sources/http_api/user.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/sources/http_api/user.md b/docs/sources/http_api/user.md index b9047187b2d..047d4473603 100644 --- a/docs/sources/http_api/user.md +++ b/docs/sources/http_api/user.md @@ -226,6 +226,40 @@ Content-Type: application/json ] ``` +## Get Teams for user + +`GET /api/users/:id/teams` + +**Example Request**: + +```http +GET /api/users/1/teams HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` + +Requires basic authentication and that the authenticated user is a Grafana Admin. + +**Example Response**: + +```http +HTTP/1.1 200 +Content-Type: application/json + +[ + { + "id":1, + "orgId":1, + "name":"team1", + "email":"", + "avatarUrl":"/avatar/3fcfe295eae3bcb67a49349377428a66", + "memberCount":1 + } +] +``` + + ## User ## Actual User