mirror of
https://github.com/grafana/grafana.git
synced 2026-09-05 04:40:13 -05:00
Chore: Use AddUserOrg from org service (#55657)
* Chore: Copy methods from sqlstore to org store * Rename method, add test * Add comments of tests * Chore: Add methods from sqlstore to org service interface * Avoiding import cycle * Add and remove some methods * User AddOrgUSer from org service in api * Fix test function calls
This commit is contained in:
+10
-9
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
@@ -31,11 +32,11 @@ import (
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) AddOrgUserToCurrentOrg(c *models.ReqContext) response.Response {
|
||||
cmd := models.AddOrgUserCommand{}
|
||||
cmd := org.AddOrgUserCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
cmd.OrgId = c.OrgID
|
||||
cmd.OrgID = c.OrgID
|
||||
return hs.addOrgUserHelper(c, cmd)
|
||||
}
|
||||
|
||||
@@ -54,20 +55,20 @@ func (hs *HTTPServer) AddOrgUserToCurrentOrg(c *models.ReqContext) response.Resp
|
||||
// 403: forbiddenError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) AddOrgUser(c *models.ReqContext) response.Response {
|
||||
cmd := models.AddOrgUserCommand{}
|
||||
cmd := org.AddOrgUserCommand{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
var err error
|
||||
cmd.OrgId, err = strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||
cmd.OrgID, err = strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
||||
}
|
||||
return hs.addOrgUserHelper(c, cmd)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) addOrgUserHelper(c *models.ReqContext, cmd models.AddOrgUserCommand) response.Response {
|
||||
func (hs *HTTPServer) addOrgUserHelper(c *models.ReqContext, cmd org.AddOrgUserCommand) response.Response {
|
||||
if !cmd.Role.IsValid() {
|
||||
return response.Error(400, "Invalid role specified", nil)
|
||||
}
|
||||
@@ -81,13 +82,13 @@ func (hs *HTTPServer) addOrgUserHelper(c *models.ReqContext, cmd models.AddOrgUs
|
||||
return response.Error(404, "User not found", nil)
|
||||
}
|
||||
|
||||
cmd.UserId = userToAdd.ID
|
||||
cmd.UserID = userToAdd.ID
|
||||
|
||||
if err := hs.SQLStore.AddOrgUser(c.Req.Context(), &cmd); err != nil {
|
||||
if err := hs.orgService.AddOrgUser(c.Req.Context(), &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||
return response.JSON(409, util.DynMap{
|
||||
"message": "User is already member of this organization",
|
||||
"userId": cmd.UserId,
|
||||
"userId": cmd.UserID,
|
||||
})
|
||||
}
|
||||
return response.Error(500, "Could not add user to organization", err)
|
||||
@@ -95,7 +96,7 @@ func (hs *HTTPServer) addOrgUserHelper(c *models.ReqContext, cmd models.AddOrgUs
|
||||
|
||||
return response.JSON(http.StatusOK, util.DynMap{
|
||||
"message": "User added to organization",
|
||||
"userId": cmd.UserId,
|
||||
"userId": cmd.UserID,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user