Skip to content

Commit

Permalink
changed list users return value to a map, makes more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianWe committed Apr 8, 2017
1 parent 6eb4900 commit e42c9b9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
15 changes: 4 additions & 11 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,8 @@ func (handler *RedisUserHandler) UpdatePassword(userName string, plainPW []byte)
return updateErr
}

func (handler *RedisUserHandler) ListUsers() ([]*UserIdentification, error) {
// redis scan can return the same value multiple times, therefor we
// create a set of all found elements
resultSet := make(map[uint64]string)
func (handler *RedisUserHandler) ListUsers() (map[uint64]string, error) {
res := make(map[uint64]string)

var cursor uint64
for {
Expand Down Expand Up @@ -389,18 +387,13 @@ func (handler *RedisUserHandler) ListUsers() ([]*UserIdentification, error) {
if !nameOK {
return nil, errors.New("Weird type in redis, should not happen")
}
resultSet[id] = nameStr
res[id] = nameStr
}
if cursor == 0 {
break
}
}
res := make([]*UserIdentification, len(resultSet))
var nextPos uint64
for id, username := range resultSet {
res[nextPos] = &UserIdentification{ID: id, UserName: username}
nextPos++
}

return res, nil
}

Expand Down
6 changes: 3 additions & 3 deletions sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ func (handler *SQLUserHandler) UpdatePassword(username string, plainPW []byte) e
return err
}

func (handler *SQLUserHandler) ListUsers() ([]*UserIdentification, error) {
func (handler *SQLUserHandler) ListUsers() (map[uint64]string, error) {
if handler.blockDB {
handler.mutex.RLock()
defer handler.mutex.RUnlock()
Expand All @@ -754,15 +754,15 @@ func (handler *SQLUserHandler) ListUsers() ([]*UserIdentification, error) {
return nil, err
}
defer rows.Close()
res := make([]*UserIdentification, 0)
res := make(map[uint64]string, 0)
for rows.Next() {
var id uint64
var username string
scanErr := rows.Scan(&id, &username)
if scanErr != nil {
return nil, scanErr
}
res = append(res, &UserIdentification{ID: id, UserName: username})
res[id] = username
}
err = rows.Err()
if err != nil {
Expand Down
11 changes: 1 addition & 10 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,6 @@ func (handler *ScryptHandler) PasswordHashLength() int {
// was not found.
var ErrUserNotFound = errors.New("Username not found.")

// UserIdentification is used to group username and user id together,
// used for lookups.
//
// New in version v0.4
type UserIdentification struct {
ID uint64
UserName string
}

// UserHandler is an interface to deal with the management of
// users.
// It should use a PasswordHandler for generating passwords to store.
Expand Down Expand Up @@ -225,7 +216,7 @@ type UserHandler interface {
// ListUsers returns all users currently present in the storage (by id).
//
// New in version v0.4
ListUsers() ([]*UserIdentification, error)
ListUsers() (map[uint64]string, error)

// GetUserName returns the username for a given id.
// Returns "" and ErrUserNotFound if the id is not valid.
Expand Down

0 comments on commit e42c9b9

Please sign in to comment.