mndUser.c 41.4 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndUser.h"
S
Shengliang Guan 已提交
18
#include "mndDb.h"
S
Shengliang Guan 已提交
19
#include "mndPrivilege.h"
S
Shengliang Guan 已提交
20
#include "mndShow.h"
21
#include "mndTopic.h"
S
Shengliang Guan 已提交
22
#include "mndTrans.h"
S
tbase64  
Shengliang Guan 已提交
23
#include "tbase64.h"
C
table  
cademfly 已提交
24
#include "mndStb.h"
S
Shengliang Guan 已提交
25

C
table  
cademfly 已提交
26
#define USER_VER_NUMBER   3
27
#define USER_RESERVE_SIZE 64
S
Shengliang Guan 已提交
28

S
Shengliang Guan 已提交
29 30 31 32
static int32_t  mndCreateDefaultUsers(SMnode *pMnode);
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw);
static int32_t  mndUserActionInsert(SSdb *pSdb, SUserObj *pUser);
static int32_t  mndUserActionDelete(SSdb *pSdb, SUserObj *pUser);
S
Shengliang Guan 已提交
33
static int32_t  mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew);
S
Shengliang Guan 已提交
34 35 36 37 38 39
static int32_t  mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq);
static int32_t  mndProcessCreateUserReq(SRpcMsg *pReq);
static int32_t  mndProcessAlterUserReq(SRpcMsg *pReq);
static int32_t  mndProcessDropUserReq(SRpcMsg *pReq);
static int32_t  mndProcessGetUserAuthReq(SRpcMsg *pReq);
static int32_t  mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
40
static void     mndCancelGetNextUser(SMnode *pMnode, void *pIter);
41 42
static int32_t  mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void     mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter);
S
Shengliang Guan 已提交
43 44

int32_t mndInitUser(SMnode *pMnode) {
S
Shengliang Guan 已提交
45 46 47 48 49 50 51 52 53 54
  SSdbTable table = {
      .sdbType = SDB_USER,
      .keyType = SDB_KEY_BINARY,
      .deployFp = (SdbDeployFp)mndCreateDefaultUsers,
      .encodeFp = (SdbEncodeFp)mndUserActionEncode,
      .decodeFp = (SdbDecodeFp)mndUserActionDecode,
      .insertFp = (SdbInsertFp)mndUserActionInsert,
      .updateFp = (SdbUpdateFp)mndUserActionUpdate,
      .deleteFp = (SdbDeleteFp)mndUserActionDelete,
  };
S
Shengliang Guan 已提交
55

S
Shengliang Guan 已提交
56 57 58
  mndSetMsgHandle(pMnode, TDMT_MND_CREATE_USER, mndProcessCreateUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_ALTER_USER, mndProcessAlterUserReq);
  mndSetMsgHandle(pMnode, TDMT_MND_DROP_USER, mndProcessDropUserReq);
S
Shengliang Guan 已提交
59
  mndSetMsgHandle(pMnode, TDMT_MND_GET_USER_AUTH, mndProcessGetUserAuthReq);
S
Shengliang Guan 已提交
60

S
Shengliang Guan 已提交
61 62
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_USER, mndRetrieveUsers);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_USER, mndCancelGetNextUser);
S
Shengliang Guan 已提交
63 64
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndRetrievePrivileges);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_PRIVILEGES, mndCancelGetNextPrivileges);
S
Shengliang Guan 已提交
65 66 67 68 69 70 71
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupUser(SMnode *pMnode) {}

static int32_t mndCreateDefaultUser(SMnode *pMnode, char *acct, char *user, char *pass) {
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
72
  taosEncryptPass_c((uint8_t *)pass, strlen(pass), userObj.pass);
S
Shengliang Guan 已提交
73 74 75 76
  tstrncpy(userObj.user, user, TSDB_USER_LEN);
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
77 78
  userObj.sysInfo = 1;
  userObj.enable = 1;
S
Shengliang Guan 已提交
79 80

  if (strcmp(user, TSDB_DEFAULT_USER) == 0) {
81
    userObj.superUser = 1;
S
Shengliang Guan 已提交
82 83 84 85
  }

  SSdbRaw *pRaw = mndUserActionEncode(&userObj);
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
86
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
87

88
  mInfo("user:%s, will be created when deploying, raw:%p", userObj.user, pRaw);
89

90
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-user");
91
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
92
    sdbFreeRaw(pRaw);
93 94 95
    mError("user:%s, failed to create since %s", userObj.user, terrstr());
    return -1;
  }
96
  mInfo("trans:%d, used to create user:%s", pTrans->id, userObj.user);
97 98 99 100 101 102

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
103
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
104 105 106 107 108 109 110 111 112

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
113 114 115 116 117 118 119 120 121 122
}

static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
  if (mndCreateDefaultUser(pMnode, TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) {
    return -1;
  }

  return 0;
}

123
SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
124 125
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
126 127
  int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
  int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
C
table  
cademfly 已提交
128 129
  int32_t numOfReadStbs = taosHashGetSize(pUser->readStbs);
  int32_t numOfWriteStbs = taosHashGetSize(pUser->writeStbs);
130
  int32_t numOfTopics = taosHashGetSize(pUser->topics);
C
table  
cademfly 已提交
131 132 133
  int32_t size = sizeof(SUserObj) + USER_RESERVE_SIZE + 
                (numOfReadDbs + numOfWriteDbs ) * TSDB_DB_FNAME_LEN +
                (numOfReadStbs + numOfWriteStbs) * TSDB_TABLE_FNAME_LEN + 
134
                 numOfTopics * TSDB_TOPIC_FNAME_LEN;
S
Shengliang Guan 已提交
135

136
  SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
137
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
138 139

  int32_t dataPos = 0;
140 141 142 143 144 145
  SDB_SET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pUser->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pUser->updateTime, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->superUser, _OVER)
146 147 148
  SDB_SET_INT8(pRaw, dataPos, pUser->sysInfo, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->enable, _OVER)
  SDB_SET_INT8(pRaw, dataPos, pUser->reserve, _OVER)
149
  SDB_SET_INT32(pRaw, dataPos, pUser->authVersion, _OVER)
150 151
  SDB_SET_INT32(pRaw, dataPos, numOfReadDbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteDbs, _OVER)
152
  SDB_SET_INT32(pRaw, dataPos, numOfTopics, _OVER)
