mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
chore(handlers): replace structs by functions for HTTP errors EE-4227 (#7664)
This commit is contained in:
parent
7accdf704c
commit
9ef5636718
157 changed files with 1123 additions and 1147 deletions
|
@ -29,27 +29,27 @@ import (
|
|||
func (handler *Handler) userDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
userID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid user identifier route variable", err}
|
||||
return httperror.BadRequest("Invalid user identifier route variable", err)
|
||||
}
|
||||
|
||||
if userID == 1 {
|
||||
return &httperror.HandlerError{http.StatusForbidden, "Cannot remove the initial admin account", errors.New("Cannot remove the initial admin account")}
|
||||
return httperror.Forbidden("Cannot remove the initial admin account", errors.New("Cannot remove the initial admin account"))
|
||||
}
|
||||
|
||||
tokenData, err := security.RetrieveTokenData(r)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user authentication token", err}
|
||||
return httperror.InternalServerError("Unable to retrieve user authentication token", err)
|
||||
}
|
||||
|
||||
if tokenData.ID == portainer.UserID(userID) {
|
||||
return &httperror.HandlerError{http.StatusForbidden, "Cannot remove your own user account. Contact another administrator", errAdminCannotRemoveSelf}
|
||||
return httperror.Forbidden("Cannot remove your own user account. Contact another administrator", errAdminCannotRemoveSelf)
|
||||
}
|
||||
|
||||
user, err := handler.DataStore.User().User(portainer.UserID(userID))
|
||||
if handler.DataStore.IsErrObjectNotFound(err) {
|
||||
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a user with the specified identifier inside the database", err}
|
||||
return httperror.NotFound("Unable to find a user with the specified identifier inside the database", err)
|
||||
} else if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a user with the specified identifier inside the database", err}
|
||||
return httperror.InternalServerError("Unable to find a user with the specified identifier inside the database", err)
|
||||
}
|
||||
|
||||
if user.Role == portainer.AdministratorRole {
|
||||
|
@ -66,7 +66,7 @@ func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.U
|
|||
|
||||
users, err := handler.DataStore.User().Users()
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err}
|
||||
return httperror.InternalServerError("Unable to retrieve users from the database", err)
|
||||
}
|
||||
|
||||
localAdminCount := 0
|
||||
|
@ -77,7 +77,7 @@ func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.U
|
|||
}
|
||||
|
||||
if localAdminCount < 2 {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Cannot remove local administrator user", errCannotRemoveLastLocalAdmin}
|
||||
return httperror.InternalServerError("Cannot remove local administrator user", errCannotRemoveLastLocalAdmin)
|
||||
}
|
||||
|
||||
return handler.deleteUser(w, user)
|
||||
|
@ -86,23 +86,23 @@ func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.U
|
|||
func (handler *Handler) deleteUser(w http.ResponseWriter, user *portainer.User) *httperror.HandlerError {
|
||||
err := handler.DataStore.User().DeleteUser(user.ID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user from the database", err}
|
||||
return httperror.InternalServerError("Unable to remove user from the database", err)
|
||||
}
|
||||
|
||||
err = handler.DataStore.TeamMembership().DeleteTeamMembershipByUserID(user.ID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user memberships from the database", err}
|
||||
return httperror.InternalServerError("Unable to remove user memberships from the database", err)
|
||||
}
|
||||
|
||||
// Remove all of the users persisted API keys
|
||||
apiKeys, err := handler.apiKeyService.GetAPIKeys(user.ID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user API keys from the database", err}
|
||||
return httperror.InternalServerError("Unable to retrieve user API keys from the database", err)
|
||||
}
|
||||
for _, k := range apiKeys {
|
||||
err = handler.apiKeyService.DeleteAPIKey(k.ID)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user API key from the database", err}
|
||||
return httperror.InternalServerError("Unable to remove user API key from the database", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue