project_handler.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. roleCp := role
  101. roleMap[role.UserID] = &roleCp
  102. idArr = append(idArr, role.UserID)
  103. }
  104. users, err := app.Repo.User.ListUsersByIDs(idArr)
  105. if err != nil {
  106. app.handleErrorRead(err, ErrProjectDataRead, w)
  107. return
  108. }
  109. for _, user := range users {
  110. res = append(res, &Collaborator{
  111. ID: roleMap[user.ID].ID,
  112. Kind: roleMap[user.ID].Kind,
  113. UserID: roleMap[user.ID].UserID,
  114. Email: user.Email,
  115. ProjectID: roleMap[user.ID].ProjectID,
  116. })
  117. }
  118. w.WriteHeader(http.StatusOK)
  119. if err := json.NewEncoder(w).Encode(res); err != nil {
  120. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  121. return
  122. }
  123. }
  124. // HandleReadProject returns an externalized Project (models.ProjectExternal)
  125. // based on an ID
  126. func (app *App) HandleReadProject(w http.ResponseWriter, r *http.Request) {
  127. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  128. if err != nil || id == 0 {
  129. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  130. return
  131. }
  132. proj, err := app.Repo.Project.ReadProject(uint(id))
  133. if err != nil {
  134. app.handleErrorRead(err, ErrProjectDataRead, w)
  135. return
  136. }
  137. projExt := proj.Externalize()
  138. w.WriteHeader(http.StatusOK)
  139. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  140. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  141. return
  142. }
  143. }
  144. // HandleReadProjectPolicy returns the policy document given the current user
  145. func (app *App) HandleReadProjectPolicy(w http.ResponseWriter, r *http.Request) {
  146. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  147. if err != nil || id == 0 {
  148. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  149. return
  150. }
  151. userID, err := app.getUserIDFromRequest(r)
  152. if err != nil {
  153. app.handleErrorInternal(err, w)
  154. return
  155. }
  156. role, err := app.Repo.Project.ReadProjectRole(uint(id), userID)
  157. if err != nil {
  158. app.handleErrorRead(err, ErrProjectDataRead, w)
  159. return
  160. }
  161. // case on the role to get the policy document
  162. var policy types.Policy
  163. switch role.Kind {
  164. case models.RoleAdmin:
  165. policy = types.AdminPolicy
  166. case models.RoleDeveloper:
  167. policy = types.DeveloperPolicy
  168. case models.RoleViewer:
  169. policy = types.ViewerPolicy
  170. }
  171. if err := json.NewEncoder(w).Encode(policy); err != nil {
  172. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  173. return
  174. }
  175. }
  176. // HandleUpdateProjectRole updates a project role with a new "kind"
  177. func (app *App) HandleUpdateProjectRole(w http.ResponseWriter, r *http.Request) {
  178. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  179. if err != nil || id == 0 {
  180. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  181. return
  182. }
  183. userID, err := strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  184. if err != nil || id == 0 {
  185. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  186. return
  187. }
  188. role, err := app.Repo.Project.ReadProjectRole(uint(id), uint(userID))
  189. if err != nil {
  190. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  191. return
  192. }
  193. form := &forms.UpdateProjectRoleForm{}
  194. // decode from JSON to form value
  195. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  196. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  197. return
  198. }
  199. role.Kind = form.Kind
  200. role, err = app.Repo.Project.UpdateProjectRole(uint(id), role)
  201. if err != nil {
  202. app.handleErrorRead(err, ErrProjectDataRead, w)
  203. return
  204. }
  205. if err := json.NewEncoder(w).Encode(role.Externalize()); err != nil {
  206. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  207. return
  208. }
  209. }
  210. // HandleDeleteProject deletes a project from the db, reading from the project_id
  211. // in the URL param
  212. func (app *App) HandleDeleteProject(w http.ResponseWriter, r *http.Request) {
  213. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  214. if err != nil || id == 0 {
  215. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  216. return
  217. }
  218. proj, err := app.Repo.Project.ReadProject(uint(id))
  219. if err != nil {
  220. app.handleErrorRead(err, ErrProjectDataRead, w)
  221. return
  222. }
  223. proj, err = app.Repo.Project.DeleteProject(proj)
  224. if err != nil {
  225. app.handleErrorRead(err, ErrProjectDataRead, w)
  226. return
  227. }
  228. projExternal := proj.Externalize()
  229. w.WriteHeader(http.StatusOK)
  230. if err := json.NewEncoder(w).Encode(projExternal); err != nil {
  231. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  232. return
  233. }
  234. }
  235. // HandleDeleteProjectRole deletes a project role from the db, reading from the project_id
  236. // in the URL param
  237. func (app *App) HandleDeleteProjectRole(w http.ResponseWriter, r *http.Request) {
  238. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  239. if err != nil || id == 0 {
  240. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  241. return
  242. }
  243. userID, err := strconv.ParseUint(chi.URLParam(r, "user_id"), 0, 64)
  244. if err != nil || id == 0 {
  245. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  246. return
  247. }
  248. role, err := app.Repo.Project.ReadProjectRole(uint(id), uint(userID))
  249. if err != nil {
  250. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  251. return
  252. }
  253. role, err = app.Repo.Project.DeleteProjectRole(uint(id), uint(userID))
  254. if err != nil {
  255. app.handleErrorRead(err, ErrProjectDataRead, w)
  256. return
  257. }
  258. if err := json.NewEncoder(w).Encode(role.Externalize()); err != nil {
  259. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  260. return
  261. }
  262. }