2018-06-11 13:13:19 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
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"
|
|
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type userCreatePayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
Username string `validate:"required" example:"bob"`
|
|
|
|
Password string `validate:"required" example:"cg9Wgky3"`
|
|
|
|
// User role (1 for administrator account and 2 for regular account)
|
|
|
|
Role int `validate:"required" enums:"1,2" example:"2"`
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *userCreatePayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Username) || govalidator.Contains(payload.Username, " ") {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid username. Must not contain any whitespace")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Role != 1 && payload.Role != 2 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid role value. Value must be one of: 1 (administrator) or 2 (regular user)")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id UserCreate
|
|
|
|
// @summary Create a new user
|
|
|
|
// @description Create a new Portainer user.
|
|
|
|
// @description Only team leaders and administrators can create users.
|
|
|
|
// @description Only administrators can create an administrator user account.
|
|
|
|
// @description **Access policy**: restricted
|
|
|
|
// @tags users
|
|
|
|
// @security jwt
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
|
|
|
// @param body body userCreatePayload true "User details"
|
|
|
|
// @success 200 {object} portainer.User "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 403 "Permission denied"
|
|
|
|
// @failure 409 "User already exists"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /users [post]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) userCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload userCreatePayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !securityContext.IsAdmin && !securityContext.IsTeamLeader {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to create user", httperrors.ErrResourceAccessDenied}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if securityContext.IsTeamLeader && payload.Role == 1 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to create administrator user", httperrors.ErrResourceAccessDenied}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
user, err := handler.DataStore.User().UserByUsername(payload.Username)
|
2020-07-07 21:57:52 +00:00
|
|
|
if err != nil && err != bolterrors.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err}
|
|
|
|
}
|
|
|
|
if user != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusConflict, "Another user with the same username already exists", errUserAlreadyExists}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user = &portainer.User{
|
2020-08-11 05:41:37 +00:00
|
|
|
Username: payload.Username,
|
|
|
|
Role: portainer.UserRole(payload.Role),
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationInternal {
|
|
|
|
user.Password, err = handler.CryptoService.Hash(payload.Password)
|
|
|
|
if err != nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to hash user password", errCryptoHashFailure}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.User().CreateUser(user)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideFields(user)
|
|
|
|
return response.JSON(w, user)
|
|
|
|
}
|