2018-06-11 13:13:19 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
2020-06-14 23:48:58 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id UserDelete
|
|
|
|
// @summary Remove a user
|
|
|
|
// @description Remove a user.
|
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags users
|
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
|
|
|
// @param id path int true "User identifier"
|
|
|
|
// @success 204 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 403 "Permission denied"
|
|
|
|
// @failure 404 "User not found"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /users/{id} [delete]
|
2018-06-11 13:13:19 +00:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:48:58 +00:00
|
|
|
if userID == 1 {
|
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Cannot remove the initial admin account", errors.New("Cannot remove the initial admin account")}
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
tokenData, err := security.RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user authentication token", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenData.ID == portainer.UserID(userID) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Cannot remove your own user account. Contact another administrator", errAdminCannotRemoveSelf}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
user, err := handler.DataStore.User().User(portainer.UserID(userID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "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}
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:57:38 +00:00
|
|
|
if user.Role == portainer.AdministratorRole {
|
|
|
|
return handler.deleteAdminUser(w, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.deleteUser(w, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.User) *httperror.HandlerError {
|
2019-02-25 05:54:21 +00:00
|
|
|
if user.Password == "" {
|
|
|
|
return handler.deleteUser(w, user)
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
users, err := handler.DataStore.User().Users()
|
2018-07-23 04:57:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
localAdminCount := 0
|
|
|
|
for _, u := range users {
|
|
|
|
if u.Role == portainer.AdministratorRole && u.Password != "" {
|
|
|
|
localAdminCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if localAdminCount < 2 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Cannot remove local administrator user", errCannotRemoveLastLocalAdmin}
|
2018-07-23 04:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler.deleteUser(w, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) deleteUser(w http.ResponseWriter, user *portainer.User) *httperror.HandlerError {
|
2020-05-20 05:23:15 +00:00
|
|
|
err := handler.DataStore.User().DeleteUser(user.ID)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user from the database", err}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.TeamMembership().DeleteTeamMembershipByUserID(user.ID)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user memberships from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|