Skip to content

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

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

Merged
merged 17 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,18 @@ func UpdateUser(ctx context.Context, u *User, changePrimaryEmail bool, cols ...s
return err
}

// UpdateUserCreated stores the user's `CreatedUnix` field in the database.
// This is intended to allow migration of users from another system while
// maintaining the user's creation timestamp.
func UpdateUserCreated(ctx context.Context, u *User) error {
if err := validateUser(u); err != nil {
return err
}

_, err := db.Exec(ctx, "UPDATE `user` SET created_unix=? WHERE id=?", u.CreatedUnix, u.ID)
return err
}

// UpdateUserCols update user according special columns
func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
if err := validateUser(u); err != nil {
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
13 changes: 13 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 @@ -136,6 +137,18 @@ func CreateUser(ctx *context.APIContext) {
}
log.Trace("Account created by admin (%s): %s", ctx.Doer.Name, u.Name)

// 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.UpdateUserCreated(ctx, u); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateUserCreated", err)
return
}
log.Trace("Account profile back-dated by admin (%s): %s", ctx.Doer.Name, u.Name)
}

// Send email notification.
if form.SendNotify {
mailer.SendRegisterNotifyMail(u)
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