Allow custom "created" timestamps in user creation API (#22549)
Allow back-dating user creation via the `adminCreateUser` API operation. `CreateUserOption` now has an optional field `created_at`, which can contain a datetime-formatted string. If this field is present, the user's `created_unix` database field will be updated to its value. This is important for Blender's migration of users from Phabricator to Gitea. There are many users, and the creation timestamp of their account can give us some indication as to how long someone's been part of the community. The back-dating is done in a separate query that just updates the user's `created_unix` field. This was the easiest and cleanest way I could find, as in the initial `INSERT` query the field always is set to "now".
This commit is contained in:
parent
a0b9767df8
commit
aa45777c92
5 changed files with 91 additions and 1 deletions
|
@ -20,6 +20,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/password"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/routers/api/v1/user"
|
||||
|
@ -120,6 +121,14 @@ func CreateUser(ctx *context.APIContext) {
|
|||
overwriteDefault.Visibility = &visibility
|
||||
}
|
||||
|
||||
// Update the user creation timestamp. This can only be done after the user
|
||||
// record has been inserted into the database; the insert intself will always
|
||||
// set the creation timestamp to "now".
|
||||
if form.Created != nil {
|
||||
u.CreatedUnix = timeutil.TimeStamp(form.Created.Unix())
|
||||
u.UpdatedUnix = u.CreatedUnix
|
||||
}
|
||||
|
||||
if err := user_model.CreateUser(u, overwriteDefault); err != nil {
|
||||
if user_model.IsErrUserAlreadyExist(err) ||
|
||||
user_model.IsErrEmailAlreadyUsed(err) ||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue