Add user listing.
parent
f683eabb7a
commit
2722e89299
|
@ -814,31 +814,6 @@ func (self *Coordinator) ListDbUsers(requester common.User, db string) ([]common
|
|||
return self.clusterConfiguration.GetDbUsers(db), nil
|
||||
}
|
||||
|
||||
func (self *Coordinator) GetDbUser(requester common.User, db string, username string) (common.User, error) {
|
||||
if ok, err := self.permissions.AuthorizeGetDbUser(requester, db); !ok {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbUser := self.clusterConfiguration.GetDbUser(db, username)
|
||||
if dbUser == nil {
|
||||
return nil, fmt.Errorf("Invalid username %s", username)
|
||||
}
|
||||
|
||||
return dbUser, nil
|
||||
}
|
||||
|
||||
func (self *Coordinator) ChangeDbUserPassword(requester common.User, db, username, password string) error {
|
||||
if ok, err := self.permissions.AuthorizeChangeDbUserPassword(requester, db, username); !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
hash, err := cluster.HashPassword(password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return self.raftServer.ChangeDbUserPassword(db, username, hash)
|
||||
}
|
||||
|
||||
func (self *Coordinator) ChangeDbUserPermissions(requester common.User, db, username, readPermissions, writePermissions string) error {
|
||||
if ok, err := self.permissions.AuthorizeChangeDbUserPermissions(requester, db); !ok {
|
||||
return err
|
||||
|
|
13
server.go
13
server.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -396,6 +397,18 @@ func (db *Database) User(name string) *DBUser {
|
|||
return db.users[name]
|
||||
}
|
||||
|
||||
// User returns a list of all database users.
|
||||
func (db *Database) Users() []*DBUser {
|
||||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
var a dbUsers
|
||||
for _, u := range db.users {
|
||||
a = append(a, u)
|
||||
}
|
||||
sort.Sort(a)
|
||||
return a
|
||||
}
|
||||
|
||||
// CreateUser creates a user in the database.
|
||||
func (db *Database) CreateUser(username, password string, permissions []string) error {
|
||||
// TODO: Authorization.
|
||||
|
|
|
@ -265,6 +265,28 @@ func TestDatabase_ChangePassword(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Ensure the database can return a list of all users.
|
||||
func TestDatabase_Users(t *testing.T) {
|
||||
s := OpenServer(NewMessagingClient())
|
||||
defer s.Close()
|
||||
|
||||
// Create two databases with users.
|
||||
s.CreateDatabase("foo")
|
||||
s.Database("foo").CreateUser("susy", "pass", nil)
|
||||
s.Database("foo").CreateUser("john", "pass", nil)
|
||||
s.CreateDatabase("bar")
|
||||
s.Database("bar").CreateUser("jimmy", "pass", nil)
|
||||
|
||||
// Retrieve a list of all users for "foo" (sorted by name).
|
||||
if a := s.Database("foo").Users(); len(a) != 2 {
|
||||
t.Fatalf("unexpected user count: %d", len(a))
|
||||
} else if a[0].Name != "john" {
|
||||
t.Fatalf("unexpected user(0): %s", a[0].Name)
|
||||
} else if a[1].Name != "susy" {
|
||||
t.Fatalf("unexpected user(1): %s", a[1].Name)
|
||||
}
|
||||
}
|
||||
|
||||
// Server is a wrapping test struct for influxdb.Server.
|
||||
type Server struct {
|
||||
*influxdb.Server
|
||||
|
|
7
user.go
7
user.go
|
@ -124,6 +124,13 @@ func (u *DBUser) ChangePermissions(readPermissions, writePermissions string) {
|
|||
u.WriteTo = []*Matcher{{true, writePermissions}}
|
||||
}
|
||||
|
||||
// dbUsers represents a list of database users, sortable by name.
|
||||
type dbUsers []*DBUser
|
||||
|
||||
func (p dbUsers) Len() int { return len(p) }
|
||||
func (p dbUsers) Less(i, j int) bool { return p[i].Name < p[j].Name }
|
||||
func (p dbUsers) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
func HashPassword(password string) ([]byte, error) {
|
||||
if length := len(password); length < 4 || length > 56 {
|
||||
return nil, NewQueryError(InvalidArgument, "Password must be more than 4 and less than 56 characters")
|
||||
|
|
Loading…
Reference in New Issue