C
table  
cademfly 已提交
153 154
  SDB_SET_INT32(pRaw, dataPos, numOfReadStbs, _OVER)
  SDB_SET_INT32(pRaw, dataPos, numOfWriteStbs, _OVER)
155 156 157

  char *db = taosHashIterate(pUser->readDbs, NULL);
  while (db != NULL) {
158
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
159 160 161 162 163
    db = taosHashIterate(pUser->readDbs, db);
  }

  db = taosHashIterate(pUser->writeDbs, NULL);
  while (db != NULL) {
164
    SDB_SET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER);
165 166 167
    db = taosHashIterate(pUser->writeDbs, db);
  }

168 169 170
  char *topic = taosHashIterate(pUser->topics, NULL);
  while (topic != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER);
171
    topic = taosHashIterate(pUser->topics, topic);
172 173
  }

C
table  
cademfly 已提交
174 175 176 177 178 179 180 181 182 183 184 185
  char *stb = taosHashIterate(pUser->readStbs, NULL);
  while (stb != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, stb, TSDB_TABLE_FNAME_LEN, _OVER);
    stb = taosHashIterate(pUser->readStbs, stb);
  }

  stb = taosHashIterate(pUser->writeStbs, NULL);
  while (stb != NULL) {
    SDB_SET_BINARY(pRaw, dataPos, stb, TSDB_TABLE_FNAME_LEN, _OVER);
    stb = taosHashIterate(pUser->writeStbs, stb);
  }

186 187
  SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
  SDB_SET_DATALEN(pRaw, dataPos, _OVER)
188 189 190

  terrno = 0;

