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. // User errors.
const ( const (
ErrUserNotFound = Error("User not found") ErrUserNotFound = Error("User not found")
ErrAdminAlreadyInitialized = Error("Admin user already initialized")
) )
// Endpoint errors. // Endpoint errors.

View File

@ -227,6 +227,8 @@ func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.R
return return
} }
user, err := handler.UserService.User("admin")
if err == portainer.ErrUserNotFound {
user := &portainer.User{ user := &portainer.User{
Username: "admin", Username: "admin",
} }
@ -241,6 +243,14 @@ func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.R
Error(w, err, http.StatusInternalServerError, handler.Logger) Error(w, err, http.StatusInternalServerError, handler.Logger)
return 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 { type postAdminInitRequest struct {