robotLJW commented on a change in pull request #746:
URL: 
https://github.com/apache/servicecomb-service-center/pull/746#discussion_r526086910



##########
File path: datasource/mongo/account.go
##########
@@ -19,29 +19,127 @@ package mongo
 
 import (
        "context"
+       "errors"
+       "github.com/apache/servicecomb-service-center/datasource"
+       "github.com/apache/servicecomb-service-center/datasource/mongo/client"
+       "github.com/apache/servicecomb-service-center/pkg/log"
        "github.com/apache/servicecomb-service-center/pkg/rbacframe"
+       "github.com/apache/servicecomb-service-center/pkg/util"
+       stringutil "github.com/go-chassis/foundation/string"
+       "go.mongodb.org/mongo-driver/bson"
+       "golang.org/x/crypto/bcrypt"
 )
 
 func (ds *DataSource) CreateAccount(ctx context.Context, a *rbacframe.Account) 
error {
+       exist, err := ds.AccountExist(ctx, a.Name)
+       if err != nil {
+               log.Errorf(err, "can not save account info")
+               return err
+       }
+       if exist {
+               return datasource.ErrDuplicated
+       }
+       hash, err := bcrypt.GenerateFromPassword([]byte(a.Password), 14)
+       if err != nil {
+               log.Errorf(err, "pwd hash failed")
+               return err
+       }
+       a.Password = stringutil.Bytes2str(hash)
+       a.ID = util.GenerateUUID()
+       result, err := client.GetMongoClient().Insert(ctx, Account, a)

Review comment:
       可以在初始化建表的时候添加name的唯一索引




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to