191
_OVER:
192 193 194 195 196
  if (terrno != 0) {
    mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
197

S
Shengliang Guan 已提交
198
  mTrace("user:%s, encode to raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
199
  return pRaw;
S
Shengliang Guan 已提交
200 201
}

S
Shengliang Guan 已提交
202
static SSdbRow *mndUserActionDecode(SSdbRaw *pRaw) {
203
  terrno = TSDB_CODE_OUT_OF_MEMORY;
204 205
  SSdbRow  *pRow = NULL;
  SUserObj *pUser = NULL;
206

S
Shengliang Guan 已提交
207
  int8_t sver = 0;
208
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
209

C
table  
cademfly 已提交
210
  if (sver != 1 && sver != 2 && sver != 3) {
S
Shengliang Guan 已提交
211
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
212
    goto _OVER;
S
Shengliang Guan 已提交
213
  }
S
Shengliang Guan 已提交
214

215
  pRow = sdbAllocRow(sizeof(SUserObj));
216
  if (pRow == NULL) goto _OVER;
217

218
  pUser = sdbGetRowObj(pRow);
219
  if (pUser == NULL) goto _OVER;
220

S
Shengliang Guan 已提交
221
  int32_t dataPos = 0;
222 223 224 225 226 227
  SDB_GET_BINARY(pRaw, dataPos, pUser->user, TSDB_USER_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pUser->pass, TSDB_PASSWORD_LEN, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pUser->acct, TSDB_USER_LEN, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pUser->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pUser->updateTime, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->superUser, _OVER)
228 229 230
  SDB_GET_INT8(pRaw, dataPos, &pUser->sysInfo, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->enable, _OVER)
  SDB_GET_INT8(pRaw, dataPos, &pUser->reserve, _OVER)
231
  SDB_GET_INT32(pRaw, dataPos, &pUser->authVersion, _OVER)
232 233 234

  int32_t numOfReadDbs = 0;
  int32_t numOfWriteDbs = 0;
235
  int32_t numOfTopics = 0;
C
table  
cademfly 已提交
236 237
  int32_t numOfReadStbs = 0;
  int32_t numOfWriteStbs = 0;
238 239
  SDB_GET_INT32(pRaw, dataPos, &numOfReadDbs, _OVER)
  SDB_GET_INT32(pRaw, dataPos, &numOfWriteDbs, _OVER)
240 241 242
  if (sver >= 2) {
    SDB_GET_INT32(pRaw, dataPos, &numOfTopics, _OVER)
  }
C
table  
cademfly 已提交
243 244 245 246
  if(sver >= 3){
    SDB_GET_INT32(pRaw, dataPos, &numOfReadStbs, _OVER)
    SDB_GET_INT32(pRaw, dataPos, &numOfWriteStbs, _OVER)
  }
247

S
Shengliang Guan 已提交
248 249 250
  pUser->readDbs = taosHashInit(numOfReadDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pUser->writeDbs =
      taosHashInit(numOfWriteDbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
251
  pUser->topics = taosHashInit(numOfTopics, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
C
table  
cademfly 已提交
252 253 254
  pUser->readStbs = taosHashInit(numOfReadStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
  pUser->writeStbs =
      taosHashInit(numOfWriteStbs, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
255
  if (pUser->readDbs == NULL || pUser->writeDbs == NULL || pUser->topics == NULL) goto _OVER;
256 257 258

  for (int32_t i = 0; i < numOfReadDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
259
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
260 261 262 263 264 265
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->readDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

  for (int32_t i = 0; i < numOfWriteDbs; ++i) {
    char db[TSDB_DB_FNAME_LEN] = {0};
266
    SDB_GET_BINARY(pRaw, dataPos, db, TSDB_DB_FNAME_LEN, _OVER)
267 268 269 270
    int32_t len = strlen(db) + 1;
    taosHashPut(pUser->writeDbs, db, len, db, TSDB_DB_FNAME_LEN);
  }

271 272 273 274 275 276 277 278 279
  if (sver >= 2) {
    for (int32_t i = 0; i < numOfTopics; ++i) {
      char topic[TSDB_TOPIC_FNAME_LEN] = {0};
      SDB_GET_BINARY(pRaw, dataPos, topic, TSDB_TOPIC_FNAME_LEN, _OVER)
      int32_t len = strlen(topic) + 1;
      taosHashPut(pUser->topics, topic, len, topic, TSDB_TOPIC_FNAME_LEN);
    }
  }

C
table  
cademfly 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  if(sver >= 3){
    for (int32_t i = 0; i < numOfReadStbs; ++i) {
      char stb[TSDB_TABLE_FNAME_LEN] = {0};
      SDB_GET_BINARY(pRaw, dataPos, stb, TSDB_TABLE_FNAME_LEN, _OVER)
      int32_t len = strlen(stb) + 1;
      taosHashPut(pUser->readStbs, stb, len, stb, TSDB_DB_FNAME_LEN);
    }

    for (int32_t i = 0; i < numOfWriteStbs; ++i) {
      char stb[TSDB_TABLE_FNAME_LEN] = {0};
      SDB_GET_BINARY(pRaw, dataPos, stb, TSDB_TABLE_FNAME_LEN, _OVER)
      int32_t len = strlen(stb) + 1;
      taosHashPut(pUser->writeStbs, stb, len, stb, TSDB_TABLE_FNAME_LEN);
    }
  }

296
  SDB_GET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
S
Shengliang Guan 已提交
297
  taosInitRWLatch(&pUser->lock);
298 299 300

  terrno = 0;

301
_OVER:
302
  if (terrno != 0) {
303 304 305 306 307
    mError("user:%s, failed to decode from raw:%p since %s", pUser == NULL ? "null" : pUser->user, pRaw, terrstr());
    if (pUser != NULL) {
      taosHashCleanup(pUser->readDbs);
      taosHashCleanup(pUser->writeDbs);
      taosHashCleanup(pUser->topics);
C
table  
cademfly 已提交
308 309
      taosHashCleanup(pUser->readStbs);
      taosHashCleanup(pUser->writeStbs);
310
    }
wafwerar's avatar
wafwerar 已提交
311
    taosMemoryFreeClear(pRow);
312 313
    return NULL;
  }
S
Shengliang Guan 已提交
314

S
Shengliang Guan 已提交
315
  mTrace("user:%s, decode from raw:%p, row:%p", pUser->user, pRaw, pUser);
S
Shengliang Guan 已提交
316
  return pRow;
S
Shengliang Guan 已提交
317
}
S
Shengliang Guan 已提交
318

S
Shengliang Guan 已提交
319
static int32_t mndUserActionInsert(SSdb *pSdb, SUserObj *pUser) {
S
Shengliang Guan 已提交
320
  mTrace("user:%s, perform insert action, row:%p", pUser->user, pUser);
S
Shengliang Guan 已提交
321

S
Shengliang Guan 已提交
322 323
  SAcctObj *pAcct = sdbAcquire(pSdb, SDB_ACCT, pUser->acct);
  if (pAcct == NULL) {
S
Shengliang Guan 已提交
324
    terrno = TSDB_CODE_MND_ACCT_NOT_EXIST;
S
Shengliang Guan 已提交
325
    mError("user:%s, failed to perform insert action since %s", pUser->user, terrstr());
S
Shengliang Guan 已提交
326
    return -1;
S
Shengliang Guan 已提交
327
  }
S
Shengliang Guan 已提交
328 329
  pUser->acctId = pAcct->acctId;
  sdbRelease(pSdb, pAcct);
S
Shengliang Guan 已提交
330

S
Shengliang Guan 已提交
331 332
  return 0;
}
S
Shengliang Guan 已提交
333

334 335 336 337 338 339 340 341
static int32_t mndUserDupObj(SUserObj *pUser, SUserObj *pNew) {
  memcpy(pNew, pUser, sizeof(SUserObj));
  pNew->authVersion++;
  pNew->updateTime = taosGetTimestampMs();

  taosRLockLatch(&pUser->lock);
  pNew->readDbs = mndDupDbHash(pUser->readDbs);
  pNew->writeDbs = mndDupDbHash(pUser->writeDbs);
C
table  
cademfly 已提交
342 343
  pNew->readStbs = mndDupTopicHash(pUser->readStbs);
  pNew->writeStbs = mndDupTopicHash(pUser->writeStbs);
344 345 346 347 348 349 350 351 352 353
  pNew->topics = mndDupTopicHash(pUser->topics);
  taosRUnLockLatch(&pUser->lock);

  if (pNew->readDbs == NULL || pNew->writeDbs == NULL || pNew->topics == NULL) {
    return -1;
  }
  return 0;
}

static void mndUserFreeObj(SUserObj *pUser) {
354 355
  taosHashCleanup(pUser->readDbs);
  taosHashCleanup(pUser->writeDbs);
356
  taosHashCleanup(pUser->topics);
C
table  
cademfly 已提交
357 358
  taosHashCleanup(pUser->readStbs);
  taosHashCleanup(pUser->writeStbs);
359 360
  pUser->readDbs = NULL;
  pUser->writeDbs = NULL;
361
  pUser->topics = NULL;
C
table  
cademfly 已提交
362 363
  pUser->readStbs = NULL;
  pUser->writeStbs = NULL;
364 365 366 367 368
}

static int32_t mndUserActionDelete(SSdb *pSdb, SUserObj *pUser) {
  mTrace("user:%s, perform delete action, row:%p", pUser->user, pUser);
  mndUserFreeObj(pUser);
S
Shengliang Guan 已提交
369 370 371
  return 0;
}

S
Shengliang Guan 已提交
372
static int32_t mndUserActionUpdate(SSdb *pSdb, SUserObj *pOld, SUserObj *pNew) {
S
Shengliang Guan 已提交
373
  mTrace("user:%s, perform update action, old row:%p new row:%p", pOld->user, pOld, pNew);
S
Shengliang Guan 已提交
374
  taosWLockLatch(&pOld->lock);
S
Shengliang Guan 已提交
375
  pOld->updateTime = pNew->updateTime;
D
dapan1121 已提交
376
  pOld->authVersion = pNew->authVersion;
377 378
  pOld->sysInfo = pNew->sysInfo;
  pOld->enable = pNew->enable;
S
Shengliang Guan 已提交
379
  memcpy(pOld->pass, pNew->pass, TSDB_PASSWORD_LEN);
wafwerar's avatar
wafwerar 已提交
380 381
  TSWAP(pOld->readDbs, pNew->readDbs);
  TSWAP(pOld->writeDbs, pNew->writeDbs);
382
  TSWAP(pOld->topics, pNew->topics);
C
table  
cademfly 已提交
383 384
  TSWAP(pOld->readStbs, pNew->readStbs);
  TSWAP(pOld->writeStbs, pNew->writeStbs);
S
Shengliang Guan 已提交
385
  taosWUnLockLatch(&pOld->lock);
386

S
Shengliang Guan 已提交
387 388 389
  return 0;
}

390
SUserObj *mndAcquireUser(SMnode *pMnode, const char *userName) {
S
Shengliang Guan 已提交
391 392 393
  SSdb     *pSdb = pMnode->pSdb;
  SUserObj *pUser = sdbAcquire(pSdb, SDB_USER, userName);
  if (pUser == NULL) {
dengyihao's avatar
dengyihao 已提交
394 395 396 397 398
    if (terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
      terrno = TSDB_CODE_MND_USER_NOT_EXIST;
    } else {
      terrno = TSDB_CODE_MND_USER_NOT_AVAILABLE;
    }
S
Shengliang Guan 已提交
399 400
  }
  return pUser;
S
Shengliang Guan 已提交
401
}
S
Shengliang Guan 已提交
402

S
Shengliang Guan 已提交
403 404 405
void mndReleaseUser(SMnode *pMnode, SUserObj *pUser) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pUser);
S
Shengliang Guan 已提交
406 407
}

S
Shengliang Guan 已提交
408
static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate, SRpcMsg *pReq) {
S
Shengliang Guan 已提交
409
  SUserObj userObj = {0};
S
Shengliang Guan 已提交
410 411
  taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
  tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
S
Shengliang Guan 已提交
412 413 414
  tstrncpy(userObj.acct, acct, TSDB_USER_LEN);
  userObj.createdTime = taosGetTimestampMs();
  userObj.updateTime = userObj.createdTime;
415
  userObj.superUser = 0;  // pCreate->superUser;
416 417
  userObj.sysInfo = pCreate->sysInfo;
  userObj.enable = pCreate->enable;
S
Shengliang Guan 已提交
418

419
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "create-user");
S
Shengliang Guan 已提交
420
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
421
    mError("user:%s, failed to create since %s", pCreate->user, terrstr());
S
Shengliang Guan 已提交
422 423
    return -1;
  }
424
  mInfo("trans:%d, used to create user:%s", pTrans->id, pCreate->user);
S
Shengliang Guan 已提交
425

426 427 428
  SSdbRaw *pCommitRaw = mndUserActionEncode(&userObj);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
429
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
430
    return -1;
S
Shengliang Guan 已提交
431
  }
S
Shengliang Guan 已提交
432
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
433

S
Shengliang Guan 已提交
434
  if (mndTransPrepare(pMnode, pTrans) != 0) {
S
Shengliang Guan 已提交
435
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
436
    mndTransDrop(pTrans);
S
Shengliang Guan 已提交
437
    return -1;
S
Shengliang Guan 已提交
438 439
  }

S
Shengliang Guan 已提交
440
  mndTransDrop(pTrans);
S
Shengliang Guan 已提交
441
  return 0;
S
Shengliang Guan 已提交
442 443
}

S
Shengliang Guan 已提交
444 445
static int32_t mndProcessCreateUserReq(SRpcMsg *pReq) {
  SMnode        *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
446 447 448 449 450
  int32_t        code = -1;
  SUserObj      *pUser = NULL;
  SUserObj      *pOperUser = NULL;
  SCreateUserReq createReq = {0};

S
Shengliang Guan 已提交
451
  if (tDeserializeSCreateUserReq(pReq->pCont, pReq->contLen, &createReq) != 0) {
S
Shengliang Guan 已提交
452
    terrno = TSDB_CODE_INVALID_MSG;
453
    goto _OVER;
S
Shengliang Guan 已提交
454
  }
S
Shengliang Guan 已提交
455

456
  mInfo("user:%s, start to create", createReq.user);
457 458 459
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CREATE_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
460

S
Shengliang Guan 已提交
461
  if (createReq.user[0] == 0) {
S
Shengliang Guan 已提交
462
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
463
    goto _OVER;
S
Shengliang Guan 已提交
464 465
  }

S
Shengliang Guan 已提交
466
  if (createReq.pass[0] == 0) {
S
Shengliang Guan 已提交
467
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
468
    goto _OVER;
S
Shengliang Guan 已提交
469 470
  }

S
Shengliang Guan 已提交
471
  pUser = mndAcquireUser(pMnode, createReq.user);
S
Shengliang Guan 已提交
472
  if (pUser != NULL) {
S
Shengliang Guan 已提交
473
    terrno = TSDB_CODE_MND_USER_ALREADY_EXIST;
474
    goto _OVER;
S
Shengliang Guan 已提交
475 476
  }

477
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
478
  if (pOperUser == NULL) {
S
Shengliang Guan 已提交
479
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
480
    goto _OVER;
S
Shengliang Guan 已提交
481 482
  }

C
Cary Xu 已提交
483 484 485 486
  if ((terrno = grantCheck(TSDB_GRANT_USER)) != 0) {
    code = terrno;
    goto _OVER;
  }
487

S
Shengliang Guan 已提交
488
  code = mndCreateUser(pMnode, pOperUser->acct, &createReq, pReq);
S
Shengliang Guan 已提交
489
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
490

491
_OVER:
S
Shengliang Guan 已提交
492
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
493
    mError("user:%s, failed to create since %s", createReq.user, terrstr());
S
Shengliang Guan 已提交
494 495
  }

S
Shengliang Guan 已提交
496 497 498 499
  mndReleaseUser(pMnode, pUser);
  mndReleaseUser(pMnode, pOperUser);

  return code;
S
Shengliang Guan 已提交
500 501
}

