project_handler.go 8.5 KB

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