Skip to content

Commit

Permalink
fix(api): check if admin user already exists when calling the /users/…
Browse files Browse the repository at this point in the history
…admin/init endpoint (#494)
  • Loading branch information
deviantony committed Jan 12, 2017
1 parent 2bdc932 commit 27e584f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const (

// User errors.
const (
ErrUserNotFound = Error("User not found")
ErrUserNotFound = Error("User not found")
ErrAdminAlreadyInitialized = Error("Admin user already initialized")
)

// Endpoint errors.
Expand Down
30 changes: 20 additions & 10 deletions api/http/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,28 @@ func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.R
return
}

user := &portainer.User{
Username: "admin",
}
user.Password, err = handler.CryptoService.Hash(req.Password)
if err != nil {
Error(w, portainer.ErrCryptoHashFailure, http.StatusBadRequest, handler.Logger)
user, err := handler.UserService.User("admin")
if err == portainer.ErrUserNotFound {
user := &portainer.User{
Username: "admin",
}
user.Password, err = handler.CryptoService.Hash(req.Password)
if err != nil {
Error(w, portainer.ErrCryptoHashFailure, http.StatusBadRequest, handler.Logger)
return
}

err = handler.UserService.UpdateUser(user)
if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
return
}
} else if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
return
}

err = handler.UserService.UpdateUser(user)
if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
if user != nil {
Error(w, portainer.ErrAdminAlreadyInitialized, http.StatusForbidden, handler.Logger)
return
}
}
Expand Down

0 comments on commit 27e584f

Please sign in to comment.