adminUserCreate creates a new user based on the provided data
(w http.ResponseWriter, r *http.Request)
| 153 | |
| 154 | // adminUserCreate creates a new user based on the provided data |
| 155 | func (a *API) adminUserCreate(w http.ResponseWriter, r *http.Request) error { |
| 156 | ctx := r.Context() |
| 157 | instanceID := getInstanceID(ctx) |
| 158 | adminUser := getAdminUser(ctx) |
| 159 | params, err := a.getAdminParams(r) |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | |
| 164 | if err := a.validateEmail(ctx, params.Email); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | aud := a.requestAud(ctx, r) |
| 169 | if params.Aud != "" { |
| 170 | aud = params.Aud |
| 171 | } |
| 172 | |
| 173 | if exists, err := models.IsDuplicatedEmail(a.db, instanceID, params.Email, aud); err != nil { |
| 174 | return internalServerError("Database error checking email").WithInternalError(err) |
| 175 | } else if exists { |
| 176 | return unprocessableEntityError("Email address already registered by another user") |
| 177 | } |
| 178 | |
| 179 | user, err := models.NewUser(instanceID, params.Email, params.Password, aud, params.UserMetaData) |
| 180 | if err != nil { |
| 181 | return internalServerError("Error creating user").WithInternalError(err) |
| 182 | } |
| 183 | if user.AppMetaData == nil { |
| 184 | user.AppMetaData = make(map[string]interface{}) |
| 185 | } |
| 186 | user.AppMetaData["provider"] = "email" |
| 187 | |
| 188 | config := a.getConfig(ctx) |
| 189 | err = a.db.Transaction(func(tx *storage.Connection) error { |
| 190 | if terr := models.NewAuditLogEntry(tx, instanceID, adminUser, models.UserSignedUpAction, map[string]interface{}{ |
| 191 | "user_id": user.ID, |
| 192 | "user_email": user.Email, |
| 193 | }); terr != nil { |
| 194 | return terr |
| 195 | } |
| 196 | |
| 197 | if terr := tx.Create(user); terr != nil { |
| 198 | return terr |
| 199 | } |
| 200 | |
| 201 | role := config.JWT.DefaultGroupName |
| 202 | if params.Role != "" { |
| 203 | role = params.Role |
| 204 | } |
| 205 | if terr := user.SetRole(tx, role); terr != nil { |
| 206 | return terr |
| 207 | } |
| 208 | |
| 209 | if params.Confirm { |
| 210 | if terr := user.Confirm(tx); terr != nil { |
| 211 | return terr |
| 212 | } |
nothing calls this directly
no test coverage detected