zrok/controller/listOrganizationMembers.go

51 lines
1.7 KiB
Go
Raw Permalink Normal View History

package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/admin"
"github.com/sirupsen/logrus"
)
type listOrganizationMembersHandler struct{}
func newListOrganizationMembersHandler() *listOrganizationMembersHandler {
return &listOrganizationMembersHandler{}
}
func (h *listOrganizationMembersHandler) Handle(params admin.ListOrganizationMembersParams, principal *rest_model_zrok.Principal) middleware.Responder {
if !principal.Admin {
logrus.Error("invalid admin principal")
return admin.NewListOrganizationMembersUnauthorized()
}
trx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return admin.NewListOrganizationMembersInternalServerError()
}
defer func() { _ = trx.Rollback() }()
org, err := str.FindOrganizationByToken(params.Body.Token, trx)
if err != nil {
logrus.Errorf("error finding organization by token: %v", err)
2024-12-09 22:42:30 +01:00
return admin.NewListOrganizationMembersNotFound()
}
if org == nil {
logrus.Errorf("organization '%v' not found", params.Body.Token)
return admin.NewListOrganizationMembersNotFound()
}
2024-12-09 22:42:30 +01:00
oms, err := str.FindAccountsForOrganization(org.Id, trx)
if err != nil {
logrus.Errorf("error finding accounts for organization: %v", err)
return admin.NewListOrganizationMembersInternalServerError()
}
var members []*admin.ListOrganizationMembersOKBodyMembersItems0
2024-12-09 22:42:30 +01:00
for _, om := range oms {
members = append(members, &admin.ListOrganizationMembersOKBodyMembersItems0{Email: om.Email, Admin: om.Admin})
}
return admin.NewListOrganizationMembersOK().WithPayload(&admin.ListOrganizationMembersOKBody{Members: members})
}