Semaphore/api/projects/project.go

118 lines
3.0 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
"github.com/gorilla/mux"
"net/http"
2017-02-23 00:21:49 +01:00
"github.com/gorilla/context"
2016-04-02 14:40:07 +02:00
)
// ProjectMiddleware ensures a project exists and loads it to the context
func ProjectMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*db.User)
projectID, err := helpers.GetIntParam("project_id", w, r)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid project ID",
})
2016-04-02 14:40:07 +02:00
return
}
// check if user in project's team
2023-08-26 18:48:16 +02:00
projectUser, err := helpers.Store(r).GetProjectUser(projectID, user.ID)
2023-09-17 16:15:44 +02:00
if !user.Admin && err != nil {
helpers.WriteError(w, err)
return
}
project, err := helpers.Store(r).GetProject(projectID)
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
2023-08-26 18:48:16 +02:00
context.Set(r, "projectUserRole", projectUser.Role)
context.Set(r, "project", project)
next.ServeHTTP(w, r)
})
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 {
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-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
}
next.ServeHTTP(w, r)
})
}
}
// GetProject returns a project details
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)
}
// UpdateProject saves updated project details to the database
func UpdateProject(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
var body db.Project
if !helpers.Bind(w, r, &body) {
return
}
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)
if err != nil {
helpers.WriteError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
// DeleteProject removes a project from the database
func DeleteProject(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
err := helpers.Store(r).DeleteProject(project.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}