mirror of https://github.com/portainer/portainer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.3 KiB
198 lines
5.3 KiB
7 years ago
|
package teammembership
|
||
8 years ago
|
|
||
|
import (
|
||
|
"github.com/portainer/portainer"
|
||
|
"github.com/portainer/portainer/bolt/internal"
|
||
|
|
||
|
"github.com/boltdb/bolt"
|
||
|
)
|
||
|
|
||
7 years ago
|
const (
|
||
|
// BucketName represents the name of the bucket where this service stores data.
|
||
|
BucketName = "team_membership"
|
||
|
)
|
||
8 years ago
|
|
||
7 years ago
|
// Service represents a service for managing endpoint data.
|
||
|
type Service struct {
|
||
|
db *bolt.DB
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// NewService creates a new instance of a service.
|
||
|
func NewService(db *bolt.DB) (*Service, error) {
|
||
|
err := internal.CreateBucket(db, BucketName)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
7 years ago
|
return &Service{
|
||
|
db: db,
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// TeamMembership returns a TeamMembership object by ID
|
||
|
func (service *Service) TeamMembership(ID portainer.TeamMembershipID) (*portainer.TeamMembership, error) {
|
||
8 years ago
|
var membership portainer.TeamMembership
|
||
7 years ago
|
identifier := internal.Itob(int(ID))
|
||
|
|
||
|
err := internal.GetObject(service.db, BucketName, identifier, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
return &membership, nil
|
||
|
}
|
||
|
|
||
|
// TeamMemberships return an array containing all the TeamMembership objects.
|
||
7 years ago
|
func (service *Service) TeamMemberships() ([]portainer.TeamMembership, error) {
|
||
8 years ago
|
var memberships = make([]portainer.TeamMembership, 0)
|
||
7 years ago
|
|
||
|
err := service.db.View(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
cursor := bucket.Cursor()
|
||
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||
|
var membership portainer.TeamMembership
|
||
7 years ago
|
err := internal.UnmarshalObject(v, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
memberships = append(memberships, membership)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
7 years ago
|
return memberships, err
|
||
8 years ago
|
}
|
||
|
|
||
|
// TeamMembershipsByUserID return an array containing all the TeamMembership objects where the specified userID is present.
|
||
7 years ago
|
func (service *Service) TeamMembershipsByUserID(userID portainer.UserID) ([]portainer.TeamMembership, error) {
|
||
8 years ago
|
var memberships = make([]portainer.TeamMembership, 0)
|
||
7 years ago
|
|
||
|
err := service.db.View(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
cursor := bucket.Cursor()
|
||
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||
|
var membership portainer.TeamMembership
|
||
7 years ago
|
err := internal.UnmarshalObject(v, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
if membership.UserID == userID {
|
||
|
memberships = append(memberships, membership)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
7 years ago
|
return memberships, err
|
||
8 years ago
|
}
|
||
|
|
||
|
// TeamMembershipsByTeamID return an array containing all the TeamMembership objects where the specified teamID is present.
|
||
7 years ago
|
func (service *Service) TeamMembershipsByTeamID(teamID portainer.TeamID) ([]portainer.TeamMembership, error) {
|
||
8 years ago
|
var memberships = make([]portainer.TeamMembership, 0)
|
||
7 years ago
|
|
||
|
err := service.db.View(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
cursor := bucket.Cursor()
|
||
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||
|
var membership portainer.TeamMembership
|
||
7 years ago
|
err := internal.UnmarshalObject(v, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
if membership.TeamID == teamID {
|
||
|
memberships = append(memberships, membership)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
|
||
7 years ago
|
return memberships, err
|
||
8 years ago
|
}
|
||
|
|
||
|
// UpdateTeamMembership saves a TeamMembership object.
|
||
7 years ago
|
func (service *Service) UpdateTeamMembership(ID portainer.TeamMembershipID, membership *portainer.TeamMembership) error {
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
return internal.UpdateObject(service.db, BucketName, identifier, membership)
|
||
8 years ago
|
}
|
||
|
|
||
|
// CreateTeamMembership creates a new TeamMembership object.
|
||
7 years ago
|
func (service *Service) CreateTeamMembership(membership *portainer.TeamMembership) error {
|
||
|
return service.db.Update(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
id, _ := bucket.NextSequence()
|
||
|
membership.ID = portainer.TeamMembershipID(id)
|
||
|
|
||
7 years ago
|
data, err := internal.MarshalObject(membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
return bucket.Put(internal.Itob(int(membership.ID)), data)
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
|
// DeleteTeamMembership deletes a TeamMembership object.
|
||
7 years ago
|
func (service *Service) DeleteTeamMembership(ID portainer.TeamMembershipID) error {
|
||
|
identifier := internal.Itob(int(ID))
|
||
|
return internal.DeleteObject(service.db, BucketName, identifier)
|
||
8 years ago
|
}
|
||
|
|
||
|
// DeleteTeamMembershipByUserID deletes all the TeamMembership object associated to a UserID.
|
||
7 years ago
|
func (service *Service) DeleteTeamMembershipByUserID(userID portainer.UserID) error {
|
||
|
return service.db.Update(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
cursor := bucket.Cursor()
|
||
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||
|
var membership portainer.TeamMembership
|
||
7 years ago
|
err := internal.UnmarshalObject(v, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
if membership.UserID == userID {
|
||
|
err := bucket.Delete(internal.Itob(int(membership.ID)))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// DeleteTeamMembershipByTeamID deletes all the TeamMembership object associated to a TeamID.
|
||
7 years ago
|
func (service *Service) DeleteTeamMembershipByTeamID(teamID portainer.TeamID) error {
|
||
|
return service.db.Update(func(tx *bolt.Tx) error {
|
||
|
bucket := tx.Bucket([]byte(BucketName))
|
||
8 years ago
|
|
||
|
cursor := bucket.Cursor()
|
||
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
||
|
var membership portainer.TeamMembership
|
||
7 years ago
|
err := internal.UnmarshalObject(v, &membership)
|
||
8 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
if membership.TeamID == teamID {
|
||
|
err := bucket.Delete(internal.Itob(int(membership.ID)))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
})
|
||
|
}
|