fix(api): check if admin user already exists when calling the /users/admin/init endpoint (#494)

pull/495/head
Anthony Lapenna 2017-01-12 18:17:28 +13:00 committed by GitHub
parent 2bdc9322de
commit 27e584fc14
2 changed files with 22 additions and 11 deletions

View File

@ -8,6 +8,7 @@ const (
// User errors.
const (
ErrUserNotFound = Error("User not found")
ErrAdminAlreadyInitialized = Error("Admin user already initialized")
)
// Endpoint errors.

View File

@ -227,6 +227,8 @@ func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.R
return
}
user, err := handler.UserService.User("admin")
if err == portainer.ErrUserNotFound {
user := &portainer.User{
Username: "admin",
}
@ -241,6 +243,14 @@ func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.R
Error(w, err, http.StatusInternalServerError, handler.Logger)
return
}
} else if err != nil {
Error(w, err, http.StatusInternalServerError, handler.Logger)
return
}
if user != nil {
Error(w, portainer.ErrAdminAlreadyInitialized, http.StatusForbidden, handler.Logger)
return
}
}
type postAdminInitRequest struct {