mirror of https://github.com/portainer/portainer
fix(endpointgroups): add transactions support to the User model to avoid a nil pointer dereference EE-5328 (#9221)
parent
7acd1080ad
commit
9fa097d45f
|
@ -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
|
||||
},
|
||||
)
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
|
Loading…
Reference in New Issue