1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-10 16:25:22 +02:00

migrate user dataservice

This commit is contained in:
Prabhat Khera 2023-05-12 10:55:42 +12:00
parent 74615bff91
commit cc1b8c05d4
15 changed files with 114 additions and 182 deletions

View file

@ -73,10 +73,10 @@ func (service *Service) endpoints() ([]portainer.Endpoint, error) {
var endpoints []portainer.Endpoint var endpoints []portainer.Endpoint
var err error var err error
err = service.connection.ViewTx(func(tx portainer.Transaction) error { // err = service.connection.ViewTx(func(tx portainer.Transaction) error {
endpoints, err = service.Tx(tx).Endpoints() // endpoints, err = service.Tx(tx).Endpoints()
return err // return err
}) // })
return endpoints, err return endpoints, err
} }

View file

@ -57,7 +57,6 @@ type (
Create(customTemplate *portainer.CustomTemplate) error Create(customTemplate *portainer.CustomTemplate) error
UpdateCustomTemplate(ID portainer.CustomTemplateID, customTemplate *portainer.CustomTemplate) error UpdateCustomTemplate(ID portainer.CustomTemplateID, customTemplate *portainer.CustomTemplate) error
DeleteCustomTemplate(ID portainer.CustomTemplateID) error DeleteCustomTemplate(ID portainer.CustomTemplateID) error
BucketName() string
} }
// EdgeGroupService represents a service to manage Edge groups // EdgeGroupService represents a service to manage Edge groups
@ -68,7 +67,6 @@ type (
UpdateEdgeGroup(ID portainer.EdgeGroupID, group *portainer.EdgeGroup) error UpdateEdgeGroup(ID portainer.EdgeGroupID, group *portainer.EdgeGroup) error
UpdateEdgeGroupFunc(ID portainer.EdgeGroupID, updateFunc func(group *portainer.EdgeGroup)) error UpdateEdgeGroupFunc(ID portainer.EdgeGroupID, updateFunc func(group *portainer.EdgeGroup)) error
DeleteEdgeGroup(ID portainer.EdgeGroupID) error DeleteEdgeGroup(ID portainer.EdgeGroupID) error
BucketName() string
} }
// EdgeJobService represents a service to manage Edge jobs // EdgeJobService represents a service to manage Edge jobs
@ -79,7 +77,6 @@ type (
UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error
UpdateEdgeJobFunc(ID portainer.EdgeJobID, updateFunc func(edgeJob *portainer.EdgeJob)) error UpdateEdgeJobFunc(ID portainer.EdgeJobID, updateFunc func(edgeJob *portainer.EdgeJob)) error
DeleteEdgeJob(ID portainer.EdgeJobID) error DeleteEdgeJob(ID portainer.EdgeJobID) error
BucketName() string
} }
// EdgeStackService represents a service to manage Edge stacks // EdgeStackService represents a service to manage Edge stacks
@ -92,7 +89,6 @@ type (
UpdateEdgeStack(ID portainer.EdgeStackID, edgeStack *portainer.EdgeStack) error UpdateEdgeStack(ID portainer.EdgeStackID, edgeStack *portainer.EdgeStack) error
UpdateEdgeStackFunc(ID portainer.EdgeStackID, updateFunc func(edgeStack *portainer.EdgeStack)) error UpdateEdgeStackFunc(ID portainer.EdgeStackID, updateFunc func(edgeStack *portainer.EdgeStack)) error
DeleteEdgeStack(ID portainer.EdgeStackID) error DeleteEdgeStack(ID portainer.EdgeStackID) error
BucketName() string
} }
// EndpointService represents a service for managing environment(endpoint) data // EndpointService represents a service for managing environment(endpoint) data
@ -105,7 +101,6 @@ type (
Create(endpoint *portainer.Endpoint) error Create(endpoint *portainer.Endpoint) error
UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error
DeleteEndpoint(ID portainer.EndpointID) error DeleteEndpoint(ID portainer.EndpointID) error
BucketName() string
} }
// EndpointGroupService represents a service for managing environment(endpoint) group data // EndpointGroupService represents a service for managing environment(endpoint) group data
@ -115,7 +110,6 @@ type (
Create(group *portainer.EndpointGroup) error Create(group *portainer.EndpointGroup) error
UpdateEndpointGroup(ID portainer.EndpointGroupID, group *portainer.EndpointGroup) error UpdateEndpointGroup(ID portainer.EndpointGroupID, group *portainer.EndpointGroup) error
DeleteEndpointGroup(ID portainer.EndpointGroupID) error DeleteEndpointGroup(ID portainer.EndpointGroupID) error
BucketName() string
} }
// EndpointRelationService represents a service for managing environment(endpoint) relations data // EndpointRelationService represents a service for managing environment(endpoint) relations data
@ -125,7 +119,6 @@ type (
Create(endpointRelation *portainer.EndpointRelation) error Create(endpointRelation *portainer.EndpointRelation) error
UpdateEndpointRelation(EndpointID portainer.EndpointID, endpointRelation *portainer.EndpointRelation) error UpdateEndpointRelation(EndpointID portainer.EndpointID, endpointRelation *portainer.EndpointRelation) error
DeleteEndpointRelation(EndpointID portainer.EndpointID) error DeleteEndpointRelation(EndpointID portainer.EndpointID) error
BucketName() string
} }
// FDOProfileService represents a service to manage FDO Profiles // FDOProfileService represents a service to manage FDO Profiles
@ -135,7 +128,6 @@ type (
Create(FDOProfile *portainer.FDOProfile) error Create(FDOProfile *portainer.FDOProfile) error
Update(ID portainer.FDOProfileID, FDOProfile *portainer.FDOProfile) error Update(ID portainer.FDOProfileID, FDOProfile *portainer.FDOProfile) error
Delete(ID portainer.FDOProfileID) error Delete(ID portainer.FDOProfileID) error
BucketName() string
} }
// HelmUserRepositoryService represents a service to manage HelmUserRepositories // HelmUserRepositoryService represents a service to manage HelmUserRepositories
@ -145,7 +137,6 @@ type (
Create(record *portainer.HelmUserRepository) error Create(record *portainer.HelmUserRepository) error
UpdateHelmUserRepository(ID portainer.HelmUserRepositoryID, repository *portainer.HelmUserRepository) error UpdateHelmUserRepository(ID portainer.HelmUserRepositoryID, repository *portainer.HelmUserRepository) error
DeleteHelmUserRepository(ID portainer.HelmUserRepositoryID) error DeleteHelmUserRepository(ID portainer.HelmUserRepositoryID) error
BucketName() string
} }
// JWTService represents a service for managing JWT tokens // JWTService represents a service for managing JWT tokens
@ -164,7 +155,6 @@ type (
Create(registry *portainer.Registry) error Create(registry *portainer.Registry) error
UpdateRegistry(ID portainer.RegistryID, registry *portainer.Registry) error UpdateRegistry(ID portainer.RegistryID, registry *portainer.Registry) error
DeleteRegistry(ID portainer.RegistryID) error DeleteRegistry(ID portainer.RegistryID) error
BucketName() string
} }
// ResourceControlService represents a service for managing resource control data // ResourceControlService represents a service for managing resource control data
@ -175,7 +165,6 @@ type (
Create(rc *portainer.ResourceControl) error Create(rc *portainer.ResourceControl) error
UpdateResourceControl(ID portainer.ResourceControlID, resourceControl *portainer.ResourceControl) error UpdateResourceControl(ID portainer.ResourceControlID, resourceControl *portainer.ResourceControl) error
DeleteResourceControl(ID portainer.ResourceControlID) error DeleteResourceControl(ID portainer.ResourceControlID) error
BucketName() string
} }
// RoleService represents a service for managing user roles // RoleService represents a service for managing user roles
@ -184,7 +173,6 @@ type (
Roles() ([]portainer.Role, error) Roles() ([]portainer.Role, error)
Create(role *portainer.Role) error Create(role *portainer.Role) error
UpdateRole(ID portainer.RoleID, role *portainer.Role) error UpdateRole(ID portainer.RoleID, role *portainer.Role) error
BucketName() string
} }
// APIKeyRepositoryService // APIKeyRepositoryService
@ -201,7 +189,6 @@ type (
SettingsService interface { SettingsService interface {
Settings() (*portainer.Settings, error) Settings() (*portainer.Settings, error)
UpdateSettings(settings *portainer.Settings) error UpdateSettings(settings *portainer.Settings) error
BucketName() string
} }
SnapshotService interface { SnapshotService interface {
@ -210,14 +197,12 @@ type (
UpdateSnapshot(snapshot *portainer.Snapshot) error UpdateSnapshot(snapshot *portainer.Snapshot) error
DeleteSnapshot(endpointID portainer.EndpointID) error DeleteSnapshot(endpointID portainer.EndpointID) error
Create(snapshot *portainer.Snapshot) error Create(snapshot *portainer.Snapshot) error
BucketName() string
} }
// SSLSettingsService represents a service for managing application settings // SSLSettingsService represents a service for managing application settings
SSLSettingsService interface { SSLSettingsService interface {
Settings() (*portainer.SSLSettings, error) Settings() (*portainer.SSLSettings, error)
UpdateSettings(settings *portainer.SSLSettings) error UpdateSettings(settings *portainer.SSLSettings) error
BucketName() string
} }
// StackService represents a service for managing stack data // StackService represents a service for managing stack data
@ -231,7 +216,6 @@ type (
DeleteStack(ID portainer.StackID) error DeleteStack(ID portainer.StackID) error
StackByWebhookID(ID string) (*portainer.Stack, error) StackByWebhookID(ID string) (*portainer.Stack, error)
RefreshableStacks() ([]portainer.Stack, error) RefreshableStacks() ([]portainer.Stack, error)
BucketName() string
} }
// TagService represents a service for managing tag data // TagService represents a service for managing tag data
@ -242,7 +226,6 @@ type (
UpdateTag(ID portainer.TagID, tag *portainer.Tag) error UpdateTag(ID portainer.TagID, tag *portainer.Tag) error
UpdateTagFunc(ID portainer.TagID, updateFunc func(tag *portainer.Tag)) error UpdateTagFunc(ID portainer.TagID, updateFunc func(tag *portainer.Tag)) error
DeleteTag(ID portainer.TagID) error DeleteTag(ID portainer.TagID) error
BucketName() string
} }
// TeamService represents a service for managing user data // TeamService represents a service for managing user data
@ -253,7 +236,6 @@ type (
Create(team *portainer.Team) error Create(team *portainer.Team) error
UpdateTeam(ID portainer.TeamID, team *portainer.Team) error UpdateTeam(ID portainer.TeamID, team *portainer.Team) error
DeleteTeam(ID portainer.TeamID) error DeleteTeam(ID portainer.TeamID) error
BucketName() string
} }
// TeamMembershipService represents a service for managing team membership data // TeamMembershipService represents a service for managing team membership data
@ -267,7 +249,6 @@ type (
DeleteTeamMembership(ID portainer.TeamMembershipID) error DeleteTeamMembership(ID portainer.TeamMembershipID) error
DeleteTeamMembershipByUserID(userID portainer.UserID) error DeleteTeamMembershipByUserID(userID portainer.UserID) error
DeleteTeamMembershipByTeamID(teamID portainer.TeamID) error DeleteTeamMembershipByTeamID(teamID portainer.TeamID) error
BucketName() string
DeleteTeamMembershipByTeamIDAndUserID(teamID portainer.TeamID, userID portainer.UserID) error DeleteTeamMembershipByTeamIDAndUserID(teamID portainer.TeamID, userID portainer.UserID) error
} }
@ -275,7 +256,6 @@ type (
TunnelServerService interface { TunnelServerService interface {
Info() (*portainer.TunnelServerInfo, error) Info() (*portainer.TunnelServerInfo, error)
UpdateInfo(info *portainer.TunnelServerInfo) error UpdateInfo(info *portainer.TunnelServerInfo) error
BucketName() string
} }
// UserService represents a service for managing user data // UserService represents a service for managing user data
@ -287,7 +267,6 @@ type (
Create(user *portainer.User) error Create(user *portainer.User) error
UpdateUser(ID portainer.UserID, user *portainer.User) error UpdateUser(ID portainer.UserID, user *portainer.User) error
DeleteUser(ID portainer.UserID) error DeleteUser(ID portainer.UserID) error
BucketName() string
} }
// VersionService represents a service for managing version data // VersionService represents a service for managing version data
@ -310,7 +289,6 @@ type (
WebhookByResourceID(resourceID string) (*portainer.Webhook, error) WebhookByResourceID(resourceID string) (*portainer.Webhook, error)
WebhookByToken(token string) (*portainer.Webhook, error) WebhookByToken(token string) (*portainer.Webhook, error)
DeleteWebhook(ID portainer.WebhookID) error DeleteWebhook(ID portainer.WebhookID) error
BucketName() string
} }
) )

