mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 21:39:40 +02:00
* feat(internal-auth): ability to set minimum password length [EE-3175] * pass props to react component * fixes + WIP slider * fix slider updating + add styles * remove nested ternary * fix slider updating + add remind me later button * add length to settings + value & onchange method * finish my account view * fix slider updating * slider styles * update style * move slider in * update size of slider * allow admin to browse to authentication view * use feather icons instead of font awesome * feat(settings): add colors to password rules * clean up tooltip styles * more style changes * styles * fixes + use requiredLength in password field for icon logic * simplify logic * simplify slider logic and remove debug code * use required length for logic to display pwd length warning * fix slider styles * use requiredPasswordLength to determine if password is valid * style tooltip based on theme * reset skips when password is changed * misc cleanup * reset skips when required length is changed * fix formatting * fix issues * implement some suggestions * simplify logic * update broken test * pick min password length from DB * fix suggestions * set up min password length in the DB * fix test after migration * fix formatting issue * fix bug with icon * refactored migration * fix typo * fixes * fix logic * set skips per user * reset skips for all users on length change Co-authored-by: Chaim Lev-Ari <chiptus@gmail.com> Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com>
114 lines
4.3 KiB
Go
114 lines
4.3 KiB
Go
package users
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
httperror "github.com/portainer/libhttp/error"
|
|
"github.com/portainer/libhttp/request"
|
|
"github.com/portainer/libhttp/response"
|
|
portainer "github.com/portainer/portainer/api"
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
|
"github.com/portainer/portainer/api/http/security"
|
|
)
|
|
|
|
type userCreatePayload struct {
|
|
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"`
|
|
}
|
|
|
|
func (payload *userCreatePayload) Validate(r *http.Request) error {
|
|
if govalidator.IsNull(payload.Username) || govalidator.Contains(payload.Username, " ") {
|
|
return errors.New("Invalid username. Must not contain any whitespace")
|
|
}
|
|
|
|
if payload.Role != 1 && payload.Role != 2 {
|
|
return errors.New("Invalid role value. Value must be one of: 1 (administrator) or 2 (regular user)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @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 ApiKeyAuth
|
|
// @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]
|
|
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 {
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to create user", httperrors.ErrResourceAccessDenied}
|
|
}
|
|
|
|
if securityContext.IsTeamLeader && payload.Role == 1 {
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to create administrator user", httperrors.ErrResourceAccessDenied}
|
|
}
|
|
|
|
user, err := handler.DataStore.User().UserByUsername(payload.Username)
|
|
if err != nil && !handler.DataStore.IsErrObjectNotFound(err) {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err}
|
|
}
|
|
if user != nil {
|
|
return &httperror.HandlerError{http.StatusConflict, "Another user with the same username already exists", errUserAlreadyExists}
|
|
}
|
|
|
|
user = &portainer.User{
|
|
Username: payload.Username,
|
|
Role: portainer.UserRole(payload.Role),
|
|
}
|
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
|
}
|
|
|
|
// when ldap/oauth is on, can only add users without password
|
|
if (settings.AuthenticationMethod == portainer.AuthenticationLDAP || settings.AuthenticationMethod == portainer.AuthenticationOAuth) && payload.Password != "" {
|
|
errMsg := "A user with password can not be created when authentication method is Oauth or LDAP"
|
|
return &httperror.HandlerError{http.StatusBadRequest, errMsg, errors.New(errMsg)}
|
|
}
|
|
|
|
if settings.AuthenticationMethod == portainer.AuthenticationInternal {
|
|
if !handler.passwordStrengthChecker.Check(payload.Password) {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Password does not meet the requirements", nil}
|
|
}
|
|
|
|
user.Password, err = handler.CryptoService.Hash(payload.Password)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to hash user password", errCryptoHashFailure}
|
|
}
|
|
}
|
|
|
|
err = handler.DataStore.User().Create(user)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err}
|
|
}
|
|
|
|
hideFields(user)
|
|
return response.JSON(w, user)
|
|
}
|