Browse Source

[3743] Adds support for user search based on login or email (#7334)

* [3743] Adds support for user search based on login or email

* Use query parameter instead

* Use macaron Query() instead
Pavlos Daoglou 9 năm trước cách đây
mục cha
commit
3e741315b4
3 tập tin đã thay đổi với 66 bổ sung6 xóa
  1. 34 0
      docs/sources/http_api/user.md
  2. 2 0
      pkg/api/api.go
  3. 30 6
      pkg/api/user.go

+ 34 - 0
docs/sources/http_api/user.md

@@ -69,6 +69,40 @@ parent = "http_api"
       "isGrafanaAdmin": true
       "isGrafanaAdmin": true
     }
     }
 
 
+## Get single user by Username(login) or Email
+
+    `GET /api/users/lookup`
+
+    **Parameter:** `loginOrEmail`
+
+    **Example Request using the email as option**:
+
+        GET /api/users/lookup?loginOrEmail=user@mygraf.com HTTP/1.1
+        Accept: application/json
+        Content-Type: application/json
+        Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
+
+    **Example Request using the username as option**:
+        GET /api/users/lookup?loginOrEmail=admin HTTP/1.1
+        Accept: application/json
+        Content-Type: application/json
+        Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
+
+    **Example Response**:
+
+        HTTP/1.1 200
+        Content-Type: application/json
+
+        {
+          "email": "user@mygraf.com"
+          "name": "admin",
+          "login": "admin",
+          "theme": "light",
+          "orgId": 1,
+          "isGrafanaAdmin": true
+        }
+
+
 ## User Update
 ## User Update
 
 
 `PUT /api/users/:id`
 `PUT /api/users/:id`

+ 2 - 0
pkg/api/api.go

@@ -126,6 +126,8 @@ func Register(r *macaron.Macaron) {
 			r.Get("/", wrap(SearchUsers))
 			r.Get("/", wrap(SearchUsers))
 			r.Get("/:id", wrap(GetUserById))
 			r.Get("/:id", wrap(GetUserById))
 			r.Get("/:id/orgs", wrap(GetUserOrgList))
 			r.Get("/:id/orgs", wrap(GetUserOrgList))
+			// query parameters /users/lookup?loginOrEmail=admin@example.com
+			r.Get("/lookup", wrap(GetUserByLoginOrEmail))
 			r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
 			r.Put("/:id", bind(m.UpdateUserCommand{}), wrap(UpdateUser))
 			r.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
 			r.Post("/:id/using/:orgId", wrap(UpdateUserActiveOrg))
 		}, reqGrafanaAdmin)
 		}, reqGrafanaAdmin)

+ 30 - 6
pkg/api/user.go

@@ -13,7 +13,7 @@ func GetSignedInUser(c *middleware.Context) Response {
 	return getUserUserProfile(c.UserId)
 	return getUserUserProfile(c.UserId)
 }
 }
 
 
-// GET /api/user/:id
+// GET /api/users/:id
 func GetUserById(c *middleware.Context) Response {
 func GetUserById(c *middleware.Context) Response {
 	return getUserUserProfile(c.ParamsInt64(":id"))
 	return getUserUserProfile(c.ParamsInt64(":id"))
 }
 }
@@ -22,12 +22,36 @@ func getUserUserProfile(userId int64) Response {
 	query := m.GetUserProfileQuery{UserId: userId}
 	query := m.GetUserProfileQuery{UserId: userId}
 
 
 	if err := bus.Dispatch(&query); err != nil {
 	if err := bus.Dispatch(&query); err != nil {
+		if err == m.ErrUserNotFound {
+			return ApiError(404, m.ErrUserNotFound.Error(), nil)
+		}
 		return ApiError(500, "Failed to get user", err)
 		return ApiError(500, "Failed to get user", err)
 	}
 	}
 
 
 	return Json(200, query.Result)
 	return Json(200, query.Result)
 }
 }
 
 
+// GET /api/users/lookup
+func GetUserByLoginOrEmail(c *middleware.Context) Response {
+	query := m.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
+	if err := bus.Dispatch(&query); err != nil {
+		if err == m.ErrUserNotFound {
+			return ApiError(404, m.ErrUserNotFound.Error(), nil)
+		}
+		return ApiError(500, "Failed to get user", err)
+	}
+	user := query.Result
+	result := m.UserProfileDTO{
+		Name:           user.Name,
+		Email:          user.Email,
+		Login:          user.Login,
+		Theme:          user.Theme,
+		IsGrafanaAdmin: user.IsAdmin,
+		OrgId:          user.OrgId,
+	}
+	return Json(200, &result)
+}
+
 // POST /api/user
 // POST /api/user
 func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
 func UpdateSignedInUser(c *middleware.Context, cmd m.UpdateUserCommand) Response {
 	if setting.AuthProxyEnabled {
 	if setting.AuthProxyEnabled {
@@ -60,7 +84,7 @@ func UpdateUserActiveOrg(c *middleware.Context) Response {
 	cmd := m.SetUsingOrgCommand{UserId: userId, OrgId: orgId}
 	cmd := m.SetUsingOrgCommand{UserId: userId, OrgId: orgId}
 
 
 	if err := bus.Dispatch(&cmd); err != nil {
 	if err := bus.Dispatch(&cmd); err != nil {
-		return ApiError(500, "Failed change active organization", err)
+		return ApiError(500, "Failed to change active organization", err)
 	}
 	}
 
 
 	return ApiSuccess("Active organization changed")
 	return ApiSuccess("Active organization changed")
@@ -70,12 +94,12 @@ func handleUpdateUser(cmd m.UpdateUserCommand) Response {
 	if len(cmd.Login) == 0 {
 	if len(cmd.Login) == 0 {
 		cmd.Login = cmd.Email
 		cmd.Login = cmd.Email
 		if len(cmd.Login) == 0 {
 		if len(cmd.Login) == 0 {
-			return ApiError(400, "Validation error, need specify either username or email", nil)
+			return ApiError(400, "Validation error, need to specify either username or email", nil)
 		}
 		}
 	}
 	}
 
 
 	if err := bus.Dispatch(&cmd); err != nil {
 	if err := bus.Dispatch(&cmd); err != nil {
-		return ApiError(500, "failed to update user", err)
+		return ApiError(500, "Failed to update user", err)
 	}
 	}
 
 
 	return ApiSuccess("User updated")
 	return ApiSuccess("User updated")
@@ -95,7 +119,7 @@ func getUserOrgList(userId int64) Response {
 	query := m.GetUserOrgListQuery{UserId: userId}
 	query := m.GetUserOrgListQuery{UserId: userId}
 
 
 	if err := bus.Dispatch(&query); err != nil {
 	if err := bus.Dispatch(&query); err != nil {
-		return ApiError(500, "Faile to get user organziations", err)
+		return ApiError(500, "Failed to get user organizations", err)
 	}
 	}
 
 
 	return Json(200, query.Result)
 	return Json(200, query.Result)
@@ -130,7 +154,7 @@ func UserSetUsingOrg(c *middleware.Context) Response {
 	cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
 	cmd := m.SetUsingOrgCommand{UserId: c.UserId, OrgId: orgId}
 
 
 	if err := bus.Dispatch(&cmd); err != nil {
 	if err := bus.Dispatch(&cmd); err != nil {
-		return ApiError(500, "Failed change active organization", err)
+		return ApiError(500, "Failed to change active organization", err)
 	}
 	}
 
 
 	return ApiSuccess("Active organization changed")
 	return ApiSuccess("Active organization changed")