2019-05-24 06:04:58 +00:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
2021-04-06 10:08:43 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2019-05-24 06:04:58 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
|
|
BucketName = "roles"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a service for managing endpoint data.
|
|
|
|
type Service struct {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2019-05-24 06:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new instance of a service.
|
2021-04-06 10:08:43 +00:00
|
|
|
func NewService(connection *internal.DbConnection) (*Service, error) {
|
|
|
|
err := internal.CreateBucket(connection, BucketName)
|
2019-05-24 06:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2019-05-24 06:04:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Role returns a Role by ID
|
|
|
|
func (service *Service) Role(ID portainer.RoleID) (*portainer.Role, error) {
|
|
|
|
var set portainer.Role
|
|
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &set)
|
2019-05-24 06:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &set, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roles return an array containing all the sets.
|
|
|
|
func (service *Service) Roles() ([]portainer.Role, error) {
|
|
|
|
var sets = make([]portainer.Role, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2019-05-24 06:04:58 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var set portainer.Role
|
|
|
|
err := internal.UnmarshalObject(v, &set)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sets = append(sets, set)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return sets, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRole creates a new Role.
|
2019-10-08 00:17:58 +00:00
|
|
|
func (service *Service) CreateRole(role *portainer.Role) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2019-05-24 06:04:58 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
id, _ := bucket.NextSequence()
|
2019-10-08 00:17:58 +00:00
|
|
|
role.ID = portainer.RoleID(id)
|
2019-05-24 06:04:58 +00:00
|
|
|
|
2019-10-08 00:17:58 +00:00
|
|
|
data, err := internal.MarshalObject(role)
|
2019-05-24 06:04:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-08 00:17:58 +00:00
|
|
|
return bucket.Put(internal.Itob(int(role.ID)), data)
|
2019-05-24 06:04:58 +00:00
|
|
|
})
|
|
|
|
}
|
2019-10-08 00:17:58 +00:00
|
|
|
|
|
|
|
// UpdateRole updates a role.
|
|
|
|
func (service *Service) UpdateRole(ID portainer.RoleID, role *portainer.Role) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, role)
|
2019-10-08 00:17:58 +00:00
|
|
|
}
|