2015-05-01 04:08:42 +00:00
|
|
|
package builtin
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// User returns a user by user login.
|
|
|
|
func (db *DB) User(login string) (*common.User, error) {
|
2015-04-07 08:20:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// UserCount returns a count of all registered users.
|
|
|
|
func (db *DB) UserCount() (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
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// UserList returns a list of all registered users.
|
|
|
|
func (db *DB) UserList() ([]*common.User, error) {
|
2015-04-07 08:20:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// SetUser inserts or updates a user.
|
|
|
|
func (db *DB) SetUser(user *common.User) error {
|
2015-04-07 08:20:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// SetUserNotExists inserts a new user into the datastore.
|
|
|
|
// If the user login already exists ErrConflict is returned.
|
|
|
|
func (db *DB) SetUserNotExists(user *common.User) error {
|
2015-04-07 08:20:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-15 05:04:38 +00:00
|
|
|
// DelUser deletes the user.
|
|
|
|
func (db *DB) DelUser(user *common.User) error {
|
2015-04-07 08:20:55 +00:00
|
|
|
key := []byte(user.Login)
|
2015-04-13 08:22:51 +00:00
|
|
|
return db.Update(func(t *bolt.Tx) error {
|
2015-04-17 05:16:32 +00:00
|
|
|
err := delete(t, bucketUserTokens, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = delete(t, bucketUserRepos, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-01 03:40:59 +00:00
|
|
|
// IDEA: deleteKeys(t, bucketTokens, keys)
|
|
|
|
deleteWithPrefix(t, bucketTokens, append(key, '/'))
|
2015-04-10 19:52:33 +00:00
|
|
|
return delete(t, bucketUser, key)
|
|
|
|
})
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|