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"
|
2024-08-28 22:37:20 +00:00
|
|
|
"strings"
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2024-09-06 01:28:04 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
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 {
|
2024-08-28 22:37:20 +00:00
|
|
|
if len(payload.Username) == 0 || strings.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
|
|
|
}
|
2024-06-26 21:14:22 +00:00
|
|
|
|
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.
|
2022-06-03 04:44:42 +00:00
|
|
|
// @description Only administrators can create users.
|
2021-02-23 03:21:39 +00:00
|
|
|
// @description **Access policy**: restricted
|
|
|
|
// @tags users
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @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
|
2024-06-26 21:14:22 +00:00
|
|
|
if err := request.DecodeAndValidateJSONPayload(r, &payload); err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 01:28:04 +00:00
|
|
|
var user *portainer.User
|
|
|
|
|
|
|
|
if err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
|
|
var err error
|
|
|
|
user, err = handler.createUser(tx, payload)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
var httpErr *httperror.HandlerError
|
|
|
|
if errors.As(err, &httpErr) {
|
|
|
|
return httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unexpected error", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2024-09-06 01:28:04 +00:00
|
|
|
|
|
|
|
return response.JSON(w, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createUser(tx dataservices.DataStoreTx, payload userCreatePayload) (*portainer.User, error) {
|
|
|
|
user, err := tx.User().UserByUsername(payload.Username)
|
|
|
|
if err != nil && !tx.IsErrObjectNotFound(err) {
|
|
|
|
return nil, httperror.InternalServerError("Unable to retrieve users from the database", err)
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
if user != nil {
|
2024-09-06 01:28:04 +00:00
|
|
|
return nil, httperror.Conflict("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
|
|
|
}
|
|
|
|
|
2024-09-06 01:28:04 +00:00
|
|
|
settings, err := tx.Settings().Settings()
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2024-09-06 01:28:04 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to retrieve settings from the database", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2024-09-06 01:28:04 +00:00
|
|
|
// When LDAP/OAuth is on, can only add users without password
|
2022-01-13 05:27:26 +00:00
|
|
|
if (settings.AuthenticationMethod == portainer.AuthenticationLDAP || settings.AuthenticationMethod == portainer.AuthenticationOAuth) && payload.Password != "" {
|
2024-09-06 01:28:04 +00:00
|
|
|
errMsg := "a user with password can not be created when authentication method is Oauth or LDAP"
|
|
|
|
return nil, httperror.BadRequest(errMsg, errors.New(errMsg))
|
2022-01-13 05:27:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationInternal {
|
2022-06-03 04:00:13 +00:00
|
|
|
if !handler.passwordStrengthChecker.Check(payload.Password) {
|
2024-09-06 01:28:04 +00:00
|
|
|
return nil, httperror.BadRequest("Password does not meet the requirements", nil)
|
2022-04-14 01:45:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
user.Password, err = handler.CryptoService.Hash(payload.Password)
|
|
|
|
if err != nil {
|
2024-09-06 01:28:04 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to hash user password", errCryptoHashFailure)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 01:28:04 +00:00
|
|
|
if err := tx.User().Create(user); err != nil {
|
|
|
|
return nil, httperror.InternalServerError("Unable to persist user inside the database", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hideFields(user)
|
2024-06-26 21:14:22 +00:00
|
|
|
|
2024-09-06 01:28:04 +00:00
|
|
|
return user, nil
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|