View file

@ -19,12 +19,12 @@ func (service ServiceTx) BucketName() string {
// Registry returns a registry by ID. // Registry returns a registry by ID.
func (service ServiceTx) Registry(ID portainer.RegistryID) (*portainer.Registry, error) { func (service ServiceTx) Registry(ID portainer.RegistryID) (*portainer.Registry, error) {
var registry portainer.Registry var registry portainer.Registry
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
err := service.tx.GetObject(BucketName, identifier, &registry) // err := service.tx.GetObject(BucketName, identifier, &registry)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return &registry, nil return &registry, nil
} }
@ -64,12 +64,14 @@ func (service ServiceTx) Create(registry *portainer.Registry) error {
// UpdateRegistry updates a registry. // UpdateRegistry updates a registry.
func (service ServiceTx) UpdateRegistry(ID portainer.RegistryID, registry *portainer.Registry) error { func (service ServiceTx) UpdateRegistry(ID portainer.RegistryID, registry *portainer.Registry) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.UpdateObject(BucketName, identifier, registry) // return service.tx.UpdateObject(BucketName, identifier, registry)
return nil
} }
// DeleteRegistry deletes a registry. // DeleteRegistry deletes a registry.
func (service ServiceTx) DeleteRegistry(ID portainer.RegistryID) error { func (service ServiceTx) DeleteRegistry(ID portainer.RegistryID) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.DeleteObject(BucketName, identifier) // return service.tx.DeleteObject(BucketName, identifier)
return nil
} }

