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"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2020-06-16 07:58:16 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type adminInitPayload struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *adminInitPayload) 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 govalidator.IsNull(payload.Password) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid password")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST request on /api/users/admin/init
|
|
|
|
func (handler *Handler) adminInit(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload adminInitPayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
users, err := handler.DataStore.User().UsersByRole(portainer.AdministratorRole)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(users) != 0 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return &httperror.HandlerError{http.StatusConflict, "Unable to create administrator user", errAdminAlreadyInitialized}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := &portainer.User{
|
2019-09-09 22:58:26 +00:00
|
|
|
Username: payload.Username,
|
|
|
|
Role: portainer.AdministratorRole,
|
2020-06-16 07:58:16 +00:00
|
|
|
PortainerAuthorizations: authorization.DefaultPortainerAuthorizations(),
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, user)
|
|
|
|
}
|