You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2020/11/18 09:02:54 UTC

[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #746: SCB-2094 Implement the Mongo account interface

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



##########
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:
       没有事务吗?如果并发一个accont 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:
users@infra.apache.org