summaryrefslogtreecommitdiffstats
path: root/models/user/user.go
diff options
context:
space:
mode:
authorSybren <122987084+drsybren@users.noreply.github.com>2023-02-16 17:32:01 +0100
committerGitHub <noreply@github.com>2023-02-16 10:32:01 -0600
commitaa45777c926adba2bbe9e269960476acf55abb33 (patch)
treebcf28d4a45eb80c9598b827b6c7bc284416b0790 /models/user/user.go
parenta0b9767df8720578910d49437db37dc34821bd0b (diff)
downloadgitea-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 'models/user/user.go')
-rw-r--r--models/user/user.go15
1 files changed, 14 insertions, 1 deletions
diff --git a/models/user/user.go b/models/user/user.go
index 0a43de7435..7e896e26da 100644
--- a/models/user/user.go
+++ b/models/user/user.go
@@ -640,6 +640,11 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e
u.IsRestricted = setting.Service.DefaultUserIsRestricted
u.IsActive = !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm)
+ // Ensure consistency of the dates.
+ if u.UpdatedUnix < u.CreatedUnix {
+ u.UpdatedUnix = u.CreatedUnix
+ }
+
// overwrite defaults if set
if len(overwriteDefault) != 0 && overwriteDefault[0] != nil {
overwrite := overwriteDefault[0]
@@ -717,7 +722,15 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e
return err
}
- if err = db.Insert(ctx, u); err != nil {
+ if u.CreatedUnix == 0 {
+ // Caller expects auto-time for creation & update timestamps.
+ err = db.Insert(ctx, u)
+ } else {
+ // Caller sets the timestamps themselves. They are responsible for ensuring
+ // both `CreatedUnix` and `UpdatedUnix` are set appropriately.
+ _, err = db.GetEngine(ctx).NoAutoTime().Insert(u)
+ }
+ if err != nil {
return err
}