Semaphore/api/projects/projects.go

71 lines
1.5 KiB
Go
Raw Normal View History

2016-04-02 14:40:07 +02:00
package projects
import (
log "github.com/Sirupsen/logrus"
"github.com/ansible-semaphore/semaphore/api/helpers"
"github.com/ansible-semaphore/semaphore/db"
2023-07-24 16:04:03 +02:00
"github.com/ansible-semaphore/semaphore/util"
"net/http"
"github.com/gorilla/context"
2016-04-02 14:40:07 +02:00
)
// GetProjects returns all projects in this users context
func GetProjects(w http.ResponseWriter, r *http.Request) {
user := context.Get(r, "user").(*db.User)
2016-04-02 14:40:07 +02:00
projects, err := helpers.Store(r).GetProjects(user.ID)
2016-04-02 14:40:07 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
helpers.WriteJSON(w, http.StatusOK, projects)
2016-04-02 14:40:07 +02:00
}
// AddProject adds a new project to the database
func AddProject(w http.ResponseWriter, r *http.Request) {
var body db.Project
user := context.Get(r, "user").(*db.User)
2016-04-02 14:40:07 +02:00
2023-07-24 16:04:03 +02:00
if !user.Admin && !util.Config.NonAdminCanCreateProject {
log.Warn(user.Username + " is not permitted to edit users")
w.WriteHeader(http.StatusUnauthorized)
return
}
if !helpers.Bind(w, r, &body) {
return
}
2016-04-02 14:40:07 +02:00
body, err := helpers.Store(r).CreateProject(body)
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
_, err = helpers.Store(r).CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Role: db.ProjectOwner})
if err != nil {
helpers.WriteError(w, err)
return
}
2016-04-02 14:40:07 +02:00
desc := "Project Created"
oType := db.EventProject
_, err = helpers.Store(r).CreateEvent(db.Event{
2021-08-20 08:28:50 +02:00
UserID: &user.ID,
ProjectID: &body.ID,
Description: &desc,
ObjectType: &oType,
ObjectID: &body.ID,
})
if err != nil {
log.Error(err)
}
2016-04-17 02:20:23 +02:00
helpers.WriteJSON(w, http.StatusCreated, body)
2016-04-02 14:40:07 +02:00
}