From 9fa097d45f81f9be1114f7c5a54bff9ebecd8111 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Mon, 17 Jul 2023 21:23:35 -0300 Subject: [PATCH] fix(endpointgroups): add transactions support to the User model to avoid a nil pointer dereference EE-5328 (#9221) --- api/dataservices/user/tx.go | 63 +++++++++++++++++++++++++++++++++++ api/dataservices/user/user.go | 10 ++++++ api/datastore/services_tx.go | 10 ++++-- 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 api/dataservices/user/tx.go diff --git a/api/dataservices/user/tx.go b/api/dataservices/user/tx.go new file mode 100644 index 000000000..6c32ff4ad --- /dev/null +++ b/api/dataservices/user/tx.go @@ -0,0 +1,63 @@ +package user + +import ( + "errors" + "strings" + + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/dataservices" + dserrors "github.com/portainer/portainer/api/dataservices/errors" +) + +type ServiceTx struct { + dataservices.BaseDataServiceTx[portainer.User, portainer.UserID] +} + +// UserByUsername returns a user by username. +func (service ServiceTx) UserByUsername(username string) (*portainer.User, error) { + var u portainer.User + + err := service.Tx.GetAll( + BucketName, + &portainer.User{}, + dataservices.FirstFn(&u, func(e portainer.User) bool { + return strings.EqualFold(e.Username, username) + }), + ) + + if errors.Is(err, dataservices.ErrStop) { + return &u, nil + } + + if err == nil { + return nil, dserrors.ErrObjectNotFound + } + + return nil, err +} + +// UsersByRole return an array containing all the users with the specified role. +func (service ServiceTx) UsersByRole(role portainer.UserRole) ([]portainer.User, error) { + var users = make([]portainer.User, 0) + + return users, service.Tx.GetAll( + BucketName, + &portainer.User{}, + dataservices.FilterFn(&users, func(e portainer.User) bool { + return e.Role == role + }), + ) +} + +// CreateUser creates a new user. +func (service ServiceTx) Create(user *portainer.User) error { + return service.Tx.CreateObject( + BucketName, + func(id uint64) (int, interface{}) { + user.ID = portainer.UserID(id) + user.Username = strings.ToLower(user.Username) + + return int(user.ID), user + }, + ) +} diff --git a/api/dataservices/user/user.go b/api/dataservices/user/user.go index b3718395c..a71f0d7a1 100644 --- a/api/dataservices/user/user.go +++ b/api/dataservices/user/user.go @@ -32,6 +32,16 @@ func NewService(connection portainer.Connection) (*Service, error) { }, nil } +func (service *Service) Tx(tx portainer.Transaction) ServiceTx { + return ServiceTx{ + BaseDataServiceTx: dataservices.BaseDataServiceTx[portainer.User, portainer.UserID]{ + Bucket: BucketName, + Connection: service.Connection, + Tx: tx, + }, + } +} + // UserByUsername returns a user by username. func (service *Service) UserByUsername(username string) (*portainer.User, error) { var u portainer.User diff --git a/api/datastore/services_tx.go b/api/datastore/services_tx.go index dcedecec7..44375baaa 100644 --- a/api/datastore/services_tx.go +++ b/api/datastore/services_tx.go @@ -78,6 +78,10 @@ func (tx *StoreTx) TeamMembership() dataservices.TeamMembershipService { func (tx *StoreTx) Team() dataservices.TeamService { return nil } func (tx *StoreTx) TunnelServer() dataservices.TunnelServerService { return nil } -func (tx *StoreTx) User() dataservices.UserService { return nil } -func (tx *StoreTx) Version() dataservices.VersionService { return nil } -func (tx *StoreTx) Webhook() dataservices.WebhookService { return nil } + +func (tx *StoreTx) User() dataservices.UserService { + return tx.store.UserService.Tx(tx.tx) +} + +func (tx *StoreTx) Version() dataservices.VersionService { return nil } +func (tx *StoreTx) Webhook() dataservices.WebhookService { return nil }