S
Shengliang Guan 已提交
502
static int32_t mndAlterUser(SMnode *pMnode, SUserObj *pOld, SUserObj *pNew, SRpcMsg *pReq) {
503
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "alter-user");
S
Shengliang Guan 已提交
504
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
505
    mError("user:%s, failed to alter since %s", pOld->user, terrstr());
S
Shengliang Guan 已提交
506 507
    return -1;
  }
508
  mInfo("trans:%d, used to alter user:%s", pTrans->id, pOld->user);
S
Shengliang Guan 已提交
509

510 511 512
  SSdbRaw *pCommitRaw = mndUserActionEncode(pNew);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
513 514 515
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
516
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
517 518 519 520 521 522 523 524 525 526 527

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

528
SHashObj *mndDupObjHash(SHashObj *pOld, int32_t dataLen) {
S
Shengliang Guan 已提交
529 530
  SHashObj *pNew =
      taosHashInit(taosHashGetSize(pOld), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
S
Shengliang Guan 已提交
531 532 533 534 535 536 537 538
  if (pNew == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  char *db = taosHashIterate(pOld, NULL);
  while (db != NULL) {
    int32_t len = strlen(db) + 1;
539
    if (taosHashPut(pNew, db, len, db, dataLen) != 0) {
S
Shengliang Guan 已提交
540 541
      taosHashCancelIterate(pOld, db);
      taosHashCleanup(pNew);
S
Shengliang Guan 已提交
542
      terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
543 544 545 546 547 548 549 550
      return NULL;
    }
    db = taosHashIterate(pOld, db);
  }

  return pNew;
}

551 552 553 554
SHashObj *mndDupDbHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_DB_FNAME_LEN); }

