(w http.ResponseWriter, r *http.Request)
| 41 | } |
| 42 | |
| 43 | func (s *Server) setupOwner(w http.ResponseWriter, r *http.Request) { |
| 44 | if !middleware.OwnerSetupEnabled() { |
| 45 | http.Error(w, "Not found", http.StatusNotFound) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | if !middleware.IsOwnerSetupRequired() { |
| 50 | http.Error(w, "Instance already initialized", http.StatusConflict) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | var req SetupOwnerRequest |
| 55 | |
| 56 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 57 | http.Error(w, "Invalid request body", http.StatusBadRequest) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | if req.Email == "" || req.FirstName == "" || req.LastName == "" || req.Password == "" { |
| 62 | http.Error(w, "All fields are required", http.StatusBadRequest) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | if req.SMTPEnabled { |
| 67 | if req.SMTPHost == "" || req.SMTPPort == 0 || req.SMTPFromEmail == "" { |
| 68 | http.Error(w, "SMTP host, port, and from email are required", http.StatusBadRequest) |
| 69 | return |
| 70 | } |
| 71 | if req.SMTPUsername != "" && req.SMTPPassword == "" { |
| 72 | http.Error(w, "SMTP password is required when username is provided", http.StatusBadRequest) |
| 73 | return |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fullName := req.FirstName + " " + req.LastName |
| 78 | |
| 79 | var organization *models.Organization |
| 80 | var account *models.Account |
| 81 | var user *models.User |
| 82 | |
| 83 | err := database.Conn().Transaction(func(tx *gorm.DB) error { |
| 84 | var err error |
| 85 | |
| 86 | account, err = models.CreateAccountInTransaction(tx, fullName, req.Email) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | // The first account is automatically an installation admin |
| 92 | if err := tx.Model(account).Update("installation_admin", true).Error; err != nil { |
| 93 | return err |
| 94 | } |
| 95 | account.InstallationAdmin = true |
| 96 | |
| 97 | if err := usage.EnsureAccountWithinLimits(r.Context(), s.usageService, account.ID.String(), &usagepb.AccountState{ |
| 98 | Organizations: 1, |
| 99 | }); err != nil { |
| 100 | return err |
nothing calls this directly
no test coverage detected