View file

@ -21,12 +21,12 @@ func (service ServiceTx) BucketName() string {
// ResourceControl returns a ResourceControl object by ID // ResourceControl returns a ResourceControl object by ID
func (service ServiceTx) ResourceControl(ID portainer.ResourceControlID) (*portainer.ResourceControl, error) { func (service ServiceTx) ResourceControl(ID portainer.ResourceControlID) (*portainer.ResourceControl, error) {
var resourceControl portainer.ResourceControl var resourceControl portainer.ResourceControl
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
err := service.tx.GetObject(BucketName, identifier, &resourceControl) // err := service.tx.GetObject(BucketName, identifier, &resourceControl)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return &resourceControl, nil return &resourceControl, nil
} }
@ -103,12 +103,14 @@ func (service ServiceTx) Create(resourceControl *portainer.ResourceControl) erro
// UpdateResourceControl saves a ResourceControl object. // UpdateResourceControl saves a ResourceControl object.
func (service ServiceTx) UpdateResourceControl(ID portainer.ResourceControlID, resourceControl *portainer.ResourceControl) error { func (service ServiceTx) UpdateResourceControl(ID portainer.ResourceControlID, resourceControl *portainer.ResourceControl) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.UpdateObject(BucketName, identifier, resourceControl) // return service.tx.UpdateObject(BucketName, identifier, resourceControl)
return nil
} }
// DeleteResourceControl deletes a ResourceControl object by ID // DeleteResourceControl deletes a ResourceControl object by ID
func (service ServiceTx) DeleteResourceControl(ID portainer.ResourceControlID) error { func (service ServiceTx) DeleteResourceControl(ID portainer.ResourceControlID) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.DeleteObject(BucketName, identifier) // return service.tx.DeleteObject(BucketName, identifier)
return nil
} }