SHashObj *mndDupTopicHash(SHashObj *pOld) { return mndDupObjHash(pOld, TSDB_TOPIC_FNAME_LEN); }

S
Shengliang Guan 已提交
555 556
static int32_t mndProcessAlterUserReq(SRpcMsg *pReq) {
  SMnode       *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
557 558
  SSdb         *pSdb = pMnode->pSdb;
  void         *pIter = NULL;
S
Shengliang Guan 已提交
559 560 561
  int32_t       code = -1;
  SUserObj     *pUser = NULL;
  SUserObj     *pOperUser = NULL;
S
Shengliang Guan 已提交
562
  SUserObj      newUser = {0};
S
Shengliang Guan 已提交
563 564
  SAlterUserReq alterReq = {0};

S
Shengliang Guan 已提交
565
  if (tDeserializeSAlterUserReq(pReq->pCont, pReq->contLen, &alterReq) != 0) {
S
Shengliang Guan 已提交
566
    terrno = TSDB_CODE_INVALID_MSG;
567
    goto _OVER;
S
Shengliang Guan 已提交
568
  }
S
Shengliang Guan 已提交
569

570
  mInfo("user:%s, start to alter", alterReq.user);
S
Shengliang Guan 已提交
571

S
Shengliang Guan 已提交
572
  if (alterReq.user[0] == 0) {
S
Shengliang Guan 已提交
573
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
574 575 576 577 578
    goto _OVER;
  }

  if (TSDB_ALTER_USER_PASSWD == alterReq.alterType && alterReq.pass[0] == 0) {
    terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT;
579
    goto _OVER;
S
Shengliang Guan 已提交
580 581
  }

S
Shengliang Guan 已提交
582
  pUser = mndAcquireUser(pMnode, alterReq.user);
S
Shengliang Guan 已提交
583 584
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
585
    goto _OVER;
S
Shengliang Guan 已提交
586 587
  }

588
  pOperUser = mndAcquireUser(pMnode, pReq->info.conn.user);
S
Shengliang Guan 已提交
589 590
  if (pOperUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
591
    goto _OVER;
S
Shengliang Guan 已提交
592 593
  }

594
  if (mndCheckAlterUserPrivilege(pOperUser, pUser, &alterReq) != 0) {
S
Shengliang Guan 已提交
595 596 597
    goto _OVER;
  }

598
  if (mndUserDupObj(pUser, &newUser) != 0) goto _OVER;
S
Shengliang Guan 已提交
599 600 601 602

  if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) {
    char pass[TSDB_PASSWORD_LEN + 1] = {0};
    taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass);
603
    memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN);
S
Shengliang Guan 已提交
604 605 606
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) {
S
Shengliang Guan 已提交
607
    newUser.superUser = alterReq.superUser;
S
Shengliang Guan 已提交
608 609
  }

610 611 612 613 614 615 616 617
  if (alterReq.alterType == TSDB_ALTER_USER_ENABLE) {
    newUser.enable = alterReq.enable;
  }

  if (alterReq.alterType == TSDB_ALTER_USER_SYSINFO) {
    newUser.sysInfo = alterReq.sysInfo;
  }

S
Shengliang Guan 已提交
618
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
619 620 621
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
622 623 624 625
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
626
      if (taosHashPut(newUser.readDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
627 628 629 630 631 632 633 634 635 636 637 638
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
    } else {
      while (1) {
        SDbObj *pDb = NULL;
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
        if (pIter == NULL) break;
        int32_t len = strlen(pDb->name) + 1;
        taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
        sdbRelease(pSdb, pDb);
      }
S
Shengliang Guan 已提交
639
    }
S
Shengliang Guan 已提交
640 641 642
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) {
643 644 645
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
646 647 648 649
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
650
      if (taosHashPut(newUser.writeDbs, alterReq.objname, len, alterReq.objname, TSDB_DB_FNAME_LEN) != 0) {
S
Shengliang Guan 已提交
651 652 653 654 655 656 657 658 659 660 661 662
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
    } else {
      while (1) {
        SDbObj *pDb = NULL;
        pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb);
        if (pIter == NULL) break;
        int32_t len = strlen(pDb->name) + 1;
        taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN);
        sdbRelease(pSdb, pDb);
      }
S
Shengliang Guan 已提交
663 664 665
    }
  }

S
Shengliang Guan 已提交
666
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
667 668 669
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
670 671 672 673
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
674
      taosHashRemove(newUser.readDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
675 676 677 678
    } else {
      taosHashClear(newUser.readDbs);
    }
  }
S
Shengliang Guan 已提交
679

S
Shengliang Guan 已提交
680
  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) {
681 682 683
    if (strcmp(alterReq.objname, "1.*") != 0) {
      int32_t len = strlen(alterReq.objname) + 1;
      SDbObj *pDb = mndAcquireDb(pMnode, alterReq.objname);
S
Shengliang Guan 已提交
684 685 686 687
      if (pDb == NULL) {
        mndReleaseDb(pMnode, pDb);
        goto _OVER;
      }
688
      taosHashRemove(newUser.writeDbs, alterReq.objname, len);
S
Shengliang Guan 已提交
689 690 691
    } else {
      taosHashClear(newUser.writeDbs);
    }
S
Shengliang Guan 已提交
692 693
  }

