Skip to content

Commit

Permalink
satellite/db: add account activation columns
Browse files Browse the repository at this point in the history
This change adds a new column to the user table, which will hold a
random code for account activation. And another to hold the signup
request ID as another layer of verification.

Issue: #6428

Change-Id: Icd46cb5d8fc76102264d599aca27686cd8b2e84e
  • Loading branch information
wilfred-asomanii committed Nov 22, 2023
1 parent 573ce71 commit 26574fb
Show file tree
Hide file tree
Showing 9 changed files with 961 additions and 27 deletions.
8 changes: 8 additions & 0 deletions satellite/console/users.go
Expand Up @@ -113,6 +113,8 @@ type CreateUser struct {
CaptchaResponse string `json:"captchaResponse"`
IP string `json:"ip"`
SignupPromoCode string `json:"signupPromoCode"`
ActivationCode string `json:"-"`
SignupId string `json:"-"`
}

// IsValid checks CreateUser validity and returns error describing whats wrong.
Expand Down Expand Up @@ -215,6 +217,9 @@ type User struct {
SignupCaptcha *float64 `json:"-"`

DefaultPlacement storj.PlacementConstraint `json:"defaultPlacement"`

ActivationCode string `json:"-"`
SignupId string `json:"-"`
}

// ResponseUser is an entity which describes db User and can be sent in response.
Expand Down Expand Up @@ -282,6 +287,9 @@ type UpdateUserRequest struct {
LoginLockoutExpiration **time.Time

DefaultPlacement storj.PlacementConstraint

ActivationCode *string
SignupId *string
}

// UserSettings contains configurations for a user.
Expand Down
150 changes: 124 additions & 26 deletions satellite/satellitedb/dbx/satellitedb.dbx.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions satellite/satellitedb/dbx/satellitedb.dbx.pgx.sql
Expand Up @@ -470,6 +470,8 @@ CREATE TABLE users (
login_lockout_expiration timestamp with time zone,
signup_captcha double precision,
default_placement integer,
activation_code text,
signup_id text,
PRIMARY KEY ( id )
);
CREATE TABLE user_settings (
Expand Down
2 changes: 2 additions & 0 deletions satellite/satellitedb/dbx/satellitedb.dbx.pgxcockroach.sql
Expand Up @@ -470,6 +470,8 @@ CREATE TABLE users (
login_lockout_expiration timestamp with time zone,
signup_captcha double precision,
default_placement integer,
activation_code text,
signup_id text,
PRIMARY KEY ( id )
);
CREATE TABLE user_settings (
Expand Down
5 changes: 5 additions & 0 deletions satellite/satellitedb/dbx/user.dbx
Expand Up @@ -79,6 +79,11 @@ model user (

// placement to be used for every new project as default for the buckets.
field default_placement int (nullable, updatable)

// activation_code is a code sent to a user's email on signup for account activation.
field activation_code text ( updatable, nullable )
// signup_id is a the request ID associated to a sign up session, used to also verify activation_code.
field signup_id text ( updatable, nullable )
)

create user ( )
Expand Down
9 changes: 9 additions & 0 deletions satellite/satellitedb/migrate.go
Expand Up @@ -2562,6 +2562,15 @@ func (db *satelliteDB) ProductionMigration() *migrate.Migration {
`ALTER TABLE bucket_metainfos ADD COLUMN versioning INTEGER NOT NULL DEFAULT 0;`,
},
},
{
DB: &db.migrationDB,
Description: "Add activation_code column to users table",
Version: 251,
Action: migrate.SQL{
`ALTER TABLE users ADD COLUMN activation_code TEXT;`,
`ALTER TABLE users ADD COLUMN signup_id TEXT;`,
},
},
// NB: after updating testdata in `testdata`, run
// `go generate` to update `migratez.go`.
},
Expand Down
4 changes: 3 additions & 1 deletion satellite/satellitedb/migratez.go
Expand Up @@ -13,7 +13,7 @@ func (db *satelliteDB) testMigration() *migrate.Migration {
{
DB: &db.migrationDB,
Description: "Testing setup",
Version: 250,
Version: 251,
Action: migrate.SQL{`-- AUTOGENERATED BY storj.io/dbx
-- DO NOT EDIT
CREATE TABLE account_freeze_events (
Expand Down Expand Up @@ -487,6 +487,8 @@ CREATE TABLE users (
login_lockout_expiration timestamp with time zone,
signup_captcha double precision,
default_placement integer,
activation_code text,
signup_id text,
PRIMARY KEY ( id )
);
CREATE TABLE user_settings (
Expand Down
784 changes: 784 additions & 0 deletions satellite/satellitedb/testdata/postgres.v251.sql

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions satellite/satellitedb/users.go
Expand Up @@ -249,6 +249,14 @@ func (users *users) Insert(ctx context.Context, user *console.User) (_ *console.
optional.DefaultPlacement = dbx.User_DefaultPlacement(int(user.DefaultPlacement))
}

if user.ActivationCode != "" {
optional.ActivationCode = dbx.User_ActivationCode(user.ActivationCode)
}

if user.SignupId != "" {
optional.SignupId = dbx.User_SignupId(user.SignupId)
}

createdUser, err := users.db.Create_User(ctx,
dbx.User_Id(user.ID[:]),
dbx.User_Email(user.Email),
Expand Down Expand Up @@ -614,6 +622,14 @@ func toUpdateUser(request console.UpdateUserRequest) (*dbx.User_Update_Fields, e
update.DefaultPlacement = dbx.User_DefaultPlacement(int(request.DefaultPlacement))
}

if request.ActivationCode != nil {
update.ActivationCode = dbx.User_ActivationCode(*request.ActivationCode)
}

if request.SignupId != nil {
update.SignupId = dbx.User_SignupId(*request.SignupId)
}

return &update, nil
}

Expand Down Expand Up @@ -704,6 +720,14 @@ func userFromDBX(ctx context.Context, user *dbx.User) (_ *console.User, err erro
result.LoginLockoutExpiration = *user.LoginLockoutExpiration
}

if user.ActivationCode != nil {
result.ActivationCode = *user.ActivationCode
}

if user.SignupId != nil {
result.SignupId = *user.SignupId
}

return &result, nil
}

Expand Down

0 comments on commit 26574fb

Please sign in to comment.