Skip to content

Commit

Permalink
fix(user): added email/mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Apr 23, 2024
1 parent d346061 commit 5b84777
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 13 deletions.
8 changes: 5 additions & 3 deletions auth_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (a *Auth) createLoginWithEmail(ctx context.Context, email, passwd, firstNam
txUserPending = true

now := time.Now()
u, txErr = a.createUser(ctx, txUser, id, passwd, firstName, lastName, now)
u, txErr = a.createUser(ctx, txUser, id, passwd, firstName, lastName, email, "", now)
if txErr != nil {
return u, txErr
}
Expand Down Expand Up @@ -272,7 +272,7 @@ func (a *Auth) createLoginWithEmail(ctx context.Context, email, passwd, firstNam
return u, nil
}

func (a *Auth) createUser(ctx context.Context, tx *sqle.Tx, id shardid.ID, passwd, firstName, lastName string, now time.Time) (User, error) {
func (a *Auth) createUser(ctx context.Context, tx *sqle.Tx, id shardid.ID, passwd, firstName, lastName, email, mobile string, now time.Time) (User, error) {
u := User{
ID: id,
Status: UserStatusWaiting,
Expand All @@ -294,6 +294,8 @@ func (a *Auth) createUser(ctx context.Context, tx *sqle.Tx, id shardid.ID, passw
Set("last_name", lastName).
Set("passwd", u.Passwd).
Set("salt", u.Salt).
If(email != "").Set("email", masker.Email(email)).
If(mobile != "").Set("mobile", masker.Mobile(mobile)).
Set("created_at", now).
Set("updated_at", now).
End())
Expand Down Expand Up @@ -477,7 +479,7 @@ func (a *Auth) createLoginWithMobile(ctx context.Context, mobile, passwd, firstN
txUserPending = true

now := time.Now()
u, txErr = a.createUser(ctx, txUser, id, passwd, firstName, lastName, now)
u, txErr = a.createUser(ctx, txUser, id, passwd, firstName, lastName, "", mobile, now)
if txErr != nil {
return u, txErr
}
Expand Down
53 changes: 53 additions & 0 deletions auth_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ func (a *Auth) CreateRole(ctx context.Context, name string) (int, error) {
return int(id), nil
}

// UpdateRole update role name
func (a *Auth) UpdateRole(ctx context.Context, id int, name string) error {
_, err := a.db.
ExecBuilder(ctx, a.createBuilder().
Update("<prefix>role").
Set("name", name).
Set("updated_at", time.Now()).
Where("id = {id}").
Param("id", id))

if err != nil {
a.logger.Error("auth: UpdateRole",
slog.String("tag", "db"),
slog.String("name", name),
slog.Any("err", err))
return ErrBadDatabase
}

return nil
}

// DeleteRole delete role, role_user, role_perm
func (a *Auth) DeleteRole(ctx context.Context, id int) error {
err := a.db.Transaction(ctx, nil, func(ctx context.Context, tx *sqle.Tx) error {
_, err := tx.ExecBuilder(ctx, a.createBuilder().Delete("<prefix>role").Where("id = {id}").Param("id", id))
if err != nil {
return err
}

_, err = tx.ExecBuilder(ctx, a.createBuilder().Delete("<prefix>role_user").Where("role_id = {role_id}").Param("role_id", id))
if err != nil {
return err
}

_, err = tx.ExecBuilder(ctx, a.createBuilder().Delete("<prefix>role_perm").Where("role_id = {role_id}").Param("role_id", id))
if err != nil {
return err
}

return nil
})

if err != nil {
a.logger.Error("auth: DeleteRole",
slog.String("tag", "db"),
slog.Int("role_id", id),
slog.Any("err", err))
return ErrBadDatabase
}

return nil
}

// GetRoleUsers get users by role id
func (a *Auth) GetRoleUsers(ctx context.Context, rid int) ([]User, error) {
var items []User
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.22
github.com/pquerna/otp v1.4.0
github.com/stretchr/testify v1.9.0
github.com/yaitoo/sqle v1.4.6-0.20240420151133-718c9c560a4d
github.com/yaitoo/sqle v1.4.6-0.20240422094859-edb8304a2559
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yaitoo/async v1.0.4 h1:u+SWuJcSckgBOcMjMYz9IviojeCatDrdni3YNGLCiHY=
github.com/yaitoo/async v1.0.4/go.mod h1:IpSO7Ei7AxiqLxFqDjN4rJaVlt8wm4ZxMXyyQaWmM1g=
github.com/yaitoo/sqle v1.4.6-0.20240420151133-718c9c560a4d h1:HhUOpV3jjYWK4eo+jsuPemnZdLRj6ce7dmXTW31GDFw=
github.com/yaitoo/sqle v1.4.6-0.20240420151133-718c9c560a4d/go.mod h1:Bv1PPG6hYZP2In3WKN1dBqYNJiWP0ZSLs6uEkRo2c9M=
github.com/yaitoo/sqle v1.4.6-0.20240422094859-edb8304a2559 h1:sSOObYzN8/7PqEJn2KVJHnMRobZEwa0NNo0Gg0e9I8U=
github.com/yaitoo/sqle v1.4.6-0.20240422094859-edb8304a2559/go.mod h1:Bv1PPG6hYZP2In3WKN1dBqYNJiWP0ZSLs6uEkRo2c9M=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
2 changes: 2 additions & 0 deletions migration/0.0.1/1_user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CREATE TABLE IF NOT EXISTS `<prefix>user` (
`last_name` varchar(255) NOT NULL,
`passwd` varchar(255) NOT NULL,
`salt` varchar(36) NOT NULL,
`email` VARCHAR(125) NULL,
`mobile` VARCHAR(25) NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
Expand Down
2 changes: 2 additions & 0 deletions migration/0.0.1/1_user.sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CREATE TABLE IF NOT EXISTS `<prefix>user` (
`last_name` varchar(255) NOT NULL,
`passwd` varchar(255) NOT NULL,
`salt` varchar(36) NOT NULL,
`email` VARCHAR(125) NULL,
`mobile` VARCHAR(25) NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
Expand Down
2 changes: 1 addition & 1 deletion migration/0.0.1/3_user_mobile.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS `<prefix>user_mobile` (
`hash` VARCHAR(255) NOT NULL,
`user_id` BIGINT NOT NULL,
`mask` VARCHAR(125) NOT NULL,
`mask` VARCHAR(25) NOT NULL,
`is_verified` BIT(1) NOT NULL DEFAULT 0,
`verified_at` DATETIME NULL,
`created_at` DATETIME NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion migration/0.0.1/3_user_mobile.sqlite
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS `<prefix>user_mobile` (
`hash` VARCHAR(255) NOT NULL,
`user_id` BIGINT NOT NULL,
`mask` VARCHAR(125) NOT NULL,
`mask` VARCHAR(25) NOT NULL,
`is_verified` BIT(1) NOT NULL DEFAULT 0,
`verified_at` DATETIME NULL,
`created_at` DATETIME NOT NULL,
Expand Down
14 changes: 9 additions & 5 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ const (
// User user info
type User struct {
ID shardid.ID `json:"id,omitempty"`
Status UserStatus `json:"status"`
Status UserStatus `json:"status,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
// Passwd the hash of user's password with salt
Passwd string `json:"-"`
Salt string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Passwd string `json:"-"`
Salt string `json:"-"`

Email string `json:"email,omitempty"`
Mobile string `json:"mobile,omitempty"`

CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}

0 comments on commit 5b84777

Please sign in to comment.