C
table  
cademfly 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_TABLE || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_TABLE) {
    if (strcmp(alterReq.tabName, "1.*") != 0) {
      char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
      snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName);

      int32_t len = strlen(tbFName) + 1;
      SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
      if (pStb == NULL) {
        mndReleaseStb(pMnode, pStb);
        goto _OVER;
      }
      if (taosHashPut(newUser.readStbs, tbFName, len, tbFName, TSDB_TABLE_NAME_LEN) != 0) {
        mndReleaseStb(pMnode, pStb);
        goto _OVER;
      }
    } else {
      while (1) {
        SStbObj *pStb = NULL;
        pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
        if (pIter == NULL) break;
        int32_t len = strlen(pStb->name) + 1;
        taosHashPut(newUser.readStbs, pStb->name, len, pStb->name, TSDB_TABLE_NAME_LEN);
        sdbRelease(pSdb, pStb);
      }
    }
  }

  if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_TABLE || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_TABLE) {
    if (strcmp(alterReq.tabName, "1.*") != 0) {
      char tbFName[TSDB_TABLE_FNAME_LEN] = {0};
      snprintf(tbFName, sizeof(tbFName), "%s.%s", alterReq.objname, alterReq.tabName);

      int32_t len = strlen(tbFName) + 1;
      SStbObj *pStb = mndAcquireStb(pMnode, tbFName);
      if (pStb == NULL) {
        mndReleaseStb(pMnode, pStb);
        goto _OVER;
      }
      if (taosHashPut(newUser.writeStbs, tbFName, len, tbFName, TSDB_TABLE_NAME_LEN) != 0) {
        mndReleaseStb(pMnode, pStb);
        goto _OVER;
      }
    } else {
      while (1) {
        SStbObj *pStb = NULL;
        pIter = sdbFetch(pSdb, SDB_STB, pIter, (void **)&pStb);
        if (pIter == NULL) break;
        int32_t len = strlen(pStb->name) + 1;
        taosHashPut(newUser.writeStbs, pStb->name, len, pStb->name, TSDB_DB_FNAME_LEN);
        sdbRelease(pSdb, pStb);
      }
    }
  }

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
  if (alterReq.alterType == TSDB_ALTER_USER_ADD_SUBSCRIBE_TOPIC) {
    int32_t      len = strlen(alterReq.objname) + 1;
    SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
    if (pTopic == NULL) {
      mndReleaseTopic(pMnode, pTopic);
      goto _OVER;
    }
    taosHashPut(newUser.topics, pTopic->name, len, pTopic->name, TSDB_TOPIC_FNAME_LEN);
  }

  if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_SUBSCRIBE_TOPIC) {
    int32_t      len = strlen(alterReq.objname) + 1;
    SMqTopicObj *pTopic = mndAcquireTopic(pMnode, alterReq.objname);
    if (pTopic == NULL) {
      mndReleaseTopic(pMnode, pTopic);
      goto _OVER;
    }
    taosHashRemove(newUser.topics, alterReq.objname, len);
  }

S
Shengliang Guan 已提交
768
  code = mndAlterUser(pMnode, pUser, &newUser, pReq);
S
Shengliang Guan 已提交
769
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
770

771
_OVER:
S
Shengliang Guan 已提交
772
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
773
    mError("user:%s, failed to alter since %s", alterReq.user, terrstr());
S
Shengliang Guan 已提交
774 775
  }

S
Shengliang Guan 已提交
776 777
  mndReleaseUser(pMnode, pOperUser);
  mndReleaseUser(pMnode, pUser);
778
  mndUserFreeObj(&newUser);
S
Shengliang Guan 已提交
779 780

  return code;
S
Shengliang Guan 已提交
781 782
}

S
Shengliang Guan 已提交
783
static int32_t mndDropUser(SMnode *pMnode, SRpcMsg *pReq, SUserObj *pUser) {
784
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "drop-user");
S
Shengliang Guan 已提交
785 786 787 788
  if (pTrans == NULL) {
    mError("user:%s, failed to drop since %s", pUser->user, terrstr());
    return -1;
  }
789
  mInfo("trans:%d, used to drop user:%s", pTrans->id, pUser->user);
S
Shengliang Guan 已提交
790

791 792 793
  SSdbRaw *pCommitRaw = mndUserActionEncode(pUser);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
S
Shengliang Guan 已提交
794 795 796
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
797
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED);
S
Shengliang Guan 已提交
798 799 800 801 802 803 804 805 806 807 808

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}

S
Shengliang Guan 已提交
809 810
static int32_t mndProcessDropUserReq(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
811 812 813 814
  int32_t      code = -1;
  SUserObj    *pUser = NULL;
  SDropUserReq dropReq = {0};

S
Shengliang Guan 已提交
815
  if (tDeserializeSDropUserReq(pReq->pCont, pReq->contLen, &dropReq) != 0) {
S
Shengliang Guan 已提交
816
    terrno = TSDB_CODE_INVALID_MSG;
817
    goto _OVER;
S
Shengliang Guan 已提交
818
  }
S
Shengliang Guan 已提交
819

820
  mInfo("user:%s, start to drop", dropReq.user);
821 822 823
  if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_DROP_USER) != 0) {
    goto _OVER;
  }
S
Shengliang Guan 已提交
824

S
Shengliang Guan 已提交
825
  if (dropReq.user[0] == 0) {
S
Shengliang Guan 已提交
826
    terrno = TSDB_CODE_MND_INVALID_USER_FORMAT;
827
    goto _OVER;
S
Shengliang Guan 已提交
828 829
  }

S
Shengliang Guan 已提交
830
  pUser = mndAcquireUser(pMnode, dropReq.user);
S
Shengliang Guan 已提交
831 832
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
833
    goto _OVER;
S
Shengliang Guan 已提交
834 835
  }

S
Shengliang Guan 已提交
836
  code = mndDropUser(pMnode, pReq, pUser);
S
Shengliang Guan 已提交
837
  if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
S
Shengliang Guan 已提交
838

839
_OVER:
S
Shengliang Guan 已提交
840
  if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
S
Shengliang Guan 已提交
841
    mError("user:%s, failed to drop since %s", dropReq.user, terrstr());
S
Shengliang Guan 已提交
842 843
  }

S
Shengliang Guan 已提交
844 845
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
846 847
}

S
Shengliang Guan 已提交
848 849
static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
  SMnode         *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
850 851 852 853 854
  int32_t         code = -1;
  SUserObj       *pUser = NULL;
  SGetUserAuthReq authReq = {0};
  SGetUserAuthRsp authRsp = {0};

S
Shengliang Guan 已提交
855
  if (tDeserializeSGetUserAuthReq(pReq->pCont, pReq->contLen, &authReq) != 0) {
S
Shengliang Guan 已提交
856
    terrno = TSDB_CODE_INVALID_MSG;
857
    goto _OVER;
S
Shengliang Guan 已提交
858
  }
