mirror of
https://github.com/semaphoreui/semaphore.git
synced 2025-01-20 15:29:28 +01:00
refactor(be): split SqlDb to several files
This commit is contained in:
parent
c472dc66dd
commit
d10b421538
@ -6,7 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
db2 "github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -25,13 +25,13 @@ const (
|
||||
)
|
||||
|
||||
type task struct {
|
||||
store db2.Store
|
||||
task db2.Task
|
||||
template db2.Template
|
||||
sshKey db2.AccessKey
|
||||
inventory db2.Inventory
|
||||
repository db2.Repository
|
||||
environment db2.Environment
|
||||
store db.Store
|
||||
task db.Task
|
||||
template db.Template
|
||||
sshKey db.AccessKey
|
||||
inventory db.Inventory
|
||||
repository db.Repository
|
||||
environment db.Environment
|
||||
users []int
|
||||
projectID int
|
||||
hosts []string
|
||||
@ -58,7 +58,7 @@ func (t *task) prepareRun() {
|
||||
objType := taskTypeID
|
||||
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " finished - " + strings.ToUpper(t.task.Status)
|
||||
|
||||
_, err := t.store.CreateEvent(db2.Event{
|
||||
_, err := t.store.CreateEvent(db.Event{
|
||||
ProjectID: &t.projectID,
|
||||
ObjectType: &objType,
|
||||
ObjectID: &t.task.ID,
|
||||
@ -87,7 +87,7 @@ func (t *task) prepareRun() {
|
||||
|
||||
objType := taskTypeID
|
||||
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " is preparing"
|
||||
_, err = t.store.CreateEvent(db2.Event{
|
||||
_, err = t.store.CreateEvent(db.Event{
|
||||
ProjectID: &t.projectID,
|
||||
ObjectType: &objType,
|
||||
ObjectID: &t.task.ID,
|
||||
@ -170,7 +170,7 @@ func (t *task) run() {
|
||||
objType := taskTypeID
|
||||
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " finished - " + strings.ToUpper(t.task.Status)
|
||||
|
||||
_, err := t.store.CreateEvent(db2.Event{
|
||||
_, err := t.store.CreateEvent(db.Event{
|
||||
ProjectID: &t.projectID,
|
||||
ObjectType: &objType,
|
||||
ObjectID: &t.task.ID,
|
||||
@ -195,7 +195,7 @@ func (t *task) run() {
|
||||
desc := "Task ID " + strconv.Itoa(t.task.ID) + " (" + t.template.Alias + ")" + " is running"
|
||||
|
||||
|
||||
_, err := t.store.CreateEvent(db2.Event{
|
||||
_, err := t.store.CreateEvent(db.Event{
|
||||
ProjectID: &t.projectID,
|
||||
ObjectType: &objType,
|
||||
ObjectID: &t.task.ID,
|
||||
@ -220,6 +220,20 @@ func (t *task) run() {
|
||||
t.updateStatus()
|
||||
}
|
||||
|
||||
func (t *task) prepareError(err error, errMsg string) error {
|
||||
if err == sql.ErrNoRows {
|
||||
t.log(errMsg)
|
||||
return err
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.fail()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *task) fetch(errMsg string, ptr interface{}, query string, args ...interface{}) error {
|
||||
err := t.store.Sql().SelectOne(ptr, query, args...)
|
||||
if err == sql.ErrNoRows {
|
||||
@ -238,25 +252,24 @@ func (t *task) fetch(errMsg string, ptr interface{}, query string, args ...inter
|
||||
//nolint: gocyclo
|
||||
func (t *task) populateDetails() error {
|
||||
// get template
|
||||
if err := t.fetch("Template not found!", &t.template, "select * from project__template where id=?", t.task.TemplateID); err != nil {
|
||||
return err
|
||||
var err error
|
||||
|
||||
t.template, err = t.store.GetTemplate(t.projectID, t.task.TemplateID)
|
||||
if err != nil {
|
||||
return t.prepareError(err, "Template not found!")
|
||||
}
|
||||
|
||||
var project db2.Project
|
||||
// get project alert setting
|
||||
if err := t.fetch("Alert setting not found!", &project, "select alert, alert_chat from project where id=?", t.template.ProjectID); err != nil {
|
||||
return err
|
||||
project, err := t.store.GetProject(t.template.ProjectID)
|
||||
if err != nil {
|
||||
return t.prepareError(err, "Project not found!")
|
||||
}
|
||||
|
||||
t.alert = project.Alert
|
||||
t.alertChat = project.AlertChat
|
||||
|
||||
// get project users
|
||||
var users []struct {
|
||||
ID int `db:"id"`
|
||||
}
|
||||
if _, err := t.store.Sql().Select(&users, "select user_id as id from project__user where project_id=?", t.template.ProjectID); err != nil {
|
||||
return err
|
||||
}
|
||||
users, err := t.store.GetProjectUsers(t.template.ProjectID, db.RetrieveQueryParams{})
|
||||
|
||||
t.users = []int{}
|
||||
for _, user := range users {
|
||||
@ -264,21 +277,24 @@ func (t *task) populateDetails() error {
|
||||
}
|
||||
|
||||
// get access key
|
||||
if err := t.fetch("Template Access Key not found!", &t.sshKey, "select * from access_key where id=?", t.template.SSHKeyID); err != nil {
|
||||
return err
|
||||
t.sshKey, err = t.store.GetAccessKey(t.template.ProjectID, t.template.SSHKeyID)
|
||||
if err != nil {
|
||||
return t.prepareError(err, "Template AccessKey not found!")
|
||||
}
|
||||
|
||||
if t.sshKey.Type != "ssh" {
|
||||
t.log("Non ssh-type keys are currently not supported: " + t.sshKey.Type)
|
||||
return errors.New("Unsupported SSH Key")
|
||||
return errors.New("unsupported SSH Key")
|
||||
}
|
||||
|
||||
// get inventory
|
||||
if err := t.fetch("Template Inventory not found!", &t.inventory, "select * from project__inventory where id=?", t.template.InventoryID); err != nil {
|
||||
return err
|
||||
t.inventory, err = t.store.GetInventory(t.template.ProjectID, t.template.InventoryID)
|
||||
if err != nil {
|
||||
return t.prepareError(err, "Template Inventory not found!")
|
||||
}
|
||||
|
||||
// get inventory services key
|
||||
|
||||
if t.inventory.KeyID != nil {
|
||||
if err := t.fetch("Inventory AccessKey not found!", &t.inventory.Key, "select * from access_key where id=?", *t.inventory.KeyID); err != nil {
|
||||
return err
|
||||
@ -303,7 +319,7 @@ func (t *task) populateDetails() error {
|
||||
}
|
||||
if t.repository.SSHKey.Type != "ssh" {
|
||||
t.log("Repository Access Key is not 'SSH': " + t.repository.SSHKey.Type)
|
||||
return errors.New("Unsupported SSH Key")
|
||||
return errors.New("unsupported SSH Key")
|
||||
}
|
||||
|
||||
// get environment
|
||||
@ -319,7 +335,7 @@ func (t *task) populateDetails() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *task) installKey(key db2.AccessKey) error {
|
||||
func (t *task) installKey(key db.AccessKey) error {
|
||||
t.log("access key " + key.Name + " installed")
|
||||
|
||||
path := key.GetPath()
|
||||
|
13
db/Store.go
13
db/Store.go
@ -65,6 +65,12 @@ type Store interface {
|
||||
DeleteAccessKey(projectID int, accessKeyID int) error
|
||||
DeleteAccessKeySoft(projectID int, accessKeyID int) error
|
||||
|
||||
GetGlobalAccessKey(accessKeyID int) (AccessKey, error)
|
||||
GetGlobalAccessKeys(params RetrieveQueryParams) ([]AccessKey, error)
|
||||
UpdateGlobalAccessKey(accessKey AccessKey) error
|
||||
CreateGlobalAccessKey(accessKey AccessKey) (AccessKey, error)
|
||||
DeleteGlobalAccessKey(accessKeyID int) error
|
||||
DeleteGlobalAccessKeySoft(accessKeyID int) error
|
||||
|
||||
GetUsers(params RetrieveQueryParams) ([]User, error)
|
||||
CreateUser(user User) (User, error)
|
||||
@ -80,7 +86,6 @@ type Store interface {
|
||||
DeleteProject(projectID int) error
|
||||
UpdateProject(project Project) error
|
||||
|
||||
|
||||
GetTemplates(projectID int, params RetrieveQueryParams) ([]Template, error)
|
||||
CreateTemplate(template Template) (Template, error)
|
||||
UpdateTemplate(template Template) error
|
||||
@ -94,6 +99,8 @@ type Store interface {
|
||||
UpdateProjectUser(projectUser ProjectUser) error
|
||||
|
||||
CreateEvent(event Event) (Event, error)
|
||||
GetUserEvents(userID int, params RetrieveQueryParams) ([]Event, error)
|
||||
GetEvents(projectID int, params RetrieveQueryParams) ([]Event, error)
|
||||
|
||||
GetAPITokens(userID int) ([]APIToken, error)
|
||||
CreateAPIToken(token APIToken) (APIToken, error)
|
||||
@ -104,10 +111,6 @@ type Store interface {
|
||||
ExpireSession(userID int, sessionID int) error
|
||||
TouchSession(userID int, sessionID int) error
|
||||
|
||||
|
||||
GetUserEvents(userID int, params RetrieveQueryParams) ([]Event, error)
|
||||
GetEvents(projectID int, params RetrieveQueryParams) ([]Event, error)
|
||||
|
||||
Sql() *gorp.DbMap
|
||||
}
|
||||
|
||||
|
847
db/sql/SqlDb.go
847
db/sql/SqlDb.go
@ -10,7 +10,6 @@ import (
|
||||
_ "github.com/go-sql-driver/mysql" // imports mysql driver
|
||||
"github.com/gobuffalo/packr"
|
||||
"github.com/masterminds/squirrel"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
@ -189,14 +188,8 @@ func createDb() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (d *SqlDb) getObject(projectID int, tableName string, objectID int, object interface{}) (err error) {
|
||||
query, args, err := squirrel.Select("*").
|
||||
From(tableName).
|
||||
Where("project_id=?", projectID).
|
||||
Where("id=?", objectID).
|
||||
ToSql()
|
||||
func (d *SqlDb) getObjectByQuery(q squirrel.SelectBuilder, object interface{}) (err error) {
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
@ -211,9 +204,42 @@ func (d *SqlDb) getObject(projectID int, tableName string, objectID int, object
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) getObjects(projectID int, tableName string, sortableColumns []string, params db.RetrieveQueryParams, objects interface{}) (err error) {
|
||||
type objectProperties struct {
|
||||
TableName string
|
||||
SortableColumns []string
|
||||
IsGlobal bool
|
||||
TemplateColumnName string
|
||||
}
|
||||
|
||||
func (d *SqlDb) getObject(projectID int, props objectProperties, objectID int, object interface{}) (err error) {
|
||||
q := squirrel.Select("*").
|
||||
From(tableName + " pe").
|
||||
From(props.TableName).
|
||||
Where("id=?", objectID)
|
||||
|
||||
if props.IsGlobal {
|
||||
q = q.Where("project_id is null")
|
||||
} else {
|
||||
q = q.Where("project_id=?", projectID)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = d.sql.SelectOne(object, query, args...)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) getObjects(projectID int, props objectProperties, params db.RetrieveQueryParams, objects interface{}) (err error) {
|
||||
q := squirrel.Select("*").
|
||||
From(props.TableName + " pe").
|
||||
Where("pe.project_id=?", projectID)
|
||||
|
||||
orderDirection := "ASC"
|
||||
@ -222,7 +248,7 @@ func (d *SqlDb) getObjects(projectID int, tableName string, sortableColumns []st
|
||||
}
|
||||
|
||||
orderColumn := "name"
|
||||
if containsStr(sortableColumns, params.SortBy) {
|
||||
if containsStr(props.SortableColumns, params.SortBy) {
|
||||
orderColumn = params.SortBy
|
||||
}
|
||||
|
||||
@ -239,9 +265,9 @@ func (d *SqlDb) getObjects(projectID int, tableName string, sortableColumns []st
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) isObjectInUse(projectID int, templateColumnName string, objectID int) (bool, error) {
|
||||
func (d *SqlDb) isObjectInUse(projectID int, props objectProperties, objectID int) (bool, error) {
|
||||
templatesC, err := d.sql.SelectInt(
|
||||
"select count(1) from project__template where project_id=? and " + templateColumnName + "=?",
|
||||
"select count(1) from project__template where project_id=? and " + props.TemplateColumnName + "=?",
|
||||
projectID,
|
||||
objectID)
|
||||
|
||||
@ -252,8 +278,8 @@ func (d *SqlDb) isObjectInUse(projectID int, templateColumnName string, objectID
|
||||
return templatesC > 0, nil
|
||||
}
|
||||
|
||||
func (d *SqlDb) deleteObject(projectID int, tableName string, templateColumnName string, objectID int) error {
|
||||
inUse, err := d.isObjectInUse(projectID, templateColumnName, objectID)
|
||||
func (d *SqlDb) deleteObject(projectID int, props objectProperties, objectID int) error {
|
||||
inUse, err := d.isObjectInUse(projectID, props, objectID)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -265,21 +291,19 @@ func (d *SqlDb) deleteObject(projectID int, tableName string, templateColumnName
|
||||
|
||||
return validateMutationResult(
|
||||
d.sql.Exec(
|
||||
"delete from " + tableName + " where project_id=? and id=?",
|
||||
"delete from " + props.TableName + " where project_id=? and id=?",
|
||||
projectID,
|
||||
objectID))
|
||||
}
|
||||
|
||||
func (d *SqlDb) deleteObjectSoft(projectID int, tableName string, objectID int) error {
|
||||
func (d *SqlDb) deleteObjectSoft(projectID int, props objectProperties, objectID int) error {
|
||||
return validateMutationResult(
|
||||
d.sql.Exec(
|
||||
"update " + tableName + " set removed=1 where project_id=? and id=?",
|
||||
"update " + props.TableName + " set removed=1 where project_id=? and id=?",
|
||||
projectID,
|
||||
objectID))
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (d *SqlDb) Migrate() error {
|
||||
fmt.Println("Checking DB migrations")
|
||||
didRun := false
|
||||
@ -363,261 +387,6 @@ func (d *SqlDb) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
|
||||
project.Created = time.Now()
|
||||
|
||||
res, err := d.sql.Exec("insert into project(name, created) values (?, ?)", project.Name, project.Created)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertId, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newProject = project
|
||||
newProject.ID = int(insertId)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjects(userID int) (projects []db.Project, err error) {
|
||||
query, args, err := squirrel.Select("p.*").
|
||||
From("project as p").
|
||||
Join("project__user as pu on pu.project_id=p.id").
|
||||
Where("pu.user_id=?", userID).
|
||||
OrderBy("p.name").
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&projects, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProject(projectID int) (project db.Project, err error) {
|
||||
query, args, err := squirrel.Select("p.*").
|
||||
From("project as p").
|
||||
Where("p.id=?", projectID).
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = d.sql.SelectOne(&project, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteProject(projectID int) error {
|
||||
tx, err := d.sql.Begin()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
statements := []string{
|
||||
"delete from project__template where project_id=?",
|
||||
"delete from project__user where project_id=?",
|
||||
"delete from project__repository where project_id=?",
|
||||
"delete from project__inventory where project_id=?",
|
||||
"delete from access_key where project_id=?",
|
||||
"delete from project where id=?",
|
||||
}
|
||||
|
||||
for _, statement := range statements {
|
||||
_, err = tx.Exec(statement, projectID)
|
||||
|
||||
if err != nil {
|
||||
err = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateProject(project db.Project) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project set name=?, alert=?, alert_chat=? where id=?",
|
||||
project.Name,
|
||||
project.Alert,
|
||||
project.AlertChat,
|
||||
project.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateUser(user db.User) (newUser db.User, err error) {
|
||||
pwdHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 11)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
created := db.GetParsedTime(time.Now())
|
||||
|
||||
res, err := d.sql.Exec(
|
||||
"insert into `user`(name, username, email, password, admin, created) values (?, ?, ?, ?, true, ?)",
|
||||
user.Name,
|
||||
user.Username,
|
||||
user.Email,
|
||||
pwdHash,
|
||||
created)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newUser = user
|
||||
newUser.ID = int(insertID)
|
||||
newUser.Created = created
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteUser(userID int) error {
|
||||
res, err := d.sql.Exec("delete from `user` where id=?", userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateUser(user db.User) error {
|
||||
_, err := d.sql.Exec("update `user` set name=?, username=?, email=?, alert=?, admin=? where id=?",
|
||||
user.Name,
|
||||
user.Username,
|
||||
user.Email,
|
||||
user.Alert,
|
||||
user.Admin,
|
||||
user.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) SetUserPassword(userID int, password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), 11)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = d.sql.Exec("update `user` set password=? where id=?", string(hash), userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateProjectUser(projectUser db.ProjectUser) (newProjectUser db.ProjectUser, err error) {
|
||||
_, err = d.sql.Exec("insert into project__user (project_id, user_id, `admin`) values (?, ?, ?)",
|
||||
projectUser.ProjectID,
|
||||
projectUser.UserID,
|
||||
projectUser.Admin)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newProjectUser = projectUser
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjectUser(projectID, userID int) (db.ProjectUser, error) {
|
||||
var user db.ProjectUser
|
||||
|
||||
err := d.sql.SelectOne(&user,
|
||||
"select * from project__user where project_id=? and user_id=?",
|
||||
projectID,
|
||||
userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjectUsers(projectID int, params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
q := squirrel.Select("u.*").Column("pu.admin").
|
||||
From("project__user as pu").
|
||||
LeftJoin("user as u on pu.user_id=u.id").
|
||||
Where("pu.project_id=?", projectID)
|
||||
|
||||
sortDirection := "ASC"
|
||||
if params.SortInverted {
|
||||
sortDirection = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "name", "username", "email":
|
||||
q = q.OrderBy("u." + params.SortBy + " " + sortDirection)
|
||||
case "admin":
|
||||
q = q.OrderBy("pu." + params.SortBy + " " + sortDirection)
|
||||
default:
|
||||
q = q.OrderBy("u.name " + sortDirection)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&users, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateProjectUser(projectUser db.ProjectUser) error {
|
||||
_, err := d.sql.Exec("update `project__user` set admin=? where user_id=? and project_id = ?",
|
||||
projectUser.Admin,
|
||||
projectUser.UserID,
|
||||
projectUser.ProjectID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
|
||||
var created = time.Now()
|
||||
|
||||
_, err = d.sql.Exec(
|
||||
"insert into event(project_id, object_id, object_type, description, created) values (?, ?, ?, ?, ?)",
|
||||
evt.ProjectID,
|
||||
evt.ObjectID,
|
||||
evt.ObjectType,
|
||||
evt.Description,
|
||||
created)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newEvent = evt
|
||||
newEvent.Created = created
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteProjectUser(projectID, userID int) error {
|
||||
_, err := d.sql.Exec("delete from project__user where user_id=? and project_id=?", userID, projectID)
|
||||
return err
|
||||
}
|
||||
|
||||
//FetchUser retrieves a user from the database by ID
|
||||
func (d *SqlDb) GetUser(userID int) (db.User, error) {
|
||||
var user db.User
|
||||
|
||||
err := d.sql.SelectOne(&user, "select * from `user` where id=?", userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return user, err
|
||||
}
|
||||
|
||||
func getSqlForTable(tableName string, p db.RetrieveQueryParams) (string, []interface{}, error) {
|
||||
if p.Count > 0 && p.Offset <= 0 {
|
||||
return "", nil, fmt.Errorf("offset cannot be without limit")
|
||||
@ -646,537 +415,11 @@ func getSqlForTable(tableName string, p db.RetrieveQueryParams) (string, []inter
|
||||
return q.ToSql()
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
query, args, err := getSqlForTable("user", params)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&users, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) Sql() *gorp.DbMap {
|
||||
return d.sql
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) {
|
||||
token.Created = db.GetParsedTime(time.Now())
|
||||
err := d.sql.Insert(&token)
|
||||
return token, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAPIToken(tokenID string) (token db.APIToken, err error) {
|
||||
err = d.sql.SelectOne(&token, "select * from user__token where id=? and expired=0", tokenID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) ExpireAPIToken(userID int, tokenID string) (err error) {
|
||||
res, err := d.sql.Exec("update user__token set expired=1 where id=? and user_id=?", tokenID, userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetSession(userID int, sessionID int) (session db.Session, err error) {
|
||||
err = d.sql.SelectOne(&session, "select * from session where id=? and user_id=? and expired=0", sessionID, userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) ExpireSession(userID int, sessionID int) error {
|
||||
res, err := d.sql.Exec("update session set expired=1 where id=? and user_id=?", sessionID, userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) TouchSession(userID int, sessionID int) error {
|
||||
_, err := d.sql.Exec("update session set last_active=? where id=? and user_id=?", time.Now(), sessionID, userID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
|
||||
_, err = d.sql.Select(&tokens, "select * from user__token where user_id=?", userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEnvironment(projectID int, environmentID int) (db.Environment, error) {
|
||||
var environment db.Environment
|
||||
err := d.getObject(projectID, "project__environment", environmentID, &environment)
|
||||
return environment, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) ([]db.Environment, error) {
|
||||
var environment []db.Environment
|
||||
err := d.getObjects(projectID, "project__environment", []string{"name"}, params, &environment)
|
||||
return environment, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateEnvironment(env db.Environment) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__environment set name=?, json=? where id=?",
|
||||
env.Name,
|
||||
env.JSON,
|
||||
env.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__environment (project_id, name, json, password) values (?, ?, ?, ?)",
|
||||
env.ProjectID,
|
||||
env.Name,
|
||||
env.JSON,
|
||||
env.Password)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newEnv = env
|
||||
newEnv.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteEnvironment(projectID int, environmentID int) error {
|
||||
return d.deleteObject(projectID, "project__environment", "environment_id", environmentID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteEnvironmentSoft(projectID int, environmentID int) error {
|
||||
return d.deleteObjectSoft(projectID, "project__environment", environmentID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
|
||||
res, err := d.sql.Exec("insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=?",
|
||||
template.SSHKeyID,
|
||||
template.ProjectID,
|
||||
template.InventoryID,
|
||||
template.RepositoryID,
|
||||
template.EnvironmentID,
|
||||
template.Alias,
|
||||
template.Playbook,
|
||||
template.Arguments,
|
||||
template.OverrideArguments)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newTemplate = template
|
||||
newTemplate.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateTemplate(template db.Template) error {
|
||||
_, err := d.sql.Exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=? where id=?",
|
||||
template.SSHKeyID,
|
||||
template.InventoryID,
|
||||
template.RepositoryID,
|
||||
template.EnvironmentID,
|
||||
template.Alias,
|
||||
template.Playbook,
|
||||
template.Arguments,
|
||||
template.OverrideArguments,
|
||||
template.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
||||
q := squirrel.Select("pt.id",
|
||||
"pt.ssh_key_id",
|
||||
"pt.project_id",
|
||||
"pt.inventory_id",
|
||||
"pt.repository_id",
|
||||
"pt.environment_id",
|
||||
"pt.alias",
|
||||
"pt.playbook",
|
||||
"pt.arguments",
|
||||
"pt.override_args").
|
||||
From("project__template pt")
|
||||
|
||||
order := "ASC"
|
||||
if params.SortInverted {
|
||||
order = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "alias", "playbook":
|
||||
q = q.Where("pt.project_id=?", projectID).
|
||||
OrderBy("pt." + params.SortBy + " " + order)
|
||||
case "ssh_key":
|
||||
q = q.LeftJoin("access_key ak ON (pt.ssh_key_id = ak.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("ak.name " + order)
|
||||
case "inventory":
|
||||
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pi.name " + order)
|
||||
case "environment":
|
||||
q = q.LeftJoin("project__environment pe ON (pt.environment_id = pe.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pe.name " + order)
|
||||
case "repository":
|
||||
q = q.LeftJoin("project__repository pr ON (pt.repository_id = pr.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pr.name " + order)
|
||||
default:
|
||||
q = q.Where("pt.project_id=?", projectID).
|
||||
OrderBy("pt.alias " + order)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&templates, query, args...)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplate(projectID int, templateID int) (db.Template, error) {
|
||||
var template db.Template
|
||||
|
||||
err := d.sql.SelectOne(
|
||||
&template,
|
||||
"select * from project__template where project_id=? and id=?",
|
||||
projectID,
|
||||
templateID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return template, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteTemplate(projectID int, templateID int) error {
|
||||
res, err := d.sql.Exec(
|
||||
"delete from project__template where project_id=? and id=?",
|
||||
projectID,
|
||||
templateID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetInventory(projectID int, inventoryID int) (db.Inventory, error) {
|
||||
var inventory db.Inventory
|
||||
err := d.getObject(projectID, "project__inventory", inventoryID, &inventory)
|
||||
return inventory, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams) ([]db.Inventory, error) {
|
||||
var inventories []db.Inventory
|
||||
err := d.getObjects(projectID, "project__inventory", []string{"name"}, params, &inventories)
|
||||
return inventories, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error {
|
||||
return d.deleteObject(projectID, "project__inventory", "inventory_id", inventoryID);
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteInventorySoft(projectID int, inventoryID int) error {
|
||||
return d.deleteObjectSoft(projectID, "project__inventory", inventoryID)
|
||||
}
|
||||
|
||||
|
||||
func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__inventory set name=?, type=?, key_id=?, ssh_key_id=?, inventory=? where id=?",
|
||||
inventory.Name,
|
||||
inventory.Type,
|
||||
inventory.KeyID,
|
||||
inventory.SSHKeyID,
|
||||
inventory.Inventory,
|
||||
inventory.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__inventory set project_id=?, name=?, type=?, key_id=?, ssh_key_id=?, inventory=?",
|
||||
inventory.ProjectID,
|
||||
inventory.Name,
|
||||
inventory.Type,
|
||||
inventory.KeyID,
|
||||
inventory.SSHKeyID,
|
||||
inventory.Inventory)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newInventory = inventory
|
||||
newInventory.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (d *SqlDb) GetRepository(projectID int, repositoryID int) (db.Repository, error) {
|
||||
var repository db.Repository
|
||||
err := d.getObject(projectID, "project__repository", repositoryID, &repository)
|
||||
return repository, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetRepositories(projectID int, params db.RetrieveQueryParams) (repositories []db.Repository, err error) {
|
||||
q := squirrel.Select("*").
|
||||
From("project__repository pr")
|
||||
|
||||
order := "ASC"
|
||||
if params.SortInverted {
|
||||
order = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "name", "git_url":
|
||||
q = q.Where("pr.project_id=?", projectID).
|
||||
OrderBy("pr." + params.SortBy + " " + order)
|
||||
case "ssh_key":
|
||||
q = q.LeftJoin("access_key ak ON (pr.ssh_key_id = ak.id)").
|
||||
Where("pr.project_id=?", projectID).
|
||||
OrderBy("ak.name " + order)
|
||||
default:
|
||||
q = q.Where("pr.project_id=?", projectID).
|
||||
OrderBy("pr.name " + order)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&repositories, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateRepository(repository db.Repository) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__repository set name=?, git_url=?, ssh_key_id=? where id=?",
|
||||
repository.Name,
|
||||
repository.GitURL,
|
||||
repository.SSHKeyID,
|
||||
repository.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateRepository(repository db.Repository) (newRepo db.Repository, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__repository(project_id, git_url, ssh_key_id, name) values (?, ?, ?, ?)",
|
||||
repository.ProjectID,
|
||||
repository.GitURL,
|
||||
repository.SSHKeyID,
|
||||
repository.Name)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newRepo = repository
|
||||
newRepo.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteRepository(projectID int, repositoryId int) error {
|
||||
return d.deleteObject(projectID, "project__repository", "repository_id", repositoryId)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteRepositorySoft(projectID int, repositoryId int) error {
|
||||
return d.deleteObjectSoft(projectID, "project__repository", repositoryId)
|
||||
}
|
||||
|
||||
|
||||
func (d *SqlDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, error) {
|
||||
var key db.AccessKey
|
||||
err := d.getObject(projectID, "access_key", accessKeyID, &key)
|
||||
return key, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
||||
var keys []db.AccessKey
|
||||
err := d.getObjects(projectID, "access_key", []string{"name", "type"}, params, &keys)
|
||||
return keys, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateAccessKey(key db.AccessKey) error {
|
||||
res, err := d.sql.Exec(
|
||||
"update access_key set name=?, type=?, `key`=?, secret=? where id=?",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.Key,
|
||||
key.Secret,
|
||||
key.ID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateAccessKey(key db.AccessKey) (newKey db.AccessKey, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into access_key (name, type, project_id, `key`, secret) values (?, ?, ?, ?, ?)",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.ProjectID,
|
||||
key.Key,
|
||||
key.Secret)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newKey = key
|
||||
newKey.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteAccessKey(projectID int, accessKeyID int) error {
|
||||
return d.deleteObject(projectID, "access_key", "ssh_key_id", accessKeyID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error {
|
||||
return d.deleteObjectSoft(projectID, "access_key", accessKeyID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) getEventObjectName(evt db.Event) (string, error) {
|
||||
if evt.ObjectID == nil || evt.ObjectType == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
var q squirrel.SelectBuilder
|
||||
|
||||
switch *evt.ObjectType {
|
||||
case "task":
|
||||
q = squirrel.Select("case when length(task.playbook) > 0 then task.playbook else tpl.playbook end").
|
||||
From("task").
|
||||
Join("project__template as tpl on task.template_id=tpl.id").
|
||||
Where("task.id=?", evt.ObjectID)
|
||||
default:
|
||||
return "", nil
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var name sql.NullString
|
||||
name, err = d.sql.SelectNullStr(query, args...)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if name.Valid {
|
||||
return name.String, nil
|
||||
} else {
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *SqlDb) getEvents(q squirrel.SelectBuilder, params db.RetrieveQueryParams) (events []db.Event, err error) {
|
||||
|
||||
if params.Count > 0 {
|
||||
q = q.Limit(uint64(params.Count))
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&events, query, args...)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i, evt := range events {
|
||||
var objName string
|
||||
objName, err = d.getEventObjectName(evt)
|
||||
if objName == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
events[i].ObjectName = objName
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUserEvents(userID int, params db.RetrieveQueryParams) ([]db.Event, error) {
|
||||
q := squirrel.Select("event.*, p.name as project_name").
|
||||
From("event").
|
||||
LeftJoin("project as p on event.project_id=p.id").
|
||||
OrderBy("created desc").
|
||||
LeftJoin("project__user as pu on pu.project_id=p.id").
|
||||
Where("p.id IS NULL or pu.user_id=?", userID)
|
||||
|
||||
return d.getEvents(q, params)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEvents(projectID int, params db.RetrieveQueryParams) ([]db.Event, error) {
|
||||
q := squirrel.Select("event.*, p.name as project_name").
|
||||
From("event").
|
||||
LeftJoin("project as p on event.project_id=p.id").
|
||||
OrderBy("created desc").
|
||||
Where("event.project_id=?", projectID)
|
||||
|
||||
return d.getEvents(q, params)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) {
|
||||
err = d.sql.SelectOne(&existingUser, "select * from `user` where email=? or username=?", email, login)
|
||||
return
|
||||
}
|
||||
|
127
db/sql/access_key.go
Normal file
127
db/sql/access_key.go
Normal file
@ -0,0 +1,127 @@
|
||||
package sql
|
||||
|
||||
import "github.com/ansible-semaphore/semaphore/db"
|
||||
|
||||
var accessKeyObject = objectProperties{
|
||||
TableName: "access_key",
|
||||
SortableColumns: []string{"name", "type"},
|
||||
TemplateColumnName: "ssh_key_id",
|
||||
}
|
||||
|
||||
var globalAccessKeyObject = objectProperties{
|
||||
IsGlobal: true,
|
||||
TableName: "access_key",
|
||||
SortableColumns: []string{"name", "type"},
|
||||
TemplateColumnName: "ssh_key_id",
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAccessKey(projectID int, accessKeyID int) (db.AccessKey, error) {
|
||||
var key db.AccessKey
|
||||
err := d.getObject(projectID, accessKeyObject, accessKeyID, &key)
|
||||
return key, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
||||
var keys []db.AccessKey
|
||||
err := d.getObjects(projectID, accessKeyObject, params, &keys)
|
||||
return keys, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateAccessKey(key db.AccessKey) error {
|
||||
res, err := d.sql.Exec(
|
||||
"update access_key set name=?, type=?, `key`=?, secret=? where project_id=? and id=?",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.Key,
|
||||
key.Secret,
|
||||
key.ProjectID,
|
||||
key.ID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateAccessKey(key db.AccessKey) (newKey db.AccessKey, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into access_key (name, type, project_id, `key`, secret) values (?, ?, ?, ?, ?)",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.ProjectID,
|
||||
key.Key,
|
||||
key.Secret)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newKey = key
|
||||
newKey.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteAccessKey(projectID int, accessKeyID int) error {
|
||||
return d.deleteObject(projectID, accessKeyObject, accessKeyID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error {
|
||||
return d.deleteObjectSoft(projectID, accessKeyObject, accessKeyID)
|
||||
}
|
||||
|
||||
|
||||
func (d *SqlDb) GetGlobalAccessKey(accessKeyID int) (db.AccessKey, error) {
|
||||
var key db.AccessKey
|
||||
err := d.getObject(0, globalAccessKeyObject, accessKeyID, &key)
|
||||
return key, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetGlobalAccessKeys(params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
||||
var keys []db.AccessKey
|
||||
err := d.getObjects(0, globalAccessKeyObject, params, &keys)
|
||||
return keys, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateGlobalAccessKey(key db.AccessKey) error {
|
||||
res, err := d.sql.Exec(
|
||||
"update access_key set name=?, type=?, `key`=?, secret=? where id=?",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.Key,
|
||||
key.Secret,
|
||||
key.ID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateGlobalAccessKey(key db.AccessKey) (newKey db.AccessKey, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into access_key (name, type, `key`, secret) values (?, ?, ?, ?)",
|
||||
key.Name,
|
||||
key.Type,
|
||||
key.Key,
|
||||
key.Secret)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newKey = key
|
||||
newKey.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteGlobalAccessKey(accessKeyID int) error {
|
||||
return d.deleteObject(0, globalAccessKeyObject, accessKeyID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteGlobalAccessKeySoft(accessKeyID int) error {
|
||||
return d.deleteObjectSoft(0, globalAccessKeyObject, accessKeyID)
|
||||
}
|
60
db/sql/environment.go
Normal file
60
db/sql/environment.go
Normal file
@ -0,0 +1,60 @@
|
||||
package sql
|
||||
|
||||
import "github.com/ansible-semaphore/semaphore/db"
|
||||
|
||||
var environmentObject = objectProperties{
|
||||
TableName: "project__environment",
|
||||
SortableColumns: []string{"name"},
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEnvironment(projectID int, environmentID int) (db.Environment, error) {
|
||||
var environment db.Environment
|
||||
err := d.getObject(projectID, environmentObject, environmentID, &environment)
|
||||
return environment, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) ([]db.Environment, error) {
|
||||
var environment []db.Environment
|
||||
err := d.getObjects(projectID, environmentObject, params, &environment)
|
||||
return environment, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateEnvironment(env db.Environment) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__environment set name=?, json=? where id=?",
|
||||
env.Name,
|
||||
env.JSON,
|
||||
env.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__environment (project_id, name, json, password) values (?, ?, ?, ?)",
|
||||
env.ProjectID,
|
||||
env.Name,
|
||||
env.JSON,
|
||||
env.Password)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newEnv = env
|
||||
newEnv.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteEnvironment(projectID int, environmentID int) error {
|
||||
return d.deleteObject(projectID, environmentObject, environmentID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteEnvironmentSoft(projectID int, environmentID int) error {
|
||||
return d.deleteObjectSoft(projectID, environmentObject, environmentID)
|
||||
}
|
121
db/sql/event.go
Normal file
121
db/sql/event.go
Normal file
@ -0,0 +1,121 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *SqlDb) getEventObjectName(evt db.Event) (string, error) {
|
||||
if evt.ObjectID == nil || evt.ObjectType == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
var q squirrel.SelectBuilder
|
||||
|
||||
switch *evt.ObjectType {
|
||||
case "task":
|
||||
q = squirrel.Select("case when length(task.playbook) > 0 then task.playbook else tpl.playbook end").
|
||||
From("task").
|
||||
Join("project__template as tpl on task.template_id=tpl.id").
|
||||
Where("task.id=?", evt.ObjectID)
|
||||
default:
|
||||
return "", nil
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var name sql.NullString
|
||||
name, err = d.sql.SelectNullStr(query, args...)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if name.Valid {
|
||||
return name.String, nil
|
||||
} else {
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *SqlDb) getEvents(q squirrel.SelectBuilder, params db.RetrieveQueryParams) (events []db.Event, err error) {
|
||||
|
||||
if params.Count > 0 {
|
||||
q = q.Limit(uint64(params.Count))
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&events, query, args...)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for i, evt := range events {
|
||||
var objName string
|
||||
objName, err = d.getEventObjectName(evt)
|
||||
if objName == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
events[i].ObjectName = objName
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
|
||||
var created = time.Now()
|
||||
|
||||
_, err = d.sql.Exec(
|
||||
"insert into event(project_id, object_id, object_type, description, created) values (?, ?, ?, ?, ?)",
|
||||
evt.ProjectID,
|
||||
evt.ObjectID,
|
||||
evt.ObjectType,
|
||||
evt.Description,
|
||||
created)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newEvent = evt
|
||||
newEvent.Created = created
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUserEvents(userID int, params db.RetrieveQueryParams) ([]db.Event, error) {
|
||||
q := squirrel.Select("event.*, p.name as project_name").
|
||||
From("event").
|
||||
LeftJoin("project as p on event.project_id=p.id").
|
||||
OrderBy("created desc").
|
||||
LeftJoin("project__user as pu on pu.project_id=p.id").
|
||||
Where("p.id IS NULL or pu.user_id=?", userID)
|
||||
|
||||
return d.getEvents(q, params)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetEvents(projectID int, params db.RetrieveQueryParams) ([]db.Event, error) {
|
||||
q := squirrel.Select("event.*, p.name as project_name").
|
||||
From("event").
|
||||
LeftJoin("project as p on event.project_id=p.id").
|
||||
OrderBy("created desc").
|
||||
Where("event.project_id=?", projectID)
|
||||
|
||||
return d.getEvents(q, params)
|
||||
}
|
69
db/sql/inventory.go
Normal file
69
db/sql/inventory.go
Normal file
@ -0,0 +1,69 @@
|
||||
package sql
|
||||
|
||||
import "github.com/ansible-semaphore/semaphore/db"
|
||||
|
||||
var inventoryObject = objectProperties{
|
||||
TableName: "project__inventory",
|
||||
SortableColumns: []string{"name"},
|
||||
TemplateColumnName: "inventory_id",
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetInventory(projectID int, inventoryID int) (db.Inventory, error) {
|
||||
var inventory db.Inventory
|
||||
err := d.getObject(projectID, inventoryObject, inventoryID, &inventory)
|
||||
return inventory, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetInventories(projectID int, params db.RetrieveQueryParams) ([]db.Inventory, error) {
|
||||
var inventories []db.Inventory
|
||||
err := d.getObjects(projectID, inventoryObject, params, &inventories)
|
||||
return inventories, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteInventory(projectID int, inventoryID int) error {
|
||||
return d.deleteObject(projectID, inventoryObject, inventoryID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteInventorySoft(projectID int, inventoryID int) error {
|
||||
return d.deleteObjectSoft(projectID, inventoryObject, inventoryID)
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateInventory(inventory db.Inventory) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__inventory set name=?, type=?, key_id=?, ssh_key_id=?, inventory=? where id=?",
|
||||
inventory.Name,
|
||||
inventory.Type,
|
||||
inventory.KeyID,
|
||||
inventory.SSHKeyID,
|
||||
inventory.Inventory,
|
||||
inventory.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateInventory(inventory db.Inventory) (newInventory db.Inventory, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__inventory set project_id=?, name=?, type=?, key_id=?, ssh_key_id=?, inventory=?",
|
||||
inventory.ProjectID,
|
||||
inventory.Name,
|
||||
inventory.Type,
|
||||
inventory.KeyID,
|
||||
inventory.SSHKeyID,
|
||||
inventory.Inventory)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newInventory = inventory
|
||||
newInventory.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
95
db/sql/project.go
Normal file
95
db/sql/project.go
Normal file
@ -0,0 +1,95 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *SqlDb) CreateProject(project db.Project) (newProject db.Project, err error) {
|
||||
project.Created = time.Now()
|
||||
|
||||
res, err := d.sql.Exec("insert into project(name, created) values (?, ?)", project.Name, project.Created)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertId, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newProject = project
|
||||
newProject.ID = int(insertId)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjects(userID int) (projects []db.Project, err error) {
|
||||
query, args, err := squirrel.Select("p.*").
|
||||
From("project as p").
|
||||
Join("project__user as pu on pu.project_id=p.id").
|
||||
Where("pu.user_id=?", userID).
|
||||
OrderBy("p.name").
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&projects, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProject(projectID int) (project db.Project, err error) {
|
||||
query, args, err := squirrel.Select("p.*").
|
||||
From("project as p").
|
||||
Where("p.id=?", projectID).
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = d.sql.SelectOne(&project, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteProject(projectID int) error {
|
||||
tx, err := d.sql.Begin()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
statements := []string{
|
||||
"delete from project__template where project_id=?",
|
||||
"delete from project__user where project_id=?",
|
||||
"delete from project__repository where project_id=?",
|
||||
"delete from project__inventory where project_id=?",
|
||||
"delete from access_key where project_id=?",
|
||||
"delete from project where id=?",
|
||||
}
|
||||
|
||||
for _, statement := range statements {
|
||||
_, err = tx.Exec(statement, projectID)
|
||||
|
||||
if err != nil {
|
||||
err = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateProject(project db.Project) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project set name=?, alert=?, alert_chat=? where id=?",
|
||||
project.Name,
|
||||
project.Alert,
|
||||
project.AlertChat,
|
||||
project.ID)
|
||||
return err
|
||||
}
|
93
db/sql/repository.go
Normal file
93
db/sql/repository.go
Normal file
@ -0,0 +1,93 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
)
|
||||
|
||||
|
||||
var repositoryObject = objectProperties{
|
||||
TableName: "project__repository",
|
||||
TemplateColumnName: "repository_id",
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetRepository(projectID int, repositoryID int) (db.Repository, error) {
|
||||
var repository db.Repository
|
||||
err := d.getObject(projectID, repositoryObject, repositoryID, &repository)
|
||||
return repository, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetRepositories(projectID int, params db.RetrieveQueryParams) (repositories []db.Repository, err error) {
|
||||
q := squirrel.Select("*").
|
||||
From("project__repository pr")
|
||||
|
||||
order := "ASC"
|
||||
if params.SortInverted {
|
||||
order = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "name", "git_url":
|
||||
q = q.Where("pr.project_id=?", projectID).
|
||||
OrderBy("pr." + params.SortBy + " " + order)
|
||||
case "ssh_key":
|
||||
q = q.LeftJoin("access_key ak ON (pr.ssh_key_id = ak.id)").
|
||||
Where("pr.project_id=?", projectID).
|
||||
OrderBy("ak.name " + order)
|
||||
default:
|
||||
q = q.Where("pr.project_id=?", projectID).
|
||||
OrderBy("pr.name " + order)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&repositories, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateRepository(repository db.Repository) error {
|
||||
_, err := d.sql.Exec(
|
||||
"update project__repository set name=?, git_url=?, ssh_key_id=? where id=?",
|
||||
repository.Name,
|
||||
repository.GitURL,
|
||||
repository.SSHKeyID,
|
||||
repository.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateRepository(repository db.Repository) (newRepo db.Repository, err error) {
|
||||
res, err := d.sql.Exec(
|
||||
"insert into project__repository(project_id, git_url, ssh_key_id, name) values (?, ?, ?, ?)",
|
||||
repository.ProjectID,
|
||||
repository.GitURL,
|
||||
repository.SSHKeyID,
|
||||
repository.Name)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newRepo = repository
|
||||
newRepo.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteRepository(projectID int, repositoryId int) error {
|
||||
return d.deleteObject(projectID, repositoryObject, repositoryId)
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteRepositorySoft(projectID int, repositoryId int) error {
|
||||
return d.deleteObjectSoft(projectID, repositoryObject, repositoryId)
|
||||
}
|
||||
|
62
db/sql/session.go
Normal file
62
db/sql/session.go
Normal file
@ -0,0 +1,62 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *SqlDb) CreateAPIToken(token db.APIToken) (db.APIToken, error) {
|
||||
token.Created = db.GetParsedTime(time.Now())
|
||||
err := d.sql.Insert(&token)
|
||||
return token, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAPIToken(tokenID string) (token db.APIToken, err error) {
|
||||
err = d.sql.SelectOne(&token, "select * from user__token where id=? and expired=0", tokenID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) ExpireAPIToken(userID int, tokenID string) (err error) {
|
||||
res, err := d.sql.Exec("update user__token set expired=1 where id=? and user_id=?", tokenID, userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetSession(userID int, sessionID int) (session db.Session, err error) {
|
||||
err = d.sql.SelectOne(&session, "select * from session where id=? and user_id=? and expired=0", sessionID, userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) ExpireSession(userID int, sessionID int) error {
|
||||
res, err := d.sql.Exec("update session set expired=1 where id=? and user_id=?", sessionID, userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) TouchSession(userID int, sessionID int) error {
|
||||
_, err := d.sql.Exec("update session set last_active=? where id=? and user_id=?", time.Now(), sessionID, userID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetAPITokens(userID int) (tokens []db.APIToken, err error) {
|
||||
_, err = d.sql.Select(&tokens, "select * from user__token where user_id=?", userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
126
db/sql/template.go
Normal file
126
db/sql/template.go
Normal file
@ -0,0 +1,126 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
)
|
||||
|
||||
func (d *SqlDb) CreateTemplate(template db.Template) (newTemplate db.Template, err error) {
|
||||
res, err := d.sql.Exec("insert into project__template set ssh_key_id=?, project_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=?",
|
||||
template.SSHKeyID,
|
||||
template.ProjectID,
|
||||
template.InventoryID,
|
||||
template.RepositoryID,
|
||||
template.EnvironmentID,
|
||||
template.Alias,
|
||||
template.Playbook,
|
||||
template.Arguments,
|
||||
template.OverrideArguments)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newTemplate = template
|
||||
newTemplate.ID = int(insertID)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateTemplate(template db.Template) error {
|
||||
_, err := d.sql.Exec("update project__template set ssh_key_id=?, inventory_id=?, repository_id=?, environment_id=?, alias=?, playbook=?, arguments=?, override_args=? where id=?",
|
||||
template.SSHKeyID,
|
||||
template.InventoryID,
|
||||
template.RepositoryID,
|
||||
template.EnvironmentID,
|
||||
template.Alias,
|
||||
template.Playbook,
|
||||
template.Arguments,
|
||||
template.OverrideArguments,
|
||||
template.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplates(projectID int, params db.RetrieveQueryParams) (templates []db.Template, err error) {
|
||||
q := squirrel.Select("pt.id",
|
||||
"pt.ssh_key_id",
|
||||
"pt.project_id",
|
||||
"pt.inventory_id",
|
||||
"pt.repository_id",
|
||||
"pt.environment_id",
|
||||
"pt.alias",
|
||||
"pt.playbook",
|
||||
"pt.arguments",
|
||||
"pt.override_args").
|
||||
From("project__template pt")
|
||||
|
||||
order := "ASC"
|
||||
if params.SortInverted {
|
||||
order = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "alias", "playbook":
|
||||
q = q.Where("pt.project_id=?", projectID).
|
||||
OrderBy("pt." + params.SortBy + " " + order)
|
||||
case "ssh_key":
|
||||
q = q.LeftJoin("access_key ak ON (pt.ssh_key_id = ak.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("ak.name " + order)
|
||||
case "inventory":
|
||||
q = q.LeftJoin("project__inventory pi ON (pt.inventory_id = pi.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pi.name " + order)
|
||||
case "environment":
|
||||
q = q.LeftJoin("project__environment pe ON (pt.environment_id = pe.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pe.name " + order)
|
||||
case "repository":
|
||||
q = q.LeftJoin("project__repository pr ON (pt.repository_id = pr.id)").
|
||||
Where("pt.project_id=?", projectID).
|
||||
OrderBy("pr.name " + order)
|
||||
default:
|
||||
q = q.Where("pt.project_id=?", projectID).
|
||||
OrderBy("pt.alias " + order)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&templates, query, args...)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetTemplate(projectID int, templateID int) (db.Template, error) {
|
||||
var template db.Template
|
||||
|
||||
err := d.sql.SelectOne(
|
||||
&template,
|
||||
"select * from project__template where project_id=? and id=?",
|
||||
projectID,
|
||||
templateID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return template, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteTemplate(projectID int, templateID int) error {
|
||||
res, err := d.sql.Exec(
|
||||
"delete from project__template where project_id=? and id=?",
|
||||
projectID,
|
||||
templateID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
173
db/sql/user.go
Normal file
173
db/sql/user.go
Normal file
@ -0,0 +1,173 @@
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/ansible-semaphore/semaphore/db"
|
||||
"github.com/masterminds/squirrel"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *SqlDb) CreateUser(user db.User) (newUser db.User, err error) {
|
||||
pwdHash, err := bcrypt.GenerateFromPassword([]byte(user.Password), 11)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
created := db.GetParsedTime(time.Now())
|
||||
|
||||
res, err := d.sql.Exec(
|
||||
"insert into `user`(name, username, email, password, admin, created) values (?, ?, ?, ?, true, ?)",
|
||||
user.Name,
|
||||
user.Username,
|
||||
user.Email,
|
||||
pwdHash,
|
||||
created)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
insertID, err := res.LastInsertId()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newUser = user
|
||||
newUser.ID = int(insertID)
|
||||
newUser.Created = created
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteUser(userID int) error {
|
||||
res, err := d.sql.Exec("delete from `user` where id=?", userID)
|
||||
|
||||
return validateMutationResult(res, err)
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateUser(user db.User) error {
|
||||
_, err := d.sql.Exec("update `user` set name=?, username=?, email=?, alert=?, admin=? where id=?",
|
||||
user.Name,
|
||||
user.Username,
|
||||
user.Email,
|
||||
user.Alert,
|
||||
user.Admin,
|
||||
user.ID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) SetUserPassword(userID int, password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), 11)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = d.sql.Exec("update `user` set password=? where id=?", string(hash), userID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) CreateProjectUser(projectUser db.ProjectUser) (newProjectUser db.ProjectUser, err error) {
|
||||
_, err = d.sql.Exec("insert into project__user (project_id, user_id, `admin`) values (?, ?, ?)",
|
||||
projectUser.ProjectID,
|
||||
projectUser.UserID,
|
||||
projectUser.Admin)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newProjectUser = projectUser
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjectUser(projectID, userID int) (db.ProjectUser, error) {
|
||||
var user db.ProjectUser
|
||||
|
||||
err := d.sql.SelectOne(&user,
|
||||
"select * from project__user where project_id=? and user_id=?",
|
||||
projectID,
|
||||
userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetProjectUsers(projectID int, params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
q := squirrel.Select("u.*").Column("pu.admin").
|
||||
From("project__user as pu").
|
||||
LeftJoin("user as u on pu.user_id=u.id").
|
||||
Where("pu.project_id=?", projectID)
|
||||
|
||||
sortDirection := "ASC"
|
||||
if params.SortInverted {
|
||||
sortDirection = "DESC"
|
||||
}
|
||||
|
||||
switch params.SortBy {
|
||||
case "name", "username", "email":
|
||||
q = q.OrderBy("u." + params.SortBy + " " + sortDirection)
|
||||
case "admin":
|
||||
q = q.OrderBy("pu." + params.SortBy + " " + sortDirection)
|
||||
default:
|
||||
q = q.OrderBy("u.name " + sortDirection)
|
||||
}
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&users, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) UpdateProjectUser(projectUser db.ProjectUser) error {
|
||||
_, err := d.sql.Exec("update `project__user` set admin=? where user_id=? and project_id = ?",
|
||||
projectUser.Admin,
|
||||
projectUser.UserID,
|
||||
projectUser.ProjectID)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *SqlDb) DeleteProjectUser(projectID, userID int) error {
|
||||
_, err := d.sql.Exec("delete from project__user where user_id=? and project_id=?", userID, projectID)
|
||||
return err
|
||||
}
|
||||
|
||||
//FetchUser retrieves a user from the database by ID
|
||||
func (d *SqlDb) GetUser(userID int) (db.User, error) {
|
||||
var user db.User
|
||||
|
||||
err := d.sql.SelectOne(&user, "select * from `user` where id=?", userID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = db.ErrNotFound
|
||||
}
|
||||
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUsers(params db.RetrieveQueryParams) (users []db.User, err error) {
|
||||
query, args, err := getSqlForTable("user", params)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = d.sql.Select(&users, query, args...)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *SqlDb) GetUserByLoginOrEmail(login string, email string) (existingUser db.User, err error) {
|
||||
err = d.sql.SelectOne(&existingUser, "select * from `user` where email=? or username=?", email, login)
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user