Add user listing.

pull/903/head
Ben Johnson 2014-10-27 17:31:45 -06:00
parent f683eabb7a
commit 2722e89299
5 changed files with 51 additions and 25 deletions

9
TODO Normal file
View File

@ -0,0 +1,9 @@
TODO
====
### Uncompleted
- [ ]
### Completed

View File

@ -814,31 +814,6 @@ func (self *Coordinator) ListDbUsers(requester common.User, db string) ([]common
return self.clusterConfiguration.GetDbUsers(db), nil 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 { func (self *Coordinator) ChangeDbUserPermissions(requester common.User, db, username, readPermissions, writePermissions string) error {
if ok, err := self.permissions.AuthorizeChangeDbUserPermissions(requester, db); !ok { if ok, err := self.permissions.AuthorizeChangeDbUserPermissions(requester, db); !ok {
return err return err

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -396,6 +397,18 @@ func (db *Database) User(name string) *DBUser {
return db.users[name] 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. // CreateUser creates a user in the database.
func (db *Database) CreateUser(username, password string, permissions []string) error { func (db *Database) CreateUser(username, password string, permissions []string) error {
// TODO: Authorization. // TODO: Authorization.

View File

@ -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. // Server is a wrapping test struct for influxdb.Server.
type Server struct { type Server struct {
*influxdb.Server *influxdb.Server

View File

@ -124,6 +124,13 @@ func (u *DBUser) ChangePermissions(readPermissions, writePermissions string) {
u.WriteTo = []*Matcher{{true, writePermissions}} 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) { func HashPassword(password string) ([]byte, error) {
if length := len(password); length < 4 || length > 56 { if length := len(password); length < 4 || length > 56 {
return nil, NewQueryError(InvalidArgument, "Password must be more than 4 and less than 56 characters") return nil, NewQueryError(InvalidArgument, "Password must be more than 4 and less than 56 characters")