1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/server/database/user.go

128 lines
2.8 KiB
Go
Raw Normal View History

package database
2014-06-05 00:25:38 +03:00
import (
"database/sql"
"time"
"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.
Find(id int64) (*model.User, error)
2014-06-05 00:25:38 +03:00
// FindLogin finds the User by remote login.
FindLogin(remote, login string) (*model.User, error)
2014-06-05 00:25:38 +03:00
// FindToken finds the User by token.
FindToken(token string) (*model.User, error)
2014-06-05 00:25:38 +03:00
// List finds all registered users of the system.
List() ([]*model.User, error)
2014-06-05 00:25:38 +03:00
// Insert persists the User to the datastore.
Insert(user *model.User) error
2014-06-05 00:25:38 +03:00
// Update persists changes to the User to the datastore.
Update(user *model.User) error
2014-06-05 00:25:38 +03:00
// Delete removes the User from the datastore.
Delete(user *model.User) error
// 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.
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.
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.
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.
const deleteUserStmt = `
2014-06-05 00:25:38 +03:00
DELETE FROM users WHERE user_id=?
`
// SQL statement to check if users exist.
const confirmUserStmt = `
select 0 from users limit 1
`
// NewUserManager initiales a new UserManager intended to
2014-06-05 00:25:38 +03:00
// manage and persist commits.
func NewUserManager(db *sql.DB) UserManager {
2014-06-05 00:25:38 +03:00
return &userManager{db}
}
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
}
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
}
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
}
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
}
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)
}
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)
}
func (db *userManager) Delete(user *model.User) error {
_, err := db.Exec(deleteUserStmt, user.ID)
2014-06-05 00:25:38 +03:00
return err
}
func (db *userManager) Exist() bool {
row := db.QueryRow(confirmUserStmt)
var result int
row.Scan(&result)
return result == 1
}