View file

@ -21,12 +21,12 @@ func (service ServiceTx) BucketName() string {
// Role returns a Role by ID // Role returns a Role by ID
func (service ServiceTx) Role(ID portainer.RoleID) (*portainer.Role, error) { func (service ServiceTx) Role(ID portainer.RoleID) (*portainer.Role, error) {
var set portainer.Role var set portainer.Role
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
err := service.tx.GetObject(BucketName, identifier, &set) // err := service.tx.GetObject(BucketName, identifier, &set)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return &set, nil return &set, nil
} }
@ -66,6 +66,7 @@ func (service ServiceTx) Create(role *portainer.Role) error {
// UpdateRole updates a role. // UpdateRole updates a role.
func (service ServiceTx) UpdateRole(ID portainer.RoleID, role *portainer.Role) error { func (service ServiceTx) UpdateRole(ID portainer.RoleID, role *portainer.Role) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.UpdateObject(BucketName, identifier, role) // return service.tx.UpdateObject(BucketName, identifier, role)
return nil
} }

View file

@ -20,12 +20,12 @@ func (service ServiceTx) BucketName() string {
// TeamMembership returns a TeamMembership object by ID // TeamMembership returns a TeamMembership object by ID
func (service ServiceTx) TeamMembership(ID portainer.TeamMembershipID) (*portainer.TeamMembership, error) { func (service ServiceTx) TeamMembership(ID portainer.TeamMembershipID) (*portainer.TeamMembership, error) {
var membership portainer.TeamMembership var membership portainer.TeamMembership
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
err := service.tx.GetObject(BucketName, identifier, &membership) // err := service.tx.GetObject(BucketName, identifier, &membership)
if err != nil { // if err != nil {
return nil, err // return nil, err
} // }
return &membership, nil return &membership, nil
} }
@ -102,8 +102,9 @@ func (service ServiceTx) TeamMembershipsByTeamID(teamID portainer.TeamID) ([]por
// UpdateTeamMembership saves a TeamMembership object. // UpdateTeamMembership saves a TeamMembership object.
func (service ServiceTx) UpdateTeamMembership(ID portainer.TeamMembershipID, membership *portainer.TeamMembership) error { func (service ServiceTx) UpdateTeamMembership(ID portainer.TeamMembershipID, membership *portainer.TeamMembership) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.UpdateObject(BucketName, identifier, membership) // return service.tx.UpdateObject(BucketName, identifier, membership)
return nil
} }
// CreateTeamMembership creates a new TeamMembership object. // CreateTeamMembership creates a new TeamMembership object.
@ -119,8 +120,9 @@ func (service ServiceTx) Create(membership *portainer.TeamMembership) error {
// DeleteTeamMembership deletes a TeamMembership object. // DeleteTeamMembership deletes a TeamMembership object.
func (service ServiceTx) DeleteTeamMembership(ID portainer.TeamMembershipID) error { func (service ServiceTx) DeleteTeamMembership(ID portainer.TeamMembershipID) error {
identifier := service.service.connection.ConvertToKey(int(ID)) // identifier := service.service.connection.ConvertToKey(int(ID))
return service.tx.DeleteObject(BucketName, identifier) // return service.tx.DeleteObject(BucketName, identifier)
return nil
} }
// DeleteTeamMembershipByUserID deletes all the TeamMembership object associated to a UserID. // DeleteTeamMembershipByUserID deletes all the TeamMembership object associated to a UserID.

View file

@ -2,11 +2,7 @@ package user
import ( import (
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
) dserrors "github.com/portainer/portainer/api/dataservices/errors"
const (
// BucketName represents the name of the bucket where this service stores data.
BucketName = "users"
) )
// Service represents a service for managing environment(endpoint) data. // Service represents a service for managing environment(endpoint) data.
@ -14,17 +10,8 @@ type Service struct {
connection portainer.Connection connection portainer.Connection
} }
func (service *Service) BucketName() string {
return BucketName
}
// NewService creates a new instance of a service. // NewService creates a new instance of a service.
func NewService(connection portainer.Connection) (*Service, error) { func NewService(connection portainer.Connection) (*Service, error) {
// err := connection.SetServiceName(BucketName)
// if err != nil {
// return nil, err
// }
return &Service{ return &Service{
connection: connection, connection: connection,
}, nil }, nil
@ -32,70 +19,44 @@ func NewService(connection portainer.Connection) (*Service, error) {
// User returns a user by ID // User returns a user by ID
func (service *Service) User(ID portainer.UserID) (*portainer.User, error) { func (service *Service) User(ID portainer.UserID) (*portainer.User, error) {
var user portainer.User db := service.connection.GetDB()
// identifier := service.connection.ConvertToKey(int(ID)) obj := portainer.User{}
// err := service.connection.GetObject(BucketName, identifier, &user) tx := db.First(&obj, `id = ?`, ID)
// if err != nil { if tx.Error != nil {
// return nil, err return nil, tx.Error
// } }
return &user, nil return &obj, nil
} }
// UserByUsername returns a user by username. // UserByUsername returns a user by username.
func (service *Service) UserByUsername(username string) (*portainer.User, error) { func (service *Service) UserByUsername(username string) (*portainer.User, error) {
// var u *portainer.User u := portainer.User{}
// stop := fmt.Errorf("ok")
// err := service.connection.GetAll(
// BucketName,
// &portainer.User{},
// func(obj interface{}) (interface{}, error) {
// user, ok := obj.(*portainer.User)
// if !ok {
// log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to User object")
// return nil, fmt.Errorf("Failed to convert to User object: %s", obj) db := service.connection.GetDB()
// } tx := db.First(&u, `username = ?`, username)
// if strings.EqualFold(user.Username, username) { if tx.Error != nil {
// u = user return nil, tx.Error
// return nil, stop }
// }
// return &portainer.User{}, nil if tx.Error == nil {
// }) return nil, dserrors.ErrObjectNotFound
}
// if errors.Is(err, stop) { return &u, nil
// return u, nil
// }
// if err == nil {
// return nil, dserrors.ErrObjectNotFound
// }
return nil, nil
} }
// Users return an array containing all the users. // Users return an array containing all the users.
func (service *Service) Users() ([]portainer.User, error) { func (service *Service) Users() ([]portainer.User, error) {
var users = make([]portainer.User, 0) var users = make([]portainer.User, 0)
// err := service.connection.GetAll( db := service.connection.GetDB()
// BucketName, tx := db.Find(&users)
// &portainer.User{}, if tx.Error != nil {
// func(obj interface{}) (interface{}, error) { return nil, tx.Error
// user, ok := obj.(*portainer.User) }
// if !ok {
// log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to User object")
// return nil, fmt.Errorf("Failed to convert to User object: %s", obj)
// }
// users = append(users, *user)
// return &portainer.User{}, nil
// })
return users, nil return users, nil
} }
@ -104,53 +65,43 @@ func (service *Service) Users() ([]portainer.User, error) {
func (service *Service) UsersByRole(role portainer.UserRole) ([]portainer.User, error) { func (service *Service) UsersByRole(role portainer.UserRole) ([]portainer.User, error) {
var users = make([]portainer.User, 0) var users = make([]portainer.User, 0)
// err := service.connection.GetAll( db := service.connection.GetDB()
// BucketName, tx := db.Find(&users, `role = ?`, role)
// &portainer.User{}, if tx.Error != nil {
// func(obj interface{}) (interface{}, error) { return nil, tx.Error
// user, ok := obj.(*portainer.User) }
// if !ok {
// log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to User object")
// return nil, fmt.Errorf("Failed to convert to User object: %s", obj)
// }
// if user.Role == role {
// users = append(users, *user)
// }
// return &portainer.User{}, nil
// })
return users, nil return users, nil
} }
// UpdateUser saves a user. // UpdateUser saves a user.
func (service *Service) UpdateUser(ID portainer.UserID, user *portainer.User) error { func (service *Service) UpdateUser(ID portainer.UserID, user *portainer.User) error {
// identifier := service.connection.ConvertToKey(int(ID))
// user.Username = strings.ToLower(user.Username) db := service.connection.GetDB()
// return service.connection.UpdateObject(BucketName, identifier, user) tx := db.Model(&portainer.User{}).Where("id = ?", ID).UpdateColumns(&user)
if tx.Error != nil {
return tx.Error
}
return nil return nil
} }
// CreateUser creates a new user. // CreateUser creates a new user.
func (service *Service) Create(user *portainer.User) error { func (service *Service) Create(user *portainer.User) error {
// return service.connection.CreateObject( db := service.connection.GetDB()
// BucketName, tx := db.Model(&portainer.User{}).Create(&user)
// func(id uint64) (int, interface{}) { if tx.Error != nil {
// user.ID = portainer.UserID(id) return tx.Error
// user.Username = strings.ToLower(user.Username) }
// return int(user.ID), user
// },
// )
return nil return nil
} }
// DeleteUser deletes a user. // DeleteUser deletes a user.
func (service *Service) DeleteUser(ID portainer.UserID) error { func (service *Service) DeleteUser(ID portainer.UserID) error {
// identifier := service.connection.ConvertToKey(int(ID)) db := service.connection.GetDB()
// return service.connection.DeleteObject(BucketName, identifier) tx := db.Model(&portainer.User{}).Delete("id = ?", ID)
if tx.Error != nil {
return tx.Error
}
return nil return nil
} }

View file

@ -29,10 +29,10 @@ func (handler *Handler) edgeStackCreate(w http.ResponseWriter, r *http.Request)
if featureflags.IsEnabled(portainer.FeatureNoTx) { if featureflags.IsEnabled(portainer.FeatureNoTx) {
edgeStack, err = handler.createSwarmStack(handler.DataStore, method, dryrun, tokenData.ID, r) edgeStack, err = handler.createSwarmStack(handler.DataStore, method, dryrun, tokenData.ID, r)
} else { } else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { // err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
edgeStack, err = handler.createSwarmStack(tx, method, dryrun, tokenData.ID, r) // edgeStack, err = handler.createSwarmStack(tx, method, dryrun, tokenData.ID, r)
return err // return err
}) // })
} }
if err != nil { if err != nil {
switch { switch {

View file

@ -33,9 +33,9 @@ func (handler *Handler) edgeStackDelete(w http.ResponseWriter, r *http.Request)
if featureflags.IsEnabled(portainer.FeatureNoTx) { if featureflags.IsEnabled(portainer.FeatureNoTx) {
err = handler.deleteEdgeStack(handler.DataStore, portainer.EdgeStackID(edgeStackID)) err = handler.deleteEdgeStack(handler.DataStore, portainer.EdgeStackID(edgeStackID))
} else { } else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { // err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
return handler.deleteEdgeStack(tx, portainer.EdgeStackID(edgeStackID)) // return handler.deleteEdgeStack(tx, portainer.EdgeStackID(edgeStackID))
}) // })
} }
if err != nil { if err != nil {

View file

@ -46,10 +46,10 @@ func (handler *Handler) edgeStackStatusDelete(w http.ResponseWriter, r *http.Req
if featureflags.IsEnabled(portainer.FeatureNoTx) { if featureflags.IsEnabled(portainer.FeatureNoTx) {
stack, err = handler.deleteEdgeStackStatus(handler.DataStore, portainer.EdgeStackID(stackID), endpoint) stack, err = handler.deleteEdgeStackStatus(handler.DataStore, portainer.EdgeStackID(stackID), endpoint)
} else { } else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { // err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
stack, err = handler.deleteEdgeStackStatus(tx, portainer.EdgeStackID(stackID), endpoint) // stack, err = handler.deleteEdgeStackStatus(tx, portainer.EdgeStackID(stackID), endpoint)
return err // return err
}) // })
} }
if err != nil { if err != nil {
var httpErr *httperror.HandlerError var httpErr *httperror.HandlerError

View file

@ -65,10 +65,10 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req
if featureflags.IsEnabled(portainer.FeatureNoTx) { if featureflags.IsEnabled(portainer.FeatureNoTx) {
stack, err = handler.updateEdgeStackStatus(handler.DataStore, r, portainer.EdgeStackID(stackID), payload) stack, err = handler.updateEdgeStackStatus(handler.DataStore, r, portainer.EdgeStackID(stackID), payload)
} else { } else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { // err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
stack, err = handler.updateEdgeStackStatus(tx, r, portainer.EdgeStackID(stackID), payload) // stack, err = handler.updateEdgeStackStatus(tx, r, portainer.EdgeStackID(stackID), payload)
return err // return err
}) // })
} }
if err != nil { if err != nil {

View file

@ -70,10 +70,10 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request)
if featureflags.IsEnabled(portainer.FeatureNoTx) { if featureflags.IsEnabled(portainer.FeatureNoTx) {
stack, err = handler.updateEdgeStack(handler.DataStore, portainer.EdgeStackID(stackID), payload) stack, err = handler.updateEdgeStack(handler.DataStore, portainer.EdgeStackID(stackID), payload)
} else { } else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { // err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
stack, err = handler.updateEdgeStack(tx, portainer.EdgeStackID(stackID), payload) // stack, err = handler.updateEdgeStack(tx, portainer.EdgeStackID(stackID), payload)
return err // return err
}) // })
} }
if err != nil { if err != nil {

View file

@ -127,7 +127,7 @@ func DecorateCustomTemplates(templates []portainer.CustomTemplate, resourceContr
resourceControl := GetResourceControlByResourceIDAndType(strconv.Itoa(int(template.ID)), portainer.CustomTemplateResourceControl, resourceControls) resourceControl := GetResourceControlByResourceIDAndType(strconv.Itoa(int(template.ID)), portainer.CustomTemplateResourceControl, resourceControls)
if resourceControl != nil { if resourceControl != nil {
templates[idx].ResourceControlID = resourceControl.ID templates[idx].ResourceControl = resourceControl
} }
} }
@ -152,7 +152,7 @@ func FilterAuthorizedCustomTemplates(customTemplates []portainer.CustomTemplate,
authorizedTemplates := make([]portainer.CustomTemplate, 0) authorizedTemplates := make([]portainer.CustomTemplate, 0)
for _, customTemplate := range customTemplates { for _, customTemplate := range customTemplates {
if customTemplate.CreatedByUserID == user.ID || (customTemplate.ResourceControlID == 0 && UserCanAccessResource(user.ID, userTeamIDs, customTemplate.ResourceControl)) { if customTemplate.CreatedByUserID == user.ID || (customTemplate.ResourceControl == nil && UserCanAccessResource(user.ID, userTeamIDs, customTemplate.ResourceControl)) {
authorizedTemplates = append(authorizedTemplates, customTemplate) authorizedTemplates = append(authorizedTemplates, customTemplate)
} }
} }

View file

@ -78,9 +78,7 @@ func createTLSSecuredEndpoint(flags *portainer.CLIFlags, dataStore dataservices.
tlsConfiguration.TLS = true tlsConfiguration.TLS = true
} }
endpointID := dataStore.Endpoint().GetNextIdentifier()
endpoint := &portainer.Endpoint{ endpoint := &portainer.Endpoint{
ID: portainer.EndpointID(endpointID),
Name: "primary", Name: "primary",
URL: *flags.EndpointURL, URL: *flags.EndpointURL,
GroupID: portainer.EndpointGroupID(1), GroupID: portainer.EndpointGroupID(1),
@ -143,9 +141,7 @@ func createUnsecuredEndpoint(endpointURL string, dataStore dataservices.DataStor
} }
} }
endpointID := dataStore.Endpoint().GetNextIdentifier()
endpoint := &portainer.Endpoint{ endpoint := &portainer.Endpoint{
ID: portainer.EndpointID(endpointID),
Name: "primary", Name: "primary",
URL: endpointURL, URL: endpointURL,
GroupID: portainer.EndpointGroupID(1), GroupID: portainer.EndpointGroupID(1),

View file

@ -181,10 +181,10 @@ type (
// * 1 - swarm // * 1 - swarm
// * 2 - compose // * 2 - compose
// * 3 - kubernetes // * 3 - kubernetes
Type StackType `json:"Type" example:"1"` Type StackType `json:"Type" example:"1"`
ResourceControlID ResourceControlID `json:"ResourceControl" gorm:"foreignKey"` ResourceControl *ResourceControl `json:"ResourceControl" gorm:"foreignKey"`
Variables []CustomTemplateVariableDefinition `json:"Variables" gorm:"serialize:json"` Variables []CustomTemplateVariableDefinition `json:"Variables" gorm:"serialize:json"`
GitConfig *gittypes.RepoConfig `json:"GitConfig" gorm:"serialize:json"` GitConfig *gittypes.RepoConfig `json:"GitConfig" gorm:"serialize:json"`
// IsComposeFormat indicates if the Kubernetes template is created from a Docker Compose file // IsComposeFormat indicates if the Kubernetes template is created from a Docker Compose file
IsComposeFormat bool `example:"false"` IsComposeFormat bool `example:"false"`
} }