adminUserCreate creates a new user based on the provided data
(w http.ResponseWriter, r *http.Request)
| 323 | |
| 324 | // adminUserCreate creates a new user based on the provided data |
| 325 | func (a *API) adminUserCreate(w http.ResponseWriter, r *http.Request) error { |
| 326 | ctx := r.Context() |
| 327 | db := a.db.WithContext(ctx) |
| 328 | config := a.config |
| 329 | |
| 330 | adminUser := getAdminUser(ctx) |
| 331 | params, err := a.getAdminParams(r) |
| 332 | if err != nil { |
| 333 | return err |
| 334 | } |
| 335 | |
| 336 | aud := a.requestAud(ctx, r) |
| 337 | if params.Aud != "" { |
| 338 | aud = params.Aud |
| 339 | } |
| 340 | |
| 341 | if params.Email == "" && params.Phone == "" { |
| 342 | return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Cannot create a user without either an email or phone") |
| 343 | } |
| 344 | |
| 345 | var providers []string |
| 346 | if params.Email != "" { |
| 347 | params.Email, err = a.validateEmail(params.Email) |
| 348 | if err != nil { |
| 349 | return err |
| 350 | } |
| 351 | if user, err := models.IsDuplicatedEmail(db, params.Email, aud, nil, config.Experimental.ProvidersWithOwnLinkingDomain); err != nil { |
| 352 | return apierrors.NewInternalServerError("Database error checking email").WithInternalError(err) |
| 353 | } else if user != nil { |
| 354 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg) |
| 355 | } |
| 356 | providers = append(providers, "email") |
| 357 | } |
| 358 | |
| 359 | if params.Phone != "" { |
| 360 | params.Phone, err = validatePhone(params.Phone) |
| 361 | if err != nil { |
| 362 | return err |
| 363 | } |
| 364 | if exists, err := models.IsDuplicatedPhone(db, params.Phone, aud); err != nil { |
| 365 | return apierrors.NewInternalServerError("Database error checking phone").WithInternalError(err) |
| 366 | } else if exists { |
| 367 | return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodePhoneExists, "Phone number already registered by another user") |
| 368 | } |
| 369 | providers = append(providers, "phone") |
| 370 | } |
| 371 | |
| 372 | if params.Password != nil && params.PasswordHash != "" { |
| 373 | return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Only a password or a password hash should be provided") |
| 374 | } |
| 375 | |
| 376 | if (params.Password == nil || *params.Password == "") && params.PasswordHash == "" { |
| 377 | password, err := password.Generate(64, 10, 0, false, true) |
| 378 | if err != nil { |
| 379 | return apierrors.NewInternalServerError("Error generating password").WithInternalError(err) |
| 380 | } |
| 381 | params.Password = &password |
| 382 | } |
nothing calls this directly
no test coverage detected