2016-04-02 14:40:07 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2023-09-17 16:15:44 +02:00
|
|
|
"fmt"
|
2024-04-12 09:23:13 +02:00
|
|
|
"net/http"
|
|
|
|
|
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-09-18 19:46:55 +02:00
|
|
|
"github.com/gorilla/context"
|
2016-04-02 14:40:07 +02:00
|
|
|
)
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// UserMiddleware ensures a user exists and loads it to the context
|
2019-07-09 18:14:06 +02:00
|
|
|
func UserMiddleware(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)
|
2020-12-03 14:51:15 +01:00
|
|
|
userID, err := helpers.GetIntParam("user_id", w, r)
|
2019-07-09 18:14:06 +02:00
|
|
|
if err != nil {
|
2016-04-09 21:09:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 15:00:05 +01:00
|
|
|
_, err = helpers.Store(r).GetProjectUser(project.ID, userID)
|
2019-07-09 18:14:06 +02:00
|
|
|
|
2020-12-17 15:00:05 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := helpers.Store(r).GetUser(userID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:14:06 +02:00
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2019-07-09 18:14:06 +02:00
|
|
|
context.Set(r, "projectUser", user)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 19:46:55 +02:00
|
|
|
type projUser struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Role db.ProjectUserRole `json:"role"`
|
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// GetUsers returns all users in a project
|
2019-07-09 18:11:01 +02:00
|
|
|
func GetUsers(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:22:05 +01:00
|
|
|
|
|
|
|
// get single user if user ID specified in the request
|
2020-11-03 21:56:22 +01:00
|
|
|
if user := context.Get(r, "projectUser"); user != nil {
|
2020-12-04 23:41:26 +01:00
|
|
|
helpers.WriteJSON(w, http.StatusOK, user.(db.User))
|
2020-11-03 21:56:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2021-10-13 16:33:07 +02:00
|
|
|
users, err := helpers.Store(r).GetProjectUsers(project.ID, helpers.QueryParams(r.URL))
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2020-12-17 15:00:05 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 22:04:23 +02:00
|
|
|
var result = make([]projUser, 0)
|
2023-09-18 19:46:55 +02:00
|
|
|
|
|
|
|
for _, user := range users {
|
|
|
|
result = append(result, projUser{
|
|
|
|
ID: user.ID,
|
|
|
|
Name: user.Name,
|
|
|
|
Username: user.Username,
|
|
|
|
Role: user.Role,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusOK, result)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 22:12:47 +02:00
|
|
|
// AddUser adds a user to a projects team in the database
|
2019-07-09 18:11:01 +02:00
|
|
|
func AddUser(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2021-08-20 08:28:50 +02:00
|
|
|
var projectUser struct {
|
2023-07-08 12:41:57 +02:00
|
|
|
UserID int `json:"user_id" binding:"required"`
|
|
|
|
Role db.ProjectUserRole `json:"role"`
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 08:28:50 +02:00
|
|
|
if !helpers.Bind(w, r, &projectUser) {
|
2019-07-09 18:11:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-09 10:23:13 +02:00
|
|
|
if !projectUser.Role.IsValid() {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
_, err := helpers.Store(r).CreateProjectUser(db.ProjectUser{
|
|
|
|
ProjectID: project.ID,
|
|
|
|
UserID: projectUser.UserID,
|
2023-07-08 12:41:57 +02:00
|
|
|
Role: projectUser.Role,
|
2023-07-07 23:15:30 +02:00
|
|
|
})
|
2020-12-01 20:06:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogCreate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: project.ID,
|
|
|
|
ObjectType: db.EventUser,
|
|
|
|
ObjectID: projectUser.UserID,
|
|
|
|
Description: fmt.Sprintf("User ID %d added to team", projectUser.UserID),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 21:43:13 +02:00
|
|
|
// removeUser removes a user from a project team
|
|
|
|
func removeUser(targetUser db.User, w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2023-09-18 21:43:13 +02:00
|
|
|
me := context.Get(r, "user").(*db.User) // logged in user
|
|
|
|
myRole := context.Get(r, "projectUserRole").(db.ProjectUserRole)
|
2019-07-09 18:11:01 +02:00
|
|
|
|
2023-09-18 21:43:13 +02:00
|
|
|
if !me.Admin && targetUser.ID == me.ID && myRole == db.ProjectOwner {
|
2023-09-17 16:15:44 +02:00
|
|
|
helpers.WriteError(w, fmt.Errorf("owner can not left the project"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := helpers.Store(r).DeleteProjectUser(project.ID, targetUser.ID)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2020-12-17 15:00:05 +01:00
|
|
|
helpers.WriteError(w, err)
|
2020-12-01 20:06:49 +01:00
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogDelete, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: project.ID,
|
|
|
|
ObjectType: db.EventUser,
|
|
|
|
ObjectID: targetUser.ID,
|
|
|
|
Description: fmt.Sprintf("User ID %d removed from team", targetUser.ID),
|
2020-12-01 20:06:49 +01:00
|
|
|
})
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-02 14:40:07 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2023-09-18 21:43:13 +02:00
|
|
|
// LeftProject removes a user from a project team
|
|
|
|
func LeftProject(w http.ResponseWriter, r *http.Request) {
|
|
|
|
me := context.Get(r, "user").(*db.User) // logged in user
|
|
|
|
removeUser(*me, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveUser removes a user from a project team
|
|
|
|
func RemoveUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
targetUser := context.Get(r, "projectUser").(db.User) // target user
|
|
|
|
removeUser(targetUser, w, r)
|
|
|
|
}
|
|
|
|
|
2023-07-08 13:42:50 +02:00
|
|
|
func UpdateUser(w http.ResponseWriter, r *http.Request) {
|
2020-12-04 23:41:26 +01:00
|
|
|
project := context.Get(r, "project").(db.Project)
|
2023-09-17 16:15:44 +02:00
|
|
|
me := context.Get(r, "user").(*db.User) // logged in user
|
|
|
|
targetUser := context.Get(r, "projectUser").(db.User)
|
|
|
|
targetUserRole := context.Get(r, "projectUserRole").(db.ProjectUserRole)
|
|
|
|
|
|
|
|
if !me.Admin && targetUser.ID == me.ID && targetUserRole == db.ProjectOwner {
|
|
|
|
helpers.WriteError(w, fmt.Errorf("owner can not change his role in the project"))
|
|
|
|
return
|
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2023-07-08 13:42:50 +02:00
|
|
|
var projectUser struct {
|
|
|
|
Role db.ProjectUserRole `json:"role"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if !helpers.Bind(w, r, &projectUser) {
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-04 15:44:34 +02:00
|
|
|
|
2023-07-09 10:23:13 +02:00
|
|
|
if !projectUser.Role.IsValid() {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-07 23:15:30 +02:00
|
|
|
err := helpers.Store(r).UpdateProjectUser(db.ProjectUser{
|
2023-09-17 16:15:44 +02:00
|
|
|
UserID: targetUser.ID,
|
2023-07-07 23:15:30 +02:00
|
|
|
ProjectID: project.ID,
|
2023-07-08 13:42:50 +02:00
|
|
|
Role: projectUser.Role,
|
2023-07-07 23:15:30 +02:00
|
|
|
})
|
2020-12-17 15:00:05 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
2019-07-09 18:11:01 +02:00
|
|
|
}
|
2016-04-09 21:09:57 +02:00
|
|
|
|
2024-04-12 09:23:13 +02:00
|
|
|
helpers.EventLog(r, helpers.EventLogUpdate, helpers.EventLogItem{
|
|
|
|
UserID: helpers.UserFromContext(r).ID,
|
|
|
|
ProjectID: project.ID,
|
|
|
|
ObjectType: db.EventUser,
|
|
|
|
ObjectID: targetUser.ID,
|
|
|
|
Description: fmt.Sprintf("Changed role for User ID %d", targetUser.ID),
|
|
|
|
})
|
|
|
|
|
2019-07-09 18:11:01 +02:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-04-04 15:44:34 +02:00
|
|
|
}
|