project_handler.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/internal/forms"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // Enumeration of user API error codes, represented as int64
  12. const (
  13. ErrProjectDecode ErrorCode = iota + 600
  14. ErrProjectValidateFields
  15. ErrProjectDataRead
  16. )
  17. // HandleCreateProject validates a project form entry, converts the project to a gorm
  18. // model, and saves the user to the database
  19. func (app *App) HandleCreateProject(w http.ResponseWriter, r *http.Request) {
  20. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  21. if err != nil {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. return
  24. }
  25. userID, _ := session.Values["user_id"].(uint)
  26. form := &forms.CreateProjectForm{}
  27. // decode from JSON to form value
  28. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  29. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  30. return
  31. }
  32. // validate the form
  33. if err := app.validator.Struct(form); err != nil {
  34. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  35. return
  36. }
  37. // convert the form to a project model
  38. projModel, err := form.ToProject(app.Repo.Project)
  39. if err != nil {
  40. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  41. return
  42. }
  43. // handle write to the database
  44. projModel, err = app.Repo.Project.CreateProject(projModel)
  45. if err != nil {
  46. app.handleErrorDataWrite(err, w)
  47. return
  48. }
  49. // create a new Role with the user as the admin
  50. _, err = app.Repo.Project.CreateProjectRole(projModel, &models.Role{
  51. UserID: userID,
  52. ProjectID: projModel.ID,
  53. Kind: models.RoleAdmin,
  54. })
  55. if err != nil {
  56. app.handleErrorDataWrite(err, w)
  57. return
  58. }
  59. app.Logger.Info().Msgf("New project created: %d", projModel.ID)
  60. w.WriteHeader(http.StatusCreated)
  61. projExt := projModel.Externalize()
  62. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  63. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  64. return
  65. }
  66. }
  67. // HandleGetProjectRoles lists the roles available to the project. For now, these
  68. // roles are static.
  69. func (app *App) HandleGetProjectRoles(w http.ResponseWriter, r *http.Request) {
  70. roles := []string{models.RoleAdmin, models.RoleDeveloper, models.RoleViewer}
  71. w.WriteHeader(http.StatusOK)
  72. if err := json.NewEncoder(w).Encode(&roles); err != nil {
  73. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  74. return
  75. }
  76. }
  77. type Collaborator struct {
  78. ID uint `json:"id"`
  79. Kind string `json:"kind"`
  80. UserID uint `json:"user_id"`
  81. Email string `json:"email"`
  82. ProjectID uint `json:"project_id"`
  83. }
  84. // HandleListProjectCollaborators lists the collaborators in the project
  85. func (app *App) HandleListProjectCollaborators(w http.ResponseWriter, r *http.Request) {
  86. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  87. if err != nil || id == 0 {
  88. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  89. return
  90. }
  91. roles, err := app.Repo.Project.ListProjectRoles(uint(id))
  92. if err != nil {
  93. app.handleErrorRead(err, ErrProjectDataRead, w)
  94. return
  95. }
  96. res := make([]*Collaborator, 0)
  97. roleMap := make(map[uint]*models.Role)
  98. idArr := make([]uint, 0)
  99. for _, role := range roles {
  100. roleMap[role.UserID] = &role
  101. idArr = append(idArr, role.UserID)
  102. }
  103. users, err := app.Repo.User.ListUsersByIDs(idArr)
  104. if err != nil {
  105. app.handleErrorRead(err, ErrProjectDataRead, w)
  106. return
  107. }
  108. for _, user := range users {
  109. res = append(res, &Collaborator{
  110. ID: roleMap[user.ID].ID,
  111. Kind: roleMap[user.ID].Kind,
  112. UserID: roleMap[user.ID].UserID,
  113. Email: user.Email,
  114. ProjectID: roleMap[user.ID].ProjectID,
  115. })
  116. }
  117. w.WriteHeader(http.StatusOK)
  118. if err := json.NewEncoder(w).Encode(res); err != nil {
  119. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  120. return
  121. }
  122. }
  123. // HandleReadProject returns an externalized Project (models.ProjectExternal)
  124. // based on an ID
  125. func (app *App) HandleReadProject(w http.ResponseWriter, r *http.Request) {
  126. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  127. if err != nil || id == 0 {
  128. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  129. return
  130. }
  131. proj, err := app.Repo.Project.ReadProject(uint(id))
  132. if err != nil {
  133. app.handleErrorRead(err, ErrProjectDataRead, w)
  134. return
  135. }
  136. projExt := proj.Externalize()
  137. w.WriteHeader(http.StatusOK)
  138. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  139. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  140. return
  141. }
  142. }
  143. // HandleReadProjectPolicy returns the policy document given the current user
  144. func (app *App) HandleReadProjectPolicy(w http.ResponseWriter, r *http.Request) {
  145. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  146. if err != nil || id == 0 {
  147. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  148. return
  149. }
  150. userID, err := app.getUserIDFromRequest(r)
  151. if err != nil {
  152. app.handleErrorInternal(err, w)
  153. return
  154. }
  155. role, err := app.Repo.Project.ReadProjectRole(uint(id), userID)
  156. if err != nil {
  157. app.handleErrorRead(err, ErrProjectDataRead, w)
  158. return
  159. }
  160. // case on the role to get the policy document
  161. var policy types.Policy
  162. switch role.Kind {
  163. case models.RoleAdmin:
  164. policy = types.AdminPolicy
  165. case models.RoleDeveloper:
  166. policy = types.DeveloperPolicy
  167. case models.RoleViewer:
  168. policy = types.ViewerPolicy
  169. }
  170. if err := json.NewEncoder(w).Encode(policy); err != nil {
  171. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  172. return
  173. }
  174. }
  175. // HandleUpdateProjectRole updates a project role with a new "kind"
  176. func (app *App) HandleUpdateProjectRole(w http.ResponseWriter, r *http.Request) {
  177. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  178. if err != nil || id == 0 {
  179. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  180. return
  181. }
  182. userID, err := strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  183. if err != nil || id == 0 {
  184. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  185. return
  186. }
  187. role, err := app.Repo.Project.ReadProjectRole(uint(id), uint(userID))
  188. if err != nil {
  189. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  190. return
  191. }
  192. form := &forms.UpdateProjectRoleForm{}
  193. // decode from JSON to form value
  194. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  195. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  196. return
  197. }
  198. role.Kind = form.Kind
  199. role, err = app.Repo.Project.UpdateProjectRole(uint(id), role)
  200. if err != nil {
  201. app.handleErrorRead(err, ErrProjectDataRead, w)
  202. return
  203. }
  204. if err := json.NewEncoder(w).Encode(role.Externalize()); err != nil {
  205. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  206. return
  207. }
  208. }
  209. // HandleDeleteProject deletes a project from the db, reading from the project_id
  210. // in the URL param
  211. func (app *App) HandleDeleteProject(w http.ResponseWriter, r *http.Request) {
  212. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  213. if err != nil || id == 0 {
  214. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  215. return
  216. }
  217. proj, err := app.Repo.Project.ReadProject(uint(id))
  218. if err != nil {
  219. app.handleErrorRead(err, ErrProjectDataRead, w)
  220. return
  221. }
  222. proj, err = app.Repo.Project.DeleteProject(proj)
  223. if err != nil {
  224. app.handleErrorRead(err, ErrProjectDataRead, w)
  225. return
  226. }
  227. projExternal := proj.Externalize()
  228. w.WriteHeader(http.StatusOK)
  229. if err := json.NewEncoder(w).Encode(projExternal); err != nil {
  230. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  231. return
  232. }
  233. }
  234. // HandleDeleteProjectRole deletes a project role from the db, reading from the project_id
  235. // in the URL param
  236. func (app *App) HandleDeleteProjectRole(w http.ResponseWriter, r *http.Request) {
  237. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  238. if err != nil || id == 0 {
  239. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  240. return
  241. }
  242. userID, err := strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  243. if err != nil || id == 0 {
  244. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  245. return
  246. }
  247. role, err := app.Repo.Project.ReadProjectRole(uint(id), uint(userID))
  248. if err != nil {
  249. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  250. return
  251. }
  252. role, err = app.Repo.Project.DeleteProjectRole(uint(id), uint(userID))
  253. if err != nil {
  254. app.handleErrorRead(err, ErrProjectDataRead, w)
  255. return
  256. }
  257. if err := json.NewEncoder(w).Encode(role.Externalize()); err != nil {
  258. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  259. return
  260. }
  261. }