S
Shengliang Guan 已提交
859 860 861 862 863 864

  mTrace("user:%s, start to get auth", authReq.user);

  pUser = mndAcquireUser(pMnode, authReq.user);
  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_USER_NOT_EXIST;
865
    goto _OVER;
S
Shengliang Guan 已提交
866 867
  }

D
dapan 已提交
868 869 870
  code = mndSetUserAuthRsp(pMnode, pUser, &authRsp);
  if (code) {
    goto _OVER;
S
Shengliang Guan 已提交
871 872
  }

S
Shengliang Guan 已提交
873
  int32_t contLen = tSerializeSGetUserAuthRsp(NULL, 0, &authRsp);
S
Shengliang Guan 已提交
874 875 876
  void   *pRsp = rpcMallocCont(contLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
877
    goto _OVER;
S
Shengliang Guan 已提交
878 879
  }

S
Shengliang Guan 已提交
880
  tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
S
Shengliang Guan 已提交
881

S
Shengliang Guan 已提交
882 883
  pReq->info.rsp = pRsp;
  pReq->info.rspLen = contLen;
S
Shengliang Guan 已提交
884 885
  code = 0;

886
_OVER:
887

S
Shengliang Guan 已提交
888
  mndReleaseUser(pMnode, pUser);
S
Shengliang Guan 已提交
889
  tFreeSGetUserAuthRsp(&authRsp);
S
Shengliang Guan 已提交
890 891 892 893

  return code;
}

S
Shengliang Guan 已提交
894 895
static int32_t mndRetrieveUsers(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
S
Shengliang Guan 已提交
896 897 898 899 900 901 902 903 904 905 906
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   numOfRows = 0;
  SUserObj *pUser = NULL;
  int32_t   cols = 0;
  char     *pWrite;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
    if (pShow->pIter == NULL) break;

    cols = 0;
907
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
908
    char             name[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
909
    STR_WITH_MAXSIZE_TO_VARSTR(name, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
910
    colDataSetVal(pColInfo, numOfRows, (const char *)name, false);
911

wafwerar's avatar
wafwerar 已提交
912 913
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
914
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->superUser, false);
915

916 917
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
918
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->enable, false);
919 920 921

    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
922
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->sysInfo, false);
923

wafwerar's avatar
wafwerar 已提交
924 925
    cols++;
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols);
926
    colDataSetVal(pColInfo, numOfRows, (const char *)&pUser->createdTime, false);
S
Shengliang Guan 已提交
927 928 929 930 931

    numOfRows++;
    sdbRelease(pSdb, pUser);
  }

932
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
933 934 935 936 937 938
  return numOfRows;
}

static void mndCancelGetNextUser(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
939
}
D
dapan 已提交
940

S
Shengliang Guan 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
static int32_t mndRetrievePrivileges(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode   *pMnode = pReq->info.node;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   numOfRows = 0;
  SUserObj *pUser = NULL;
  int32_t   cols = 0;
  char     *pWrite;

  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_USER, pShow->pIter, (void **)&pUser);
    if (pShow->pIter == NULL) break;

    int32_t numOfReadDbs = taosHashGetSize(pUser->readDbs);
    int32_t numOfWriteDbs = taosHashGetSize(pUser->writeDbs);
    int32_t numOfTopics = taosHashGetSize(pUser->topics);
    if (numOfRows + numOfReadDbs + numOfWriteDbs + numOfTopics >= rows) break;

