2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2020-12-01 20:06:49 +01:00
|
|
|
log "github.com/Sirupsen/logrus"
|
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-24 16:04:03 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2020-12-01 20:06:49 +01:00
|
|
|
"net/http"
|
2019-07-09 19:45:27 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/context"
|
2016-04-02 14:40:07 +02:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetProjects returns all projects in this users context
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetProjects(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
user := context.Get(r, "user").(*db.User)
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-16 20:19:20 +01:00
|
|
|
projects, err := helpers.Store(r).GetProjects(user.ID)
|
2016-04-02 14:40:07 +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-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, projects)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddProject adds a new project to the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddProject(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
var body db.Project
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
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 {
|
2022-01-18 22:50:15 +01:00
|
|
|
log.Warn(user.Username + " is not permitted to edit users")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2020-12-03 14:51:15 +01:00
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &body) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
body, err := helpers.Store(r).CreateProject(body)
|
2019-07-09 18:11:01 +02:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
_, err = helpers.Store(r).CreateProjectUser(db.ProjectUser{ProjectID: body.ID, UserID: user.ID, Role: db.ProjectOwner})
|
2020-12-01 20:06:49 +01:00
|
|
|
if err != nil {
|
2020-12-16 20:19:20 +01:00
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-02 14:40:07 +02:00
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
desc := "Project Created"
|
2021-10-13 16:07:22 +02:00
|
|
|
oType := db.EventProject
|
2020-12-04 23:41:26 +01:00
|
|
|
_, err = helpers.Store(r).CreateEvent(db.Event{
|
2021-08-20 08:28:50 +02:00
|
|
|
UserID: &user.ID,
|
2019-07-09 18:11:01 +02:00
|
|
|
ProjectID: &body.ID,
|
|
|
|
Description: &desc,
|
|
|
|
ObjectType: &oType,
|
|
|
|
ObjectID: &body.ID,
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-17 02:20:23 +02:00
|
|
|
|
2020-12-03 14:51:15 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusCreated, body)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|