zrok/controller/updateShare.go

98 lines
3.1 KiB
Go
Raw Normal View History

2022-12-02 19:21:05 +01:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/share"
2022-12-02 19:21:05 +01:00
"github.com/sirupsen/logrus"
)
type updateShareHandler struct{}
func newUpdateShareHandler() *updateShareHandler {
return &updateShareHandler{}
}
func (h *updateShareHandler) Handle(params share.UpdateShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
shrToken := params.Body.ShrToken
2022-12-02 19:21:05 +01:00
backendProxyEndpoint := params.Body.BackendProxyEndpoint
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
defer func() { _ = tx.Rollback() }()
sshr, err := str.FindShareWithToken(shrToken, tx)
2022-12-02 19:21:05 +01:00
if err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("share '%v' not found: %v", shrToken, err)
return share.NewUpdateShareNotFound()
2022-12-02 19:21:05 +01:00
}
senvs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
envFound := false
for _, senv := range senvs {
2023-06-14 19:18:44 +02:00
if senv.Id == sshr.EnvironmentId {
2022-12-02 19:21:05 +01:00
envFound = true
break
}
}
if !envFound {
2023-01-04 20:21:23 +01:00
logrus.Errorf("environment not found for share '%v'", shrToken)
return share.NewUpdateShareNotFound()
2022-12-02 19:21:05 +01:00
}
doCommit := false
if backendProxyEndpoint != "" {
sshr.BackendProxyEndpoint = &backendProxyEndpoint
if err := str.UpdateShare(sshr, tx); err != nil {
logrus.Errorf("error updating share '%v': %v", shrToken, err)
return share.NewUpdateShareInternalServerError()
}
doCommit = true
2022-12-02 19:21:05 +01:00
}
for _, addr := range params.Body.AddAccessGrants {
acct, err := str.FindAccountWithEmail(addr, tx)
if err != nil {
logrus.Errorf("error looking up account by email '%v' for user '%v': %v", addr, principal.Email, err)
return share.NewUpdateShareBadRequest()
}
if _, err := str.CreateAccessGrant(sshr.Id, acct.Id, tx); err != nil {
logrus.Errorf("error adding access grant '%v' for share '%v': %v", acct.Email, shrToken, err)
return share.NewUpdateShareInternalServerError()
}
logrus.Infof("added access grant '%v' to share '%v'", acct.Email, shrToken)
doCommit = true
}
for _, addr := range params.Body.RemoveAccessGrants {
acct, err := str.FindAccountWithEmail(addr, tx)
if err != nil {
logrus.Errorf("error looking up account by email '%v' for user '%v': %v", addr, principal.Email, err)
return share.NewUpdateShareBadRequest()
}
if err := str.DeleteAccessGrantsForShareAndAccount(sshr.Id, acct.Id, tx); err != nil {
logrus.Errorf("error removing access grant '%v' for share '%v': %v", acct.Email, shrToken, err)
return share.NewUpdateShareInternalServerError()
}
logrus.Infof("removed access grant '%v' from share '%v'", acct.Email, shrToken)
doCommit = true
}
if doCommit {
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing transaction for share '%v' update: %v", shrToken, err)
return share.NewUpdateShareInternalServerError()
}
2022-12-02 19:21:05 +01:00
}
return share.NewUpdateShareOK()
2022-12-02 19:21:05 +01:00
}