958 959
    if (pUser->superUser) {
      cols = 0;
X
Xiaoyu Wang 已提交
960
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
961
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
962
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
963
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
964 965 966 967

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "all", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
968
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
969 970 971 972

      char objName[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(objName, "all", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
973
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
974 975 976 977

      numOfRows++;
    }

S
Shengliang Guan 已提交
978 979 980
    char *db = taosHashIterate(pUser->readDbs, NULL);
    while (db != NULL) {
      cols = 0;
X
Xiaoyu Wang 已提交
981
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
982
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
X
Xiaoyu Wang 已提交
983
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
984
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
985 986 987 988

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
989
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
990 991 992 993 994 995 996

      SName name = {0};
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
      tNameGetDbName(&name, varDataVal(objName));
      varDataSetLen(objName, strlen(varDataVal(objName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
997
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
998 999 1000 1001 1002 1003 1004 1005

      numOfRows++;
      db = taosHashIterate(pUser->readDbs, db);
    }

    db = taosHashIterate(pUser->writeDbs, NULL);
    while (db != NULL) {
      cols = 0;
1006
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
1007
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1008
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1009
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
1010 1011 1012 1013

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1014
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
1015 1016 1017 1018 1019 1020 1021

      SName name = {0};
      char  objName[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      tNameFromString(&name, db, T_NAME_ACCT | T_NAME_DB);
      tNameGetDbName(&name, varDataVal(objName));
      varDataSetLen(objName, strlen(varDataVal(objName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1022
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);
S
Shengliang Guan 已提交
1023 1024 1025 1026 1027

      numOfRows++;
      db = taosHashIterate(pUser->writeDbs, db);
    }

C
table  
cademfly 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
    char *stb = taosHashIterate(pUser->readStbs, NULL);
    while (stb != NULL) {
      cols = 0;
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "read", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);

      char  objName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
      mndExtractTbNameFromStbFullName(stb, &objName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
      varDataSetLen(objName, strlen(&objName[VARSTR_HEADER_SIZE]));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);

      numOfRows++;
      stb = taosHashIterate(pUser->readStbs, stb);
    }

    stb = taosHashIterate(pUser->writeStbs, NULL);
    while (stb != NULL) {
      cols = 0;
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "write", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);

      char  objName[TSDB_TABLE_NAME_LEN] = {0};
      mndExtractTbNameFromStbFullName(stb, &objName[VARSTR_HEADER_SIZE], TSDB_TABLE_NAME_LEN);
      varDataSetLen(objName, strlen(&objName[VARSTR_HEADER_SIZE]));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
      colDataSetVal(pColInfo, numOfRows, (const char *)objName, false);

      numOfRows++;
      stb = taosHashIterate(pUser->writeStbs, stb);
    }

S
Shengliang Guan 已提交
1074 1075 1076
    char *topic = taosHashIterate(pUser->topics, NULL);
    while (topic != NULL) {
      cols = 0;
1077
      char userName[TSDB_USER_LEN + VARSTR_HEADER_SIZE] = {0};
S
Shengliang Guan 已提交
1078
      STR_WITH_MAXSIZE_TO_VARSTR(userName, pUser->user, pShow->pMeta->pSchemas[cols].bytes);
1079
      SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1080
      colDataSetVal(pColInfo, numOfRows, (const char *)userName, false);
S
Shengliang Guan 已提交
1081 1082 1083 1084

      char privilege[20] = {0};
      STR_WITH_MAXSIZE_TO_VARSTR(privilege, "subscribe", pShow->pMeta->pSchemas[cols].bytes);
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1085
      colDataSetVal(pColInfo, numOfRows, (const char *)privilege, false);
S
Shengliang Guan 已提交
1086 1087 1088 1089 1090

      char topicName[TSDB_TOPIC_NAME_LEN + VARSTR_HEADER_SIZE + 5] = {0};
      tstrncpy(varDataVal(topicName), mndGetDbStr(topic), TSDB_TOPIC_NAME_LEN - 2);
      varDataSetLen(topicName, strlen(varDataVal(topicName)));
      pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1091
      colDataSetVal(pColInfo, numOfRows, (const char *)topicName, false);
S
Shengliang Guan 已提交
1092 1093

      numOfRows++;
1094
      topic = taosHashIterate(pUser->topics, topic);
S
Shengliang Guan 已提交
1095
    }
1096

S
Shengliang Guan 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
    sdbRelease(pSdb, pUser);
  }

  pShow->numOfRows += numOfRows;
  return numOfRows;
}

static void mndCancelGetNextPrivileges(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
}
1108

1109 1110
int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_t numOfUses, void **ppRsp,
                                int32_t *pRspLen) {
D
dapan 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  SUserAuthBatchRsp batchRsp = {0};
  batchRsp.pArray = taosArrayInit(numOfUses, sizeof(SGetUserAuthRsp));
  if (batchRsp.pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  int32_t code = 0;
  for (int32_t i = 0; i < numOfUses; ++i) {
    SUserObj *pUser = mndAcquireUser(pMnode, pUsers[i].user);
    if (pUser == NULL) {
      mError("user:%s, failed to auth user since %s", pUsers[i].user, terrstr());
      continue;
    }

D
dapan1121 已提交
1126
    pUsers[i].version = ntohl(pUsers[i].version);
D
dapan 已提交
1127 1128 1129 1130
    if (pUser->authVersion <= pUsers[i].version) {
      mndReleaseUser(pMnode, pUser);
      continue;
    }
1131

D
dapan 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    SGetUserAuthRsp rsp = {0};
    code = mndSetUserAuthRsp(pMnode, pUser, &rsp);
    if (code) {
      mndReleaseUser(pMnode, pUser);
      tFreeSGetUserAuthRsp(&rsp);
      goto _OVER;
    }

    taosArrayPush(batchRsp.pArray, &rsp);
    mndReleaseUser(pMnode, pUser);
  }

  if (taosArrayGetSize(batchRsp.pArray) <= 0) {
    *ppRsp = NULL;
    *pRspLen = 0;
1147

D
dapan 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    tFreeSUserAuthBatchRsp(&batchRsp);
    return 0;
  }

  int32_t rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
  void   *pRsp = taosMemoryMalloc(rspLen);
  if (pRsp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    tFreeSUserAuthBatchRsp(&batchRsp);
    return -1;
  }
  tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);

  *ppRsp = pRsp;
  *pRspLen = rspLen;

  tFreeSUserAuthBatchRsp(&batchRsp);
  return 0;

_OVER:

  *ppRsp = NULL;
  *pRspLen = 0;
1171

D
dapan 已提交
1172 1173 1174
  tFreeSUserAuthBatchRsp(&batchRsp);
  return code;
}
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195

int32_t mndUserRemoveDb(SMnode *pMnode, STrans *pTrans, char *db) {
  int32_t   code = 0;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   len = strlen(db) + 1;
  void     *pIter = NULL;
  SUserObj *pUser = NULL;
  SUserObj  newUser = {0};

  while (1) {
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
    if (pIter == NULL) break;

    code = -1;
    if (mndUserDupObj(pUser, &newUser) != 0) break;

    bool inRead = (taosHashGet(newUser.readDbs, db, len) != NULL);
    bool inWrite = (taosHashGet(newUser.writeDbs, db, len) != NULL);
    if (inRead || inWrite) {
      (void)taosHashRemove(newUser.readDbs, db, len);
      (void)taosHashRemove(newUser.writeDbs, db, len);
C
table  
cademfly 已提交
1196 1197
      (void)taosHashRemove(newUser.readStbs, db, len);
      (void)taosHashRemove(newUser.writeStbs, db, len);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
      if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break;
      (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
    }

    mndUserFreeObj(&newUser);
    sdbRelease(pSdb, pUser);
    code = 0;
  }

  if (pUser != NULL) sdbRelease(pSdb, pUser);
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
  mndUserFreeObj(&newUser);
  return code;
}

int32_t mndUserRemoveTopic(SMnode *pMnode, STrans *pTrans, char *topic) {
  int32_t   code = 0;
  SSdb     *pSdb = pMnode->pSdb;
  int32_t   len = strlen(topic) + 1;
  void     *pIter = NULL;
  SUserObj *pUser = NULL;
  SUserObj  newUser = {0};

  while (1) {
    pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pUser);
1225 1226 1227
    if (pIter == NULL) {
      break;
    }
1228 1229

    code = -1;
1230 1231 1232
    if (mndUserDupObj(pUser, &newUser) != 0) {
      break;
    }
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

    bool inTopic = (taosHashGet(newUser.topics, topic, len) != NULL);
    if (inTopic) {
      (void)taosHashRemove(newUser.topics, topic, len);
      SSdbRaw *pCommitRaw = mndUserActionEncode(&newUser);
      if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) break;
      (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
    }

    mndUserFreeObj(&newUser);
    sdbRelease(pSdb, pUser);
    code = 0;
  }

  if (pUser != NULL) sdbRelease(pSdb, pUser);
  if (pIter != NULL) sdbCancelFetch(pSdb, pIter);
  mndUserFreeObj(&newUser);
  return code;
}