2015-04-07 08:20:55 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-04-10 19:52:33 +00:00
|
|
|
"github.com/boltdb/bolt"
|
2015-04-13 08:22:51 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetUser gets a user by user login.
|
|
|
|
func (db *DB) GetUser(login string) (*common.User, error) {
|
|
|
|
user := &common.User{}
|
|
|
|
key := []byte(login)
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return get(t, bucketUser, key, user)
|
|
|
|
})
|
|
|
|
|
2015-04-07 08:20:55 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserTokens gets a list of all tokens for
|
|
|
|
// the given user login.
|
|
|
|
func (db *DB) GetUserTokens(login string) ([]*common.Token, error) {
|
|
|
|
t, err := db.Begin(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer t.Rollback()
|
|
|
|
tokens := []*common.Token{}
|
|
|
|
|
|
|
|
// get the index of user tokens and unmarshal
|
|
|
|
// to a string array.
|
2015-04-13 08:22:51 +00:00
|
|
|
var keys [][]byte
|
|
|
|
err = get(t, bucketUserTokens, []byte(login), &keys)
|
|
|
|
if err != nil && err != ErrKeyNotFound {
|
|
|
|
return nil, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for each item in the index, get the repository
|
|
|
|
// and append to the array
|
|
|
|
for _, key := range keys {
|
|
|
|
token := &common.Token{}
|
2015-04-13 08:22:51 +00:00
|
|
|
raw := t.Bucket(bucketTokens).Get(key)
|
2015-04-07 08:20:55 +00:00
|
|
|
err = decode(raw, token)
|
|
|
|
if err != nil {
|
2015-04-13 08:22:51 +00:00
|
|
|
return nil, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
tokens = append(tokens, token)
|
|
|
|
}
|
2015-04-13 08:22:51 +00:00
|
|
|
return tokens, nil
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserRepos gets a list of repositories for the
|
|
|
|
// given user account.
|
|
|
|
func (db *DB) GetUserRepos(login string) ([]*common.Repo, error) {
|
|
|
|
repos := []*common.Repo{}
|
2015-04-14 04:39:05 +00:00
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
|
|
|
// get the index of user tokens and unmarshal
|
|
|
|
// to a string array.
|
|
|
|
var keys [][]byte
|
|
|
|
err := get(t, bucketUserRepos, []byte(login), &keys)
|
|
|
|
if err != nil && err != ErrKeyNotFound {
|
|
|
|
return err
|
2015-04-09 05:18:25 +00:00
|
|
|
}
|
2015-04-14 04:39:05 +00:00
|
|
|
|
|
|
|
// for each item in the index, get the repository
|
|
|
|
// and append to the array
|
|
|
|
for _, key := range keys {
|
|
|
|
repo := &common.Repo{}
|
|
|
|
err := get(t, bucketRepo, key, repo)
|
|
|
|
if err == ErrKeyNotFound {
|
|
|
|
// TODO if we come across ErrKeyNotFound it means
|
|
|
|
// we need to re-build the index
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
repos = append(repos, repo)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
2015-04-14 04:39:05 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2015-04-07 08:20:55 +00:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserCount gets a count of all registered users
|
|
|
|
// in the system.
|
|
|
|
func (db *DB) GetUserCount() (int, error) {
|
2015-04-14 02:25:11 +00:00
|
|
|
var out int
|
|
|
|
var err = db.View(func(t *bolt.Tx) error {
|
|
|
|
out = t.Bucket(bucketUser).Stats().KeyN
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return out, err
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserList gets a list of all registered users.
|
|
|
|
func (db *DB) GetUserList() ([]*common.User, error) {
|
|
|
|
users := []*common.User{}
|
2015-04-14 02:25:11 +00:00
|
|
|
err := db.View(func(t *bolt.Tx) error {
|
|
|
|
return t.Bucket(bucketUser).ForEach(func(key, raw []byte) error {
|
|
|
|
user := &common.User{}
|
|
|
|
err := decode(raw, user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
return nil
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
})
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser updates an existing user. If the user
|
|
|
|
// does not exists an error is returned.
|
|
|
|
func (db *DB) UpdateUser(user *common.User) error {
|
|
|
|
key := []byte(user.Login)
|
|
|
|
user.Updated = time.Now().UTC().Unix()
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return update(t, bucketUser, key, user)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InsertUser inserts a new user into the datastore. If
|
|
|
|
// the user login already exists an error is returned.
|
|
|
|
func (db *DB) InsertUser(user *common.User) error {
|
|
|
|
key := []byte(user.Login)
|
|
|
|
user.Created = time.Now().UTC().Unix()
|
|
|
|
user.Updated = time.Now().UTC().Unix()
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return insert(t, bucketUser, key, user)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser deletes the user.
|
|
|
|
func (db *DB) DeleteUser(user *common.User) error {
|
|
|
|
key := []byte(user.Login)
|
|
|
|
// TODO(bradrydzewski) delete user subscriptions
|
|
|
|
// TODO(bradrydzewski) delete user tokens
|
2015-04-10 19:52:33 +00:00
|
|
|
|
2015-04-13 08:22:51 +00:00
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-10 19:52:33 +00:00
|
|
|
return delete(t, bucketUser, key)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|