Explorar el Código

update 完善错误检测

Alvin hace 10 meses
padre
commit
66929dd3ca
Se han modificado 1 ficheros con 15 adiciones y 5 borrados
  1. 15 5
      game/game_cluster/nodes/webadmin/service/admin.go

+ 15 - 5
game/game_cluster/nodes/webadmin/service/admin.go

@@ -250,8 +250,13 @@ func (a *Admin) QueryUserByUsername(ctx context.Context, username string) (*mode
 // ChangePassword 修改管理员密码
 func (a *Admin) ChangePassword(ctx context.Context, username string, password string) *code.Result {
 	// 更新密码
-	password, _ = HashPassword(password)
-	_, err := mdb.MDB.Collection(a.GetDBName()).UpdateOne(ctx, bson.M{"username": username}, bson.M{"$set": bson.M{"password": password}})
+	newPassword, err := HashPassword(password)
+	if err != nil {
+		mhayaLogger.Warnf("ChangePassword HashPassword error:%v", err)
+		return common.NewResult(code.InternalError)
+	}
+
+	_, err = mdb.MDB.Collection(a.GetDBName()).UpdateOne(ctx, bson.M{"username": username}, bson.M{"$set": bson.M{"password": newPassword}})
 	if err != nil {
 		mhayaLogger.Warnf("ChangePassword UpdateOne error:%v", err)
 		return common.NewResult(code.InternalError)
@@ -266,10 +271,15 @@ func (a *Admin) Add(ctx context.Context, username string, password string, realN
 	admin := model.Admin{}
 	err := mdb.MDB.Collection(a.GetDBName()).FindOne(ctx, bson.M{"username": username}).Decode(&admin)
 	if errors.Is(err, mongo.ErrNoDocuments) {
-		password, _ = HashPassword(password)
-		_, err := mdb.MDB.Collection(a.GetDBName()).InsertOne(ctx, bson.M{
+		newPassword, err := HashPassword(password)
+		if err != nil {
+			mhayaLogger.Warnf("Add HashPassword error:%v", err)
+			return common.NewResult(code.InternalError)
+		}
+
+		_, err = mdb.MDB.Collection(a.GetDBName()).InsertOne(ctx, bson.M{
 			"username":   username,
-			"password":   password,
+			"password":   newPassword,
 			"real_name":  realName,
 			"pid":        pid,
 			"role_id":    roleId,