2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-05 08:58:34 +02:00
|
|
|
|
|
|
|
import (
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-05-05 08:58:34 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserHandler struct {
|
|
|
|
accountManager server.AccountManager
|
|
|
|
authAudience string
|
|
|
|
jwtExtractor jwtclaims.ClaimsExtractor
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserHandler(accountManager server.AccountManager, authAudience string) *UserHandler {
|
|
|
|
return &UserHandler{
|
|
|
|
accountManager: accountManager,
|
|
|
|
authAudience: authAudience,
|
|
|
|
jwtExtractor: *jwtclaims.NewClaimsExtractor(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsers returns a list of users of the account this user belongs to.
|
|
|
|
// It also gathers additional user data (like email and name) from the IDP manager.
|
2022-06-14 10:32:54 +02:00
|
|
|
func (h *UserHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
2022-05-05 08:58:34 +02:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
account, err := getJWTAccount(h.accountManager, h.jwtExtractor, h.authAudience, r)
|
2022-05-05 08:58:34 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
data, err := h.accountManager.GetUsersFromAccount(account.Id)
|
2022-05-05 08:58:34 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
users := []*api.User{}
|
2022-05-21 17:27:04 +02:00
|
|
|
for _, r := range data {
|
|
|
|
users = append(users, toUserResponse(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
writeJSONObject(w, users)
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func toUserResponse(user *server.UserInfo) *api.User {
|
|
|
|
return &api.User{
|
|
|
|
Id: user.ID,
|
2022-05-21 17:27:04 +02:00
|
|
|
Name: user.Name,
|
|
|
|
Email: user.Email,
|
|
|
|
Role: user.Role,
|
|
|
|
}
|
2022-05-05 08:58:34 +02:00
|
|
|
}
|