diff options
author | Sybren <122987084+drsybren@users.noreply.github.com> | 2023-02-16 17:32:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-16 10:32:01 -0600 |
commit | aa45777c926adba2bbe9e269960476acf55abb33 (patch) | |
tree | bcf28d4a45eb80c9598b827b6c7bc284416b0790 /routers/api | |
parent | a0b9767df8720578910d49437db37dc34821bd0b (diff) | |
download | gitea-aa45777c926adba2bbe9e269960476acf55abb33.tar.gz gitea-aa45777c926adba2bbe9e269960476acf55abb33.zip |
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".
Diffstat (limited to 'routers/api')
-rw-r--r-- | routers/api/v1/admin/user.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 4ee1a320cc..75d5520a0e 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -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) || |