2018-06-19 11:15:10 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2021-03-03 09:38:59 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/errors"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
2018-06-19 11:15:10 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
|
|
BucketName = "users"
|
|
|
|
)
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// Service represents a service for managing environment(endpoint) data.
|
2018-06-19 11:15:10 +00:00
|
|
|
type Service struct {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2018-06-19 11:15:10 +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)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2018-06-19 11:15:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// User returns a user by ID
|
|
|
|
func (service *Service) User(ID portainer.UserID) (*portainer.User, error) {
|
|
|
|
var user portainer.User
|
|
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &user)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserByUsername returns a user by username.
|
|
|
|
func (service *Service) UserByUsername(username string) (*portainer.User, error) {
|
|
|
|
var user *portainer.User
|
|
|
|
|
2021-02-10 02:29:28 +00:00
|
|
|
username = strings.ToLower(username)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var u portainer.User
|
|
|
|
err := internal.UnmarshalObject(v, &u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 09:38:59 +00:00
|
|
|
if strings.EqualFold(u.Username, username) {
|
2018-06-19 11:15:10 +00:00
|
|
|
user = &u
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.ErrObjectNotFound
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users return an array containing all the users.
|
|
|
|
func (service *Service) Users() ([]portainer.User, error) {
|
|
|
|
var users = make([]portainer.User, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var user portainer.User
|
|
|
|
err := internal.UnmarshalObject(v, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UsersByRole return an array containing all the users with the specified role.
|
|
|
|
func (service *Service) UsersByRole(role portainer.UserRole) ([]portainer.User, error) {
|
|
|
|
var users = make([]portainer.User, 0)
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var user portainer.User
|
|
|
|
err := internal.UnmarshalObject(v, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.Role == role {
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser saves a user.
|
|
|
|
func (service *Service) UpdateUser(ID portainer.UserID, user *portainer.User) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-02-10 02:29:28 +00:00
|
|
|
user.Username = strings.ToLower(user.Username)
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, user)
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser creates a new user.
|
|
|
|
func (service *Service) CreateUser(user *portainer.User) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
id, _ := bucket.NextSequence()
|
|
|
|
user.ID = portainer.UserID(id)
|
2021-02-10 02:29:28 +00:00
|
|
|
user.Username = strings.ToLower(user.Username)
|
2018-06-19 11:15:10 +00:00
|
|
|
|
|
|
|
data, err := internal.MarshalObject(user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(internal.Itob(int(user.ID)), data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser deletes a user.
|
|
|
|
func (service *Service) DeleteUser(ID portainer.UserID) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.DeleteObject(service.connection, BucketName, identifier)
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|