Semaphore/api/projects/project.go

112 lines
2.7 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
"database/sql"
2016-05-24 11:55:48 +02:00
database "github.com/ansible-semaphore/semaphore/db"
2016-04-02 14:40:07 +02:00
"github.com/ansible-semaphore/semaphore/models"
"github.com/ansible-semaphore/semaphore/util"
"github.com/gin-gonic/gin"
"github.com/masterminds/squirrel"
)
2017-02-22 23:17:36 +01:00
func ProjectMiddleware(w http.ResponseWriter, r *http.Request) {
2016-04-02 14:40:07 +02:00
user := c.MustGet("user").(*models.User)
projectID, err := util.GetIntParam("project_id", c)
if err != nil {
return
}
query, args, _ := squirrel.Select("p.*").
From("project as p").
Join("project__user as pu on pu.project_id=p.id").
Where("p.id=?", projectID).
Where("pu.user_id=?", user.ID).
ToSql()
var project models.Project
if err := database.Mysql.SelectOne(&project, query, args...); err != nil {
if err == sql.ErrNoRows {
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNotFound)
2016-04-02 14:40:07 +02:00
return
}
panic(err)
}
c.Set("project", project)
c.Next()
}
2017-02-22 23:17:36 +01:00
func GetProject(w http.ResponseWriter, r *http.Request) {
2016-04-02 14:40:07 +02:00
c.JSON(200, c.MustGet("project"))
}
2017-02-22 23:17:36 +01:00
func MustBeAdmin(w http.ResponseWriter, r *http.Request) {
project := c.MustGet("project").(models.Project)
user := c.MustGet("user").(*models.User)
userC, err := database.Mysql.SelectInt("select count(1) from project__user as pu join user as u on pu.user_id=u.id where pu.user_id=? and pu.project_id=? and pu.admin=1", user.ID, project.ID)
if err != nil {
panic(err)
}
if userC == 0 {
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusForbidden)
return
}
}
2017-02-22 23:17:36 +01:00
func UpdateProject(w http.ResponseWriter, r *http.Request) {
project := c.MustGet("project").(models.Project)
var body struct {
Name string `json:"name"`
}
if err := c.Bind(&body); err != nil {
return
}
if _, err := database.Mysql.Exec("update project set name=? where id=?", body.Name, project.ID); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}
2017-02-22 23:17:36 +01:00
func DeleteProject(w http.ResponseWriter, r *http.Request) {
project := c.MustGet("project").(models.Project)
tx, err := database.Mysql.Begin()
if err != nil {
panic(err)
}
statements := []string{
"delete tao from task__output as tao join task as t on t.id=tao.task_id join project__template as pt on pt.id=t.template_id where pt.project_id=?",
"delete t from task as t join project__template as pt on pt.id=t.template_id where pt.project_id=?",
"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, project.ID)
if err != nil {
tx.Rollback()
panic(err)
}
}
if err := tx.Commit(); err != nil {
panic(err)
}
2017-02-22 23:17:36 +01:00
w.WriteHeader(http.StatusNoContent)
}