ServiceAccounts: Don't create new orgs for service accounts (#51819)

* Org: use constants for status codes

* ServiceAccounts: Avoid creating new orgs for service accounts

* Document createUserBehavior

* Update pkg/services/sqlstore/org_users_test.go

* add doc string to flag
This commit is contained in:
Jguer
2022-07-07 08:50:38 -04:00
committed by GitHub
parent 438c76252a
commit 9ab210a7d7
7 changed files with 136 additions and 31 deletions
+14 -14
View File
@@ -34,10 +34,10 @@ func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) response.Response {
org, err := hs.SQLStore.GetOrgByName(web.Params(c.Req)[":name"])
if err != nil {
if errors.Is(err, models.ErrOrgNotFound) {
return response.Error(404, "Organization not found", err)
return response.Error(http.StatusNotFound, "Organization not found", err)
}
return response.Error(500, "Failed to get organization", err)
return response.Error(http.StatusInternalServerError, "Failed to get organization", err)
}
result := models.OrgDetailsDTO{
Id: org.Id,
@@ -60,9 +60,9 @@ func (hs *HTTPServer) getOrgHelper(ctx context.Context, orgID int64) response.Re
if err := hs.SQLStore.GetOrgById(ctx, &query); err != nil {
if errors.Is(err, models.ErrOrgNotFound) {
return response.Error(404, "Organization not found", err)
return response.Error(http.StatusNotFound, "Organization not found", err)
}
return response.Error(500, "Failed to get organization", err)
return response.Error(http.StatusInternalServerError, "Failed to get organization", err)
}
org := query.Result
@@ -90,15 +90,15 @@ func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
}
acEnabled := !hs.AccessControl.IsDisabled()
if !acEnabled && !(setting.AllowUserOrgCreate || c.IsGrafanaAdmin) {
return response.Error(403, "Access denied", nil)
return response.Error(http.StatusForbidden, "Access denied", nil)
}
cmd.UserId = c.UserId
if err := hs.SQLStore.CreateOrg(c.Req.Context(), &cmd); err != nil {
if errors.Is(err, models.ErrOrgNameTaken) {
return response.Error(409, "Organization name taken", err)
return response.Error(http.StatusConflict, "Organization name taken", err)
}
return response.Error(500, "Failed to create organization", err)
return response.Error(http.StatusInternalServerError, "Failed to create organization", err)
}
metrics.MApiOrgCreate.Inc()
@@ -135,9 +135,9 @@ func (hs *HTTPServer) updateOrgHelper(ctx context.Context, form dtos.UpdateOrgFo
cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
if err := hs.SQLStore.UpdateOrg(ctx, &cmd); err != nil {
if errors.Is(err, models.ErrOrgNameTaken) {
return response.Error(400, "Organization name taken", err)
return response.Error(http.StatusBadRequest, "Organization name taken", err)
}
return response.Error(500, "Failed to update organization", err)
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
}
return response.Success("Organization updated")
@@ -179,7 +179,7 @@ func (hs *HTTPServer) updateOrgAddressHelper(ctx context.Context, form dtos.Upda
}
if err := hs.SQLStore.UpdateOrgAddress(ctx, &cmd); err != nil {
return response.Error(500, "Failed to update org address", err)
return response.Error(http.StatusInternalServerError, "Failed to update org address", err)
}
return response.Success("Address updated")
@@ -193,14 +193,14 @@ func (hs *HTTPServer) DeleteOrgByID(c *models.ReqContext) response.Response {
}
// before deleting an org, check if user does not belong to the current org
if c.OrgId == orgID {
return response.Error(400, "Can not delete org for current user", nil)
return response.Error(http.StatusBadRequest, "Can not delete org for current user", nil)
}
if err := hs.SQLStore.DeleteOrg(c.Req.Context(), &models.DeleteOrgCommand{Id: orgID}); err != nil {
if errors.Is(err, models.ErrOrgNotFound) {
return response.Error(404, "Failed to delete organization. ID not found", nil)
return response.Error(http.StatusNotFound, "Failed to delete organization. ID not found", nil)
}
return response.Error(500, "Failed to update organization", err)
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
}
return response.Success("Organization deleted")
}
@@ -221,7 +221,7 @@ func (hs *HTTPServer) SearchOrgs(c *models.ReqContext) response.Response {
}
if err := hs.SQLStore.SearchOrgs(c.Req.Context(), &query); err != nil {
return response.Error(500, "Failed to search orgs", err)
return response.Error(http.StatusInternalServerError, "Failed to search orgs", err)
}
return response.JSON(http.StatusOK, query.Result)