Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: principal.phone create & update (#6464)

p0nye09926d8757d
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 8dd9617e5d..6546911c74 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -18,6 +18,8 @@ import (
"github.com/pquerna/otp/totp"
+ "github.com/nyaruka/phonenumbers"
+
"github.com/bytebase/bytebase/backend/api/auth"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/component/config"
@@ -122,8 +124,14 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
}
firstEndUser := count == 0
+ if request.User.Phone != "" {
+ if err := validatePhone(request.User.Phone); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid phone %q, error: %v", request.User.Phone, err)
+ }
+ }
+
if err := validateEmail(request.User.Email); err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid email %q format: %v", request.User.Email, err)
+ return nil, status.Errorf(codes.InvalidArgument, "invalid email %q, error: %v", request.User.Email, err)
}
existingUser, err := s.store.GetUser(ctx, &store.FindUserMessage{
Email: &request.User.Email,
@@ -151,6 +159,7 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
userMessage := &store.UserMessage{
Email: request.User.Email,
Name: request.User.Title,
+ Phone: request.User.Phone,
Type: principalType,
PasswordHash: string(passwordHash),
}
@@ -310,6 +319,14 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
}
patch.MFAConfig = &storepb.MFAConfig{}
}
+ case "phone":
+ if request.User.Phone == "" {
+ return nil, status.Errorf(codes.InvalidArgument, "phone number cannot be empty")
+ }
+ if err := validatePhone(request.User.Phone); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid phone number %q, error: %v", request.User.Phone, err)
+ }
+ patch.Phone = &request.User.Phone
}
}
if passwordPatch != nil {
@@ -458,6 +475,7 @@ func convertToUser(user *store.UserMessage) *v1pb.User {
Name: fmt.Sprintf("%s%d", userNamePrefix, user.ID),
State: convertDeletedToState(user.MemberDeleted),
Email: user.Email,
+ Phone: user.Phone,
Title: user.Name,
UserType: userType,
UserRole: role,
@@ -803,6 +821,17 @@ func validateEmail(email string) error {
return nil
}
+func validatePhone(phone string) error {
+ phoneNumber, err := phonenumbers.Parse(phone, "")
+ if err != nil {
+ return err
+ }
+ if !phonenumbers.IsValidNumber(phoneNumber) {
+ return errors.New("invalid phone number")
+ }
+ return nil
+}
+
const (
// issuerName is the name of the issuer of the OTP token.
issuerName = "Bytebase"
diff --git a/backend/store/principal.go b/backend/store/principal.go
index bb246b2ec9..6ff90e5565 100644
--- a/backend/store/principal.go
+++ b/backend/store/principal.go
@@ -88,6 +88,7 @@ type UpdateUserMessage struct {
Role *api.Role
Delete *bool
MFAConfig *storepb.MFAConfig
+ Phone *string
}
// UserMessage is the message for an user.
@@ -101,6 +102,8 @@ type UserMessage struct {
Role api.Role
MemberDeleted bool
MFAConfig *storepb.MFAConfig
+ // Phone conforms E.164 format.
+ Phone string
}
// GetUser gets an user.
@@ -208,6 +211,7 @@ func (*Store) listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) (
principal.type,
principal.password_hash,
principal.mfa_config,
+ principal.phone,
member.role,
member.row_status AS row_status
FROM principal
@@ -235,6 +239,7 @@ func (*Store) listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) (
&userMessage.Type,
&userMessage.PasswordHash,
&mfaConfigBytes,
+ &userMessage.Phone,
&role,
&rowStatus,
); err != nil {
@@ -281,8 +286,8 @@ func (s *Store) CreateUser(ctx context.Context, create *UserMessage, creatorID i
}
defer tx.Rollback()
- set := []string{"creator_id", "updater_id", "email", "name", "type", "password_hash"}
- args := []any{creatorID, creatorID, create.Email, create.Name, create.Type, create.PasswordHash}
+ set := []string{"creator_id", "updater_id", "email", "name", "type", "password_hash", "phone"}
+ args := []any{creatorID, creatorID, create.Email, create.Name, create.Type, create.PasswordHash, create.Phone}
placeholder := []string{}
for index := range set {
placeholder = append(placeholder, fmt.Sprintf("$%d", index+1))
@@ -345,6 +350,7 @@ func (s *Store) CreateUser(ctx context.Context, create *UserMessage, creatorID i
Name: create.Name,
Type: create.Type,
PasswordHash: create.PasswordHash,
+ Phone: create.Phone,
Role: role,
}
s.userIDCache.Store(user.ID, user)
@@ -367,6 +373,9 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMes
if v := patch.PasswordHash; v != nil {
principalSet, principalArgs = append(principalSet, fmt.Sprintf("password_hash = $%d", len(principalArgs)+1)), append(principalArgs, *v)
}
+ if v := patch.Phone; v != nil {
+ principalSet, principalArgs = append(principalSet, fmt.Sprintf("phone = $%d", len(principalArgs)+1)), append(principalArgs, *v)
+ }
if v := patch.MFAConfig; v != nil {
mfaConfigBytes, err := protojson.Marshal(v)
if err != nil {
@@ -401,7 +410,7 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMes
UPDATE principal
SET `+strings.Join(principalSet, ", ")+`
WHERE id = $%d
- RETURNING id, email, name, type, password_hash, mfa_config
+ RETURNING id, email, name, type, password_hash, mfa_config, phone
`, len(principalArgs)),
principalArgs...,
).Scan(
@@ -411,6 +420,7 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMes
&user.Type,
&user.PasswordHash,
&mfaConfigBytes,
+ &user.Phone,
); err != nil {
if err == sql.ErrNoRows {
return nil, nil
diff --git a/go.mod b/go.mod
index ebecffd85b..6ce904a5c6 100644
--- a/go.mod
+++ b/go.mod
@@ -38,6 +38,7 @@ require (
github.com/lib/pq v1.10.7
github.com/mattn/go-sqlite3 v1.14.16
github.com/microsoft/go-mssqldb v0.21.0
+ github.com/nyaruka/phonenumbers v1.1.7
github.com/paulmach/orb v0.9.0
github.com/pganalyze/pg_query_go/v2 v2.1.2
github.com/pingcap/tidb v1.1.0-beta.0.20220825063022-5263a0abda61
diff --git a/go.sum b/go.sum
index 2c030b5646..8be49d14d1 100644
--- a/go.sum
+++ b/go.sum
@@ -669,6 +669,8 @@ github.com/ncw/directio v1.0.5 h1:JSUBhdjEvVaJvOoyPAbcW0fnd0tvRXD76wEfZ1KcQz4=
github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7 h1:7KAv7KMGTTqSmYZtNdcNTgsos+vFzULLwyElndwn+5c=
github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef h1:K0Fn+DoFqNqktdZtdV3bPQ/0cuYh2H4rkg0tytX/07k=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
+github.com/nyaruka/phonenumbers v1.1.7 h1:5UUI9hE79Kk0dymSquXbMYB7IlNDNhvu2aNlJpm9et8=
+github.com/nyaruka/phonenumbers v1.1.7/go.mod h1:DC7jZd321FqUe+qWSNcHi10tyIyGNXGcNbfkPvdp1Vs=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=