2014-06-13 02:41:04 +03:00
|
|
|
package database
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-05 00:25:38 +03:00
|
|
|
"github.com/russross/meddler"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserManager interface {
|
|
|
|
// Find finds the User by ID.
|
2014-06-13 02:41:04 +03:00
|
|
|
Find(id int64) (*model.User, error)
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// FindLogin finds the User by remote login.
|
2014-06-13 02:41:04 +03:00
|
|
|
FindLogin(remote, login string) (*model.User, error)
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// FindToken finds the User by token.
|
2014-06-13 02:41:04 +03:00
|
|
|
FindToken(token string) (*model.User, error)
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// List finds all registered users of the system.
|
2014-06-13 02:41:04 +03:00
|
|
|
List() ([]*model.User, error)
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// Insert persists the User to the datastore.
|
2014-06-13 02:41:04 +03:00
|
|
|
Insert(user *model.User) error
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// Update persists changes to the User to the datastore.
|
2014-06-13 02:41:04 +03:00
|
|
|
Update(user *model.User) error
|
2014-06-05 00:25:38 +03:00
|
|
|
|
|
|
|
// Delete removes the User from the datastore.
|
2014-06-13 02:41:04 +03:00
|
|
|
Delete(user *model.User) error
|
2014-06-22 00:22:38 +03:00
|
|
|
|
|
|
|
// Exist returns true if Users exist in the system.
|
|
|
|
Exist() bool
|
2014-06-05 00:25:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// userManager manages a list of users in a SQL database.
|
|
|
|
type userManager struct {
|
|
|
|
*sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// SQL query to retrieve a User by remote login.
|
2014-06-13 02:41:04 +03:00
|
|
|
const findUserLoginQuery = `
|
2014-06-05 00:25:38 +03:00
|
|
|
SELECT *
|
|
|
|
FROM users
|
|
|
|
WHERE user_remote=?
|
|
|
|
AND user_login=?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve a User by remote login.
|
2014-06-13 02:41:04 +03:00
|
|
|
const findUserTokenQuery = `
|
2014-06-05 00:25:38 +03:00
|
|
|
SELECT *
|
|
|
|
FROM users
|
|
|
|
WHERE user_token=?
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL query to retrieve a list of all users.
|
2014-06-13 02:41:04 +03:00
|
|
|
const listUserQuery = `
|
2014-06-05 00:25:38 +03:00
|
|
|
SELECT *
|
|
|
|
FROM users
|
|
|
|
ORDER BY user_name ASC
|
|
|
|
`
|
|
|
|
|
|
|
|
// SQL statement to delete a User by ID.
|
2014-06-13 02:41:04 +03:00
|
|
|
const deleteUserStmt = `
|
2014-06-05 00:25:38 +03:00
|
|
|
DELETE FROM users WHERE user_id=?
|
|
|
|
`
|
|
|
|
|
2014-06-22 00:22:38 +03:00
|
|
|
// SQL statement to check if users exist.
|
|
|
|
const confirmUserStmt = `
|
|
|
|
select 0 from users limit 1
|
|
|
|
`
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
// NewUserManager initiales a new UserManager intended to
|
2014-06-05 00:25:38 +03:00
|
|
|
// manage and persist commits.
|
2014-06-13 02:41:04 +03:00
|
|
|
func NewUserManager(db *sql.DB) UserManager {
|
2014-06-05 00:25:38 +03:00
|
|
|
return &userManager{db}
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) Find(id int64) (*model.User, error) {
|
|
|
|
dst := model.User{}
|
2014-06-05 00:25:38 +03:00
|
|
|
err := meddler.Load(db, "users", &dst, id)
|
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) FindLogin(remote, login string) (*model.User, error) {
|
|
|
|
dst := model.User{}
|
|
|
|
err := meddler.QueryRow(db, &dst, findUserLoginQuery, remote, login)
|
2014-06-05 00:25:38 +03:00
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) FindToken(token string) (*model.User, error) {
|
|
|
|
dst := model.User{}
|
|
|
|
err := meddler.QueryRow(db, &dst, findUserTokenQuery, token)
|
2014-06-05 00:25:38 +03:00
|
|
|
return &dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) List() ([]*model.User, error) {
|
|
|
|
var dst []*model.User
|
|
|
|
err := meddler.QueryAll(db, &dst, listUserQuery)
|
2014-06-05 00:25:38 +03:00
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) Insert(user *model.User) error {
|
2014-06-05 00:25:38 +03:00
|
|
|
user.Created = time.Now().Unix()
|
|
|
|
user.Updated = time.Now().Unix()
|
|
|
|
return meddler.Insert(db, "users", user)
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) Update(user *model.User) error {
|
2014-06-05 00:25:38 +03:00
|
|
|
user.Updated = time.Now().Unix()
|
|
|
|
return meddler.Update(db, "users", user)
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:41:04 +03:00
|
|
|
func (db *userManager) Delete(user *model.User) error {
|
|
|
|
_, err := db.Exec(deleteUserStmt, user.ID)
|
2014-06-05 00:25:38 +03:00
|
|
|
return err
|
|
|
|
}
|
2014-06-22 00:22:38 +03:00
|
|
|
|
|
|
|
func (db *userManager) Exist() bool {
|
|
|
|
row := db.QueryRow(confirmUserStmt)
|
|
|
|
var result int
|
|
|
|
row.Scan(&result)
|
|
|
|
return result == 1
|
|
|
|
}
|