Invite is the endpoint for inviting a new user
(w http.ResponseWriter, r *http.Request)
| 18 | |
| 19 | // Invite is the endpoint for inviting a new user |
| 20 | func (a *API) Invite(w http.ResponseWriter, r *http.Request) error { |
| 21 | ctx := r.Context() |
| 22 | db := a.db.WithContext(ctx) |
| 23 | config := a.config |
| 24 | adminUser := getAdminUser(ctx) |
| 25 | params := &InviteParams{} |
| 26 | if err := retrieveRequestParams(r, params); err != nil { |
| 27 | return err |
| 28 | } |
| 29 | |
| 30 | var err error |
| 31 | params.Email, err = a.validateEmail(params.Email) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | aud := a.requestAud(ctx, r) |
| 37 | user, err := models.FindUserByEmailAndAudience(db, params.Email, aud) |
| 38 | if err != nil && !models.IsNotFoundError(err) { |
| 39 | return apierrors.NewInternalServerError("Database error finding user").WithInternalError(err) |
| 40 | } |
| 41 | |
| 42 | isCreate := user == nil |
| 43 | isConfirmed := user != nil && user.IsConfirmed() |
| 44 | |
| 45 | if isCreate { |
| 46 | signupParams := SignupParams{ |
| 47 | Email: params.Email, |
| 48 | Data: params.Data, |
| 49 | Aud: aud, |
| 50 | Provider: "email", |
| 51 | } |
| 52 | |
| 53 | // because params above sets no password, this method |
| 54 | // is not computationally hard so it can be used within |
| 55 | // a database transaction |
| 56 | user, err = signupParams.ToUserModel(false /* <- isSSOUser */) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | if err := a.triggerBeforeUserCreated(r, db, user); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | err = db.Transaction(func(tx *storage.Connection) error { |
| 67 | if !isCreate { |
| 68 | if isConfirmed { |
| 69 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg) |
| 70 | } |
| 71 | } else { |
| 72 | user, err = a.signupNewUser(tx, user) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | identity, err := a.createNewIdentity(tx, user, "email", structs.Map(provider.Claims{ |
| 77 | Subject: user.ID.String(), |
nothing calls this directly
no test coverage detected