Chore: Port user services to identity.Requester (#73851)
* port api key api to signedinuser * port users to signed in user interface * fix tests
This commit is contained in:
+12
-12
@@ -32,11 +32,11 @@ import (
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetAPIKeys(c *contextmodel.ReqContext) response.Response {
|
||||
query := apikey.GetApiKeysQuery{OrgID: c.OrgID, User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")}
|
||||
query := apikey.GetApiKeysQuery{OrgID: c.SignedInUser.GetOrgID(), User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")}
|
||||
|
||||
keys, err := hs.apiKeyService.GetAPIKeys(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to list api keys", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to list api keys", err)
|
||||
}
|
||||
|
||||
ids := map[string]bool{}
|
||||
@@ -87,7 +87,7 @@ func (hs *HTTPServer) DeleteAPIKey(c *contextmodel.ReqContext) response.Response
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
|
||||
cmd := &apikey.DeleteCommand{ID: id, OrgID: c.OrgID}
|
||||
cmd := &apikey.DeleteCommand{ID: id, OrgID: c.SignedInUser.GetOrgID()}
|
||||
err = hs.apiKeyService.DeleteApiKey(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
var status int
|
||||
@@ -126,38 +126,38 @@ func (hs *HTTPServer) AddAPIKey(c *contextmodel.ReqContext) response.Response {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
if !cmd.Role.IsValid() {
|
||||
return response.Error(400, "Invalid role specified", nil)
|
||||
return response.Error(http.StatusBadRequest, "Invalid role specified", nil)
|
||||
}
|
||||
if !c.OrgRole.Includes(cmd.Role) {
|
||||
if !c.SignedInUser.GetOrgRole().Includes(cmd.Role) {
|
||||
return response.Error(http.StatusForbidden, "Cannot assign a role higher than user's role", nil)
|
||||
}
|
||||
|
||||
if hs.Cfg.ApiKeyMaxSecondsToLive != -1 {
|
||||
if cmd.SecondsToLive == 0 {
|
||||
return response.Error(400, "Number of seconds before expiration should be set", nil)
|
||||
return response.Error(http.StatusBadRequest, "Number of seconds before expiration should be set", nil)
|
||||
}
|
||||
if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive {
|
||||
return response.Error(400, "Number of seconds before expiration is greater than the global limit", nil)
|
||||
return response.Error(http.StatusBadRequest, "Number of seconds before expiration is greater than the global limit", nil)
|
||||
}
|
||||
}
|
||||
|
||||
cmd.OrgID = c.OrgID
|
||||
cmd.OrgID = c.SignedInUser.GetOrgID()
|
||||
|
||||
newKeyInfo, err := apikeygen.New(cmd.OrgID, cmd.Name)
|
||||
if err != nil {
|
||||
return response.Error(500, "Generating API key failed", err)
|
||||
return response.Error(http.StatusInternalServerError, "Generating API key failed", err)
|
||||
}
|
||||
|
||||
cmd.Key = newKeyInfo.HashedKey
|
||||
key, err := hs.apiKeyService.AddAPIKey(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, apikey.ErrInvalidExpiration) {
|
||||
return response.Error(400, err.Error(), nil)
|
||||
return response.Error(http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
if errors.Is(err, apikey.ErrDuplicate) {
|
||||
return response.Error(409, err.Error(), nil)
|
||||
return response.Error(http.StatusConflict, err.Error(), nil)
|
||||
}
|
||||
return response.Error(500, "Failed to add API Key", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to add API Key", err)
|
||||
}
|
||||
|
||||
result := &dtos.NewApiKeyResult{
|
||||
|
||||
Reference in New Issue
Block a user