influxdb/enterprise/users.go

107 lines
2.7 KiB
Go
Raw Normal View History

package enterprise
import (
"context"
"github.com/influxdata/chronograf"
)
2017-02-23 22:02:53 +00:00
// UserStore uses a control client operate on Influx Enterprise users
type UserStore struct {
Ctrl
Logger chronograf.Logger
}
// Add creates a new User in Influx Enterprise
2017-02-23 22:02:53 +00:00
func (c *UserStore) Add(ctx context.Context, u *chronograf.User) (*chronograf.User, error) {
if err := c.Ctrl.CreateUser(ctx, u.Name, u.Passwd); err != nil {
return nil, err
}
perms := ToEnterprise(u.Permissions)
if err := c.Ctrl.SetUserPerms(ctx, u.Name, perms); err != nil {
return nil, err
}
return u, nil
}
// Delete the User from Influx Enterprise
2017-02-23 22:02:53 +00:00
func (c *UserStore) Delete(ctx context.Context, u *chronograf.User) error {
return c.Ctrl.DeleteUser(ctx, u.Name)
}
// Get retrieves a user if name exists.
2017-02-23 22:02:53 +00:00
func (c *UserStore) Get(ctx context.Context, name string) (*chronograf.User, error) {
u, err := c.Ctrl.User(ctx, name)
if err != nil {
return nil, err
}
return &chronograf.User{
Name: u.Name,
2017-02-23 22:02:53 +00:00
Permissions: ToChronograf(u.Permissions),
}, nil
}
// Update the user's permissions or roles
2017-02-23 22:02:53 +00:00
func (c *UserStore) Update(ctx context.Context, u *chronograf.User) error {
// Only allow one type of change at a time. If it is a password
// change then do it and return without any changes to permissions
if u.Passwd != "" {
return c.Ctrl.ChangePassword(ctx, u.Name, u.Passwd)
}
2017-02-23 22:02:53 +00:00
perms := ToEnterprise(u.Permissions)
return c.Ctrl.SetUserPerms(ctx, u.Name, perms)
}
2017-02-17 21:13:51 +00:00
// All is all users in influx
2017-02-23 22:02:53 +00:00
func (c *UserStore) All(ctx context.Context) ([]chronograf.User, error) {
2017-02-17 21:13:51 +00:00
all, err := c.Ctrl.Users(ctx, nil)
if err != nil {
return nil, err
}
res := make([]chronograf.User, len(all.Users))
for i, user := range all.Users {
res[i] = chronograf.User{
Name: user.Name,
2017-02-23 22:02:53 +00:00
Permissions: ToChronograf(user.Permissions),
2017-02-17 21:13:51 +00:00
}
}
return res, nil
}
2017-02-23 22:02:53 +00:00
// ToEnterprise converts chronograf permission shape to enterprise
func ToEnterprise(perms chronograf.Permissions) Permissions {
res := Permissions{}
for _, perm := range perms {
if perm.Scope == chronograf.AllScope {
// Enterprise uses empty string as the key for all databases
res[""] = perm.Allowed
} else {
res[perm.Name] = perm.Allowed
}
}
return res
}
2017-02-23 22:02:53 +00:00
// ToChronograf converts enterprise permissions shape to chronograf shape
func ToChronograf(perms Permissions) chronograf.Permissions {
res := chronograf.Permissions{}
for db, perm := range perms {
// Enterprise uses empty string as the key for all databases
if db == "" {
res = append(res, chronograf.Permission{
Scope: chronograf.AllScope,
Allowed: perm,
})
} else {
res = append(res, chronograf.Permission{
Scope: chronograf.DBScope,
Name: db,
Allowed: perm,
})
}
}
return res
}