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"
|
2023-07-08 23:35:39 +02:00
|
|
|
"github.com/gorilla/mux"
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-08 23:35:39 +02:00
|
|
|
// check if user in project's team
|
2023-08-26 18:48:16 +02:00
|
|
|
projectUser, err := helpers.Store(r).GetProjectUser(projectID, user.ID)
|
2020-12-16 20:19:20 +01:00
|
|
|
|
2023-09-17 16:15:44 +02:00
|
|
|
if !user.Admin && err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
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
|
|
|
|
2023-08-26 18:48:16 +02:00
|
|
|
context.Set(r, "projectUserRole", projectUser.Role)
|
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
|
|
|
}
|
|
|
|
|
2023-08-26 20:43:42 +02:00
|
|
|
// GetMustCanMiddleware ensures that the user has administrator rights
|
|
|
|
func GetMustCanMiddleware(permissions db.ProjectUserPermission) mux.MiddlewareFunc {
|
2023-07-08 23:35:39 +02:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-09-18 21:43:13 +02:00
|
|
|
me := context.Get(r, "user").(*db.User)
|
|
|
|
myRole := context.Get(r, "projectUserRole").(db.ProjectUserRole)
|
2023-07-08 23:35:39 +02:00
|
|
|
|
2023-09-18 21:43:13 +02:00
|
|
|
if !me.Admin && r.Method != "GET" && r.Method != "HEAD" && !myRole.Can(permissions) {
|
2023-08-26 18:48:16 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
2023-07-08 23:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2016-06-17 22:16:46 +02:00
|
|
|
}
|
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
// GetProject returns a project details
|
2020-12-16 20:19:20 +01:00
|
|
|
func GetProject(w http.ResponseWriter, r *http.Request) {
|
2023-08-26 20:43:42 +02:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, context.Get(r, "project"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserRole(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var permissions struct {
|
|
|
|
Role db.ProjectUserRole `json:"role"`
|
|
|
|
Permissions db.ProjectUserPermission `json:"permissions"`
|
2023-08-26 18:48:16 +02:00
|
|
|
}
|
2023-08-26 20:43:42 +02:00
|
|
|
permissions.Role = context.Get(r, "projectUserRole").(db.ProjectUserRole)
|
|
|
|
permissions.Permissions = permissions.Role.GetPermissions()
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, permissions)
|
2020-12-16 20:19:20 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|