Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User creation API: allow custom "created" timestamps #22549

Merged
merged 17 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,13 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e
u.IsRestricted = setting.Service.DefaultUserIsRestricted
u.IsActive = !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm)

if u.CreatedUnix == 0 {
u.CreatedUnix = timeutil.TimeStampNow()
}
if u.UpdatedUnix == 0 {
u.UpdatedUnix = u.CreatedUnix
}

// overwrite defaults if set
if len(overwriteDefault) != 0 && overwriteDefault[0] != nil {
overwrite := overwriteDefault[0]
Expand Down Expand Up @@ -742,7 +749,7 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e
return err
}

if err = db.Insert(ctx, u); err != nil {
if _, err = db.GetEngine(ctx).NoAutoTime().Insert(u); err != nil {
return err
}

Expand Down
7 changes: 7 additions & 0 deletions modules/structs/admin_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package structs

import "time"

// CreateUserOption create user options
type CreateUserOption struct {
SourceID int64 `json:"source_id"`
Expand All @@ -20,6 +22,11 @@ type CreateUserOption struct {
SendNotify bool `json:"send_notify"`
Restricted *bool `json:"restricted"`
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`

// For explicitly setting the user creation timestamp. Useful when users are
// migrated from other systems. When omitted, the user's creation timestamp
// will be set to "now".
Created *time.Time `json:"created_at"`
}

// EditUserOption edit user options
Expand Down
8 changes: 8 additions & 0 deletions routers/api/v1/admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -120,6 +121,13 @@ 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())
}

if err := user_model.CreateUser(u, overwriteDefault); err != nil {
if user_model.IsErrUserAlreadyExist(err) ||
user_model.IsErrEmailAlreadyUsed(err) ||
Expand Down
6 changes: 6 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15622,6 +15622,12 @@
"password"
],
"properties": {
"created_at": {
"description": "For explicitly setting the user creation timestamp. Useful when users are\nmigrated from other systems. When omitted, the user's creation timestamp\nwill be set to \"now\".",
"type": "string",
"format": "date-time",
"x-go-name": "Created"
},
"email": {
"type": "string",
"format": "email",
Expand Down