2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2020-12-03 14:51:15 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
2020-12-04 23:41:26 +01:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
2017-02-23 00:21:49 +01:00
|
|
|
"github.com/gorilla/context"
|
2016-04-02 14:40:07 +02:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// ProjectMiddleware ensures a project exists and loads it to the context
|
2019-07-09 09:21:49 +02:00
|
|
|
func ProjectMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2019-07-09 09:21:49 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
projectID, err := helpers.GetIntParam("project_id", w, r)
|
2020-12-16 20:19:20 +01:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Invalid project ID",
|
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
// check if user it project's team
|
|
|
|
_, err = helpers.Store(r).GetProjectUser(projectID, user.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := helpers.Store(r).GetProject(projectID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 09:21:49 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 09:21:49 +02:00
|
|
|
context.Set(r, "project", project)
|
2019-07-09 18:11:01 +02:00
|
|
|
next.ServeHTTP(w, r)
|
2019-07-09 09:21:49 +02:00
|
|
|
})
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// MustBeAdmin ensures that the user has administrator rights
|
2019-07-09 18:14:06 +02:00
|
|
|
func MustBeAdmin(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
|
|
|
user := context.Get(r, "user").(*db.User)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
projectUser, err := helpers.Store(r).GetProjectUser(project.ID, user.ID)
|
|
|
|
|
|
|
|
if err == db.ErrNotFound {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
if !projectUser.Admin {
|
2019-07-09 18:14:06 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2020-12-16 20:19:20 +01:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-06-17 22:16:46 +02:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
//GetProject returns a project details
|
|
|
|
func GetProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, context.Get(r, "project"))
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UpdateProject saves updated project details to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func UpdateProject(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2020-12-16 20:19:20 +01:00
|
|
|
var body db.Project
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
if !helpers.Bind(w, r, &body) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
if body.ID != project.ID {
|
|
|
|
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
|
|
|
|
"error": "Project ID in body and URL must be the same",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-16 23:44:42 +02:00
|
|
|
err := helpers.Store(r).UpdateProject(body)
|
2020-12-16 20:19:20 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-17 22:16:46 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// DeleteProject removes a project from the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func DeleteProject(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
err := helpers.Store(r).DeleteProject(project.ID)
|
2016-06-17 22:16:46 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-06-17 22:16:46 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-17 22:16:46 +02:00
|
|
|
}
|