mirror of
https://github.com/go-gitea/gitea
synced 2025-07-19 00:38:36 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user